Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# #if and #endif indentation is reset after formatting messing with folding of outer code #2008

Closed
aeschli opened this issue Feb 5, 2018 · 9 comments

Comments

@aeschli
Copy link
Contributor

aeschli commented Feb 5, 2018

From @uday0408 on February 3, 2018 1:54

  • VSCode Version: 1.19.3
  • OS Version: Mac OS X, 10.13
  • C# Extension version: Omnisharp 1.13.1

For C#, after formatting with Omnisharp installed, the #region and #endregion statements are indented at the same level of enclosing code. But #if and #endif are formatted to have no indentation, thus messing with folding when #if/#endif enclosing code is nested.

I don't know if this is Omnisharp or VSCode problem.

Steps to Reproduce:

  1. Apply formatting to following code
public static class Main
{
    public static int Foo(int x)
    {
        x = 5;
        #if false
        x++;
        #endif
        return x;
    }
}
  1. After formatting it looks like this:
public static class Main
{
    public static int Foo(int x)
    {
        x = 5;
#if false
        x++;
#endif
        return x;
    }
}
  1. This messes with folding of entire code!!

Does this issue occur when all extensions are disabled?: Yes/No

Copied from original issue: microsoft/vscode#42861

@aeschli
Copy link
Contributor Author

aeschli commented Feb 5, 2018

From @alexandrudima on February 5, 2018 9:37

I believe the way Omnisharp (Roslyn) formats is the generally accepted way for formatting C# macros.

The problem is that our folding regions are currently computed in a language agnostic way, based on indentation.

@aeschli
Copy link
Contributor Author

aeschli commented Feb 5, 2018

The request for language aware folding is microsoft/vscode#3422.

Moving to Omnisharp to decide whether the formatter needs to be changed or made configurable.

@DustinCampbell
Copy link
Member

This is working as designed. The C# formatter (the same formatter used in Visual Studio) always places preprocessor directives in the left column. Currently, I don't believe there's an option to change this behavior.

cc @jinujoseph, @kuhlenh (and also @Pilchie) to see if this something we might want to add to Roslyn.

@uday0408
Copy link

uday0408 commented Feb 5, 2018

So #region and #endregion are special case of preprocessor macros? They are not placed at the left most column, since September 2017 release.

@DustinCampbell
Copy link
Member

Correct #region and #endregion are treated specially by the C# formatter.

@Leopotam
Copy link
Contributor

Leopotam commented Feb 7, 2018

@aeschli you can try this extension with "csharpfixformat.style.indent.preprocessorIgnored": false setting.

@DustinCampbell
Copy link
Member

@uday0408, please see @Leopotam's reply above for a way to add this behavior.

@uday0408
Copy link

uday0408 commented Feb 7, 2018

Using C# Fix Format extension and "csharpfixformat.style.indent.preprocessorIgnored": false setting I was able to use the #if / #endif directives without having them completely outdented. This allowed the folding to work correctly in VSCode which is what I wanted. Thanks!

@DustinCampbell
Copy link
Member

@uday0408: FWIW, we've been asking for awhile to have an API from VS Code that languages can use to define custom folding (microsoft/vscode#3422). Once we have that, we'll be able to make folding work for #if / #endif correctly. Currently, I would suspect that the folding will not be accurate for nested #if / #endif when #else is interspersed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants