|
7 | 7 | using CS = Microsoft.CodeAnalysis.CSharp; |
8 | 8 | using VBasic = Microsoft.CodeAnalysis.VisualBasic; |
9 | 9 | using CSSyntax = Microsoft.CodeAnalysis.CSharp.Syntax; |
| 10 | +using System.Text.RegularExpressions; |
10 | 11 |
|
11 | 12 | namespace ICSharpCode.CodeConverter.Util |
12 | 13 | { |
@@ -68,58 +69,30 @@ public static string GetCommentText(this SyntaxTrivia trivia) |
68 | 69 | commentText = commentText.Substring(2); |
69 | 70 | } |
70 | 71 |
|
71 | | - commentText = commentText.Trim(); |
72 | 72 |
|
73 | 73 | var newLine = Environment.NewLine; |
74 | 74 | var lines = commentText.Split(new[] { newLine }, StringSplitOptions.None); |
75 | 75 | foreach (var line in lines) { |
76 | | - var trimmedLine = line.Trim(); |
77 | 76 |
|
78 | | - // Note: we trim leading '*' characters in multi-line comments. |
79 | | - // If the '*' was intentional, sorry, it's gone. |
80 | | - if (trimmedLine.StartsWith("*")) { |
81 | | - trimmedLine = trimmedLine.TrimStart('*'); |
82 | | - trimmedLine = trimmedLine.TrimStart(null); |
83 | | - } |
84 | | - |
85 | | - textBuilder.AppendLine(trimmedLine); |
| 77 | + textBuilder.AppendLine(line); |
86 | 78 | } |
87 | 79 |
|
88 | | - // remove last line break |
89 | | - textBuilder.Remove(textBuilder.Length - newLine.Length, newLine.Length); |
90 | | - |
91 | | - return textBuilder.ToString(); |
| 80 | + // remove trailing line breaks |
| 81 | + return textBuilder.ToString().TrimEnd(); |
92 | 82 | } else if (trivia.IsKind(VBasic.SyntaxKind.DocumentationCommentTrivia) || CS.CSharpExtensions.Kind(trivia) == CS.SyntaxKind.SingleLineDocumentationCommentTrivia) { |
93 | 83 | var textBuilder = new StringBuilder(); |
94 | 84 |
|
95 | | - if (commentText.EndsWith("*/")) { |
96 | | - commentText = commentText.TrimEnd('\''); |
97 | | - } |
98 | | - |
99 | | - if (commentText.StartsWith("'''")) { |
100 | | - commentText = commentText.TrimStart('\''); |
101 | | - } |
102 | | - |
103 | | - commentText = commentText.Trim(); |
104 | | - |
105 | 85 | var lines = commentText.Replace("\r\n", "\n").Split('\n'); |
106 | 86 | foreach (var line in lines) { |
107 | | - var trimmedLine = line.Trim(); |
108 | | - |
109 | | - // Note: we trim leading ' characters in multi-line comments. |
110 | | - // If the ' was intentional, sorry, it's gone. |
111 | | - if (trimmedLine.StartsWith("'")) { |
112 | | - trimmedLine = trimmedLine.TrimStart('\''); |
113 | | - trimmedLine = trimmedLine.TrimStart(null); |
114 | | - } |
115 | | - if (trimmedLine.StartsWith("/")) { |
116 | | - trimmedLine = trimmedLine.TrimStart('/'); |
117 | | - trimmedLine = trimmedLine.TrimStart(null); |
118 | | - } |
119 | | - |
120 | | - textBuilder.AppendLine(trimmedLine); |
| 87 | + // Single line comment marker is part of the comment text |
| 88 | + string trimmed = Regex.Replace(line, @"^(\ *''')?\ ?", ""); |
| 89 | + // Generated doc comments are indented by one space in VB, this is not idiomatic in C# |
| 90 | + // If someone has intentionally indented by a single space at the start for ascii art or something, this will unfortunately be lost. |
| 91 | + trimmed = Regex.Replace(trimmed, @"^\ ([^ ])", "$1"); |
| 92 | + textBuilder.AppendLine(trimmed); |
121 | 93 | } |
122 | 94 |
|
| 95 | + // remove trailing line breaks |
123 | 96 | return textBuilder.ToString().TrimEnd(); |
124 | 97 | } |
125 | 98 |
|
|
0 commit comments