PT #156965730: Get Javadoc from JDT LS or eclipse client via sts/javadoc

This commit is contained in:
BoykoAlex
2018-05-30 17:10:34 -04:00
parent d27856da64
commit 1b33036794
63 changed files with 1555 additions and 590 deletions

View File

@@ -2,6 +2,8 @@
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="src" path="src/"/>
<classpathentry exported="true" kind="lib" path="lib/remark-1.0.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jsoup-1.9.2.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@@ -0,0 +1 @@
/lib/

View File

@@ -20,8 +20,14 @@
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>

View File

@@ -16,9 +16,14 @@ Require-Bundle: org.eclipse.jdt.launching;bundle-version="3.8.0",
org.eclipse.debug.ui,
org.eclipse.ui.console,
org.springframework.tooling.jdt.ls.commons,
org.eclipse.ui.ide
org.eclipse.ui.ide,
org.eclipse.jdt.core,
org.eclipse.jdt.ui
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Export-Package: org.springframework.tooling.ls.eclipse.commons,
org.springframework.tooling.ls.eclipse.commons.console.preferences
Bundle-Activator: org.springframework.tooling.ls.eclipse.commons.LanguageServerCommonsActivator
Bundle-ClassPath: .,
lib/remark-1.0.0.jar,
lib/jsoup-1.9.2.jar

View File

@@ -3,4 +3,6 @@ output.. = bin/
bin.includes = META-INF/,\
.,\
plugin.xml,\
icons/
icons/,\
lib/remark-1.0.0.jar,\
lib/jsoup-1.9.2.jar

View File

@@ -12,4 +12,58 @@
<artifactId>org.springframework.tooling.ls.eclipse.commons</artifactId>
<packaging>eclipse-plugin</packaging>
<build>
<plugins>
<plugin>
<!-- make sure lib dir is removed after clean to avoid "dirty" build -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<filesets>
<fileset>
<directory>${basedir}/lib</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<?m2e execute onConfiguration?>
<id>get-libs</id>
<goals>
<goal>copy</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
<configuration>
<skip>false</skip>
<artifactItems>
<artifactItem>
<groupId>com.kotcrab.remark</groupId>
<artifactId>remark</artifactId>
<version>1.0.0</version>
</artifactItem>
<artifactItem>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</artifactItem>
</artifactItems>
<outputDirectory>${basedir}/lib/</outputDirectory>
<!-- baseVersion is to avoid SNAPSHOT dependencies being copied with
ever daily changing timestamp -->
<useBaseVersion>true</useBaseVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,41 @@
/*******************************************************************************
* Copyright (c) 2018 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Pivotal, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons;
public class JavadocParams {
private String projectUri;
private String bindingKey;
public JavadocParams(String projectUri, String bindingKey) {
super();
this.projectUri = projectUri;
this.bindingKey = bindingKey;
}
public String getProjectUri() {
return projectUri;
}
public void setProjectUri(String projectUri) {
this.projectUri = projectUri;
}
public String getBindingKey() {
return bindingKey;
}
public void setBindingKey(String bindingKey) {
this.bindingKey = bindingKey;
}
@Override
public String toString() {
return "JavadocParams [projectUri=" + projectUri + ", bindingKey=" + bindingKey + "]";
}
}

View File

@@ -10,6 +10,8 @@
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
@@ -19,10 +21,10 @@ public class LanguageServerCommonsActivator extends AbstractUIPlugin {
public LanguageServerCommonsActivator() {
}
@Override
public void start(BundleContext context) throws Exception {
instance = this;
instance = this;
super.start(context);
}
@@ -30,5 +32,7 @@ public class LanguageServerCommonsActivator extends AbstractUIPlugin {
return instance;
}
public static void logError(Throwable t, String message) {
instance.getLog().log(new Status(IStatus.ERROR, instance.getBundle().getSymbolicName(), message, t));
}
}

View File

@@ -15,6 +15,7 @@ import java.util.concurrent.CompletableFuture;
import org.eclipse.lsp4j.jsonrpc.services.JsonNotification;
import org.eclipse.lsp4j.jsonrpc.services.JsonRequest;
import org.eclipse.lsp4j.services.LanguageClient;
import org.springframework.tooling.jdt.ls.commons.javadoc.JavadocResponse;
/**
* Some 'custom' extensions to standard LSP {@link LanguageClient}.
@@ -37,5 +38,8 @@ public interface STS4LanguageClient extends LanguageClient {
@JsonRequest("sts/removeClasspathListener")
CompletableFuture<Object> removeClasspathListener(ClasspathListenerParams classpathListenerParams);
@JsonRequest("sts/javadoc")
CompletableFuture<JavadocResponse> javadoc(JavadocParams params);
}

View File

@@ -43,6 +43,9 @@ import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.springframework.tooling.jdt.ls.commons.Logger;
import org.springframework.tooling.jdt.ls.commons.classpath.ReusableClasspathListenerHandler;
import org.springframework.tooling.jdt.ls.commons.javadoc.JavadocResponse;
import org.springframework.tooling.jdt.ls.commons.javadoc.JavadocUtils;
import org.springframework.tooling.ls.eclipse.commons.javadoc.JavaDoc2MarkdownConverter;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
@@ -207,4 +210,18 @@ public class STS4LanguageClientImpl extends LanguageClientImpl implements STS4La
public CompletableFuture<Object> removeClasspathListener(ClasspathListenerParams params) {
return CompletableFuture.completedFuture(classpathService.removeClasspathListener(params.getCallbackCommandId()));
}
@Override
public CompletableFuture<JavadocResponse> javadoc(JavadocParams params) {
JavadocResponse response = new JavadocResponse();
try {
String content = JavadocUtils.javadoc(JavaDoc2MarkdownConverter::getMarkdownContentReader,
URI.create(params.getProjectUri()), params.getBindingKey());
response.setContent(content);
} catch (Exception e) {
LanguageServerCommonsActivator.logError(e, "Failed getting javadoc for " + params.toString());
}
return CompletableFuture.completedFuture(response);
}
}

View File

@@ -0,0 +1,64 @@
/*******************************************************************************
* Copyright (c) 2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons.javadoc;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import org.eclipse.jdt.internal.ui.text.javadoc.JavaDoc2HTMLTextReader;
/* Copied from JDT LS */
/**
* Converts JavaDoc tags into an output format.
*
* @author Fred Bricon
*/
abstract class AbstractJavaDocConverter {
private JavaDoc2HTMLTextReader reader;
private boolean read;
private String result;
public AbstractJavaDocConverter(Reader reader) {
setJavaDoc2HTMLTextReader(reader);
}
public AbstractJavaDocConverter(String javadoc) {
setJavaDoc2HTMLTextReader(javadoc == null ? null : new StringReader(javadoc));
}
private void setJavaDoc2HTMLTextReader(Reader reader) {
if (reader == null || reader instanceof JavaDoc2HTMLTextReader) {
this.reader = (JavaDoc2HTMLTextReader) reader;
} else {
this.reader = new JavaDoc2HTMLTextReader(reader);
}
}
public String getAsString() throws IOException {
if (!read && reader != null) {
String rawHtml = reader.getString();
result = convert(rawHtml);
}
return result;
}
public Reader getAsReader() throws IOException {
String m = getAsString();
return m == null ? null : new StringReader(m);
}
abstract String convert(String rawHtml);
}

View File

@@ -0,0 +1,93 @@
/*******************************************************************************
* Copyright (c) 2016-2017 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*******************************************************************************/
package org.springframework.tooling.ls.eclipse.commons.javadoc;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Field;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.internal.ui.text.javadoc.JavadocContentAccess2;
import org.jsoup.safety.Cleaner;
import org.jsoup.safety.Whitelist;
import org.springframework.tooling.ls.eclipse.commons.LanguageServerCommonsActivator;
import com.overzealous.remark.Options;
import com.overzealous.remark.Options.Tables;
import com.overzealous.remark.Remark;
/* Copied from JDT LS */
/**
* Converts JavaDoc tags into Markdown equivalent.
*
* @author Fred Bricon
*/
public class JavaDoc2MarkdownConverter extends AbstractJavaDocConverter {
private static Remark remark;
static {
Options options = new Options();
options.tables = Tables.CONVERT_TO_CODE_BLOCK;
options.hardwraps = true;
options.inlineLinks = true;
options.autoLinks = true;
options.reverseHtmlSmartPunctuation = true;
remark = new Remark(options);
//Stop remark from stripping file and jdt protocols in an href
try {
Field cleanerField = Remark.class.getDeclaredField("cleaner");
cleanerField.setAccessible(true);
Cleaner c = (Cleaner) cleanerField.get(remark);
Field whitelistField = Cleaner.class.getDeclaredField("whitelist");
whitelistField.setAccessible(true);
Whitelist w = (Whitelist) whitelistField.get(c);
w.addProtocols("a", "href", "file", "jdt");
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
LanguageServerCommonsActivator.logError(e, "Unable to modify jsoup to include file and jdt protocols");
}
}
public JavaDoc2MarkdownConverter(Reader reader) {
super(reader);
}
public JavaDoc2MarkdownConverter(String javadoc) {
super(javadoc);
}
@Override
String convert(String rawHtml) {
return remark.convert(rawHtml);
}
public static Reader getMarkdownContentReader(IJavaElement element) {
try {
String rawHtml = JavadocContentAccess2.getHTMLContent(element, true);
Reader markdownReader = new JavaDoc2MarkdownConverter(rawHtml).getAsReader();
return markdownReader;
} catch (IOException | CoreException e) {
}
return null;
}
}