This repository was archived by the owner on Jul 12, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
src/Microsoft.DotNet.CodeFormatting/Rules Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments