Skip to content

Commit

Permalink
formatting files
Browse files Browse the repository at this point in the history
  • Loading branch information
belav committed May 21, 2022
1 parent 174b6f0 commit 6a01f85
Show file tree
Hide file tree
Showing 50 changed files with 213 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Src/CSharpier.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ CancellationToken cancellationToken
try
{
var document = Node.Print(rootNode, context);
var document = Node.Print(rootNode);
var lineEnding = GetLineEnding(code, printerOptions);
var formattedCode = DocPrinter.DocPrinter.Print(
document,
Expand Down
5 changes: 1 addition & 4 deletions Src/CSharpier/SyntaxPrinter/FormattingContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
namespace CSharpier.SyntaxPrinter;

public class FormattingContext
{

}
public class FormattingContext { }
6 changes: 5 additions & 1 deletion Src/CSharpier/SyntaxPrinter/NamespaceLikePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ namespace CSharpier.SyntaxPrinter;

internal static class NamespaceLikePrinter
{
public static void Print(BaseNamespaceDeclarationSyntax node, List<Doc> docs, FormattingContext context)
public static void Print(
BaseNamespaceDeclarationSyntax node,
List<Doc> docs,
FormattingContext context
)
{
Print(node, node.Externs, node.Usings, node.Members, docs, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public static Doc Print(AnonymousObjectCreationExpressionSyntax node, Formatting
SeparatedSyntaxList.Print(
node.Initializers,
AnonymousObjectMemberDeclarator.Print,
Doc.Line, context
Doc.Line,
context
)
)
: Doc.Null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ or PostfixUnaryExpressionSyntax
Parent: { Parent: InvocationExpressionSyntax }
}
},
ArgumentListLike.Print(node.OpenParenToken, node.Arguments, node.CloseParenToken, context)
ArgumentListLike.Print(
node.OpenParenToken,
node.Arguments,
node.CloseParenToken,
context
)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public static Doc Print(ArrayCreationExpressionSyntax node, FormattingContext co
return Doc.Group(
Token.PrintWithSuffix(node.NewKeyword, " ", context),
Node.Print(node.Type, context),
node.Initializer != null ? Doc.Concat(Doc.Line, Node.Print(node.Initializer, context)) : Doc.Null
node.Initializer != null
? Doc.Concat(Doc.Line, Node.Print(node.Initializer, context))
: Doc.Null
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public static Doc Print(BaseExpressionColonSyntax node, FormattingContext contex
node.ColonToken,
node.Parent is SubpatternSyntax { Pattern: RecursivePatternSyntax { Type: null } }
? Doc.Line
: " ", context
: " ",
context
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ public static Doc Print(BaseFieldDeclarationSyntax node, FormattingContext conte
docs.Add(Token.PrintWithSuffix(eventFieldDeclarationSyntax.EventKeyword, " ", context));
}

docs.Add(VariableDeclaration.Print(node.Declaration, context), Token.Print(node.SemicolonToken, context));
docs.Add(
VariableDeclaration.Print(node.Declaration, context),
Token.Print(node.SemicolonToken, context)
);
return Doc.Concat(docs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,14 @@ public static Doc Print(CSharpSyntaxNode node, FormattingContext context)
declarationGroup.Add(
Token.PrintWithSuffix(
conversionOperatorDeclarationSyntax.ImplicitOrExplicitKeyword,
" ", context
" ",
context
),
Token.PrintWithSuffix(
conversionOperatorDeclarationSyntax.OperatorKeyword,
" ",
context
),
Token.PrintWithSuffix(conversionOperatorDeclarationSyntax.OperatorKeyword, " ", context),
Node.Print(conversionOperatorDeclarationSyntax.Type, context)
);
}
Expand Down Expand Up @@ -160,7 +165,9 @@ public static Doc Print(CSharpSyntaxNode node, FormattingContext context)
if (constructorInitializer != null)
{
var colonToken = Token.PrintWithSuffix(constructorInitializer.ColonToken, " ", context);
var argumentList = Doc.Group(ArgumentList.Print(constructorInitializer.ArgumentList, context));
var argumentList = Doc.Group(
ArgumentList.Print(constructorInitializer.ArgumentList, context)
);

declarationGroup.Add(
Doc.Group(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ internal static class ClassOrStructConstraint
{
public static Doc Print(ClassOrStructConstraintSyntax node, FormattingContext context)
{
return Doc.Concat(Token.Print(node.ClassOrStructKeyword, context), Token.Print(node.QuestionToken, context));
return Doc.Concat(
Token.Print(node.ClassOrStructKeyword, context),
Token.Print(node.QuestionToken, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ public static Doc Print(CompilationUnitSyntax node, FormattingContext context)

NamespaceLikePrinter.Print(node, docs, context);

var finalTrivia = Token.PrintLeadingTriviaWithNewLines(node.EndOfFileToken.LeadingTrivia, context);
var finalTrivia = Token.PrintLeadingTriviaWithNewLines(
node.EndOfFileToken.LeadingTrivia,
context
);
if (finalTrivia != Doc.Null)
{
// even though we include the initialNewLines above, a literalLine from directives trims the hardline, so add an extra one here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public static Doc Print(ConditionalExpressionSyntax node, FormattingContext cont
node.Parent is ReturnStatementSyntax
&& node.Condition is BinaryExpressionSyntax or IsPatternExpressionSyntax
? Doc.Indent(
Doc.Group(Doc.IfBreak(Doc.SoftLine, Doc.Null), Node.Print(node.Condition, context))
Doc.Group(
Doc.IfBreak(Doc.SoftLine, Doc.Null),
Node.Print(node.Condition, context)
)
)
: Node.Print(node.Condition, context),
node.Parent is ConditionalExpressionSyntax or ArgumentSyntax or ReturnStatementSyntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ internal static class DeclarationExpression
{
public static Doc Print(DeclarationExpressionSyntax node, FormattingContext context)
{
return Doc.Concat(Node.Print(node.Type, context), " ", Node.Print(node.Designation, context));
return Doc.Concat(
Node.Print(node.Type, context),
" ",
Node.Print(node.Designation, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public static Doc Print(DoStatementSyntax node, FormattingContext context)
ExtraNewLines.Print(node),
Token.PrintWithSuffix(
node.DoKeyword,
node.Statement is not BlockSyntax ? " " : Doc.Null, context
node.Statement is not BlockSyntax ? " " : Doc.Null,
context
),
Node.Print(node.Statement, context),
node.Statement is BlockSyntax ? " " : Doc.HardLine,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ internal static class FinallyClause
{
public static Doc Print(FinallyClauseSyntax node, FormattingContext context)
{
return Doc.Concat(Token.Print(node.FinallyKeyword, context), Node.Print(node.Block, context));
return Doc.Concat(
Token.Print(node.FinallyKeyword, context),
Node.Print(node.Block, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public static Doc Print(FixedStatementSyntax node, FormattingContext context)
Token.Print(node.FixedKeyword, context),
" ",
Token.Print(node.OpenParenToken, context),
Doc.Group(Doc.Indent(Doc.SoftLine, Node.Print(node.Declaration, context)), Doc.SoftLine),
Doc.Group(
Doc.Indent(Doc.SoftLine, Node.Print(node.Declaration, context)),
Doc.SoftLine
),
Token.Print(node.CloseParenToken, context),
Doc.IfBreak(Doc.Null, Doc.SoftLine)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public static Doc Print(ForStatementSyntax node, FormattingContext context)
Doc.Line,
Doc.Group(
Doc.Indent(
SeparatedSyntaxList.Print(node.Incrementors, Node.Print, Doc.Line, context)
SeparatedSyntaxList.Print(
node.Incrementors,
Node.Print,
Doc.Line,
context
)
)
)
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ internal static class GenericName
{
public static Doc Print(GenericNameSyntax node, FormattingContext context)
{
return Doc.Group(Token.Print(node.Identifier, context), Node.Print(node.TypeArgumentList, context));
return Doc.Group(
Token.Print(node.Identifier, context),
Node.Print(node.TypeArgumentList, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ internal static class GotoStatement
public static Doc Print(GotoStatementSyntax node, FormattingContext context)
{
var expression =
node.Expression != null ? Doc.Concat(" ", Node.Print(node.Expression, context)) : string.Empty;
node.Expression != null
? Doc.Concat(" ", Node.Print(node.Expression, context))
: string.Empty;
return Doc.Concat(
ExtraNewLines.Print(node),
Token.Print(node.GotoKeyword, context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public static Doc Print(IfStatementSyntax node, FormattingContext context)
Token.Print(node.IfKeyword, context),
" ",
Token.Print(node.OpenParenToken, context),
Doc.Group(Doc.Indent(Doc.SoftLine, Node.Print(node.Condition, context)), Doc.SoftLine),
Doc.Group(
Doc.Indent(Doc.SoftLine, Node.Print(node.Condition, context)),
Doc.SoftLine
),
Token.Print(node.CloseParenToken, context)
),
OptionalBraces.Print(node.Statement, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ namespace CSharpier.SyntaxPrinter.SyntaxNodePrinters;

internal static class ImplicitStackAllocArrayCreationExpression
{
public static Doc Print(ImplicitStackAllocArrayCreationExpressionSyntax node, FormattingContext context)
public static Doc Print(
ImplicitStackAllocArrayCreationExpressionSyntax node,
FormattingContext context
)
{
return Doc.Concat(
Token.Print(node.StackAllocKeyword, context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public static Doc Print(InitializerExpressionSyntax node, FormattingContext cont
SeparatedSyntaxList.Print(
node.Expressions,
Node.Print,
alwaysBreak ? Doc.HardLine : Doc.Line, context
alwaysBreak ? Doc.HardLine : Doc.Line,
context
)
),
node.Expressions.Any()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ internal static class InterpolatedStringExpression
{
public static Doc Print(InterpolatedStringExpressionSyntax node, FormattingContext context)
{
var docs = new List<Doc> { Token.PrintWithoutLeadingTrivia(node.StringStartToken, context) };
var docs = new List<Doc>
{
Token.PrintWithoutLeadingTrivia(node.StringStartToken, context)
};

docs.AddRange(node.Contents.Select(o => Node.Print(o, context)));
docs.Add(Token.Print(node.StringEndToken, context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ internal static class Interpolation
{
public static Doc Print(InterpolationSyntax node, FormattingContext context)
{
var docs = new List<Doc> { Token.Print(node.OpenBraceToken, context), Node.Print(node.Expression, context) };
var docs = new List<Doc>
{
Token.Print(node.OpenBraceToken, context),
Node.Print(node.Expression, context)
};
if (node.AlignmentClause != null)
{
docs.Add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,22 @@ And we want to work with them from Left to Right
}
else if (expression is ConditionalAccessExpressionSyntax conditionalAccessExpressionSyntax)
{
FlattenAndPrintNodes(conditionalAccessExpressionSyntax.Expression, printedNodes, context);
FlattenAndPrintNodes(
conditionalAccessExpressionSyntax.Expression,
printedNodes,
context
);
printedNodes.Add(
new PrintedNode(
conditionalAccessExpressionSyntax,
Token.Print(conditionalAccessExpressionSyntax.OperatorToken, context)
)
);
FlattenAndPrintNodes(conditionalAccessExpressionSyntax.WhenNotNull, printedNodes, context);
FlattenAndPrintNodes(
conditionalAccessExpressionSyntax.WhenNotNull,
printedNodes,
context
);
}
else if (
expression is PostfixUnaryExpressionSyntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ public static Doc Print(IsPatternExpressionSyntax node, FormattingContext contex
{
return Doc.Group(
Node.Print(node.Expression, context),
Doc.Indent(Doc.Line, Token.Print(node.IsKeyword, context), " ", Node.Print(node.Pattern, context))
Doc.Indent(
Doc.Line,
Token.Print(node.IsKeyword, context),
" ",
Node.Print(node.Pattern, context)
)
);
}

Expand Down
6 changes: 5 additions & 1 deletion Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/NameEquals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ internal static class NameEquals
{
public static Doc Print(NameEqualsSyntax node, FormattingContext context)
{
return Doc.Concat(Node.Print(node.Name, context), " ", Token.PrintWithSuffix(node.EqualsToken, " ", context));
return Doc.Concat(
Node.Print(node.Name, context),
" ",
Token.PrintWithSuffix(node.EqualsToken, " ", context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ internal static class NullableType
{
public static Doc Print(NullableTypeSyntax node, FormattingContext context)
{
return Doc.Concat(Node.Print(node.ElementType, context), Token.Print(node.QuestionToken, context));
return Doc.Concat(
Node.Print(node.ElementType, context),
Token.Print(node.QuestionToken, context)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public static Doc Print(ObjectCreationExpressionSyntax node, FormattingContext c
ArgumentListLike.Print(
node.ArgumentList.OpenParenToken,
node.ArgumentList.Arguments,
node.ArgumentList.CloseParenToken, context
node.ArgumentList.CloseParenToken,
context
)
)
: Doc.Null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public static Doc Print(OrderByClauseSyntax node, FormattingContext context)
: " ",
Token.Print(orderingNode.AscendingOrDescendingKeyword, context)
),
Doc.Null, context
Doc.Null,
context
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public static Doc Print(ParameterListSyntax node, FormattingContext context)
? Doc.Concat(
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(node.Parameters, Parameter.Print, Doc.Line, context)
SeparatedSyntaxList.Print(
node.Parameters,
Parameter.Print,
Doc.Line,
context
)
),
Doc.SoftLine
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ public static Doc Print(ParenthesizedLambdaExpressionSyntax node, FormattingCont
{
AttributeLists.Print(node, node.AttributeLists, context),
Modifiers.Print(node.Modifiers, context),
node.ReturnType != null ? Doc.Concat(Node.Print(node.ReturnType, context), " ") : Doc.Null,
node.ReturnType != null
? Doc.Concat(Node.Print(node.ReturnType, context), " ")
: Doc.Null,
ParameterList.Print(node.ParameterList, context),
" ",
Token.Print(node.ArrowToken, context)
Expand Down
Loading

0 comments on commit 6a01f85

Please sign in to comment.