|
12 | 12 | package org.eclipse.lsp4e.jdt; |
13 | 13 |
|
14 | 14 | import org.eclipse.jdt.annotation.Nullable; |
| 15 | +import org.eclipse.jdt.internal.codeassist.RelevanceConstants; |
15 | 16 | import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal; |
16 | 17 | import org.eclipse.jface.text.IDocument; |
17 | 18 | import org.eclipse.jface.text.contentassist.ICompletionProposal; |
|
20 | 21 | import org.eclipse.swt.graphics.Image; |
21 | 22 | import org.eclipse.swt.graphics.Point; |
22 | 23 |
|
| 24 | +@SuppressWarnings("restriction") |
23 | 25 | 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; |
24 | 33 |
|
25 | | - protected ICompletionProposal delegate; |
| 34 | + private static final int RANGE_WITHIN_CATEGORY = Math.round((MAX_BASE_RELEVANCE - DEFAULT_RELEVANCE) / 4); |
26 | 35 |
|
| 36 | + protected ICompletionProposal delegate; |
| 37 | + private boolean relevanceComputed = false; |
| 38 | + private int relevance = -1; |
| 39 | + |
27 | 40 | public LSJavaProposal(ICompletionProposal delegate) { |
28 | 41 | this.delegate = delegate; |
29 | 42 | } |
@@ -60,11 +73,39 @@ public String getDisplayString() { |
60 | 73 |
|
61 | 74 | @Override |
62 | 75 | 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; |
66 | 99 | } |
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; |
68 | 109 | } |
69 | 110 |
|
70 | 111 | } |
0 commit comments