Skip to content

Commit

Permalink
Manually exclude class list from package javadoc (Java > 8). Fixes #612
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Apr 4, 2018
1 parent c8d6f56 commit 1935320
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@
*/
public class JavadocContentAccess2 {

private static final String UL_CLASS_BLOCK_LIST = "<ul class=\"blockList\">";

private static final String BASE_URL_COMMENT_INTRO = "<!-- baseURL=\""; //$NON-NLS-1$

private static final String BLOCK_TAG_START = "<ul>"; //$NON-NLS-1$
Expand All @@ -132,6 +134,8 @@ public class JavadocContentAccess2 {
private static final String JavadocContentAccess2_getproperty_message = "<p>Gets the value ofthe property{0}.</p><dl><dt>Property Description:</dt><dd>{1}</dd></dl>";
private static final String JavadocContentAccess2_setproperty_message = "<p>Sets the value ofthe property {0}.</p><dl><dt>Property Description:</dt><dd>{1}</dd></dl>";

private static final String CONTENT_CONTAINER = "<div class=\"contentContainer\">";

/**
* Implements the "Algorithm for Inheriting Method Comments" as specified for
* <a href=
Expand Down Expand Up @@ -2123,6 +2127,27 @@ public static String getHTMLContent(IPackageDeclaration packageDeclaration) thro
* @since 3.9
*/
public static String getHTMLContent(IPackageFragment packageFragment) throws CoreException {
String content = readHTMLContent(packageFragment);
return sanitizePackageJavadoc(content);
}

private static String sanitizePackageJavadoc(String content) {
if (content == null || content.isEmpty()) {
return content;
}
//Since Java 9, Javadoc format has changed and the JDT parsing in org.eclipse.jdt.internal.core.JavadocContent hasn't really caught up
//so we need to manually remove the list of classes away from the actual package Javadoc, similar to Java < 9.
//This is a simple, suboptimal but temporary (AHAHAH!) hack until JavadocContent fixes its parsing.
if (content.indexOf(CONTENT_CONTAINER) == 0) {
int nextListIndex = content.indexOf(UL_CLASS_BLOCK_LIST);
if (nextListIndex > 0) {
content = content.substring(CONTENT_CONTAINER.length(), nextListIndex);
}
}
return content;
}

private static String readHTMLContent(IPackageFragment packageFragment) throws CoreException {
IPackageFragmentRoot root = (IPackageFragmentRoot) packageFragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);

//1==> Handle the case when the documentation is present in package-info.java or package-info.class file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

Expand Down Expand Up @@ -106,10 +107,25 @@ protected IVMInstall doCreateVMInstall(String id) {

class TestVMInstall extends AbstractVMInstall {

private URL javadoc;

public TestVMInstall(IVMInstallType type, String id) {
super(type, id);
setNotify(false);
setInstallLocation(new File(TestVMType.getFakeJDKsLocation(), id));
try {
javadoc = new URL("https://docs.oracle.com/javase/" + id.replace("1.", "") + "/docs/api/");
} catch (MalformedURLException e) {
JavaLanguageServerPlugin.logException(e.getMessage(), e);
}
}

/* (non-Javadoc)
* @see org.eclipse.jdt.launching.AbstractVMInstall#getJavadocLocation()
*/
@Override
public URL getJavadocLocation() {
return javadoc;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import static org.eclipse.jdt.ls.core.internal.JsonMessageHelper.getParams;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -114,7 +115,7 @@ public void testHover() throws Exception {
public void testHoverStandalone() throws Exception {
//given
//Hovers on the System.out
URI standalone = Paths.get("projects","maven","salut","src","main","java","java","Foo.java").toUri();
URI standalone = Paths.get("projects", "maven", "salut", "src", "main", "java", "java", "Foo.java").toUri();
String payload = createHoverRequest(standalone, 10, 71);
TextDocumentPositionParams position = getParams(payload);

Expand All @@ -128,7 +129,7 @@ public void testHoverStandalone() throws Exception {
assertEquals("Unexpected hover " + signature, "java", signature.getLanguage());
assertEquals("Unexpected hover " + signature, "java.Foo", signature.getValue());
String doc = hover.getContents().get(1).getLeft();
assertEquals("Unexpected hover "+doc, "This is foo", doc);
assertEquals("Unexpected hover " + doc, "This is foo", doc);
}

@Test
Expand All @@ -153,7 +154,7 @@ public void testHoverPackage() throws Exception {
public void testEmptyHover() throws Exception {
//given
//Hovers on the System.out
URI standalone = Paths.get("projects","maven","salut","src","main","java","java","Foo.java").toUri();
URI standalone = Paths.get("projects", "maven", "salut", "src", "main", "java", "java", "Foo.java").toUri();
String payload = createHoverRequest(standalone, 1, 2);
TextDocumentPositionParams position = getParams(payload);

Expand Down Expand Up @@ -444,7 +445,6 @@ public void testHoverWhenLinkDoesNotExist() throws Exception {
assertMatches("This link doesnt work LinkToSomethingNotFound", content);
}


@Test
public void testHoverJavadocWithExtraTags() throws Exception {
IPackageFragment pack1 = sourceFolder.createPackageFragment("test1", false, null);
Expand Down Expand Up @@ -480,6 +480,26 @@ public void testHoverJavadocWithExtraTags() throws Exception {
assertEquals("Unexpected hover ", expectedJavadoc, hover.getContents().get(1).getLeft());
}

@Test
public void testHoverOnPackageWithNewJavadoc() throws Exception {
importProjects("eclipse/java9");
project = WorkspaceHelper.getProject("java9");
handler = new HoverHandler(preferenceManager);
//given
//Hovers on the java.sql import
String payload = createHoverRequest("src/main/java/foo/bar/MyDriverAction.java", 2, 14);
TextDocumentPositionParams position = getParams(payload);

//when
Hover hover = handler.hover(position, monitor);
assertNotNull(hover);
String javadoc = hover.getContents().get(1).getLeft();
//Javadoc was read from https://docs.oracle.com/javase/9/docs/api/java/sql/package-summary.html
assertTrue(javadoc.contains("JDBC™ API Tutorial and Reference, Third Edition"));
assertFalse(javadoc.contains("----"));//no table nonsense

}

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

0 comments on commit 1935320

Please sign in to comment.