@@ -18,7 +18,7 @@ namespace StyleCop.Analyzers.DocumentationRules
1818 using Microsoft . CodeAnalysis . CodeActions ;
1919 using Microsoft . CodeAnalysis . CodeFixes ;
2020 using Microsoft . CodeAnalysis . CSharp ;
21- using Microsoft . CodeAnalysis . Formatting ;
21+ using Microsoft . CodeAnalysis . Text ;
2222 using StyleCop . Analyzers . Helpers ;
2323 using StyleCop . Analyzers . Helpers . ObjectPools ;
2424 using StyleCop . Analyzers . Settings . ObjectModel ;
@@ -79,13 +79,14 @@ private static async Task<Document> GetTransformedDocumentAsync(Document documen
7979 private static async Task < SyntaxNode > GetTransformedSyntaxRootAsync ( Document document , CancellationToken cancellationToken )
8080 {
8181 var root = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
82+ var sourceText = await document . GetTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
8283 var settings = document . Project . AnalyzerOptions . GetStyleCopSettingsInCodeFix ( root . SyntaxTree , cancellationToken ) ;
8384
8485 var fileHeader = FileHeaderHelpers . ParseFileHeader ( root ) ;
8586 SyntaxNode newSyntaxRoot ;
8687 if ( fileHeader . IsMissing )
8788 {
88- newSyntaxRoot = AddHeader ( document , root , GetFileName ( document ) , settings ) ;
89+ newSyntaxRoot = AddHeader ( document , sourceText , root , GetFileName ( document ) , settings ) ;
8990 }
9091 else
9192 {
@@ -98,18 +99,18 @@ private static async Task<SyntaxNode> GetTransformedSyntaxRootAsync(Document doc
9899 var xmlFileHeader = FileHeaderHelpers . ParseXmlFileHeader ( root ) ;
99100 if ( isMultiLineComment && ! xmlFileHeader . IsMalformed )
100101 {
101- newSyntaxRoot = ReplaceWellFormedMultiLineCommentHeader ( document , root , settings , commentIndex , xmlFileHeader ) ;
102+ newSyntaxRoot = ReplaceWellFormedMultiLineCommentHeader ( document , sourceText , root , settings , commentIndex , xmlFileHeader ) ;
102103 }
103104 else
104105 {
105- newSyntaxRoot = ReplaceHeader ( document , root , settings , xmlFileHeader . IsMalformed ) ;
106+ newSyntaxRoot = ReplaceHeader ( document , sourceText , root , settings , xmlFileHeader . IsMalformed ) ;
106107 }
107108 }
108109
109110 return newSyntaxRoot ;
110111 }
111112
112- private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader ( Document document , SyntaxNode root , StyleCopSettings settings , int commentIndex , XmlFileHeader header )
113+ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader ( Document document , SourceText sourceText , SyntaxNode root , StyleCopSettings settings , int commentIndex , XmlFileHeader header )
113114 {
114115 SyntaxTriviaList trivia = root . GetLeadingTrivia ( ) ;
115116 var commentTrivia = trivia [ commentIndex ] ;
@@ -135,7 +136,10 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
135136 string interlinePadding = " *" ;
136137
137138 int minExpectedLength = ( commentIndentation + interlinePadding ) . Length ;
138- string newLineText = document . Project . Solution . Workspace . Options . GetOption ( FormattingOptions . NewLine , LanguageNames . CSharp ) ;
139+ var options = document . Project . Solution . Workspace . Options ;
140+ var firstToken = root . GetFirstToken ( includeZeroWidth : true ) ;
141+ SyntaxTrivia newLineTrivia = FormattingHelper . GetEndOfLineForCodeFix ( firstToken , sourceText , options ) ;
142+ string newLineText = newLineTrivia . ToFullString ( ) ;
139143
140144 // Examine second line to see if we should have stars or not if it's blank
141145 // set the interline padding to be blank also.
@@ -213,7 +217,7 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
213217 return root . WithLeadingTrivia ( trivia . Replace ( commentTrivia , newTrivia ) ) ;
214218 }
215219
216- private static SyntaxNode ReplaceHeader ( Document document , SyntaxNode root , StyleCopSettings settings , bool isMalformedHeader )
220+ private static SyntaxNode ReplaceHeader ( Document document , SourceText sourceText , SyntaxNode root , StyleCopSettings settings , bool isMalformedHeader )
217221 {
218222 // If the header is well formed Xml then we parse out the copyright otherwise
219223 // Skip single line comments, whitespace, and end of line trivia until a blank line is encountered.
@@ -312,8 +316,10 @@ private static SyntaxNode ReplaceHeader(Document document, SyntaxNode root, Styl
312316 trivia = trivia . RemoveAt ( removalList [ i ] ) ;
313317 }
314318
315- string newLineText = document . Project . Solution . Workspace . Options . GetOption ( FormattingOptions . NewLine , LanguageNames . CSharp ) ;
316- var newLineTrivia = SyntaxFactory . EndOfLine ( newLineText ) ;
319+ var options = document . Project . Solution . Workspace . Options ;
320+ var firstToken = root . GetFirstToken ( includeZeroWidth : true ) ;
321+ SyntaxTrivia newLineTrivia = FormattingHelper . GetEndOfLineForCodeFix ( firstToken , sourceText , options ) ;
322+ string newLineText = newLineTrivia . ToFullString ( ) ;
317323
318324 var newHeaderTrivia = CreateNewHeader ( leadingSpaces + "//" , GetFileName ( document ) , settings , newLineText ) ;
319325 if ( ! isMalformedHeader && copyrightTriviaIndex . HasValue )
@@ -356,10 +362,12 @@ private static bool FirstLineIsComment(SyntaxTriviaList trivia)
356362 return false ;
357363 }
358364
359- private static SyntaxNode AddHeader ( Document document , SyntaxNode root , string name , StyleCopSettings settings )
365+ private static SyntaxNode AddHeader ( Document document , SourceText sourceText , SyntaxNode root , string name , StyleCopSettings settings )
360366 {
361- string newLineText = document . Project . Solution . Workspace . Options . GetOption ( FormattingOptions . NewLine , LanguageNames . CSharp ) ;
362- var newLineTrivia = SyntaxFactory . EndOfLine ( newLineText ) ;
367+ var options = document . Project . Solution . Workspace . Options ;
368+ var firstToken = root . GetFirstToken ( includeZeroWidth : true ) ;
369+ SyntaxTrivia newLineTrivia = FormattingHelper . GetEndOfLineForCodeFix ( firstToken , sourceText , options ) ;
370+ string newLineText = newLineTrivia . ToFullString ( ) ;
363371 var newTrivia = CreateNewHeader ( "//" , name , settings , newLineText ) . Add ( newLineTrivia ) . Add ( newLineTrivia ) ;
364372
365373 // Skip blank lines already at the beginning of the document, since we add our own
0 commit comments