Skip to content

Commit

Permalink
fix relevance-based ranking for java proposals to be based on lsp4e c…
Browse files Browse the repository at this point in the history
…ore rank score computation
  • Loading branch information
martinlippert committed Sep 27, 2024
1 parent 8cd5d23 commit 4b47446
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.lsp4e.jdt/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: JDT Integration for LSP4E
Bundle-SymbolicName: org.eclipse.lsp4e.jdt;singleton:=true
Bundle-Version: 0.13.1.qualifier
Bundle-Version: 0.13.2.qualifier
Automatic-Module-Name: org.eclipse.lsp4e.jdt
Bundle-RequiredExecutionEnvironment: JavaSE-17
Bundle-ClassPath: .
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,18 @@ private ICompletionProposal[] asJavaProposals(CompletableFuture<ICompletionPropo
// 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;
final var javaProposals = new ICompletionProposal[originalProposals.length];

for (int i = 0; i < originalProposals.length; i++) {
if (originalProposals[i] instanceof ICompletionProposalExtension2) {
javaProposals[i] = new LSJavaProposalExtension2(originalProposals[i], relevance--);
javaProposals[i] = new LSJavaProposalExtension2(originalProposals[i]);
} else if (originalProposals[i] instanceof ICompletionProposalExtension) {
javaProposals[i] = new LSJavaProposalExtension(originalProposals[i], relevance--);
javaProposals[i] = new LSJavaProposalExtension(originalProposals[i]);
} else {
javaProposals[i] = new LSJavaProposal(originalProposals[i], relevance--);
javaProposals[i] = new LSJavaProposal(originalProposals[i]);
}
}

return javaProposals;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022, 2023 VMware Inc. and others.
* Copyright (c) 2022, 2024 VMware Inc. and others.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand All @@ -16,17 +16,16 @@
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.lsp4e.operations.completion.LSCompletionProposal;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

class LSJavaProposal implements IJavaCompletionProposal {

protected ICompletionProposal delegate;
private int relevance;

public LSJavaProposal(ICompletionProposal delegate, int relevance) {
public LSJavaProposal(ICompletionProposal delegate) {
this.delegate = delegate;
this.relevance = relevance;
}

@Override
Expand Down Expand Up @@ -61,7 +60,11 @@ public String getDisplayString() {

@Override
public int getRelevance() {
return relevance;
if (delegate instanceof LSCompletionProposal) {
int rankScore = ((LSCompletionProposal) delegate).getRankScore();
return 1000 - rankScore;
}
return -1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

class LSJavaProposalExtension extends LSJavaProposal implements ICompletionProposalExtension {

public LSJavaProposalExtension(ICompletionProposal delegate, int relevance) {
super(delegate, relevance);
public LSJavaProposalExtension(ICompletionProposal delegate) {
super(delegate);
}

@Override
Expand All @@ -32,23 +32,23 @@ public void apply(IDocument doc, char trigger, int offset) {
@Override
public int getContextInformationPosition() {
if (delegate instanceof ICompletionProposalExtension proposalExt) {
proposalExt.getContextInformationPosition();
return proposalExt.getContextInformationPosition();
}
return -1;
}

@Override
public char @Nullable [] getTriggerCharacters() {
if (delegate instanceof ICompletionProposalExtension proposalExt) {
proposalExt.getTriggerCharacters();
return proposalExt.getTriggerCharacters();
}
return null;
}

@Override
public boolean isValidFor(IDocument doc, int offset) {
if (delegate instanceof ICompletionProposalExtension proposalExt) {
proposalExt.isValidFor(doc, offset);
return proposalExt.isValidFor(doc, offset);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

class LSJavaProposalExtension2 extends LSJavaProposal implements ICompletionProposalExtension2 {

public LSJavaProposalExtension2(ICompletionProposal delegate, int relevance) {
super(delegate, relevance);
public LSJavaProposalExtension2(ICompletionProposal delegate) {
super(delegate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
@SuppressWarnings("restriction")
public class LspJavaQuickAssistProcessor extends LSPCodeActionQuickAssistProcessor implements IQuickAssistProcessor {

private static final int RELEVANCE = 100;
private static final IJavaCompletionProposal[] NO_JAVA_COMPLETION_PROPOSALS = new IJavaCompletionProposal[0];

private IQuickAssistInvocationContext getContext(IInvocationContext context) {
Expand Down Expand Up @@ -73,11 +72,11 @@ public boolean hasAssists(@NonNullByDefault({}) IInvocationContext context) thro
* proper completion proposal extension
*/
if (proposals[i] instanceof ICompletionProposalExtension2) {
javaProposals[i] = new LSJavaProposalExtension2(proposals[i], RELEVANCE);
javaProposals[i] = new LSJavaProposalExtension2(proposals[i]);
} else if (proposals[i] instanceof ICompletionProposalExtension) {
javaProposals[i] = new LSJavaProposalExtension(proposals[i], RELEVANCE);
javaProposals[i] = new LSJavaProposalExtension(proposals[i]);
} else {
javaProposals[i] = new LSJavaProposal(proposals[i], RELEVANCE);
javaProposals[i] = new LSJavaProposal(proposals[i]);
}
}
return javaProposals;
Expand Down

0 comments on commit 4b47446

Please sign in to comment.