Skip to content

Commit

Permalink
GH-22 figuring out a (kind of ugly) solution to get new lines working…
Browse files Browse the repository at this point in the history
… in trivia.
  • Loading branch information
belav committed Mar 29, 2021
1 parent 69fad5e commit 4dad0d3
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 84 deletions.
6 changes: 6 additions & 0 deletions Src/CSharpier.Tests/TestFiles/Comments/ClassComments.cst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ClassComments.cst" company="CSharpier">
// NewLineAfterThis
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

// public class
public class ClassName { }

Expand Down

This file was deleted.

5 changes: 0 additions & 5 deletions Src/CSharpier.Tests/TestFiles/Comments/_CommentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public void ClassComments()
this.RunTest("Comments", "ClassComments");
}
[Test]
public void LeadingAndTrailingComments()
{
this.RunTest("Comments", "LeadingAndTrailingComments");
}
[Test]
public void MethodComments()
{
this.RunTest("Comments", "MethodComments");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class BasicClass

// leading with line
public // trailing public
// leading identifier
// leading identifier
BasicClass // trailing identifier
(int x) { } // trailing comments1

Expand Down
12 changes: 11 additions & 1 deletion Src/CSharpier.Tests/TestFiles/Directives/Directives.cst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#error Error message
#warning Warning message
#warning Warning new line test

#pragma warning disable 414, 3021
#pragma warning restore 3021
#pragma checksum "file.txt" "{00000000-0000-0000-0000-000000000000}" "2453"
Expand Down Expand Up @@ -36,3 +37,12 @@ namespace Namespace
void MethodName() { }
}
}

#region Region

#region Nested
class ClassName { }
#endregion Nested

#endregion

5 changes: 0 additions & 5 deletions Src/CSharpier.Tests/TestFiles/Directives/Regions.cst

This file was deleted.

5 changes: 0 additions & 5 deletions Src/CSharpier.Tests/TestFiles/Directives/_DirectivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@ public void Directives()
{
this.RunTest("Directives", "Directives");
}
[Test]
public void Regions()
{
this.RunTest("Directives", "Regions");
}
}
}
9 changes: 2 additions & 7 deletions Src/CSharpier/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private Indent MakeIndent(Indent indent, Options options)
{
return this.GenerateIndent(
indent,
new IndentType("indent", 0),
newPart: new IndentType("indent", 0),
options
);
}
Expand Down Expand Up @@ -405,10 +405,6 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
{
if (output.Length > 0)
{
// if we have to undo this, another option I was considering was modifying NamespaceDeclarationSyntax
// when joining things with HardLines there, if the first item in each thing was a LiteralLine, skip the HardLine
// the problem with that approach is we'd have to make that same change in other places, but maybe
// there aren't a ton of those places.
Trim(output);
if (newLine.Length == 2)
{
Expand All @@ -424,7 +420,6 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
output.Length -= 1;
}
}

output.Append(newLine);
position = 0;
}
Expand Down Expand Up @@ -466,7 +461,7 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
}

output.Append(
command.Indent.Value + leadingComment.Comment + newLine + command.Indent.Value
command.Indent.Value + leadingComment.Comment
);
position = command.Indent.Length;
newLineNextStringValue = false;
Expand Down
57 changes: 19 additions & 38 deletions Src/CSharpier/PrinterHelpers/SyntaxTrivia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,49 +65,39 @@ private Doc PrintLeadingTrivia(SyntaxToken syntaxToken)
return this.PrintLeadingTrivia(syntaxToken.LeadingTrivia);
}

// TODO 1 probably ditch this, but leave it around for now
private readonly Stack<bool> printNewLinesInLeadingTrivia = new();

private Doc PrintLeadingTrivia(SyntaxTriviaList leadingTrivia)
{
var parts = new Parts();

this.printNewLinesInLeadingTrivia.TryPeek(out var doNewLines);
// we don't print any new lines until we run into a comment or directive, the PrintNewLines method takes care of printing the initial new lines for a given node
// we also print double HardLines in some cases with directives. That's because a LiteralLine currently trims the previous new line it finds
// If it doesn't, we end up adding extra lines in some situations
// namespace Namespace
// { - HardLine - if we didn't trim this HardLine, then we'd end up inserting a blank line between this and #pragma
// #pragma - LiteralLine, #pragma
// vs
// #region Region - LiteralLine, #region, HardLine - we end each directive with a hardline to ensure we get a double hardline in this situation
// - HardLine - so the literal line trims this hardline, but we retain the newline between the region directives
// #region Nested - LiteralLine, #region, HardLine - because of the double hardline.
var printNewLines = false;

var hadDirective = false;
for (var x = 0; x < leadingTrivia.Count; x++)
{
var trivia = leadingTrivia[x];

if (doNewLines && trivia.Kind() == SyntaxKind.EndOfLineTrivia)
if (
printNewLines
&& trivia.Kind() == SyntaxKind.EndOfLineTrivia
)
{
SyntaxKind? kind = null;
if (x < leadingTrivia.Count - 1)
{
kind = leadingTrivia[x + 1].Kind();
}
// TODO 0 this may screw up with regions that aren't at the beginning of the line? should we deal with new lines/trivia between things differently??
if (
!kind.HasValue
|| kind == SyntaxKind.SingleLineCommentTrivia
|| kind == SyntaxKind.EndOfLineTrivia
|| kind == SyntaxKind.WhitespaceTrivia
)
{
parts.Push(HardLine);
}
parts.Push(HardLine);
}
if (
trivia.Kind() != SyntaxKind.EndOfLineTrivia
&& trivia.Kind() != SyntaxKind.WhitespaceTrivia
)
{
if (doNewLines)
{
doNewLines = false;
this.printNewLinesInLeadingTrivia.Pop();
this.printNewLinesInLeadingTrivia.Push(false);
}
printNewLines = true;
}
if (
trivia.Kind() == SyntaxKind.SingleLineCommentTrivia
Expand Down Expand Up @@ -155,10 +145,7 @@ private Doc PrintLeadingTrivia(SyntaxTriviaList leadingTrivia)
|| trivia.Kind() == SyntaxKind.NullableDirectiveTrivia
)
{
hadDirective = true;
// GH-22 the problem is that we have a hardline followed by a literalline. What we really mean by literalLine is, add a line and dedent if this isn't preceded by a hardline.... I think
// we should probably figure out better new lines for directives, because that could affect this.
parts.Push(LiteralLine, trivia.ToString());
parts.Push(LiteralLine, trivia.ToString(), HardLine);
}
else if (
trivia.Kind() == SyntaxKind.RegionDirectiveTrivia
Expand All @@ -176,16 +163,10 @@ private Doc PrintLeadingTrivia(SyntaxTriviaList leadingTrivia)
triviaText = leadingTrivia[x - 1] + triviaText;
}

hadDirective = true;
parts.Push(LiteralLine, triviaText);
parts.Push(LiteralLine, triviaText, HardLine);
}
}

if (hadDirective)
{
parts.Push(HardLine);
}

return parts.Count > 0 ? Concat(parts) : Doc.Null;
}

Expand Down

0 comments on commit 4dad0d3

Please sign in to comment.