Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions JavaToCSharp.Tests/CommentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,86 @@ public class Foo

Assert.Equal(expected.ReplaceLineEndings(), parsed.ReplaceLineEndings());
}

[Theory]
[InlineData("\n")]
[InlineData("\r\n")]
public void MultiLineJavadoc_ShouldConvertToXmlDoc_RegardlessOfLineEndings(string newLine)
{
// Issue #97: the Javadoc content was split on Environment.NewLine, so a Java file whose
// line endings differ from the host OS collapsed into a single unparsed line.
string javaCode = """
package foo;

public class Foo {
/**
* Really cool field that contains something.
* Keep in mind that this field is cooler than awesomeField.
*/
public int reallyAwesomeField;
}
""".ReplaceLineEndings(newLine);

var options = new JavaConversionOptions();
options.Usings.Clear();

var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? "";

testOutputHelper.WriteLine(parsed);

const string expected = """
namespace Foo
{
public class Foo
{
/// <summary>
/// Really cool field that contains something.
/// Keep in mind that this field is cooler than awesomeField.
/// </summary>
public int reallyAwesomeField;
}
}
""";

Assert.Equal(expected.ReplaceLineEndings(), parsed.ReplaceLineEndings());
}

[Theory]
[InlineData("\n")]
[InlineData("\r\n")]
public void MultiLineJavadocWithTags_ShouldConvertToXmlDoc_RegardlessOfLineEndings(string newLine)
{
string javaCode = """
package foo;

public class Foo {
/**
* Does something useful.
* Second line of the summary.
*
* @param name the name to use
* @return the computed value
* @throws IllegalStateException if it breaks
*/
public int doIt(String name) {
return 1;
}
}
""".ReplaceLineEndings(newLine);

var options = new JavaConversionOptions();
options.Usings.Clear();

var parsed = JavaToCSharpConverter.ConvertText(javaCode, options) ?? "";

testOutputHelper.WriteLine(parsed);

Assert.Contains("/// <summary>", parsed);
Assert.Contains("/// Does something useful.", parsed);
Assert.Contains("/// Second line of the summary.", parsed);
Assert.Contains("/// </summary>", parsed);
Assert.Contains("""/// <param name="name">the name to use</param>""", parsed);
Assert.Contains("/// <returns>the computed value</returns>", parsed);
Assert.Contains("""/// <exception cref="IllegalStateException">if it breaks</exception>""", parsed);
}
}
24 changes: 18 additions & 6 deletions JavaToCSharp/CommentsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static CompilationUnitSyntax AddPackageComments(CompilationUnitSyntax syn
}
else
{
var commentTrivia = SyntaxFactory.SyntaxTrivia(kind, pre + comment.getContent() + post);
var commentTrivia = SyntaxFactory.SyntaxTrivia(kind, pre + NormalizeLineEndings(comment.getContent()) + post);
if (pos == CommentPosition.Leading)
{
leadingTriviaList.Add(commentTrivia);
Expand Down Expand Up @@ -136,13 +136,25 @@ private static SyntaxTrivia CreateNonMemberCommentTrivia(JavaComments.Comment co
FormatAsBlockComment(comment.getContent()) + suffix);
}

return SyntaxFactory.SyntaxTrivia(kind, pre + comment.getContent() + post + suffix);
return SyntaxFactory.SyntaxTrivia(kind, pre + NormalizeLineEndings(comment.getContent()) + post + suffix);
}

/// <summary>
/// Splits text that originated from the Java source into lines. The line endings of the parsed file are
/// preserved by JavaParser and are independent of the host OS, so splitting on <see cref="Environment.NewLine"/>
/// would fail whenever the two differ (see issue #97).
/// </summary>
private static string[] SplitLines(string text) => text.ReplaceLineEndings("\n").Split('\n');

/// <summary>
/// Rewrites line endings that came from the Java source to the ones used for the generated C# output, so that
/// multi-line comments do not introduce line endings foreign to the rest of the file.
/// </summary>
private static string NormalizeLineEndings(string text) => text.ReplaceLineEndings(Environment.NewLine);

private static string FormatAsBlockComment(string content)
{
var lines = content.ReplaceLineEndings("\n")
.Split('\n')
var lines = SplitLines(content)
.Select(line => line.TrimStart().TrimStart('*').Trim())
.ToList();

Expand Down Expand Up @@ -293,7 +305,7 @@ public static IEnumerable<SyntaxTrivia> ConvertToComment(IEnumerable<JavaAst.Nod
var outputs = new List<string>();
foreach (var code in codes)
{
string[] input = code.ToString().Split([Environment.NewLine], StringSplitOptions.None);
string[] input = SplitLines(code.ToString());
outputs.AddRange(input);
}

Expand All @@ -318,7 +330,7 @@ public static IEnumerable<SyntaxTrivia> ConvertToComment(IEnumerable<JavaAst.Nod

private static IEnumerable<SyntaxTrivia> ConvertDocComment(JavaComments.Comment comment, string? post)
{
string[] input = comment.getContent().Split([Environment.NewLine], StringSplitOptions.None);
string[] input = SplitLines(comment.getContent());
var output = new List<string>();
var remarks = new List<string>(); // For Java tags unknown in C#
var currentOutput = output;
Expand Down
Loading