Skip to content

Commit acea8e3

Browse files
committed
Support unicode characters in markdown comments
- convert unicode chars in CoreMarkdownAccessImpl.removeDocLineIntros() - add new test to MarkdownCommentTests - fixes eclipse-jdt#1787
1 parent cedc392 commit acea8e3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

org.eclipse.jdt.core.manipulation/common/org/eclipse/jdt/core/manipulation/internal/javadoc/CoreMarkdownAccessImpl.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.util.ArrayList;
1717
import java.util.Arrays;
1818
import java.util.List;
19+
import java.util.regex.Pattern;
1920

2021
import org.commonmark.Extension;
2122
import org.commonmark.ext.gfm.tables.TablesExtension;
@@ -52,12 +53,17 @@ private void init() {
5253
fRenderer= HtmlRenderer.builder().extensions(extensions).build();
5354
}
5455

56+
final static Pattern UnicodePattern= Pattern.compile("\\\\u([0-9A-Fa-f]{4})"); //$NON-NLS-1$
57+
5558
@Override
5659
protected String removeDocLineIntros(String textWithSlashes) {
60+
// replace unicode characters
61+
String content= UnicodePattern.matcher(textWithSlashes).replaceAll(s -> String.valueOf((char)Integer.parseInt(s.group(1), 16)));
62+
5763
String lineBreakGroup= "(\\r\\n?|\\n)"; //$NON-NLS-1$
5864
String noBreakSpace= "[^\r\n&&\\s]"; //$NON-NLS-1$
5965
// in the markdown case relevant leading whitespace is contained in TextElements, no need to preserve blanks *between* elements
60-
return textWithSlashes.replaceAll(lineBreakGroup + noBreakSpace + "*///" + noBreakSpace + '*', "$1"); //$NON-NLS-1$ //$NON-NLS-2$
66+
return content.replaceAll(lineBreakGroup + noBreakSpace + "*///" + noBreakSpace + '*', "$1"); //$NON-NLS-1$ //$NON-NLS-2$
6167
}
6268

6369
@Override

org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/hover/MarkdownCommentTests.java

+24
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,30 @@ <p>This is a test Javadoc for method test4()
900900
assertHtmlContent(expectedContent, actualHtmlContent);
901901
}
902902
@Test
903+
public void testGH1787() throws CoreException {
904+
String source= """
905+
package p;
906+
907+
public class E {
908+
/// Unicode in markdown \u000A///\u000D///\u000D\u000A///here
909+
public void m() {}
910+
}
911+
912+
""";
913+
ICompilationUnit cu= getWorkingCopy("/TestSetupProject/src/p/E.java", source, null);
914+
assertNotNull("E.java", cu);
915+
916+
IType type= cu.getType("E");
917+
918+
IMethod method= type.getMethods()[0];
919+
String actualHtmlContent= getHoverHtmlContent(cu, method);
920+
assertHtmlContent("""
921+
<p>Unicode in markdown</p>
922+
<p>here</p>
923+
""",
924+
actualHtmlContent);
925+
}
926+
@Test
903927
public void testFenceLenFour_1() throws CoreException {
904928
String source= """
905929
/// ````

0 commit comments

Comments
 (0)