Skip to content

Commit

Permalink
Fixing some breaking early cases with SpaceBrace and leading comments.
Browse files Browse the repository at this point in the history
closes #100
  • Loading branch information
belav committed May 1, 2021
1 parent bdfb94c commit 3e8cb58
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,16 @@ public class BasicClass
public BasicClass(string one) { }

public ClassName() => this.Value = 1;

public BasicClass(string one, string exactly80_____________________________)
{
return;
}

public BasicClass(
int two,
string justOver80________________________________
) {
return;
}
}
17 changes: 17 additions & 0 deletions Src/CSharpier.Tests/TestFiles/FixedStatement/FixedStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,22 @@ class ClassName
) {
*intref = 1;
}

fixed (int* exactly80____________________________ = stackalloc int[100])
{
return;
}

// leading comment doesn't break differently
fixed (int* exactly80____________________________ = stackalloc int[100])
{
return;
}

fixed (
int* justOver80____________________________ = stackalloc int[100]
) {
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,41 @@ public class ClassName
}

foreach (
var entityDefinition in this.EntityDefinitionRepository.GetList()
var x // trailing
in y
) {
return;
}

foreach (var entityDefinition in EntityDefinitionRepository.GetList())
foreach (var exactly80 in Something____________________________________)
{
return;
}

// leading comment doesn't break differently
foreach (var exactly80 in Something____________________________________)
{
return;
}

// leading comment doesn't break differently
await foreach (var exactly80 in Something______________________________)
{
return;
}

foreach (
var x // trailing
in y
var justOver80 in Something____________________________________
) {
return;
}

// leading await
await
// leading using
foreach (var y in Something())
{
return;
}
}
}
19 changes: 19 additions & 0 deletions Src/CSharpier.Tests/TestFiles/ForStatement/ForStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,24 @@ class ClassName
) {
break;
}

for (y = 0; y < exactly80_________________________________________; y++)
{
break;
}

// leading comment doesn't break differently
for (y = 0; y < exactly80_________________________________________; y++)
{
break;
}

for (
y = 0;
y < justOver80_________________________________________;
y++
) {
break;
}
}
}
24 changes: 22 additions & 2 deletions Src/CSharpier.Tests/TestFiles/UsingStatement/UsingStatements.cst
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,36 @@ class ClassName
return;
}

using (var response = await this.SomethingThatIs(JustTooShort))
using (var x = SomeMethodCallThatHitsExactly80_______________________())
{
return;
}

// leading comment doesn't break differently
using (var x = SomeMethodCallThatHitsExactly80_______________________())
{
return;
}

// leading comment doesn't break differently
await using (var x = SomeMethodCallThatHitsExactly80_________________())
{
return;
}

using (
var response = await this.SomethingThatIs(JustLongEnoughhhhhhh)
var x = SomeMethodCallThatIsJustOver80_________________________()
) {
return;
}

// leading await
await
// leading using
using (var y = Something())
{
return;
}
}
}

2 changes: 0 additions & 2 deletions Src/CSharpier/Printer/AnonymousMethodExpressionSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ AnonymousMethodExpressionSyntax node
);
}

// TODO 80

docs.Add(
groupId == null
? this.PrintBlockSyntax(node.Block)
Expand Down
13 changes: 8 additions & 5 deletions Src/CSharpier/Printer/ConstructorDeclarationSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ ConstructorDeclarationSyntax node
) {
var groupId = Guid.NewGuid().ToString();

return Doc.Group(
ExtraNewLines.Print(node),
return Docs.Group(
this.PrintExtraNewLines(node),
this.PrintAttributeLists(node, node.AttributeLists),
Modifiers.Print(node.Modifiers),
Token.Print(node.Identifier),
this.PrintParameterListSyntax(node.ParameterList, groupId),
Docs.Group(
this.PrintModifiers(node.Modifiers),
SyntaxTokens.Print(node.Identifier),
this.PrintParameterListSyntax(node.ParameterList, groupId),
Docs.IfBreak(Docs.Null, Docs.SoftLine)
),
node.Initializer != null
? this.Print(node.Initializer)
: Doc.Null,
Expand Down
29 changes: 17 additions & 12 deletions Src/CSharpier/Printer/FixedStatementSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ private Doc PrintFixedStatementSyntax(FixedStatementSyntax node)
{
var groupId = Guid.NewGuid().ToString();

return Doc.Concat(
ExtraNewLines.Print(node),
this.PrintSyntaxToken(
node.FixedKeyword,
afterTokenIfNoTrailing: " "
return Docs.Concat(
this.PrintExtraNewLines(node),
SyntaxTokens.PrintLeadingTrivia(node.FixedKeyword),
Docs.Group(
SyntaxTokens.PrintWithoutLeadingTrivia(node.FixedKeyword),
" ",
SyntaxTokens.Print(node.OpenParenToken),
Docs.GroupWithId(
groupId,
Docs.Indent(
Docs.SoftLine,
SyntaxNodes.Print(node.Declaration)
),
Docs.SoftLine
),
SyntaxTokens.Print(node.CloseParenToken),
Docs.IfBreak(Docs.Null, Docs.SoftLine)
),
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(Doc.SoftLine, Node.Print(node.Declaration)),
Doc.SoftLine
),
Token.Print(node.CloseParenToken),
node.Statement is BlockSyntax blockSyntax
? this.PrintBlockSyntaxWithConditionalSpace(
blockSyntax,
Expand Down
56 changes: 36 additions & 20 deletions Src/CSharpier/Printer/ForEachStatementSyntax.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using CSharpier.DocTypes;
using CSharpier.SyntaxPrinter;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace CSharpier
Expand All @@ -11,27 +12,42 @@ private Doc PrintForEachStatementSyntax(ForEachStatementSyntax node)
{
var groupId = Guid.NewGuid().ToString();

var result = Doc.Concat(
ExtraNewLines.Print(node),
Token.PrintWithSuffix(node.AwaitKeyword, " "),
Token.Print(node.ForEachKeyword),
" ",
Token.Print(node.OpenParenToken),
Doc.GroupWithId(
groupId,
Doc.Indent(
Doc.SoftLine,
Node.Print(node.Type),
" ",
Token.Print(node.Identifier),
" ",
Token.Print(node.InKeyword),
" ",
Node.Print(node.Expression)
var leadingTrivia = node.AwaitKeyword.Kind() != SyntaxKind.None
? SyntaxTokens.PrintLeadingTrivia(node.AwaitKeyword)
: SyntaxTokens.PrintLeadingTrivia(node.ForEachKeyword);

var docs = Docs.Concat(
this.PrintExtraNewLines(node),
leadingTrivia,
Docs.Group(
SyntaxTokens.PrintWithoutLeadingTrivia(node.AwaitKeyword),
node.AwaitKeyword.Kind() != SyntaxKind.None
? " "
: Docs.Null,
node.AwaitKeyword.Kind() == SyntaxKind.None
? SyntaxTokens.PrintWithoutLeadingTrivia(
node.ForEachKeyword
)
: SyntaxTokens.Print(node.ForEachKeyword),
" ",
SyntaxTokens.Print(node.OpenParenToken),
Docs.GroupWithId(
groupId,
Docs.Indent(
Docs.SoftLine,
SyntaxNodes.Print(node.Type),
" ",
SyntaxTokens.Print(node.Identifier),
" ",
SyntaxTokens.Print(node.InKeyword),
" ",
SyntaxNodes.Print(node.Expression)
),
Docs.SoftLine
),
Doc.SoftLine
SyntaxTokens.Print(node.CloseParenToken),
Docs.IfBreak(Docs.Null, Docs.SoftLine)
),
Token.Print(node.CloseParenToken),
node.Statement is BlockSyntax blockSyntax
? this.PrintBlockSyntaxWithConditionalSpace(
blockSyntax,
Expand All @@ -40,7 +56,7 @@ node.Statement is BlockSyntax blockSyntax
: Node.Print(node.Statement)
);

return result;
return docs;
}
}
}
34 changes: 19 additions & 15 deletions Src/CSharpier/Printer/ForStatementSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ private Doc PrintForStatementSyntax(ForStatementSyntax node)
{
var groupId = Guid.NewGuid().ToString();

var docs = new List<Doc>
{
ExtraNewLines.Print(node),
this.PrintSyntaxToken(
node.ForKeyword,
afterTokenIfNoTrailing: " "
),
Token.Print(node.OpenParenToken)
};

var innerGroup = new List<Doc> { Doc.SoftLine };
var innerGroup = new List<Doc> { Docs.SoftLine };
if (node.Declaration != null)
{
innerGroup.Add(
Expand Down Expand Up @@ -61,11 +51,25 @@ private Doc PrintForStatementSyntax(ForStatementSyntax node)
)
)
);
docs.Add(
Doc.GroupWithId(groupId, Doc.Indent(innerGroup), Doc.SoftLine)
);

docs.Add(Token.Print(node.CloseParenToken));
var docs = new List<Doc>
{
this.PrintExtraNewLines(node),
SyntaxTokens.PrintLeadingTrivia(node.ForKeyword),
Docs.Group(
SyntaxTokens.PrintWithoutLeadingTrivia(node.ForKeyword),
" ",
SyntaxTokens.Print(node.OpenParenToken),
Docs.GroupWithId(
groupId,
Docs.Indent(innerGroup),
Docs.SoftLine
),
SyntaxTokens.Print(node.CloseParenToken),
Docs.IfBreak(Docs.Null, Docs.SoftLine)
)
};

if (node.Statement is BlockSyntax blockSyntax)
{
docs.Add(
Expand Down
Loading

0 comments on commit 3e8cb58

Please sign in to comment.