Skip to content

Commit

Permalink
Improving formatting of InvocationExpressions (#444)
Browse files Browse the repository at this point in the history
* Cleaning up the doc tree

* Code review changes. Adding way to hide nulls on doc tree. Some minor performance tweaks

* Formatting files

* Moving away from SpaceBrace

closes #423

* A little cleanup

* Fixing extra indent in conditionals

Closes #7

* Cleaning up the doc tree

* Making some progress on chained invocations

Fixing bug with ConditionalGroup

* Switch back to printedNode

* A little progress, possibly backwards

* Possibly forwards this time

* Testing notes

* Merge predefinedTypes

* Changing my mind again

* Baby step forward

* Another baby step

* Fixing issues with ConditionalAccessExpression

* Fix case where chain contains a hardline

* Fixing comment edge case

* Another edge case

* more notes

* Merge when identifier is short

* Handling more edge cases

* Some cleanup

* Fixing edge case + some cleanup

* Review changes + more efficient DocSerializer
  • Loading branch information
belav authored Oct 18, 2021
1 parent ecfd219 commit c1a0e40
Show file tree
Hide file tree
Showing 12 changed files with 890 additions and 176 deletions.
26 changes: 26 additions & 0 deletions Src/CSharpier.Tests/DocPrinterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,32 @@ public void Conditional_Group_Does_Not_Propagate_Breaks_To_Parent()
PrintedDocShouldBe(doc, "1 2", 10);
}

[Test]
public void Conditional_Group_Prints_Alternative_If_Initial_Group_Does_Not_Fit()
{
var doc = Doc.ConditionalGroup(
Doc.Concat(
Doc.Group("(", Doc.SoftLine, "1111111", ")"),
Doc.Group("(", Doc.SoftLine, "1111111", ")")
),
Doc.Concat("(1111111)", Doc.HardLine, "(1111111)")
);
PrintedDocShouldBe(doc, $"(1111111){NewLine}(1111111)", 10);
}

[Test]
public void Conditional_Group_Prints_Initial_Group_If_It_Fit()
{
var doc = Doc.ConditionalGroup(
Doc.Concat(
Doc.Group("(", Doc.SoftLine, "1", ")"),
Doc.Group("(", Doc.SoftLine, "1", ")")
),
Doc.Concat("(1)", Doc.HardLine, "(1)")
);
PrintedDocShouldBe(doc, "(1)(1)", 10);
}

[Test]
public void Align_Should_Print_Basic_Case()
{
Expand Down
233 changes: 233 additions & 0 deletions Src/CSharpier.Tests/DocSerializerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
using CSharpier.DocTypes;
using FluentAssertions;
using NUnit.Framework;

namespace CSharpier.Tests
{
[TestFixture]
public class DocSerializerTests
{
[Test]
public void Should_Format_Directive()
{
var doc = Doc.Directive("1");

var actual = DocSerializer.Serialize(doc);

actual.Should().Be("Doc.Directive(\"1\")");
}

[Test]
public void Should_Format_Basic_Types()
{
var doc = Doc.Concat(
Doc.Line,
Doc.LiteralLine,
Doc.HardLine,
Doc.HardLineIfNoPreviousLine,
Doc.HardLineSkipBreakIfFirstInGroup,
Doc.HardLineIfNoPreviousLineSkipBreakIfFirstInGroup,
Doc.SoftLine,
Doc.Null,
Doc.Trim,
"1"
);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.Concat(
Doc.Line,
Doc.LiteralLine,
Doc.HardLine,
Doc.HardLineIfNoPreviousLine,
Doc.HardLineSkipBreakIfFirstInGroup,
Doc.HardLineIfNoPreviousLineSkipBreakIfFirstInGroup,
Doc.SoftLine,
Doc.Null,
Doc.Trim,
""1""
)"
);
}

[Test]
public void Should_Print_Basic_Group()
{
var doc = Doc.Group(Doc.Null, Doc.Null);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.Group(
Doc.Null,
Doc.Null
)"
);
}

[Test]
public void Should_Print_Group_With_Id()
{
var doc = Doc.GroupWithId("1", Doc.Null, Doc.Null);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.GroupWithId(
""1"",
Doc.Null,
Doc.Null
)"
);
}

[Test]
public void Should_Print_ConditionalGroup()
{
var doc = Doc.ConditionalGroup(
Doc.Concat(Doc.Line, Doc.Line),
Doc.Concat(Doc.LiteralLine, Doc.LiteralLine)
);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.ConditionalGroup(
Doc.Concat(
Doc.Line,
Doc.Line
),
Doc.Concat(
Doc.LiteralLine,
Doc.LiteralLine
)
)"
);
}

[Test]
public void Should_Print_Align()
{
var doc = Doc.Align(2, Doc.Null, Doc.Null);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.Align(
2,
Doc.Null,
Doc.Null
)"
);
}

[Test]
public void Should_Print_ForceFlat()
{
var doc = Doc.ForceFlat(Doc.Null, Doc.Null);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.ForceFlat(
Doc.Null,
Doc.Null
)"
);
}

[Test]
public void Should_Print_Indent()
{
var doc = Doc.Indent(Doc.Null, Doc.Null);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.Indent(
Doc.Null,
Doc.Null
)"
);
}

[Test]
public void Should_Print_IndentIfBreak()
{
var doc = Doc.IndentIfBreak(Doc.Null, "1");

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.IndentIfBreak(
Doc.Null,
""1""
)"
);
}

[Test]
public void Should_Print_IfBreak_With_Id()
{
var doc = Doc.IfBreak(Doc.Null, Doc.Line, "1");

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.IfBreak(
Doc.Null,
Doc.Line,
""1""
)"
);
}

[Test]
public void Should_Print_IfBreak_Without_Id()
{
var doc = Doc.IfBreak(Doc.Null, Doc.Line);

var actual = DocSerializer.Serialize(doc);

actual.Should()
.Be(
@"Doc.IfBreak(
Doc.Null,
Doc.Line
)"
);
}

[TestCase(CommentType.SingleLine)]
[TestCase(CommentType.MultiLine)]
public void Should_Print_LeadingComment(CommentType commentType)
{
var doc = Doc.LeadingComment("1", commentType);

var actual = DocSerializer.Serialize(doc);

actual.Should().Be(@$"Doc.LeadingComment(""1"", CommentType.{commentType})");
}

[TestCase(CommentType.SingleLine)]
[TestCase(CommentType.MultiLine)]
public void Should_Print_TrailingComment(CommentType commentType)
{
var doc = Doc.TrailingComment("1", commentType);

var actual = DocSerializer.Serialize(doc);

actual.Should().Be(@$"Doc.TrailingComment(""1"", CommentType.{commentType})");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class TestClass
) ?? someOtherValue;

var notIdealSee355 =
variable.Replace(someParameter_______________________, '.')
variable
.Replace(someParameter_______________________, '.')
.Replace(someParameter_______________________, '.') + ".";

UglyButConsistentWithPrettier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ public class ClassName
from l2 in Enumerable.Range(1, 7)
from l3 in Enumerable.Range(1, 7)
select (l2, l3)
).AsParallel()
)
.AsParallel()
.Select(l => (l, res: GenerateTable(l.l2, l.l3, cutOff)))
.OrderBy(v => v.res.Total)
)
Expand Down
Loading

0 comments on commit c1a0e40

Please sign in to comment.