Skip to content

Commit

Permalink
GH-22 remove newlines before literallines, otherwise we get extra new…
Browse files Browse the repository at this point in the history
… liens
  • Loading branch information
belav committed Mar 6, 2021
1 parent 1a23d08 commit ae9aa37
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
32 changes: 31 additions & 1 deletion Src/CSharpier.Tests/DocPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ public void ForceFlat_Prevents_Breaking()

result.Should().Be("1 2");
}

[Test]
public void LiteralLine_Trims_Space()
{
var doc = Concat("{", Indent(HardLine,"indent", LiteralLine), "}");

var result = this.Print(doc);

result.Should().Be("{\r\n indent\r\n}");
}

[Test]
public void HardLine_LiteralLine_Skips_HardLine_And_Trims()
{
var doc = Concat("{", Indent(HardLine, LiteralLine,"noindent"), HardLine, "}");

var result = this.Print(doc);

result.Should().Be("{\r\nnoindent\r\n}");
}

[Test]
public void HardLine_LiteralLine_Skips_HardLine()
{
var doc = Concat("1", HardLine, LiteralLine, "2");

var result = this.Print(doc);

result.Should().Be("1\r\n2");
}

[Test]
public void ForceFlat_Prevents_Breaking_With_Long_Content()
Expand Down Expand Up @@ -213,7 +243,7 @@ public static Doc Concat(params Doc[] parts)
return Printer.Concat(parts);
}

public static Doc Indent(Doc contents)
public static Doc Indent(params Doc[] contents)
{
return Printer.Indent(contents);
}
Expand Down
8 changes: 0 additions & 8 deletions Src/CSharpier.Tests/Samples/AllInOne.Formatted.cst
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,23 @@ using System.Linq.Expressions;
using System.Text;
using M = System.Math;


#if DEBUG || TRACE
using System.Diagnostics;
#elif SILVERLIGHT && WINDOWS_PHONE || DEBUG || foo == true || foo != false
using System.Diagnostics;
#else
using System.Diagnostics;

#endif
#region Region
#region more
using ConsoleApplication2.Test;

#endregion
using X = int1;
using Y = ABC.X<int>;

using static System.Math;
using static System.DayOfWeek;
using static System.Linq.Enumerable;

#endregion
[assembly: System.Copyright(@"(C)""

Expand Down Expand Up @@ -76,7 +72,6 @@ namespace My
var s2 = $@"x {1, -2:d}";
}


#if DEBUG
Console.WriteLine(export.iefSupplied.command);
#endif
Expand Down Expand Up @@ -417,15 +412,13 @@ namespace My
A,
B = A,
C = 2 + A,

#if DEBUG
D,
}
#else
E,

}

#endif
public delegate void Delegate(object P);
namespace Test
Expand Down Expand Up @@ -849,7 +842,6 @@ namespace Comments.XmlComments.UndocumentedKeywords
}
}
}

#line 6
#line 2 "test.cs"
#line default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class ClassName

namespace Namespace
{

#pragma
class ExtraLineChecker { }
}
27 changes: 19 additions & 8 deletions Src/CSharpier/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,22 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
{
if (output.Length > 0)
{
Trim(output);
if (newLine.Length == 2)
{
if (output[^2] == '\r')
{
output.Length -= 2;
}
}
else
{
if (output[^1] == '\n')
{
output.Length -= 1;
}
}

output.Append(newLine);
position = 0;
}
Expand Down Expand Up @@ -497,28 +513,23 @@ private int GetStringWidth(string value)
return value.Length;
}

private int Trim(StringBuilder stringBuilder)
private void Trim(StringBuilder stringBuilder)
{
if (stringBuilder.Length == 0)
{
return 0;
return;
}

var trimCount = 0;

var i = stringBuilder.Length - 1;
for (; i >= 0; i--)
{
if (stringBuilder[i] != ' ' && stringBuilder[i] != '\t')
{
break;
}

trimCount++;
}

stringBuilder.Length = i + 1;

return trimCount;
}

// // TODO 2 does the above method do the same thing as this method?
Expand Down
5 changes: 0 additions & 5 deletions Src/CSharpier/Printer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ public static Doc ForceFlat(params Doc[] contents)
};
}

public static Doc String(string value)
{
return new StringDoc(value);
}

public static Doc Join(Doc separator, IEnumerable<Doc> array)
{
var parts = new Parts();
Expand Down

0 comments on commit ae9aa37

Please sign in to comment.