Skip to content

Commit

Permalink
Include new line before comments that end a block
Browse files Browse the repository at this point in the history
closes #513
  • Loading branch information
belav committed Dec 20, 2021
1 parent dcd11ca commit 1086bc1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
12 changes: 12 additions & 0 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/Comments.cst
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,15 @@ public class ClassName
// regular comment does not cause breaking
IReadOnlyList<IArgument> ICommand.Arguments => Arguments;
}

class ClassName
{
void MethodName()
{
var x = 1;

// keep line before this
}

// keep line before this
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,7 @@ public static Doc Print(BaseTypeDeclarationSyntax node)
Token.Print(node.OpenBraceToken),
members,
Doc.HardLine,
node.CloseBraceToken.LeadingTrivia.Any(
o => o.Kind() == SyntaxKind.DisabledTextTrivia
)
? Doc.Concat(
Token.PrintLeadingTriviaWithNewLines(node.CloseBraceToken.LeadingTrivia),
Token.PrintWithoutLeadingTrivia(node.CloseBraceToken)
)
: Token.Print(node.CloseBraceToken)
Token.Print(node.CloseBraceToken)
);
}
else if (node.OpenBraceToken.Kind() != SyntaxKind.None)
Expand Down
7 changes: 4 additions & 3 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ is InterpolatedStringExpressionSyntax

public static Doc PrintLeadingTrivia(SyntaxToken syntaxToken)
{
var indentTrivia = syntaxToken.Kind() == SyntaxKind.CloseBraceToken;
var isClosingBrace = syntaxToken.Kind() == SyntaxKind.CloseBraceToken;

var printedTrivia = PrivatePrintLeadingTrivia(
syntaxToken.LeadingTrivia,
skipLastHardline: indentTrivia
includeInitialNewLines: isClosingBrace,
skipLastHardline: isClosingBrace
);

return indentTrivia && printedTrivia != Doc.Null
return isClosingBrace && printedTrivia != Doc.Null
? Doc.Concat(Doc.Indent(printedTrivia), Doc.HardLine)
: printedTrivia;
}
Expand Down

0 comments on commit 1086bc1

Please sign in to comment.