|
4 | 4 | namespace StyleCop.Analyzers.DocumentationRules |
5 | 5 | { |
6 | 6 | using System; |
| 7 | + using System.Collections.Generic; |
7 | 8 | using System.Collections.Immutable; |
8 | 9 | using System.Composition; |
9 | 10 | using System.Text; |
@@ -212,6 +213,9 @@ private static SyntaxNode ReplaceHeader(Document document, SyntaxNode root, Styl |
212 | 213 | var leadingSpaces = string.Empty; |
213 | 214 | string possibleLeadingSpaces = string.Empty; |
214 | 215 |
|
| 216 | + // remove header decoration lines, they will be re-generated |
| 217 | + trivia = RemoveHeaderDecorationLines(trivia, settings); |
| 218 | + |
215 | 219 | // Need to do this with index so we get the line endings correct. |
216 | 220 | for (int i = 0; i < trivia.Count; i++) |
217 | 221 | { |
@@ -365,16 +369,56 @@ private static string WrapInXmlComment(string prefixWithLeadingSpaces, string co |
365 | 369 | string encodedCompanyName = new XAttribute("t", settings.DocumentationRules.CompanyName).ToString().Substring(2).Trim('"'); |
366 | 370 | string encodedCopyrightText = new XText(copyrightText).ToString(); |
367 | 371 |
|
368 | | - return |
| 372 | + string copyrightString = |
369 | 373 | $"{prefixWithLeadingSpaces} <copyright file=\"{encodedFilename}\" company=\"{encodedCompanyName}\">" + newLineText |
370 | 374 | + encodedCopyrightText + newLineText |
371 | 375 | + prefixWithLeadingSpaces + " </copyright>"; |
| 376 | + |
| 377 | + if (!string.IsNullOrEmpty(settings.DocumentationRules.HeaderDecoration)) |
| 378 | + { |
| 379 | + return |
| 380 | + $"{prefixWithLeadingSpaces} {settings.DocumentationRules.HeaderDecoration}" + newLineText |
| 381 | + + copyrightString + newLineText |
| 382 | + + $"{prefixWithLeadingSpaces} {settings.DocumentationRules.HeaderDecoration}"; |
| 383 | + } |
| 384 | + |
| 385 | + return copyrightString; |
372 | 386 | } |
373 | 387 |
|
374 | 388 | private static string GetCopyrightText(string prefixWithLeadingSpaces, string copyrightText, string newLineText) |
375 | 389 | { |
376 | 390 | copyrightText = copyrightText.Replace("\r\n", "\n"); |
377 | 391 | return string.Join(newLineText + prefixWithLeadingSpaces + " ", copyrightText.Split('\n')).Replace(prefixWithLeadingSpaces + " " + newLineText, prefixWithLeadingSpaces + newLineText); |
378 | 392 | } |
| 393 | + |
| 394 | + private static SyntaxTriviaList RemoveHeaderDecorationLines(SyntaxTriviaList trivia, StyleCopSettings settings) |
| 395 | + { |
| 396 | + if (!string.IsNullOrEmpty(settings.DocumentationRules.HeaderDecoration)) |
| 397 | + { |
| 398 | + var decorationRemovalList = new List<int>(); |
| 399 | + for (int i = 0; i < trivia.Count; i++) |
| 400 | + { |
| 401 | + var triviaLine = trivia[i]; |
| 402 | + if (triviaLine.Kind() == SyntaxKind.SingleLineCommentTrivia && triviaLine.ToFullString().Contains(settings.DocumentationRules.HeaderDecoration)) |
| 403 | + { |
| 404 | + decorationRemovalList.Add(i); |
| 405 | + |
| 406 | + // also remove the line break |
| 407 | + if (i + 1 < trivia.Count && trivia[i + 1].Kind() == SyntaxKind.EndOfLineTrivia) |
| 408 | + { |
| 409 | + decorationRemovalList.Add(i + 1); |
| 410 | + } |
| 411 | + } |
| 412 | + } |
| 413 | + |
| 414 | + // Remove decoration lines in reverse order. |
| 415 | + for (int i = decorationRemovalList.Count - 1; i >= 0; i--) |
| 416 | + { |
| 417 | + trivia = trivia.RemoveAt(decorationRemovalList[i]); |
| 418 | + } |
| 419 | + } |
| 420 | + |
| 421 | + return trivia; |
| 422 | + } |
379 | 423 | } |
380 | 424 | } |
0 commit comments