Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor improvements of org.eclipse.lsp4e.jdt #421

Merged
merged 5 commits into from
Jan 31, 2023
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
2 changes: 1 addition & 1 deletion org.eclipse.lsp4e.jdt/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
<classpathentry kind="output" path="bin"/>
Expand Down
6 changes: 3 additions & 3 deletions org.eclipse.lsp4e.jdt/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
org.eclipse.jdt.core.compiler.source=17
6 changes: 3 additions & 3 deletions org.eclipse.lsp4e.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JDT Integration for LSP4E
Bundle-SymbolicName: org.eclipse.lsp4e.jdt;singleton:=true
Bundle-Version: 0.10.2.qualifier
Bundle-Version: 0.11.0.qualifier
Automatic-Module-Name: org.eclipse.lsp4e.jdt
Bundle-RequiredExecutionEnvironment: JavaSE-11
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ClassPath: .
Bundle-Localization: plugin
Bundle-Vendor: Eclipse.org
Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
org.eclipse.jface.text;bundle-version="3.13.0",
org.eclipse.ui;bundle-version="3.108.0",
org.eclipse.lsp4e;bundle-version="0.13.2",
org.eclipse.lsp4e;bundle-version="0.16.0",
org.eclipse.jdt.core;bundle-version="3.20.0",
org.eclipse.jdt.ui;bundle-version="3.20.0",
org.eclipse.swt
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@

@SuppressWarnings({ "restriction" })
public class LSJavaCompletionProposalComputer implements IJavaCompletionProposalComputer {

private static TimeUnit TIMEOUT_UNIT = TimeUnit.MILLISECONDS;
private static long TIMEOUT_LENGTH = 300;

private LSContentAssistProcessor lsContentAssistProcessor;
private String javaCompletionSpecificErrorMessage;
private static final TimeUnit TIMEOUT_UNIT = TimeUnit.MILLISECONDS;
private static final long TIMEOUT_LENGTH = 300;

public LSJavaCompletionProposalComputer() {
lsContentAssistProcessor = new LSContentAssistProcessor(false);
}
private final LSContentAssistProcessor lsContentAssistProcessor = new LSContentAssistProcessor(false);
private String javaCompletionSpecificErrorMessage;

@Override
public void sessionStarted() {
Expand All @@ -47,17 +43,12 @@ public void sessionStarted() {
@Override
public List<ICompletionProposal> computeCompletionProposals(ContentAssistInvocationContext context,
IProgressMonitor monitor) {
CompletableFuture<ICompletionProposal[]> future = CompletableFuture.supplyAsync(() -> {
return lsContentAssistProcessor.computeCompletionProposals(context.getViewer(), context.getInvocationOffset());
});

CompletableFuture<ICompletionProposal[]> future = CompletableFuture.supplyAsync(() ->
lsContentAssistProcessor.computeCompletionProposals(context.getViewer(), context.getInvocationOffset()));

try {
return Arrays.asList(asJavaProposals(future));
} catch (TimeoutException e) {
LanguageServerPlugin.logError(e);
javaCompletionSpecificErrorMessage = createErrorMessage(e);
return Collections.emptyList();
} catch (ExecutionException e) {
} catch (ExecutionException | TimeoutException e) {
LanguageServerPlugin.logError(e);
javaCompletionSpecificErrorMessage = createErrorMessage(e);
return Collections.emptyList();
Expand All @@ -78,24 +69,24 @@ private String createErrorMessage(Exception ex) {
* The LSPCompletionProposal that LSP4E computes is NOT IJavaCompletionProposal, and as a consequence JDT
* will by default sort any non Java proposals by display value, which is why we would get a strange sorting order,
* even if our the LS and LSP4E both return a proposal list in the right order.
*
*
* This method wraps around the LSCompletionProposal with a IJavaCompletionProposal, and it sets the relevance
* number that JDT uses to sort proposals in a desired order.
*/
private ICompletionProposal[] asJavaProposals(CompletableFuture<ICompletionProposal[]> future)
throws InterruptedException, ExecutionException, TimeoutException {
ICompletionProposal[] originalProposals = future.get(TIMEOUT_LENGTH, TIMEOUT_UNIT);

// We assume that the original proposals are in the correct order, so we set relevance
// based on this existing order. Note that based on IJavaCompletionProposal javadoc,
// relevance values are [0,1000] so we start at 1000
int relevance = 1000;
ICompletionProposal[] javaProposals = new ICompletionProposal[originalProposals.length];
final var javaProposals = new ICompletionProposal[originalProposals.length];

for (int i = 0; i < originalProposals.length; i++) {
javaProposals[i] = new LSJavaProposal(originalProposals[i], relevance--);
}

return javaProposals;
}

Expand All @@ -105,7 +96,7 @@ public List<IContextInformation> computeContextInformation(ContentAssistInvocati
IContextInformation[] contextInformation = lsContentAssistProcessor.computeContextInformation(context.getViewer(), context.getInvocationOffset());
return Arrays.asList(contextInformation);
}

@Override
public String getErrorMessage() {
return javaCompletionSpecificErrorMessage != null ? javaCompletionSpecificErrorMessage : lsContentAssistProcessor.getErrorMessage();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,23 @@

@SuppressWarnings("restriction")
public class LSJavaHoverProvider extends JavadocHover {

private static String fgStyleSheet;
private static String BODY_OPEN = "<body";
private static String BODY_CLOSE = "</body>";
private static String SEPARATOR = "<hr/>";

private LSPTextHover lsBasedHover;

public LSJavaHoverProvider() {
super();
lsBasedHover = new LSPTextHover();
}
private static final String BODY_OPEN = "<body";
private static final String BODY_CLOSE = "</body>";
private static final String SEPARATOR = "<hr/>";

private final LSPTextHover lsBasedHover = new LSPTextHover();

@Override
public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
if (textViewer == null || hoverRegion == null) {
return super.getHoverInfo2(textViewer, hoverRegion);
}
CompletableFuture<String> lsHoverFuture = this.lsBasedHover.getHoverInfoFuture(textViewer, hoverRegion);
AtomicReference<String> lsHtmlHoverContent = new AtomicReference<>();
AtomicReference<JavadocBrowserInformationControlInput> jdtHoverControlInput = new AtomicReference<>();
final var lsHtmlHoverContent = new AtomicReference<String>();
final var jdtHoverControlInput = new AtomicReference<JavadocBrowserInformationControlInput>();

JavadocBrowserInformationControlInput input;
IJavaElement javaElement = null;
JavadocBrowserInformationControlInput previous = null;
Expand All @@ -80,17 +75,21 @@ public Object getHoverInfo2(ITextViewer textViewer, IRegion hoverRegion) {
leadingImageWidth = input.getLeadingImageWidth();
jdtHtmlHoverContent = input.getHtml();
}

} catch (InterruptedException | ExecutionException e) {
} catch (ExecutionException e) {
LanguageServerPlugin.logWarning("Javadoc unavailable. Failed to obtain it.", e);
// Return null to let JDT compute the hover using its own Hover Providers
return null;
} catch (InterruptedException e) {
LanguageServerPlugin.logWarning("Javadoc unavailable. Failed to obtain it.", e);
Thread.currentThread().interrupt();
// Return null to let JDT compute the hover using its own Hover Providers
return null;
} catch (TimeoutException e) {
LanguageServerPlugin.logWarning("Timeout waiting for data to generate LS hover", e);
// Return null to let JDT compute the hover using its own Hover Providers
return null;
}

/*
* LS HTML and JDT HTML are two HTML docs that need to be combined. JDT HTML comes with embedded CSS.
* Therefore it is best to insert LS HTML body inside the body of JDT HTML to take advantage of the JDT CSS.
Expand All @@ -109,16 +108,16 @@ private String formatContent(String lsContent, String jdtContent) {
return (lsContent == null ? "" : lsContent) + (jdtContent == null ? "" : jdtContent);
}
}

private static StringBuilder wrapHtml(String html) {
/*
* No JDT content. Means no JDT CSS part either. Therefore add JDT CSS chunk to it.
*/
ColorRegistry registry = JFaceResources.getColorRegistry();
RGB fgRGB = registry.getRGB("org.eclipse.jdt.ui.Javadoc.foregroundColor"); //$NON-NLS-1$
RGB bgRGB= registry.getRGB("org.eclipse.jdt.ui.Javadoc.backgroundColor"); //$NON-NLS-1$
RGB fgRGB = registry.getRGB("org.eclipse.jdt.ui.Javadoc.foregroundColor"); //$NON-NLS-1$
RGB bgRGB= registry.getRGB("org.eclipse.jdt.ui.Javadoc.backgroundColor"); //$NON-NLS-1$

StringBuilder buffer = new StringBuilder(html);
final var buffer = new StringBuilder(html);
HTMLPrinter.insertPageProlog(buffer, 0, fgRGB, bgRGB, getStyleSheet());
HTMLPrinter.addPageEpilog(buffer);
return buffer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public class LspJavaQuickAssistProcessor extends LSPCodeActionQuickAssistProcess

private IQuickAssistInvocationContext getContext(IInvocationContext context) {
return new IQuickAssistInvocationContext() {

@Override
public ISourceViewer getSourceViewer() {
// Should be of instance or a subclass of TextInvocationContext
return ((TextInvocationContext) context).getSourceViewer();
}

@Override
public int getOffset() {
return context.getSelectionOffset();
}

@Override
public int getLength() {
return context.getSelectionLength();
Expand All @@ -51,13 +51,13 @@ public int getLength() {
public boolean hasAssists(IInvocationContext context) throws CoreException {
return this.canAssist(getContext(context));
}

@Override
public IJavaCompletionProposal[] getAssists(IInvocationContext context, IProblemLocation[] locations)
throws CoreException {

ICompletionProposal[] proposals = computeQuickAssistProposals(getContext(context));
IJavaCompletionProposal[] javaProposals = new IJavaCompletionProposal[proposals.length];
final var javaProposals = new IJavaCompletionProposal[proposals.length];
for (int i = 0; i < proposals.length; i++) {
javaProposals[i] = new LSJavaProposal(proposals[i], RELEVANCE);
}
Expand Down