Skip to content

Commit

Permalink
Progress towards properly indenting regions
Browse files Browse the repository at this point in the history
closes #812
  • Loading branch information
belav committed Jan 30, 2023
1 parent d90fb8a commit ad211bf
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 51 deletions.
18 changes: 9 additions & 9 deletions Src/CSharpier.Tests/CSharpier.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DiffEngine"/>
<PackageReference Include="FluentAssertions"/>
<PackageReference Include="Microsoft.NET.Test.Sdk"/>
<PackageReference Include="NUnit"/>
<PackageReference Include="NUnit3TestAdapter"/>
<PackageReference Include="DiffEngine" />
<PackageReference Include="FluentAssertions" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CSharpier.Cli\CSharpier.Cli.csproj"/>
<ProjectReference Include="..\CSharpier.Tests.Generators\CSharpier.Tests.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\CSharpier\CSharpier.csproj"/>
<ProjectReference Include="..\CSharpier.Cli\CSharpier.Cli.csproj" />
<ProjectReference Include="..\CSharpier.Tests.Generators\CSharpier.Tests.Generators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\CSharpier\CSharpier.csproj" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="FormattingTests\TestFiles\*.cst"/>
<AdditionalFiles Include="FormattingTests\TestFiles\*.cst" />
</ItemGroup>
<ItemGroup>
<Content Include="FormattingTests\TestFiles\*.actual.cst">
Expand Down
22 changes: 0 additions & 22 deletions Src/CSharpier.Tests/FormattingTests/TestFiles/Directives.cst
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,11 @@
#endif
#undef foo
#line 6
#region Region
using static System.Linq.Enumerable;

#endregion

class ClassName { }

public class ClassName
{
#region Region
private int x; // trailing comment here shouldn't give extra indent to the endregion after it
#endregion

public bool SomeProperty =>
#if !DEBUG
someValue
Expand Down Expand Up @@ -62,14 +54,6 @@ namespace Namespace
}
}

#region Region

#region Nested
class ClassName { }
#endregion Nested

#endregion

#if DIRECTIVES_AROUND_MODIFIERS_BREAK_CORRECTLY
public
#else
Expand All @@ -79,12 +63,6 @@ static class ConditionallyPublic { }

class EdgeCases
{
string RegionsIndentAndNewLineProperly =
#region one
@"using System;"
#endregion one
;

void MethodWithNestedIfsDoesNotGetExtraLineBreak()
{
#if DEBUG
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class ClassName
{
#region Region
public int LongUglyMethod() => 42;
#endregion
}

public class ClassName
{
#region Region
public int LongUglyMethod() => 42;

#endregion
}

#region class
public class ClassName
{
#region method
public void Method()
{
#region content
#endregion
}
#endregion
}
#endregion

public class ClassName
{
#region Region
private int x; // trailing comment here shouldn't give extra indent to the endregion after it
#endregion

private int y;

// TODO is this a real thing?
string RegionsIndentAndNewLineProperly =
#region one
@"using System;"
#endregion one
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ public class ClassName

public class ClassName
{
#region RegionWithSpaceBeforeAndAfterMethod
#region RegionWithSpaceBeforeAndAfterMethod

void SomeMethod() { }

#endregion
#endregion

void SomeMethod() { }
}
10 changes: 9 additions & 1 deletion Src/CSharpier/DocPrinter/DocPrinter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@ private void ProcessNextCommand()
}
else if (doc is IndentDoc indentDoc)
{
this.Push(indentDoc.Contents, mode, this.Indenter.IncreaseIndent(indent));
var nextIndent = this.Indenter.IncreaseIndent(indent);

if (indentDoc.EnsureIndent)
{
this.Output.TrimTrailingWhitespace();
this.Output.Append(nextIndent.Value);
}

this.Push(indentDoc.Contents, mode, nextIndent);
}
else if (doc is Trim)
{
Expand Down
3 changes: 3 additions & 0 deletions Src/CSharpier/DocTypes/Doc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ public static Group GroupWithId(string groupId, params Doc[] contents)

public static IndentDoc Indent(List<Doc> contents) => new() { Contents = Concat(contents) };

public static IndentDoc EnsureIndent(params Doc[] contents) =>
new() { Contents = Concat(contents), EnsureIndent = true };

public static Doc IndentIf(bool condition, Doc contents)
{
return condition ? Indent(contents) : contents;
Expand Down
1 change: 1 addition & 0 deletions Src/CSharpier/DocTypes/IndentDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ namespace CSharpier.DocTypes;
internal class IndentDoc : Doc, IHasContents
{
public Doc Contents { get; set; } = Null;
public bool EnsureIndent { get; set; }
}
60 changes: 43 additions & 17 deletions Src/CSharpier/SyntaxPrinter/Token.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,53 @@ public static Doc PrintLeadingTrivia(SyntaxToken syntaxToken, FormattingContext
{
var isClosingBrace = syntaxToken.RawSyntaxKind() == SyntaxKind.CloseBraceToken;

var printedTrivia = PrivatePrintLeadingTrivia(
syntaxToken.LeadingTrivia,
context,
skipLastHardline: isClosingBrace
);

if (syntaxToken.RawSyntaxKind() != SyntaxKind.CloseBraceToken)
{
return printedTrivia;
}

Doc extraNewLines = Doc.Null;

if (isClosingBrace && syntaxToken.LeadingTrivia.Any(o => o.IsDirective || o.IsComment()))
if (syntaxToken.LeadingTrivia.Any(o => o.IsDirective || o.IsComment()))
{
extraNewLines = ExtraNewLines.Print(syntaxToken.LeadingTrivia);
}

var printedTrivia = PrivatePrintLeadingTrivia(
syntaxToken.LeadingTrivia,
context,
skipLastHardline: isClosingBrace
);
if (
syntaxToken.LeadingTrivia.Any(
o => o.RawSyntaxKind() is SyntaxKind.EndRegionDirectiveTrivia
)
&& syntaxToken.LeadingTrivia.All(
o =>
o.RawSyntaxKind()
is SyntaxKind.EndOfLineTrivia
or SyntaxKind.WhitespaceTrivia
or SyntaxKind.EndRegionDirectiveTrivia
)
)
{
var docs = new List<Doc> { extraNewLines };
foreach (
var trivia in syntaxToken.LeadingTrivia.Where(
o => o.RawSyntaxKind() is SyntaxKind.EndRegionDirectiveTrivia
)
)
{
docs.Add(trivia.ToFullString().TrimEnd('\n', '\r'));
docs.Add(Doc.HardLine);
}
docs.RemoveAt(docs.Count - 1);

return isClosingBrace && (printedTrivia != Doc.Null || extraNewLines != Doc.Null)
return Doc.Concat(Doc.EnsureIndent(Doc.Concat(docs)), Doc.HardLine);
}

return printedTrivia != Doc.Null || extraNewLines != Doc.Null
? Doc.Concat(
extraNewLines,
Doc.IndentIf(printedTrivia != Doc.Null, printedTrivia),
Expand Down Expand Up @@ -189,21 +222,14 @@ void AddLeadingComment(CommentType commentType)
else if (IsDirective(kind) || IsRegion(kind))
{
var triviaText = trivia.ToString();
if (
IsRegion(kind)
&& x > 0
&& leadingTrivia[x - 1].RawSyntaxKind() == SyntaxKind.WhitespaceTrivia
)
{
triviaText = leadingTrivia[x - 1] + triviaText;
}

docs.Add(
// adding two of these to ensure we get a new line when a directive follows a trailing comment
Doc.HardLineIfNoPreviousLineSkipBreakIfFirstInGroup,
Doc.HardLineIfNoPreviousLineSkipBreakIfFirstInGroup,
Doc.Trim,
Doc.Directive(triviaText),
IsRegion(kind)
? Doc.Indent(triviaText)
: Doc.Concat(Doc.Trim, Doc.Directive(triviaText)),
Doc.HardLineSkipBreakIfFirstInGroup
);

Expand Down

0 comments on commit ad211bf

Please sign in to comment.