Skip to content

Commit

Permalink
Fixing issues with weird indent on raw string literals as arguments (#…
Browse files Browse the repository at this point in the history
…1175)

* Fixing issues with weird indent on raw string literals as arguments

* format file

* update comments

* Update InvocationExpression.cs

* tracking down the problem of raw string literals being weird as arguments when they start an invocation.

* Update DocPrinterTests.cs

* little fix
  • Loading branch information
belav authored Feb 17, 2024
1 parent 0b239f6 commit 3a3807f
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 8 deletions.
23 changes: 23 additions & 0 deletions Src/CSharpier.Tests/DocPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,29 @@ public void Print_Should_Include_Single_NewLine_To_End_File(int instances, strin
result.Should().Be($"1{endOfLine}");
}

[Test]
public void ConditionalGroup_Does_Not_Propagate_Breaks()
{
var doc = Doc.Group(
Doc.ConditionalGroup(
Doc.Concat("1", Doc.HardLine, "2"),
Doc.Concat("1", Doc.HardLine, "2")
),
Doc.SoftLine,
"3"
);
// this seems odd, but this is how conditional groups work
// I assume partly because if only one of the potential groups contains a hardline
// then you don't want to always break the conditional group
PrintedDocShouldBe(
doc,
"""
1
23
"""
);
}

[Test]
public void Scratch()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,39 @@ CallMethod(
TrailingSpaceHere
"""
);

CallMethod(
"""
SomeRawString
""".CallMethod()
);

CallMethod(
"""
SomeRawString
""".CallMethod().CallMethod()
);

CallMethod(
"""
SomeRawString
""".CallMethod().CallMethod().CallMethod()
);

CallMethod(
$"""
SomeRawString
""".CallMethod()
);

CallMethod(
$"""
SomeRawString
""".CallMethod().CallMethod()
);

CallMethod(
$"""
SomeRawString
""".CallMethod().CallMethod().CallMethod()
);
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,23 @@ four
}
"""
);

CallMethod(
@"
SomeVerbatimString
".CallMethod()
);

CallMethod(
@"
SomeVerbatimString
".CallMethod().CallMethod()
);

CallMethod(
@"
SomeVerbatimString
".CallMethod().CallMethod().CallMethod()
);
}
}
4 changes: 1 addition & 3 deletions Src/CSharpier/DocPrinter/PropagateBreaks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ bool OnEnter(Doc doc)
{
canSkipBreak = true;
groupStack.Push(group);
if (alreadyVisitedSet.Contains(group))
if (!alreadyVisitedSet.Add(group))
{
return false;
}

alreadyVisitedSet.Add(group);
}
else if (doc is StringDoc { IsDirective: false })
{
Expand Down
22 changes: 22 additions & 0 deletions Src/CSharpier/SyntaxNodeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace CSharpier;

internal static class SyntaxNodeExtensions
{
public static bool HasParent(this SyntaxNode? node, Type theType)
{
while (true)
{
if (node?.Parent?.GetType() == theType)
{
return true;
}

if (node is null)
{
return false;
}

node = node.Parent;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,6 @@ private static Doc RawString(InterpolatedStringExpressionSyntax node, Formatting
contents.Add(lastLineIsIndented ? Doc.HardLineNoTrim : Doc.LiteralLine);
contents.Add(Token.Print(node.StringEndToken, context));

return Doc.IndentIf(node.Parent is not ArgumentSyntax, Doc.Concat(contents));
return Doc.IndentIf(!node.HasParent(typeof(ArgumentSyntax)), Doc.Concat(contents));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,30 @@ or PostfixUnaryExpressionSyntax
)
);

if (forceOneLine)
if (
forceOneLine
// this handles the case of a multiline string being part of an invocation chain
// conditional groups don't propagate breaks so we need to avoid the conditional group
|| groups[0]
.Any(o =>
o.Node
is LiteralExpressionSyntax
{
Token.RawKind: (int)SyntaxKind.MultiLineRawStringLiteralToken
}
or InterpolatedStringExpressionSyntax
{
StringStartToken.RawKind: (int)
SyntaxKind.InterpolatedMultiLineRawStringStartToken
}
|| o.Node
is LiteralExpressionSyntax
{
Token.Text.Length: > 0
} literalExpressionSyntax
&& literalExpressionSyntax.Token.Text.Contains('\n')
)
)
{
return Doc.Group(oneLine);
}
Expand Down
6 changes: 3 additions & 3 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ is InterpolatedStringExpressionSyntax

contents.Add(linesIncludingQuotes[^1].TrimStart());

docs.Add(
Doc.IndentIf(syntaxToken.Parent?.Parent is not ArgumentSyntax, Doc.Concat(contents))
);
var hasArgumentParent = syntaxToken.Parent.HasParent(typeof(ArgumentSyntax));

docs.Add(Doc.IndentIf(!hasArgumentParent, Doc.Concat(contents)));
}
else if (
syntaxToken.RawSyntaxKind()
Expand Down

0 comments on commit 3a3807f

Please sign in to comment.