Skip to content

Commit

Permalink
Merge pull request #69 from belav/gh-40-extra-lines
Browse files Browse the repository at this point in the history
closes #40 - empty line handling
  • Loading branch information
belav committed Apr 10, 2021
2 parents 81ebaba + e22f9ab commit 5132dbc
Show file tree
Hide file tree
Showing 15 changed files with 547 additions and 144 deletions.
164 changes: 164 additions & 0 deletions Src/CSharpier.Tests/DocUtilitiesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Tests
{
public class DocUtilitiesTests
{
[Test]
public void RemoveInitialDoubleHardLine_Should_Handle_Empty_List()
{
var doc = new List<Doc>();

DocUtilities.RemoveInitialDoubleHardLine(doc);

doc.Should().BeEmpty();
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Null()
{
var doc = new List<Doc> { Docs.Null };

DocUtilities.RemoveInitialDoubleHardLine(doc);

doc.Should().BeEmpty();
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Simple_HardLine()
{
var doc = new List<Doc> { Docs.HardLine };

DocUtilities.RemoveInitialDoubleHardLine(doc);

doc.Should().HaveCount(1);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Simple_Double_HardLine()
{
var doc = new List<Doc> { Docs.HardLine, Docs.HardLine };

DocUtilities.RemoveInitialDoubleHardLine(doc);

doc.Should().HaveCount(1);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Concated_HardLine()
{
var concat = Docs.Concat(Docs.HardLine);
var doc = new List<Doc> { concat };

DocUtilities.RemoveInitialDoubleHardLine(doc);

concat.Parts.Should().BeEquivalentTo(Docs.HardLine);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Concated_HardLine()
{
var concat = Docs.Concat(Docs.HardLine, Docs.HardLine);
var doc = new List<Doc> { concat };

DocUtilities.RemoveInitialDoubleHardLine(doc);

concat.Parts.Should().ContainSingle();
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Deep_Concated_HardLine()
{
var concat = Docs.Concat(Docs.HardLine);
var doc = new List<Doc> { Docs.Concat(concat) };

DocUtilities.RemoveInitialDoubleHardLine(doc);

concat.Parts.Should().BeEquivalentTo(Docs.HardLine);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Deep_Concated_HardLine()
{
var concat = Docs.Concat(Docs.HardLine, Docs.HardLine);
var doc = new List<Doc> { Docs.Concat(concat) };

DocUtilities.RemoveInitialDoubleHardLine(doc);

concat.Parts.Should().ContainSingle();
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Single_HardLine()
{
var concat = Docs.Concat(
Docs.HardLine,
Docs.HardLine,
Docs.HardLine
);
var doc = new List<Doc> { Docs.Concat(concat) };

DocUtilities.RemoveInitialDoubleHardLine(doc);

concat.Parts.Should().HaveCount(2);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Indented_HardLine()
{
var indent = Docs.Indent(Docs.HardLine);
var doc = new List<Doc> { indent };

DocUtilities.RemoveInitialDoubleHardLine(doc);

indent.Contents.Should().BeEquivalentTo(Docs.HardLine);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Indented_HardLine()
{
var indent = Docs.Indent(Docs.HardLine);
var doc = new List<Doc> { Docs.HardLine, indent };

DocUtilities.RemoveInitialDoubleHardLine(doc);

indent.Contents.Should().Be(Docs.Null);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Not_Remove_Deep_Indented_HardLine()
{
var indent = Docs.Indent(Docs.HardLine);
var doc = new List<Doc> { Docs.Indent(indent) };

DocUtilities.RemoveInitialDoubleHardLine(doc);

indent.Contents.Should().BeEquivalentTo(Docs.HardLine);
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Remove_Grouped_Double_HardLine()
{
var contents = new List<Doc> { Docs.HardLine, Docs.HardLine };
var doc = Docs.Group(contents);

DocUtilities.RemoveInitialDoubleHardLine(doc);

contents.Should().ContainSingle();
}

[Test]
public void RemoveInitialDoubleHardLine_Should_Only_Remove_Initial_HardLines()
{
var doc = Docs.Concat("1", Docs.HardLine, Docs.HardLine);

DocUtilities.RemoveInitialDoubleHardLine(doc);

doc.Should()
.BeEquivalentTo(Docs.Concat("1", Docs.HardLine, Docs.HardLine));
}
}
}
1 change: 0 additions & 1 deletion Src/CSharpier.Tests/TestFiles/Comments/ClassComments.cst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ static // trailing
class // trailing
ClassName2 { }


// this
public
// is
Expand Down
35 changes: 35 additions & 0 deletions Src/CSharpier.Tests/TestFiles/EmptyLines/EmptyLines.cst
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Namespace
{

public class RemovesLineBefore { }


public class RemovesDoubledLines { }

public class RemoveTrailingLine { }

}

public class ClassName
{

public int RemoveLineBefore;


public int RemoveDoubleLines;

public void MethodName()
{

var removeLineBefore = 1;


var removeDoubleLines = 2;

var removeTrailingLine = 3;

}

public int RemoveTrailingLine;

}
26 changes: 26 additions & 0 deletions Src/CSharpier.Tests/TestFiles/EmptyLines/EmptyLines.expected.cst
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Namespace
{
public class RemovesLineBefore { }

public class RemovesDoubledLines { }

public class RemoveTrailingLine { }
}

public class ClassName
{
public int RemoveLineBefore;

public int RemoveDoubleLines;

public void MethodName()
{
var removeLineBefore = 1;

var removeDoubleLines = 2;

var removeTrailingLine = 3;
}

public int RemoveTrailingLine;
}
14 changes: 14 additions & 0 deletions Src/CSharpier.Tests/TestFiles/EmptyLines/_EmptyLineTests.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 EmptyLineTests : BaseTest
{
[Test]
public void EmptyLines()
{
this.RunTest("EmptyLines", "EmptyLines");
}
}
}
29 changes: 13 additions & 16 deletions Src/CSharpier/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ namespace CSharpier
{
public class Doc
{
public bool IsHardLine()
{
return this is Concat concat
&& concat.Parts.FirstOrDefault() is LineDoc { Type: LineDoc.LineType.Hard } ;
}

public static implicit operator Doc(string value)
{
return new StringDoc(value);
Expand All @@ -19,6 +13,18 @@ public bool IsHardLine()
public static NullDoc Null { get; } = NullDoc.Instance;
}

public class HardLine : Concat
{
public HardLine()
: base(
new List<Doc>
{
new LineDoc { Type = LineDoc.LineType.Hard },
new BreakParent()
}
) { }
}

public class NullDoc : Doc
{
public static NullDoc Instance { get; } = new NullDoc();
Expand Down Expand Up @@ -80,21 +86,12 @@ public class ForceFlat : Doc, IHasContents
public Doc Contents { get; set; } = Doc.Null;
}

// should possibly be used by ternary operator
public class Align : Doc, IHasContents
{
public Doc Contents { get; set; } = Doc.Null;
}

public class Fill : Doc, IHasContents
{
public Doc Contents { get; set; } = Doc.Null;
}

public class LineSuffix : Doc, IHasContents
{
public Doc Contents { get; set; } = Doc.Null;
}

public class LeadingComment : Doc
{
public CommentType Type { get; set; }
Expand Down
1 change: 1 addition & 0 deletions Src/CSharpier/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace CSharpier
// a big chunk of the code in here is ported from prettier. The names and layout of the file were
// kept consistent with how they looked in prettier because not everything
// was ported over and porting over more code would be easier if this file looked basically the same
// taken from prettier 2.2.1 or so
public static class DocPrinter
{
private static Indent RootIndent()
Expand Down
28 changes: 14 additions & 14 deletions Src/CSharpier/DocTreePrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static string PrintDocTree(Doc document, string indent)
switch (document)
{
case NullDoc:
return indent + "Doc.Null";
return indent + "Docs.Null";
case StringDoc stringDoc:
return indent + "\"" + stringDoc.Value?.Replace(
"\"",
Expand All @@ -32,11 +32,11 @@ private static string PrintDocTree(Doc document, string indent)
)
{
return indent + (line.IsLiteral
? "LiteralLine"
: "HardLine");
? "Docs.LiteralLine"
: "Docs.HardLine");
}

var result = indent + "Concat(";
var result = indent + "Docs.Concat(";
if (concat.Parts.Count > 0)
{
result += Environment.NewLine;
Expand All @@ -58,35 +58,35 @@ private static string PrintDocTree(Doc document, string indent)
return result;
case LineDoc lineDoc:
return indent + (lineDoc.IsLiteral
? "LiteralLine"
? "Docs.LiteralLine"
: lineDoc.Type == LineDoc.LineType.Normal
? "Line"
? "Docs.Line"
: lineDoc.Type == LineDoc.LineType.Hard
? "HardLine"
: "SoftLine");
? "Docs.HardLine"
: "Docs.SoftLine");
case BreakParent:
return "";
case ForceFlat forceFlat:
return indent + "ForceFlat(" + Environment.NewLine + PrintDocTree(
return indent + "Docs.ForceFlat(" + Environment.NewLine + PrintDocTree(
forceFlat.Contents,
indent + " "
) + ")";
case IndentDoc indentDoc:
return indent + "Indent(" + Environment.NewLine + PrintDocTree(
return indent + "Docs.Indent(" + Environment.NewLine + PrintDocTree(
indentDoc.Contents,
indent + " "
) + ")";
case Group group:
return indent + "Group(" + Environment.NewLine + PrintDocTree(
return indent + "Docs.Group(" + Environment.NewLine + PrintDocTree(
group.Contents,
indent + " "
) + ")";
case LeadingComment leadingComment:
return $"{indent}LeadingComment(\"{leadingComment.Comment}\", CommentType.{(leadingComment.Type == CommentType.SingleLine ? "SingleLine" : "MultiLine")})";
return $"{indent}Docs.LeadingComment(\"{leadingComment.Comment}\", CommentType.{(leadingComment.Type == CommentType.SingleLine ? "SingleLine" : "MultiLine")})";
case TrailingComment trailingComment:
return $"{indent}TrailingComment(\"{trailingComment.Comment}\", CommentType.{(trailingComment.Type == CommentType.SingleLine ? "SingleLine" : "MultiLine")})";
return $"{indent}Docs.TrailingComment(\"{trailingComment.Comment}\", CommentType.{(trailingComment.Type == CommentType.SingleLine ? "SingleLine" : "MultiLine")})";
case SpaceIfNoPreviousComment:
return indent + "SpaceIfNoPreviousComment";
return indent + "Docs.SpaceIfNoPreviousComment";
default:
throw new Exception("Can't handle " + document);
}
Expand Down

0 comments on commit 5132dbc

Please sign in to comment.