Skip to content

Commit

Permalink
Update Formatter.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Oct 23, 2023
1 parent bb6aa27 commit 1d67b04
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Draco.Compiler/Internal/Syntax/Formatting/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Draco.Compiler.Internal.Syntax.Formatting;
/// </summary>
internal sealed class Formatter : SyntaxRewriter
{
private static readonly object Skip = new();
private static readonly object Space = new();
private static readonly object Newline = new();
private static readonly object Newline2 = new();
Expand Down Expand Up @@ -88,6 +89,15 @@ public override SyntaxNode VisitExpressionStatement(ExpressionStatementSyntax no
node.Semicolon,
Newline));

public override SyntaxNode VisitGotoExpression(GotoExpressionSyntax node) => node.Update(this.AppendSequence(
node.GotoKeyword,
Space,
node.Target));

public override SyntaxNode VisitReturnExpression(ReturnExpressionSyntax node) => node.Update(this.AppendSequence(
node.ReturnKeyword,
node.Value is null ? Skip : Space));

public override SyntaxNode VisitIfExpression(IfExpressionSyntax node) => node.Update(this.AppendSequence(
node.IfKeyword,
Space,
Expand All @@ -113,12 +123,27 @@ public override SyntaxNode VisitWhileExpression(WhileExpressionSyntax node) => n
Space,
node.Then));

public override SyntaxNode VisitForExpression(ForExpressionSyntax node) => node.Update(this.AppendSequence(
node.ForKeyword,
Space,
node.OpenParen,
node.Iterator,
node.ElementType,
Space,
node.InKeyword,
Space,
node.Sequence,
node.CloseParen,
Space,
node.Then));

public override SyntaxNode VisitBlockExpression(BlockExpressionSyntax node) => node.Update(this.AppendSequence(
node.OpenBrace,
Newline,
Indent,
node.Statements,
node.Value,
Newline,
Unindent,
node.CloseBrace));

Expand Down Expand Up @@ -155,6 +180,13 @@ public override SyntaxNode VisitValueSpecifier(ValueSpecifierSyntax node) => nod
Space,
node.Value));

public override SyntaxNode VisitLabelDeclaration(LabelDeclarationSyntax node) => node.Update(this.AppendSequence(
Unindent,
node.Name,
node.Colon,
Newline,
Indent));

public override SyntaxNode VisitSyntaxToken(SyntaxToken node)
{
this.AddNormalizedLeadingTrivia(node.LeadingTrivia);
Expand Down Expand Up @@ -189,6 +221,10 @@ public override SyntaxNode VisitSyntaxToken(SyntaxToken node)
{
yield return null;
}
else if (ReferenceEquals(element, Skip))
{
// No-op
}
else if (ReferenceEquals(element, Indent))
{
++this.indentation;
Expand Down

0 comments on commit 1d67b04

Please sign in to comment.