haacked / Encourage

A bit of encouragment added to Visual Studio
75 stars 32 forks source link

Issue formatting .cs files #34

Closed jaredpar closed 9 years ago

jaredpar commented 9 years ago

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:

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 commented 9 years ago

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 commented 9 years ago

LOL!