Skip to content

Commit

Permalink
Merge pull request #59 from belav/GH-22-trivia
Browse files Browse the repository at this point in the history
GH-22 figuring out a (kind of ugly) solution to get new lines working in trivia
  • Loading branch information
belav committed Apr 3, 2021
2 parents bafe726 + 9732a89 commit 4ff7955
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 106 deletions.
51 changes: 34 additions & 17 deletions Src/CSharpier.Tests/Samples/AllInOne.cst
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,36 @@ namespace My

var привет = local;
var мир = local;
var local3 = 0, local4 = 1;
var local3 = 0,
local4 = 1;
local3 = local4 = 1;
var local5 = null as Action ?? null;
var local6 = local5 is Action;

var u = 1u;
var U = 1U;
long hex = 0xBADC0DE, Hex = 0XDEADBEEF, l = -1L, L = 1L, l2 = 2l;
ulong ul = 1ul, Ul = 1Ul, uL = 1uL, UL = 1UL, lu = 1lu, Lu = 1Lu, lU = 1lU, LU = 1LU;
long hex = 0xBADC0DE,
Hex = 0XDEADBEEF,
l = -1L,
L = 1L,
l2 = 2l;
ulong ul = 1ul,
Ul = 1Ul,
uL = 1uL,
UL = 1UL,
lu = 1lu,
Lu = 1Lu,
lU = 1lU,
LU = 1LU;
int minInt32Value = -2147483648;
int minInt64Value = -9223372036854775808L;

bool @bool;
byte @byte;
char @char = 'c', \u0066 = '\u0066', hexchar = '\x0130', hexchar2 = (char)0xBAD;
char @char = 'c',
\u0066 = '\u0066',
hexchar = '\x0130',
hexchar2 = (char)0xBAD;
string \U00000065 = "\U00000065";
decimal @decimal = 1.44M;
@decimal = 1.2m;
Expand Down Expand Up @@ -299,17 +314,20 @@ namespace My
orderby g.Count() ascending
orderby g.Key descending
select new { Country = g.Key, CustCount = g.Count() };
query = from c in customers
select c into d
select d;
query = from c in customers select c into d select d;
}
~A() { }
private readonly int f1;
[Obsolete]
[NonExisting]
[Foo::NonExisting(var, 5)]
[CLSCompliant(false)]
[Obsolete, System.NonSerialized, NonSerialized, CLSCompliant(true || false & true)]
[
Obsolete,
System.NonSerialized,
NonSerialized,
CLSCompliant(true || false & true)
]
private volatile int f2;
[return: Obsolete]
[method: Obsolete]
Expand Down Expand Up @@ -512,9 +530,7 @@ namespace ConsoleApplication1
var x = new Boo.Bar<int>.Foo<object>();
x.Method<string, string>(" ", 5, new object());

var q = from i in new int[] { 1, 2, 3, 4 }
where i > 5
select i;
var q = from i in new int[] { 1, 2, 3, 4 } where i > 5 select i;
}

public static implicit operator Test(string s)
Expand Down Expand Up @@ -584,7 +600,8 @@ namespace ConsoleApplication1
public void Constants()
{
int i = 1 + 2 + 3 + 5;
global::System.String s = "a" + (System.String)"a" + "a" + "a" + "a" + "A";
global::System.String s =
"a" + (System.String)"a" + "a" + "a" + "a" + "A";
}

public void ConstructedType()
Expand Down Expand Up @@ -683,7 +700,7 @@ namespace Comments.XmlComments.UndocumentedKeywords
b = i == i && i != i && i <= i && i >= i; /*= == && != <= >=*/
i += 5.0; /*+=*/
i -= i; /*-=*/
i *= i; /**=*/
i *= i; /* *=*/
i /= i; /*/=*/
i %= i; /*%=*/
i &= i; /*&=*/
Expand All @@ -696,7 +713,7 @@ namespace Comments.XmlComments.UndocumentedKeywords
Point point;
unsafe
{
Point* p = &point; /** &*/
Point* p = &point; /* * &*/
p->x = 10; /*->*/
}
IO::BinaryReader br = null;
Expand Down Expand Up @@ -729,8 +746,8 @@ namespace Comments.XmlComments.UndocumentedKeywords
// Expression bodies on method-like members
public Point Move(int dx, int dy) => new Point(x + dx, y + dy);
public static Complex operator +(Complex a, Complex b) => a.Add(b);
public static implicit operator string(Person p)
=> p.First + " " + p.Last;
public static implicit operator string(Person p) =>
p.First + " " + p.Last;
public void Print() => Console.WriteLine(First + " " + Last);

// Expression bodies on property-like function members
Expand Down Expand Up @@ -817,7 +834,7 @@ class CSharp70
break;
}

(A<B,C> D, E<F,G> H) = e;
(A<B, C> D, E<F, G> H) = e;

if (x?.y?.z is Type value2)
{
Expand Down
9 changes: 9 additions & 0 deletions Src/CSharpier.Tests/TestFiles/Comments/ClassComments.cst
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ClassComments.cst" company="CSharpier">
// NewLineAfterThis
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

// public class
public class ClassName { }

Expand Down Expand Up @@ -26,3 +32,6 @@ ClassName3 // more trailing
// no
readonly string Tester = "lajsdf";
}

/// single line doc comment
class ClassName { }

This file was deleted.

5 changes: 0 additions & 5 deletions Src/CSharpier.Tests/TestFiles/Comments/_CommentsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public void ClassComments()
this.RunTest("Comments", "ClassComments");
}
[Test]
public void LeadingAndTrailingComments()
{
this.RunTest("Comments", "LeadingAndTrailingComments");
}
[Test]
public void MethodComments()
{
this.RunTest("Comments", "MethodComments");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class BasicClass

// leading with line
public // trailing public
// leading identifier
// leading identifier
BasicClass // trailing identifier
(int x) { } // trailing comments1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#region Region
using static System.Linq.Enumerable;

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

2009")]
18 changes: 17 additions & 1 deletion Src/CSharpier.Tests/TestFiles/Directives/Directives.cst
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#error Error message
#warning Warning message
#warning Warning new line test

#pragma warning disable 414, 3021
#pragma warning restore 3021
#pragma checksum "file.txt" "{00000000-0000-0000-0000-000000000000}" "2453"
Expand All @@ -10,6 +11,11 @@
#endif
#undef foo
#line 6
#region Region
using static System.Linq.Enumerable;

#endregion

class ClassName { }

public class ClassName
Expand All @@ -36,3 +42,13 @@ namespace Namespace
void MethodName() { }
}
}

#region Region

#region Nested
class ClassName { }
#endregion Nested

#endregion


Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#region HardToReproduceWithOtherCodeAround
class ClassName { }

#endregion
5 changes: 0 additions & 5 deletions Src/CSharpier.Tests/TestFiles/Directives/Regions.cst

This file was deleted.

5 changes: 0 additions & 5 deletions Src/CSharpier.Tests/TestFiles/Directives/_DirectivesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,5 @@ public void Directives()
{
this.RunTest("Directives", "Directives");
}
[Test]
public void Regions()
{
this.RunTest("Directives", "Regions");
}
}
}
9 changes: 2 additions & 7 deletions Src/CSharpier/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private Indent MakeIndent(Indent indent, Options options)
{
return this.GenerateIndent(
indent,
new IndentType("indent", 0),
newPart: new IndentType("indent", 0),
options
);
}
Expand Down Expand Up @@ -405,10 +405,6 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
{
if (output.Length > 0)
{
// if we have to undo this, another option I was considering was modifying NamespaceDeclarationSyntax
// when joining things with HardLines there, if the first item in each thing was a LiteralLine, skip the HardLine
// the problem with that approach is we'd have to make that same change in other places, but maybe
// there aren't a ton of those places.
Trim(output);
if (newLine.Length == 2)
{
Expand All @@ -424,7 +420,6 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
output.Length -= 1;
}
}

output.Append(newLine);
position = 0;
}
Expand Down Expand Up @@ -466,7 +461,7 @@ void Push(Doc doc, PrintMode printMode, Indent indent)
}

output.Append(
command.Indent.Value + leadingComment.Comment + newLine + command.Indent.Value
command.Indent.Value + leadingComment.Comment
);
position = command.Indent.Length;
newLineNextStringValue = false;
Expand Down
5 changes: 5 additions & 0 deletions Src/CSharpier/Printer/AttributeListSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public partial class Printer
private Doc PrintAttributeListSyntax(AttributeListSyntax node)
{
var parts = new Parts();
if (node.Parent is CompilationUnitSyntax)
{
parts.Push(this.PrintExtraNewLines(node));
}

parts.Push(this.PrintSyntaxToken(node.OpenBracketToken));
if (node.Target != null)
{
Expand Down
8 changes: 5 additions & 3 deletions Src/CSharpier/Printer/CompilationUnitSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ private Doc PrintCompilationUnitSyntax(CompilationUnitSyntax node)
}

var finalTrivia = this.PrintLeadingTrivia(
node.EndOfFileToken.LeadingTrivia
node.EndOfFileToken.LeadingTrivia,
includeInitialNewLines: true
);
if (finalTrivia != null)
if (finalTrivia != Doc.Null)
{
parts.Push(finalTrivia);
// even though we include the initialNewLines above, a literalLine from directives trims the hardline, so add an extra one here
parts.Push(HardLine, finalTrivia);
}

return Concat(parts);
Expand Down
Loading

0 comments on commit 4ff7955

Please sign in to comment.