Skip to content

Commit

Permalink
Handle more comments on switch arms
Browse files Browse the repository at this point in the history
closes #1262
  • Loading branch information
belav committed May 18, 2024
1 parent 831e5cc commit 45749a1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,19 @@ class ClassName
)
=> originalField.Type.Equals(updatedField.Type)
};

return someValue switch
{
// comment
Some.One => 1,
Some.Two => 2,
};

return someValue switch
{
Some.One => 1,
// comment
Some.Two => 2,
};
}
}
8 changes: 7 additions & 1 deletion Src/CSharpier/SyntaxPrinter/FormattingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ internal class FormattingContext
// context.State.PrintingDepth
public int PrintingDepth { get; set; }
public bool NextTriviaNeedsLine { get; set; }
public bool ShouldSkipNextLeadingTrivia { get; set; }
public bool SkipNextLeadingTrivia { get; set; }

// we need to keep track if we reordered modifiers because when modifiers are moved inside
// of an #if, then we can't compare the before and after disabled text in the source file
public bool ReorderedModifiers { get; set; }

// we also need to keep track if we move around usings with disabledText
public bool ReorderedUsingsWithDisabledText { get; set; }

public FormattingContext WithSkipNextLeadingTrivia()
{
this.SkipNextLeadingTrivia = true;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ void PrintMethodUnformattedWithoutAttributes(SyntaxTriviaList syntaxTriviaList)
if (modifiers is not { Count: > 0 })
{
docs.Add(Token.PrintLeadingTrivia(returnType.GetLeadingTrivia(), context));
context.ShouldSkipNextLeadingTrivia = true;
context.SkipNextLeadingTrivia = true;
}

declarationGroup.Add(Node.Print(returnType, context), " ");
context.ShouldSkipNextLeadingTrivia = false;
context.SkipNextLeadingTrivia = false;
}

if (explicitInterfaceSpecifier != null)
Expand Down
12 changes: 2 additions & 10 deletions Src/CSharpier/SyntaxPrinter/SyntaxNodePrinters/RecursivePattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ FormattingContext context
node.Parent is SwitchExpressionArmSyntax or CasePatternSwitchLabelSyntax
? Doc.Null
: Doc.SoftLine,
Token.PrintLeadingTrivia(node.PositionalPatternClause.OpenParenToken, context),
Doc.Group(
Token.PrintWithoutLeadingTrivia(
node.PositionalPatternClause.OpenParenToken,
context
),
Token.Print(node.PositionalPatternClause.OpenParenToken, context),
Doc.Indent(
Doc.SoftLine,
SeparatedSyntaxList.Print(
Expand Down Expand Up @@ -73,18 +69,14 @@ node.Parent is SwitchExpressionArmSyntax or CasePatternSwitchLabelSyntax
else
{
result.Add(
Token.PrintLeadingTrivia(node.PropertyPatternClause.OpenBraceToken, context),
Doc.Group(
node.Type != null
&& !node.PropertyPatternClause.OpenBraceToken.LeadingTrivia.Any(o =>
o.IsDirective || o.IsComment()
)
? Doc.Line
: Doc.Null,
Token.PrintWithoutLeadingTrivia(
node.PropertyPatternClause.OpenBraceToken,
context
),
Token.Print(node.PropertyPatternClause.OpenBraceToken, context),
Doc.Indent(
node.PropertyPatternClause.Subpatterns.Any() ? Doc.Line : Doc.Null,
SeparatedSyntaxList.Print(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ public static Doc Print(SwitchExpressionSyntax node, FormattingContext context)
(o, _) =>
Doc.Concat(
ExtraNewLines.Print(o),
Token.PrintLeadingTrivia(
o.Pattern.GetLeadingTrivia(),
context.WithSkipNextLeadingTrivia()
),
Doc.Group(
Node.Print(o.Pattern, context),
o.WhenClause != null ? Node.Print(o.WhenClause, context) : Doc.Null,
Expand Down
4 changes: 2 additions & 2 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private static Doc PrintSyntaxToken(
}

var docs = new List<Doc>();
if (!skipLeadingTrivia && !context.ShouldSkipNextLeadingTrivia)
if (!skipLeadingTrivia && !context.SkipNextLeadingTrivia)
{
var leadingTrivia = PrintLeadingTrivia(syntaxToken, context);
if (leadingTrivia != Doc.Null)
Expand All @@ -48,7 +48,7 @@ private static Doc PrintSyntaxToken(
}
}

context.ShouldSkipNextLeadingTrivia = false;
context.SkipNextLeadingTrivia = false;

if (
(
Expand Down

0 comments on commit 45749a1

Please sign in to comment.