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

Commit 4f7f51a

Browse files
author
Lakshmi Priya Sekar
committed
Add rule 'HasNewLinesAtStartOfDocument'
1 parent d1dc15e commit 4f7f51a

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 HasNewLinesAtStartOfDocumentFormattingRule : 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+
Document newDocument;
25+
// NewLine between copyright and using directives.
26+
var firstUsing = syntaxRoot.DescendantNodesAndSelf().OfType<UsingDirectiveSyntax>().First();
27+
var hasEndOfLineTrivia = firstUsing.GetLeadingTrivia().First().CSharpKind() == SyntaxKind.EndOfLineTrivia;
28+
if (!hasEndOfLineTrivia)
29+
{
30+
var newTriviaList = firstUsing.GetLeadingTrivia().Insert(0, SyntaxFactory.EndOfLine("\n"));
31+
newDocument = document.WithSyntaxRoot(syntaxRoot.ReplaceNode(firstUsing, firstUsing.WithLeadingTrivia(newTriviaList)));
32+
}
33+
34+
return document;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)