belav / csharpier

CSharpier is an opinionated code formatter for c#.
https://csharpier.com
MIT License
1.41k stars 98 forks source link

File with only file scoped namespace + comments gets extra lines #1256

Open belav opened 6 months ago

belav commented 6 months ago

If you repeatedly format this file, it gets a new blank line each time.

Input:

namespace InspectorGadget.TestPublicSurfaceArea;

//    public class ClassAdded
//    {
//        public void TestMethod()
//        {
//        }
//    }

Output:

namespace InspectorGadget.TestPublicSurfaceArea;

//    public class ClassAdded
//    {
//        public void TestMethod()
//        {
//        }
//    }
belav commented 6 months ago

The namespace printing ends with two hardlines, this ensures that it has space between it and the next line (using, statement, etc)

However the way we print comments, especially comments that are on the leading trivia of the EndOfFileToken, tries to ensure there is a line in front of them.

Getting this case happy seems difficult, and it is a case that is easy to just ignore the file because there isn't anything in it to format.

If comments were reworked, this may be easier.