Skip to content

Commit c432f1e

Browse files
authored
LS completions relevance in Java editor (#1223)
Rather than just push LS proposals at the top of the proposals list attempt to compute relevance in java editor completion terms. Compute base relevance based on LSP4E proposal category and rank.
1 parent a638a62 commit c432f1e

2 files changed

Lines changed: 49 additions & 7 deletions

File tree

org.eclipse.lsp4e.jdt/META-INF/MANIFEST.MF

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: JDT Integration for LSP4E
44
Bundle-SymbolicName: org.eclipse.lsp4e.jdt;singleton:=true
5-
Bundle-Version: 0.13.4.qualifier
5+
Bundle-Version: 0.13.5.qualifier
66
Automatic-Module-Name: org.eclipse.lsp4e.jdt
77
Bundle-RequiredExecutionEnvironment: JavaSE-17
88
Bundle-ClassPath: .
@@ -14,4 +14,5 @@ Require-Bundle: org.eclipse.core.runtime;bundle-version="3.12.0",
1414
org.eclipse.lsp4e;bundle-version="0.18.13",
1515
org.eclipse.jdt.core;bundle-version="3.20.0",
1616
org.eclipse.jdt.ui;bundle-version="3.20.0",
17-
org.eclipse.swt
17+
org.eclipse.swt,
18+
org.eclipse.lsp4j

org.eclipse.lsp4e.jdt/src/org/eclipse/lsp4e/jdt/LSJavaProposal.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package org.eclipse.lsp4e.jdt;
1313

1414
import org.eclipse.jdt.annotation.Nullable;
15+
import org.eclipse.jdt.internal.codeassist.RelevanceConstants;
1516
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal;
1617
import org.eclipse.jface.text.IDocument;
1718
import org.eclipse.jface.text.contentassist.ICompletionProposal;
@@ -20,10 +21,22 @@
2021
import org.eclipse.swt.graphics.Image;
2122
import org.eclipse.swt.graphics.Point;
2223

24+
@SuppressWarnings("restriction")
2325
class LSJavaProposal implements IJavaCompletionProposal {
26+
27+
private static final int LS_DEFAULT_RELEVANCE = 18;
28+
29+
private static final int MAX_BASE_RELEVANCE = 51 * 16; // Looks like JDT's max for exact match is 52
30+
31+
// Based on org.eclipse.jdt.internal.ui.text.java.RelevanceComputer
32+
private static final int DEFAULT_RELEVANCE = (RelevanceConstants.R_DEFAULT + LS_DEFAULT_RELEVANCE) * 16;
2433

25-
protected ICompletionProposal delegate;
34+
private static final int RANGE_WITHIN_CATEGORY = Math.round((MAX_BASE_RELEVANCE - DEFAULT_RELEVANCE) / 4);
2635

36+
protected ICompletionProposal delegate;
37+
private boolean relevanceComputed = false;
38+
private int relevance = -1;
39+
2740
public LSJavaProposal(ICompletionProposal delegate) {
2841
this.delegate = delegate;
2942
}
@@ -60,11 +73,39 @@ public String getDisplayString() {
6073

6174
@Override
6275
public int getRelevance() {
63-
if (delegate instanceof LSCompletionProposal) {
64-
int rankScore = ((LSCompletionProposal) delegate).getRankScore();
65-
return 1000 - rankScore;
76+
if (!relevanceComputed) {
77+
if (delegate instanceof LSCompletionProposal c) {
78+
// Based on org.eclipse.jdt.internal.ui.text.java.RelevanceComputer
79+
relevance = computeBaseRelevance(c);
80+
switch (c.getItem().getKind()) {
81+
case Class:
82+
relevance += 3;
83+
break;
84+
case Field:
85+
case Property:
86+
relevance += 5;
87+
break;
88+
case Method:
89+
relevance += 4;
90+
break;
91+
case Variable:
92+
case Value:
93+
relevance += 6;
94+
break;
95+
default:
96+
}
97+
}
98+
relevanceComputed = true;
6699
}
67-
return -1;
100+
return relevance;
101+
}
102+
103+
private int computeBaseRelevance(LSCompletionProposal c) {
104+
// Incorporate LSP4E category and rank into base relevance.
105+
int base = MAX_BASE_RELEVANCE - (c.getRankCategory() - 1) * RANGE_WITHIN_CATEGORY;
106+
int rank = c.getRankScore();
107+
base -= (rank >= 0 && rank < RANGE_WITHIN_CATEGORY ? rank : RANGE_WITHIN_CATEGORY);
108+
return base;
68109
}
69110

70111
}

0 commit comments

Comments
 (0)