Skip to content

Commit

Permalink
maybe fixing last edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed Jul 4, 2023
1 parent 3383f7a commit 9878a55
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 102 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,29 +1,13 @@
// leading
using First; // trailing

using A; // trailing
// leading with space
using // another trailing
Second;

using
// static leading
static // static trailing
Third;

using M = System.Math;
using Point = (int x, int y);

using static System.Math;

global using System;

using First;
using Second;
B;

namespace Namespace
{
using Third;
using One.Two.Three;
using Third;

public class ClassName { }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
#if DEBUG
using Insite.Bad;
#endif
using System;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ global using Global;
using System;
using Custom;
using static Expression;
using Point = (int x, int y);
using Index = Microsoft.Framework.Index;

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ using Microsoft.Build.Framework;

namespace RepoTasks;

class ClassName { }

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.IO;
#if DEBUG
using System;
#else
using Microsoft;
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.IO;
#if !DEBUG
using System;
#else
using Microsoft;
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#if DEBUG
using A;
#else
using B;
#endif
#if !DEBUG
using C;
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using C;
#if DEBUG
using A;
#else
using B;
#endif

#if !DEBUG
using C;
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#if DEBUG
using A;
#else
using B;
#endif
using C;
#if !DEBUG
using C;
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;

using System.Web; // keeping the blank line above this in the expected is odd, but there isn't a good way to know which spaces to remove besides the first one

using AWord; // the blank line above here tests that we don't add two blank lines between system and non-system
using System.Web;
using AWord;
using ZWord;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using ZWord;

using AWord; // the blank line above here tests that we don't add two blank lines between system and non-system

using System.Web; // keeping the blank line above this in the expected is odd, but there isn't a good way to know which spaces to remove besides the first one
using AWord;
using System.Web;
using System;
1 change: 1 addition & 0 deletions Src/CSharpier/CSharpFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bool TryGetCompilationFailure(out CodeFormatterResult compilationResult)
var formattingContext = new FormattingContext { LineEnding = lineEnding };
var document = Node.Print(rootNode, formattingContext);
var formattedCode = DocPrinter.DocPrinter.Print(document, printerOptions, lineEnding);
DebugLogger.Log(formattedCode);
var reorderedModifiers = formattingContext.ReorderedModifiers;

foreach (var symbolSet in PreprocessorSymbols.GetSets(syntaxTree))
Expand Down
82 changes: 35 additions & 47 deletions Src/CSharpier/SyntaxPrinter/UsingDirectives.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,31 @@ bool printExtraLines
{
var docs = new List<Doc>();

// what is this is an #if? only comments
docs.Add(Token.PrintLeadingTrivia(usings.First().GetLeadingTrivia(), context));
var initialComments = new List<SyntaxTrivia>();
var otherStuff = new List<SyntaxTrivia>();
var foundOtherStuff = false;
foreach (var leadingTrivia in usings.First().GetLeadingTrivia())
{
if (leadingTrivia.RawSyntaxKind() == SyntaxKind.IfDirectiveTrivia)
{
foundOtherStuff = true;
}

if (foundOtherStuff)
{
otherStuff.Add(leadingTrivia);
}
else
{
initialComments.Add(leadingTrivia);
}
}

docs.Add(Token.PrintLeadingTrivia(new SyntaxTriviaList(initialComments), context));
var isFirst = true;
foreach (var groupOfUsingData in GroupUsings(usings, context))
foreach (
var groupOfUsingData in GroupUsings(usings, new SyntaxTriviaList(otherStuff), context)
)
{
foreach (var usingData in groupOfUsingData)
{
Expand Down Expand Up @@ -65,17 +86,15 @@ bool printExtraLines

private static IEnumerable<List<UsingData>> GroupUsings(
SyntaxList<UsingDirectiveSyntax> usings,
SyntaxTriviaList otherStuff,
FormattingContext context
)
{
var globalUsings = new List<UsingData>();
var regularUsings = new List<UsingData>();
var staticUsings = new List<UsingData>();
var aliasUsings = new List<UsingData>();
// TODO what about multiple ifs?
var directiveGroup = new List<UsingData>();
// TODO this is leftovers for the first group
var leftOvers = new List<UsingData>();
var ifCount = 0;
var isFirst = true;
foreach (var usingDirective in usings)
Expand All @@ -97,8 +116,8 @@ Doc PrintStuff(UsingDirectiveSyntax value)
{
// TODO what about something with comments and a close #endif?
return isFirst
? Doc.Null
: Doc.Concat(Token.PrintLeadingTrivia(value.GetLeadingTrivia(), context));
? Token.PrintLeadingTrivia(otherStuff, context)
: Token.PrintLeadingTrivia(value.GetLeadingTrivia(), context);
}

if (ifCount > 0)
Expand All @@ -115,7 +134,9 @@ Doc PrintStuff(UsingDirectiveSyntax value)
{
if (openIf)
{
leftOvers.Add(new UsingData { LeadingTrivia = PrintStuff(usingDirective) });
directiveGroup.Add(
new UsingData { LeadingTrivia = PrintStuff(usingDirective) }
);
}

var usingData = new UsingData
Expand Down Expand Up @@ -146,35 +167,11 @@ Doc PrintStuff(UsingDirectiveSyntax value)
isFirst = false;
}

if (globalUsings.Any())
{
yield return globalUsings.OrderBy(o => o.Using!, Comparer).ToList();
}

if (regularUsings.Any())
{
yield return regularUsings.OrderBy(o => o.Using!, Comparer).ToList();
}

if (directiveGroup.Any())
{
yield return directiveGroup.OrderBy(o => o.Using!, Comparer).ToList();
}

if (leftOvers.Any())
{
yield return leftOvers;
}

if (staticUsings.Any())
{
yield return staticUsings.OrderBy(o => o.Using!, Comparer).ToList();
}

if (aliasUsings.Any())
{
yield return aliasUsings.OrderBy(o => o.Using!, Comparer).ToList();
}
yield return globalUsings.OrderBy(o => o.Using!, Comparer).ToList();
yield return regularUsings.OrderBy(o => o.Using!, Comparer).ToList();
yield return directiveGroup;
yield return staticUsings.OrderBy(o => o.Using!, Comparer).ToList();
yield return aliasUsings.OrderBy(o => o.Using!, Comparer).ToList();
}

private class UsingData
Expand All @@ -195,11 +192,6 @@ private static bool IsSystemName(NameSyntax value)
}
}

private class UsingGroup
{
public required List<UsingDirectiveSyntax> Usings { get; init; }
}

private class DefaultOrder : IComparer<UsingDirectiveSyntax>
{
public int Compare(UsingDirectiveSyntax? x, UsingDirectiveSyntax? y)
Expand All @@ -219,10 +211,6 @@ public int Compare(UsingDirectiveSyntax? x, UsingDirectiveSyntax? y)

int Return(int value)
{
DebugLogger.Log(
$"{x.ToFullString().Trim()} {xIsSystem} vs {y.ToFullString().Trim()} {yIsSystem} = {value}"
);

return value;
}

Expand Down

0 comments on commit 9878a55

Please sign in to comment.