44namespace StyleCop . Analyzers . DocumentationRules
55{
66 using System ;
7+ using System . Collections . Generic ;
78 using System . Collections . Immutable ;
89 using System . Composition ;
910 using System . Text ;
@@ -18,6 +19,7 @@ namespace StyleCop.Analyzers.DocumentationRules
1819 using Microsoft . CodeAnalysis . Formatting ;
1920 using StyleCop . Analyzers . Helpers ;
2021 using StyleCop . Analyzers . Settings . ObjectModel ;
22+ using Path = System . IO . Path ;
2123
2224 /// <summary>
2325 /// Implements a code fix for file header diagnostics.
@@ -136,8 +138,9 @@ private static SyntaxNode ReplaceWellFormedMultiLineCommentHeader(Document docum
136138 // Pad line that used to be next to a /*
137139 triviaStringParts [ 0 ] = commentIndentation + interlinePadding + " " + triviaStringParts [ 0 ] ;
138140 StringBuilder sb = StringBuilderPool . Allocate ( ) ;
141+ string fileName = Path . GetFileName ( document . FilePath ) ;
139142 var copyrightText = commentIndentation + interlinePadding + " " +
140- GetCopyrightText ( commentIndentation + interlinePadding , settings . DocumentationRules . CopyrightText , newLineText ) ;
143+ GetCopyrightText ( commentIndentation + interlinePadding , settings . DocumentationRules . GetCopyrightText ( fileName ) , newLineText ) ;
141144 var newHeader = WrapInXmlComment ( commentIndentation + interlinePadding , copyrightText , document . Name , settings , newLineText ) ;
142145
143146 sb . Append ( commentIndentation ) ;
@@ -212,6 +215,9 @@ private static SyntaxNode ReplaceHeader(Document document, SyntaxNode root, Styl
212215 var leadingSpaces = string . Empty ;
213216 string possibleLeadingSpaces = string . Empty ;
214217
218+ // remove header decoration lines, they will be re-generated
219+ trivia = RemoveHeaderDecorationLines ( trivia , settings ) ;
220+
215221 // Need to do this with index so we get the line endings correct.
216222 for ( int i = 0 ; i < trivia . Count ; i ++ )
217223 {
@@ -350,31 +356,71 @@ private static SyntaxNode AddHeader(Document document, SyntaxNode root, string n
350356 return root . WithLeadingTrivia ( newTrivia ) ;
351357 }
352358
353- private static SyntaxTriviaList CreateNewHeader ( string prefixWithLeadingSpaces , string filename , StyleCopSettings settings , string newLineText )
359+ private static SyntaxTriviaList CreateNewHeader ( string prefixWithLeadingSpaces , string fileName , StyleCopSettings settings , string newLineText )
354360 {
355- var copyrightText = prefixWithLeadingSpaces + " " + GetCopyrightText ( prefixWithLeadingSpaces , settings . DocumentationRules . CopyrightText , newLineText ) ;
361+ var copyrightText = prefixWithLeadingSpaces + " " + GetCopyrightText ( prefixWithLeadingSpaces , settings . DocumentationRules . GetCopyrightText ( fileName ) , newLineText ) ;
356362 var newHeader = settings . DocumentationRules . XmlHeader
357- ? WrapInXmlComment ( prefixWithLeadingSpaces , copyrightText , filename , settings , newLineText )
363+ ? WrapInXmlComment ( prefixWithLeadingSpaces , copyrightText , fileName , settings , newLineText )
358364 : copyrightText ;
359365 return SyntaxFactory . ParseLeadingTrivia ( newHeader ) ;
360366 }
361367
362- private static string WrapInXmlComment ( string prefixWithLeadingSpaces , string copyrightText , string filename , StyleCopSettings settings , string newLineText )
368+ private static string WrapInXmlComment ( string prefixWithLeadingSpaces , string copyrightText , string fileName , StyleCopSettings settings , string newLineText )
363369 {
364- string encodedFilename = new XAttribute ( "t" , filename ) . ToString ( ) . Substring ( 2 ) . Trim ( '"' ) ;
370+ string encodedFilename = new XAttribute ( "t" , fileName ) . ToString ( ) . Substring ( 2 ) . Trim ( '"' ) ;
365371 string encodedCompanyName = new XAttribute ( "t" , settings . DocumentationRules . CompanyName ) . ToString ( ) . Substring ( 2 ) . Trim ( '"' ) ;
366372 string encodedCopyrightText = new XText ( copyrightText ) . ToString ( ) ;
367373
368- return
374+ string copyrightString =
369375 $ "{ prefixWithLeadingSpaces } <copyright file=\" { encodedFilename } \" company=\" { encodedCompanyName } \" >" + newLineText
370376 + encodedCopyrightText + newLineText
371377 + prefixWithLeadingSpaces + " </copyright>" ;
378+
379+ if ( ! string . IsNullOrEmpty ( settings . DocumentationRules . HeaderDecoration ) )
380+ {
381+ return
382+ $ "{ prefixWithLeadingSpaces } { settings . DocumentationRules . HeaderDecoration } " + newLineText
383+ + copyrightString + newLineText
384+ + $ "{ prefixWithLeadingSpaces } { settings . DocumentationRules . HeaderDecoration } ";
385+ }
386+
387+ return copyrightString ;
372388 }
373389
374390 private static string GetCopyrightText ( string prefixWithLeadingSpaces , string copyrightText , string newLineText )
375391 {
376392 copyrightText = copyrightText . Replace ( "\r \n " , "\n " ) ;
377393 return string . Join ( newLineText + prefixWithLeadingSpaces + " " , copyrightText . Split ( '\n ' ) ) . Replace ( prefixWithLeadingSpaces + " " + newLineText , prefixWithLeadingSpaces + newLineText ) ;
378394 }
395+
396+ private static SyntaxTriviaList RemoveHeaderDecorationLines ( SyntaxTriviaList trivia , StyleCopSettings settings )
397+ {
398+ if ( ! string . IsNullOrEmpty ( settings . DocumentationRules . HeaderDecoration ) )
399+ {
400+ var decorationRemovalList = new List < int > ( ) ;
401+ for ( int i = 0 ; i < trivia . Count ; i ++ )
402+ {
403+ var triviaLine = trivia [ i ] ;
404+ if ( triviaLine . Kind ( ) == SyntaxKind . SingleLineCommentTrivia && triviaLine . ToFullString ( ) . Contains ( settings . DocumentationRules . HeaderDecoration ) )
405+ {
406+ decorationRemovalList . Add ( i ) ;
407+
408+ // also remove the line break
409+ if ( i + 1 < trivia . Count && trivia [ i + 1 ] . Kind ( ) == SyntaxKind . EndOfLineTrivia )
410+ {
411+ decorationRemovalList . Add ( i + 1 ) ;
412+ }
413+ }
414+ }
415+
416+ // Remove decoration lines in reverse order.
417+ for ( int i = decorationRemovalList . Count - 1 ; i >= 0 ; i -- )
418+ {
419+ trivia = trivia . RemoveAt ( decorationRemovalList [ i ] ) ;
420+ }
421+ }
422+
423+ return trivia ;
424+ }
379425 }
380426}
0 commit comments