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

Commit 334bc04

Browse files
author
Lakshmi Priya Sekar
committed
Add more rules
1 parent b22acb2 commit 334bc04

5 files changed

+63
-68
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@
7979
<Compile Include="Filters\IgnoreDesignerGeneratedCodeFilter.cs" />
8080
<Compile Include="Properties\AssemblyInfo.cs" />
8181
<Compile Include="Rules\HasCopyrightHeaderFormattingRule.cs" />
82-
<Compile Include="Rules\HasNewLinesAtStartOfDocumentFormattingRule.cs" />
82+
<Compile Include="Rules\HasNewLineBeforeFirstNamespaceFormattingRule.cs" />
83+
<Compile Include="Rules\HasNewLineBeforeFirstUsingFormattingRule.cs" />
8384
<Compile Include="Rules\HasNoNewLineAfterOpenBraceFormattingRule.cs" />
8485
<Compile Include="Rules\HasNoNewLineBeforeEndBraceFormattingRule.cs" />
8586
<Compile Include="Rules\HasNoXmlBasedCopyrightHeaderFormattingRule.cs" />
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
using Microsoft.CodeAnalysis.CSharp.Syntax;
13+
14+
namespace Microsoft.DotNet.CodeFormatting.Rules
15+
{
16+
[Export(typeof(IFormattingRule))]
17+
internal sealed class HasNewLineBeforeFirstNamespaceFormattingRule : IFormattingRule
18+
{
19+
public async Task<Document> ProcessAsync(Document document, CancellationToken cancellationToken)
20+
{
21+
var syntaxRoot = await document.GetSyntaxRootAsync(cancellationToken) as CSharpSyntaxNode;
22+
if (syntaxRoot == null)
23+
return document;
24+
25+
var firstNamespace = syntaxRoot.DescendantNodesAndSelf().OfType<NamespaceDeclarationSyntax>().FirstOrDefault();
26+
IEnumerable<SyntaxTrivia> newTrivia = Enumerable.Empty<SyntaxTrivia>();
27+
28+
if (firstNamespace == null)
29+
return document;
30+
31+
if (firstNamespace.HasLeadingTrivia)
32+
{
33+
var trivia = firstNamespace.GetLeadingTrivia();
34+
if (trivia.Last().CSharpKind() == SyntaxKind.EndOfLineTrivia)
35+
{
36+
int index = trivia.Count - 2;
37+
while (index >= 0)
38+
{
39+
if (trivia.ElementAt(index).CSharpKind() != SyntaxKind.EndOfLineTrivia)
40+
break;
41+
index--;
42+
}
43+
44+
newTrivia = trivia.Take(index + 1);
45+
}
46+
else
47+
{
48+
newTrivia = trivia;
49+
}
50+
}
51+
52+
newTrivia = newTrivia.Concat(new[] { SyntaxFactory.EndOfLine("\n"), SyntaxFactory.EndOfLine("\n") });
53+
54+
return document.WithSyntaxRoot(syntaxRoot.ReplaceNode(firstNamespace, firstNamespace.WithLeadingTrivia(newTrivia)));
55+
}
56+
}
57+
}

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
2424
var openBraceTokens = syntaxRoot.DescendantTokens().Where((token) => token.CSharpKind() == SyntaxKind.OpenBraceToken);
2525
Func<SyntaxToken, SyntaxToken, SyntaxToken> replacementForTokens = (token, dummy) =>
2626
{
27-
var triviaItem = token.LeadingTrivia.First();
2827
int elementsToRemove = 1;
2928
if (token.LeadingTrivia.Count > 1)
3029
{
31-
while (triviaItem == token.LeadingTrivia.ElementAt(elementsToRemove))
30+
while (elementsToRemove < token.LeadingTrivia.Count &&
31+
token.LeadingTrivia.ElementAt(elementsToRemove).CSharpKind() == SyntaxKind.EndOfLineTrivia)
3232
elementsToRemove++;
3333
}
3434

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public async Task<Document> ProcessAsync(Document document, CancellationToken ca
2424
var closeBraceTokens = syntaxRoot.DescendantTokens().Where((token) => token.CSharpKind() == SyntaxKind.CloseBraceToken);
2525
Func<SyntaxToken, SyntaxToken, SyntaxToken> replacementForTokens = (token, dummy) =>
2626
{
27-
var triviaItem = token.LeadingTrivia.First();
2827
int elementsToRemove = 1;
2928
if (token.LeadingTrivia.Count > 1)
3029
{
31-
while (triviaItem == token.LeadingTrivia.ElementAt(elementsToRemove))
30+
while (elementsToRemove < token.LeadingTrivia.Count &&
31+
token.LeadingTrivia.ElementAt(elementsToRemove).CSharpKind() == SyntaxKind.EndOfLineTrivia)
3232
elementsToRemove++;
3333
}
3434

0 commit comments

Comments
 (0)