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

Issue formatting .cs files #34

Closed
jaredpar opened this issue Feb 23, 2015 · 2 comments
Closed

Issue formatting .cs files #34

jaredpar opened this issue Feb 23, 2015 · 2 comments
Labels

Comments

@jaredpar
Copy link
Collaborator

There is a problem while formatting (indenting, adding/erasing whitespaces and/or newlines, etc.) directives such as #endregion, #pragma, etc..

The problem lies within the following classes/functions:

  • SyntaxFormatter
  • NormalizeWhitespaceCore
  • NormalizeWhitespace

These functions are inconsistent with the CSharp compiler, which allows lines such as:

#endregion foo

where the conventional way of doing this is:

#endregion //foo

The formatting erases the whitespace after the directive, giving a non-compilable file like this:

#endregionfoo <--- here, having written "#endregion //foo" wouldn´t cause any problem, because one would be getting a compilable "#endregion//foo"

Similar problem with #pragma, where an extra enter is erased while formatting the following:

#pragma warning disable 123

namespace foo {
}

#pragma warning restore 123

one gets the following non-compilable code:

#pragma warning disable 123
namespace foo {
}#pragma warning restore 123  <--- extra enter erased here, after the closing bracket

Sample repro for the #pragma issue:

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Formatting;
using static System.Console;
class Program
{
    static void Main(string[] args)
    {
        var tree = SyntaxFactory.ParseSyntaxTree(@"
            #pragma warning disable 123
            namespace foo {
            }
            #pragma warning restore 123
            ");
        WriteLine("NormalizeWhitespace()");
        WriteLine("=====================");
        WriteLine(tree.GetRoot().NormalizeWhitespace().ToFullString());
        WriteLine();
        WriteLine("Formatter.Format()");
        WriteLine("=====================");
        WriteLine(Formatter.Format(tree.GetRoot(), new AdhocWorkspace()).ToFullString());
    }
}
@jaredpar jaredpar added the bug label Feb 23, 2015
@jaredpar
Copy link
Collaborator Author

Ooops ...

Ignore this. Was writing a tool to port some issues from CodePlex -> GitHub and looks like it grabbed the wrong repo.

Sorry bout that.

@haacked
Copy link
Owner

haacked commented Feb 24, 2015

LOL!

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

No branches or pull requests

2 participants