Skip to content

Commit 7fd6f7a

Browse files
VB -> C#: doc comment tidyup for differing line endings - fixes #123
At some point this method could do with a few unit-level tests since the current test framework homogenizes line endings artificially and there will be a bunch more edge cases.
1 parent e2bfd76 commit 7fd6f7a

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

ICSharpCode.CodeConverter/Util/SyntaxTriviaExtensions.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public static bool IsMultiLineDocComment(this SyntaxTrivia trivia)
148148
return trivia.Kind() == SyntaxKind.MultiLineDocumentationCommentTrivia;
149149
}
150150

151+
/// <remarks>Good candidate for unit testing to catch newline issues hidden by the test harness</remarks>
151152
public static string GetCommentText(this SyntaxTrivia trivia)
152153
{
153154
var commentText = trivia.ToString();
@@ -208,8 +209,8 @@ public static string GetCommentText(this SyntaxTrivia trivia)
208209

209210
commentText = commentText.Trim();
210211

211-
var newLine = Environment.NewLine;
212-
var lines = commentText.Split(new[] { newLine }, StringSplitOptions.None);
212+
var newLine = commentText.Contains('\n') ? '\n' : '\r';
213+
var lines = commentText.Split(new []{newLine}, StringSplitOptions.None);
213214
foreach (var line in lines) {
214215
var trimmedLine = line.Trim();
215216

@@ -223,13 +224,10 @@ public static string GetCommentText(this SyntaxTrivia trivia)
223224
textBuilder.AppendLine(trimmedLine);
224225
}
225226

226-
// remove last line break
227-
textBuilder.Remove(textBuilder.Length - newLine.Length, newLine.Length);
227+
return textBuilder.ToString().TrimEnd();
228+
}
228229

229-
return textBuilder.ToString();
230-
} else {
231-
throw new InvalidOperationException();
232-
}
230+
throw new NotImplementedException($"Comment cannot be parsed:\r\n'{commentText}'");
233231
}
234232

235233
public static string AsString(this IEnumerable<SyntaxTrivia> trivia)

Tests/CSharp/NamespaceLevelTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,29 @@ Inherits System.IO.InvalidDataException
263263
End Class",
264264
@"class test : System.IO.InvalidDataException
265265
{
266+
}");
267+
}
268+
269+
[Fact]
270+
public void MultilineDocComment()
271+
{
272+
TestConversionVisualBasicToCSharp(@"Public Class MyTestClass
273+
''' <summary>
274+
''' Returns empty
275+
''' </summary>
276+
Private Function MyFunc() As String
277+
Return """"
278+
End Function
279+
End Class",
280+
@"public class MyTestClass
281+
{
282+
/// <summary>
283+
/// Returns empty
284+
/// </summary>
285+
private string MyFunc()
286+
{
287+
return """";
288+
}
266289
}");
267290
}
268291
}

0 commit comments

Comments
 (0)