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

Commit e3041f2

Browse files
author
Lakshmi Priya Sekar
committed
Add rule 'NoNewLineBeforeEndBrace'
1 parent 7ba96d7 commit e3041f2

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="Properties\AssemblyInfo.cs" />
8181
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
8282
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRule.cs" />
83+
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRule.cs" />
8384
<Compile Include="Rules\HasNoXmlBasedCopyrightHeaderFormattingRule.cs" />
8485
<Compile Include="Rules\HasUsingsOutsideOfNamespaceFormattingRule.cs" />
8586
<Compile Include="Rules\IsFormattedFormattingRule.cs" />

src/Microsoft.DotNet.CodeFormatting/Rules/HasNoNewLineAfterOpenBraceFormattingRule.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,12 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
2626
{
2727
var triviaItem = token.LeadingTrivia.First();
2828
int elementsToRemove = 1;
29-
while (triviaItem == token.LeadingTrivia.ElementAt(elementsToRemove))
30-
elementsToRemove++;
29+
if (token.LeadingTrivia.Count > 1)
30+
{
31+
while (triviaItem == token.LeadingTrivia.ElementAt(elementsToRemove))
32+
elementsToRemove++;
33+
}
34+
3135
var newToken = token.WithLeadingTrivia(token.LeadingTrivia.Skip(elementsToRemove));
3236
return newToken;
3337
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under MIT. See LICENSE in the project root for license information.
3+
using System;
4+
using System.Collections.Generic;
5+
using System.ComponentModel.Composition;
6+
using System.Linq;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
10+
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.CSharp;
12+
13+
namespace Microsoft.DotNet.CodeFormatting.Rules
14+
{
15+
[Export(typeof(IFormattingRule))]
16+
internal sealed class HasNoNewLineBeforeEndBraceFormattingRule : IFormattingRule
17+
{
18+
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
19+
{
20+
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
21+
if (syntaxRoot == null)
22+
return document;
23+
24+
var closeBraceTokens = syntaxRoot.DescendantTokens().Where((token) => token.CSharpKind() == SyntaxKind.CloseBraceToken);
25+
Func<SyntaxToken, SyntaxToken, SyntaxToken> replacementForTokens = (token, dummy) =>
26+
{
27+
var triviaItem = token.LeadingTrivia.First();
28+
int elementsToRemove = 1;
29+
if (token.LeadingTrivia.Count > 1)
30+
{
31+
while (triviaItem == token.LeadingTrivia.ElementAt(elementsToRemove))
32+
elementsToRemove++;
33+
}
34+
35+
var newToken = token.WithLeadingTrivia(token.LeadingTrivia.Skip(elementsToRemove));
36+
return newToken;
37+
};
38+
39+
var tokensToReplace = closeBraceTokens.Where((token) => token.HasLeadingTrivia && token.LeadingTrivia.First().CSharpKind() == SyntaxKind.EndOfLineTrivia);
40+
41+
return document.WithSyntaxRoot(syntaxRoot.ReplaceTokens(tokensToReplace, replacementForTokens));
42+
}
43+
}
44+
}

src/Microsoft.DotNet.CodeFormatting/Rules/IsFormattedFormattingRule.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
2323
var oldTrivia = syntaxRoot.DescendantTrivia().Where(trivia => trivia.CSharpKind() == SyntaxKind.DisabledTextTrivia);
2424
Func<SyntaxTrivia, SyntaxTrivia, SyntaxTrivia> replacementTrivia = (trivia, dummy) =>
2525
{
26+
var levelToIndent = trivia.Token.Parent.Ancestors().Count();
2627
var compilation = SyntaxFactory.ParseCompilationUnit(trivia.ToString());
2728
var formattedTrivia = Formatter.Format(compilation.SyntaxTree.GetRoot(), document.Project.Solution.Workspace).GetText().ToString();
2829
return SyntaxFactory.DisabledText(formattedTrivia);

0 commit comments

Comments
 (0)