Skip to content

Commit

Permalink
Finally a good solution to verbatim string literals
Browse files Browse the repository at this point in the history
closes #183
  • Loading branch information
belav committed May 14, 2021
1 parent 465f217 commit f7380db
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
[assembly: System.Copyright(@"(C)""

2009")]
[assembly: System.Copyright("(C) 2009")]
[module: System.Copyright("\n\t\u0123(C) \"2009" + "\u0123")]
[Obsolete]
class ClassName
Expand Down
31 changes: 31 additions & 0 deletions Src/CSharpier.Tests/TestFiles/StringLiteral/VerbatimStrings.cst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[assembly: System.Copyright(@"(C)""
2009")]
class ClassName
{
private string stayOnLine1 = @"one
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
";

private string stayOnLine2 = @"one
two
three
four";

private void MethodName()
{
CallSomeLongMethod(@"one
two
three
four
jhaksdlfklasdfjlkasdjflaksdfklasldkjfljkasdljfkasljkdfakljsdfjlkaskfjlaskjldfksdjlf
");

CallSomeLongMethod(
@"one
two
three
four",
"two"
);
}
}
14 changes: 14 additions & 0 deletions Src/CSharpier.Tests/TestFiles/StringLiteral/_StringLiteralTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using CSharpier.Tests.TestFileTests;
using NUnit.Framework;

namespace CSharpier.Tests.TestFiles
{
public class StringLiteralTests : BaseTest
{
[Test]
public void VerbatimStrings()
{
this.RunTest("StringLiteral", "VerbatimStrings");
}
}
}
10 changes: 0 additions & 10 deletions Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,6 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
newLineNextStringValue = false;
}

// Option B
// if (endOfLine == "\r\n")
// {
// output.Append(stringDoc.Value.Replace("\n", "\r\n"));
// }
// else
// {
// output.Append(stringDoc.Value);
// }

output.Append(stringDoc.Value);

currentWidth += GetStringWidth(stringDoc.Value);
Expand Down
9 changes: 2 additions & 7 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ public static Doc PrintSyntaxToken(
syntaxToken.Kind() == SyntaxKind.StringLiteralToken
&& syntaxToken.Text.StartsWith("@")
) {
// this is for option B, where we replace line endings here, and then replace them back in DocPrinter
// but that feels gross. But this also feels gross because of how we store/lookup the line ending.
// option B also just happens to fix the lineEndings affect printed output problem, but doesn't really fix it properly
// docs.Add(syntaxToken.Text.Replace("\r\n", "\n"));

var lineEnding = LineEndings.GetLineEnding(syntaxToken.SyntaxTree!);
docs.Add(Regex.Replace(syntaxToken.Text, @"\r\n?|\n", lineEnding));
var lines = syntaxToken.Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
docs.Add(Doc.Join(Doc.LiteralLine, lines.Select(o => new StringDoc(o))));
}
else
{
Expand Down

0 comments on commit f7380db

Please sign in to comment.