Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Add code formatting rule to add newlines at the end of code files #127

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<Compile Include="Rules\ExplicitVisibilityRuleTests.cs" />
<Compile Include="Rules\HasNoIllegalHeadersFormattingRuleTests.cs" />
<Compile Include="Rules\NewLineAboveRuleTests.cs" />
<Compile Include="Rules\NewLineAtEndOfFileRuleTests.cs" />
<Compile Include="Rules\PrivateFieldNamingRuleTests.cs" />
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs" />
<Compile Include="Rules\MarkReadonlyFieldTests.cs" />
Expand Down
21 changes: 14 additions & 7 deletions src/Microsoft.DotNet.CodeFormatting.Tests/Rules/CombinationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ private void M()
{
N(_field);
}
}";
}
";

Verify(text, expected, runFormatter: false);
}
Expand All @@ -96,7 +97,8 @@ private void M()
{
_field = 42;
}
}";
}
";

Verify(text, expected, runFormatter: false);
}
Expand All @@ -119,7 +121,8 @@ internal class C
#if DOG
void M() { }
#endif
}";
}
";

Verify(text, expected, runFormatter: false);
}
Expand All @@ -145,7 +148,8 @@ internal void M()
{
}
#endif
}";
}
";

s_formattingEngine.PreprocessorConfigurations = ImmutableArray.CreateRange(new[] { new[] { "DOG" } });
Verify(text, expected, runFormatter: false);
Expand Down Expand Up @@ -179,7 +183,8 @@ private void G()
void M() {
}
#endif
}";
}
";

Verify(text, expected, runFormatter: false);
}
Expand Down Expand Up @@ -219,7 +224,8 @@ void G()
void M() {
}
#endif
}";
}
";

s_formattingEngine.PreprocessorConfigurations = ImmutableArray.CreateRange(new[] { new[] { "TEST" } });
Verify(text, expected, runFormatter: false);
Expand Down Expand Up @@ -262,7 +268,8 @@ private void M()
{
}
}
}";
}
";

Verify(source, expected, runFormatter: false);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.DotNet.CodeFormatting.Tests
{
public class NewLineAtEndOfFileRuleTests : SyntaxRuleTestBase
{
internal override ISyntaxFormattingRule Rule
{
get { return new Rules.NewLineAtEndOfFileRule(); }
}

[Fact]
public void SimpleClassWithoutNewLineAtEndOfFile()
{
var text = @"using System;
public class TestClass
{
}";

var expected = @"using System;
public class TestClass
{
}
";

Verify(text, expected);
}

[Fact]
public void SimpleClassWithNewLineAtEndOfFile()
{
var text = @"using System;
public class TestClass
{
}
";

Verify(text, text);
}

[Fact]
public void CommentAtEndOfFile()
{
var text = @"using System;
public class TestClass
{
}
// Hello World";

var expected = @"using System;
public class TestClass
{
}
// Hello World
";

Verify(text, expected);
}

[Fact]
public void IfDefAtEndOfFile()
{
var text = @"using System;
public class TestClass
{
}
#if TEST
#endif";

var expected = @"using System;
public class TestClass
{
}
#if TEST
#endif
";

Verify(text, expected);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
<Compile Include="NameHelper.cs" />
<Compile Include="ResponseFileWorkspace.cs" />
<Compile Include="Rules\MarkReadonlyFieldsRule.cs" />
<Compile Include="Rules\NewLineAtEndOfFileRule.cs" />
<Compile Include="SemaphoreLock.cs" />
<Compile Include="SyntaxUtil.cs" />
<Compile Include="Filters\FilenameFilter.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Microsoft.DotNet.CodeFormatting.Rules
{
[SyntaxRule(SyntaxRuleOrder.NewLineAtEndOfFileRule)]
internal sealed class NewLineAtEndOfFileRule : CSharpOnlyFormattingRule, ISyntaxFormattingRule
{
public SyntaxNode Process(SyntaxNode syntaxRoot, string languageName)
{
bool needsNewLine;
var endOfFileToken = syntaxRoot.GetLastToken(true, true, true, true);
if (!endOfFileToken.IsKind(SyntaxKind.EndOfFileToken))
{
throw new InvalidOperationException("Expected last token to be EndOfFileToken, was actually: " + endOfFileToken.Kind());
}

if (endOfFileToken.HasLeadingTrivia)
{
return AddNewLineToEndOfFileTokenLeadingTriviaIfNecessary(syntaxRoot, endOfFileToken);
}

var lastToken = syntaxRoot.GetLastToken();
if (!lastToken.HasTrailingTrivia)
{
needsNewLine = true;
}
else
{
var lastTrivia = lastToken.TrailingTrivia.Last();
if (lastTrivia.IsKind(SyntaxKind.EndOfLineTrivia))
{
needsNewLine = false;
}
else
{
needsNewLine = true;
}
}

if (needsNewLine)
{
var newLine = SyntaxUtil.GetBestNewLineTriviaRecursive(lastToken.Parent);
var newLastToken = lastToken.WithTrailingTrivia(lastToken.TrailingTrivia.Concat(new[] { newLine }));
return syntaxRoot.ReplaceToken(lastToken, newLastToken);
}
return syntaxRoot;
}

SyntaxNode AddNewLineToEndOfFileTokenLeadingTriviaIfNecessary(SyntaxNode syntaxRoot, SyntaxToken endofFileToken)
{
if (endofFileToken.LeadingTrivia.Last().IsKind(SyntaxKind.EndOfLineTrivia))
{
return syntaxRoot;
}

var newLine = SyntaxUtil.GetBestNewLineTriviaRecursive(endofFileToken.Parent);
var newLastToken = endofFileToken.WithTrailingTrivia(endofFileToken.TrailingTrivia.Concat(new[] { newLine }));
return syntaxRoot.ReplaceToken(endofFileToken, newLastToken);


return syntaxRoot;
}
}
}
1 change: 1 addition & 0 deletions src/Microsoft.DotNet.CodeFormatting/Rules/RuleOrder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal static class SyntaxRuleOrder
public const int CopyrightHeaderRule = 2;
public const int UsingLocationFormattingRule = 3;
public const int NewLineAboveFormattingRule = 4;
public const int NewLineAtEndOfFileRule = 5;
public const int BraceNewLineRule = 6;
public const int NonAsciiChractersAreEscapedInLiterals = 7;
}
Expand Down
17 changes: 17 additions & 0 deletions src/Microsoft.DotNet.CodeFormatting/SyntaxUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ internal static SyntaxTrivia GetBestNewLineTrivia(SyntaxNode node, SyntaxTrivia?
return defaultNewLineTrivia ?? SyntaxFactory.CarriageReturnLineFeed;
}

internal static SyntaxTrivia GetBestNewLineTriviaRecursive(SyntaxNode node, SyntaxTrivia? defaultNewLineTrivia = null)
{
while(node != null)
{
SyntaxTrivia trivia;
if (TryGetExistingNewLine(node.GetLeadingTrivia(), out trivia) ||
TryGetExistingNewLine(node.GetTrailingTrivia(), out trivia))
{
return trivia;
}

node = node.Parent;
}

return defaultNewLineTrivia ?? SyntaxFactory.CarriageReturnLineFeed;
}

internal static SyntaxTrivia GetBestNewLineTrivia(SyntaxToken token, SyntaxTrivia? defaultNewLineTrivia = null)
{
SyntaxTrivia trivia;
Expand Down