Skip to content

Commit

Permalink
Only sort modifiers that we know should be sorted. (#1124)
Browse files Browse the repository at this point in the history
* Only sort modifiers that we know should be sorted.

closes #1123

* formatting files
  • Loading branch information
belav authored Jan 15, 2024
1 parent bbbbbab commit 3c8022c
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ClassName
{
// we could add ref to the sorting logic to ensure this stays in the correct order of `ref readonly`
// but being explicit about only sorting Modifiers on specific SyntaxNodes seems safer long term
void Method(ref readonly int someParameter) { }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
public class ClassName
CallMethod(static async () => { });

CallMethod(static async o => { });

public static class ClassName
{
public static int Field;

public static int Property { public private get; set; }

public static async MethodName() { }
}

public partial class PartialClass { }

protected internal delegate void D();
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
public class ClassName
CallMethod(async static () => { });

CallMethod(async static o => { });

static public class ClassName
{
static public int Field;

static public int Property { private public get; set; }

async static public MethodName() { }
}

public partial class PartialClass { }
partial public class PartialClass { }

internal protected delegate void D ();
12 changes: 11 additions & 1 deletion Src/CSharpier/SyntaxPrinter/Modifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ private static int GetIndex(string? value)
private static readonly DefaultOrder Comparer = new();

public static Doc Print(SyntaxTokenList modifiers, FormattingContext context)
{
if (modifiers.Count == 0)
{
return Doc.Null;
}

return Doc.Group(Doc.Join(" ", modifiers.Select(o => Token.Print(o, context))), " ");
}

public static Doc PrintSorted(SyntaxTokenList modifiers, FormattingContext context)
{
return PrintWithSortedModifiers(
modifiers,
Expand All @@ -50,7 +60,7 @@ public static Doc Print(SyntaxTokenList modifiers, FormattingContext context)
);
}

public static Doc PrintWithoutLeadingTrivia(
public static Doc PrintSorterWithoutLeadingTrivia(
SyntaxTokenList modifiers,
FormattingContext context
)
Expand Down
5 changes: 1 addition & 4 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ internal static class Argument
{
public static Doc Print(ArgumentSyntax node, FormattingContext context)
{
return Doc.Concat(
Argument.PrintModifiers(node, context),
Node.Print(node.Expression, context)
);
return Doc.Concat(PrintModifiers(node, context), Node.Print(node.Expression, context));
}

public static Doc PrintModifiers(ArgumentSyntax node, FormattingContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static Doc Print(BaseFieldDeclarationSyntax node, FormattingContext conte
var docs = new List<Doc>
{
AttributeLists.Print(node, node.AttributeLists, context),
Modifiers.Print(node.Modifiers, context)
Modifiers.PrintSorted(node.Modifiers, context)
};
if (node is EventFieldDeclarationSyntax eventFieldDeclarationSyntax)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,9 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
if (modifiers is { Count: > 0 })
{
docs.Add(Token.PrintLeadingTrivia(modifiers.Value[0], context));
declarationGroup.Add(Modifiers.PrintWithoutLeadingTrivia(modifiers.Value, context));
declarationGroup.Add(
Modifiers.PrintSorterWithoutLeadingTrivia(modifiers.Value, context)
);
}

if (returnType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static Doc Print(BasePropertyDeclarationSyntax node, FormattingContext co
return Doc.Group(
Doc.Concat(
Doc.Concat(docs),
Modifiers.Print(node.Modifiers, context),
Modifiers.PrintSorted(node.Modifiers, context),
eventKeyword,
Node.Print(node.Type, context),
" ",
Expand Down Expand Up @@ -126,7 +126,7 @@ FormattingContext context
}

docs.Add(AttributeLists.Print(node, node.AttributeLists, context));
docs.Add(Modifiers.Print(node.Modifiers, context));
docs.Add(Modifiers.PrintSorted(node.Modifiers, context));
docs.Add(Token.Print(node.Keyword, context));

if (node.Body != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static Doc Print(BaseTypeDeclarationSyntax node, FormattingContext contex

if (node.Modifiers.Any())
{
docs.Add(Modifiers.Print(node.Modifiers, context));
docs.Add(Modifiers.PrintSorted(node.Modifiers, context));
}

if (recordKeyword != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static Doc Print(DelegateDeclarationSyntax node, FormattingContext contex
var docs = new List<Doc>
{
AttributeLists.Print(node, node.AttributeLists, context),
Modifiers.Print(node.Modifiers, context),
Modifiers.PrintSorted(node.Modifiers, context),
Token.PrintWithSuffix(node.DelegateKeyword, " ", context),
Node.Print(node.ReturnType, context),
{ " ", Token.Print(node.Identifier, context) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static Doc PrintHead(ParenthesizedLambdaExpressionSyntax node, Formatting
{
return Doc.Concat(
AttributeLists.Print(node, node.AttributeLists, context),
Modifiers.Print(node.Modifiers, context),
Modifiers.PrintSorted(node.Modifiers, context),
node.ReturnType != null
? Doc.Concat(Node.Print(node.ReturnType, context), " ")
: Doc.Null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static Doc Print(SimpleLambdaExpressionSyntax node, FormattingContext con
public static Doc PrintHead(SimpleLambdaExpressionSyntax node, FormattingContext context)
{
return Doc.Concat(
Modifiers.Print(node.Modifiers, context),
Modifiers.PrintSorted(node.Modifiers, context),
Node.Print(node.Parameter, context),
" ",
Token.Print(node.ArrowToken, context)
Expand Down

0 comments on commit 3c8022c

Please sign in to comment.