Skip to content

Commit

Permalink
Add Markdown support (JEP 467) for Java 23.
Browse files Browse the repository at this point in the history
- Delegate to markdown content access when javadoc node starts with
  '///'
- Add basic testcase

Signed-off-by: Roland Grunberg <rgrunber@redhat.com>
  • Loading branch information
rgrunber committed Sep 25, 2024
1 parent 12b4cbc commit 0e81cc4
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccess;
import org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocAccessImpl;
import org.eclipse.jdt.core.manipulation.internal.javadoc.CoreJavadocContentAccessUtility;
import org.eclipse.jdt.core.manipulation.internal.javadoc.CoreMarkdownAccessImpl;
import org.eclipse.jdt.core.manipulation.internal.javadoc.IJavadocContentFactory;
import org.eclipse.jdt.core.manipulation.internal.javadoc.JavadocLookup;
import org.eclipse.jdt.internal.ui.viewsupport.CoreJavaElementLinks;
Expand Down Expand Up @@ -120,7 +121,23 @@ protected StringBuffer createSuperMethodReferencesHTML(ArrayList<IMethod> superI
};
}

public static final IJavadocContentFactory JDT_LS_JAVADOC_CONTENT_FACTORY=new IJavadocContentFactory(){@Override public IJavadocAccess createJavadocAccess(IJavaElement element,Javadoc javadoc,String source,JavadocLookup lookup){if(lookup==null){return new JdtLsJavadocAccessImpl(element,javadoc,source);}else{return new JdtLsJavadocAccessImpl(element,javadoc,source,lookup);}}};
public static final IJavadocContentFactory JDT_LS_JAVADOC_CONTENT_FACTORY = new IJavadocContentFactory() {
@Override
public IJavadocAccess createJavadocAccess(IJavaElement element, Javadoc javadoc, String source, JavadocLookup lookup) {
if (source.startsWith("///")) { //$NON-NLS-1$
if (lookup == null) {
return new CoreMarkdownAccessImpl(element, javadoc, source);
} else {
return new CoreMarkdownAccessImpl(element, javadoc, source, lookup);
}
}
if (lookup == null) {
return new JdtLsJavadocAccessImpl(element, javadoc, source);
} else {
return new JdtLsJavadocAccessImpl(element, javadoc, source, lookup);
}
}
};

private static class JdtLsJavadocAccessImpl extends CoreJavadocAccessImpl {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,53 @@ public void testEnum() throws Exception {
assertEquals("Unexpected hover " + signature, "ENUM1", signature.getValue());
}

@Test
public void testHoverMarkdownComment() throws Exception {
String name = "java23";
importProjects("eclipse/" + name);
IProject project = getProject(name);
IJavaProject javaProject = JavaCore.create(project);
IPackageFragmentRoot packageFragmentRoot = javaProject.getPackageFragmentRoot(project.getFolder("src/main/java"));
IPackageFragment pack1 = packageFragmentRoot.createPackageFragment("test", false, null);
StringBuilder buf = new StringBuilder();
//@formatter:off
buf.append("package test;\n"
+ "/// ## TestClass\n"
+ "///\n"
+ "/// Paragraph\n"
+ "///\n"
+ "/// - item 1\n"
+ "/// - _item 2_\n"
+ " public class Test {\n"
+ " /// ### m()\n"
+ " ///\n"
+ " /// Paragraph with _emphasis_\n"
+ " /// - item 1\n"
+ " /// - item 2\n"
+ " /// @param i an _integer_ !\n"
+ " void m(int i) {\n"
+ " }\n"
+ "}\n"
+ "");
//@formatter:on
ICompilationUnit cu = pack1.createCompilationUnit("Test.java", buf.toString(), false, null);
Hover hover = getHover(cu, 7, 18);
assertNotNull(hover);
assertEquals(2, hover.getContents().getLeft().size());

//@formatter:off
String expectedJavadoc = "## TestClass ##\n"
+ "\n"
+ "Paragraph\n"
+ "\n"
+ " * item 1\n"
+ " * *item 2*";
//@formatter:on
String actual = hover.getContents().getLeft().get(1).getLeft();
actual = ResourceUtils.dos2Unix(actual);
assertEquals("Unexpected hover ", expectedJavadoc, actual);
}

private String getTitleHover(ICompilationUnit cu, int line, int character) {
// when
Hover hover = getHover(cu, line, character);
Expand Down

0 comments on commit 0e81cc4

Please sign in to comment.