Skip to content

Commit ca7d197

Browse files
Sort completion by relevance & respect completion filters registered by JLS (microsoft#359)
* Sort completion by relevance & respect completion filters registered by JLS * Make checkstyle happy
1 parent 59e6079 commit ca7d197

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

com.microsoft.java.debug.core/src/main/java/com/microsoft/java/debug/core/protocol/Types.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ public static class CompletionItem {
278278
public String label;
279279
public String text;
280280
public String type;
281+
/**
282+
* A string that should be used when comparing this item with other items.
283+
*/
284+
public String sortText;
281285

282286
public int start;
283287
public int number;

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/CompletionProposalRequestor.java

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.logging.Logger;
2323

2424
import org.apache.commons.lang3.StringUtils;
25+
import org.eclipse.core.runtime.Assert;
2526
import org.eclipse.core.runtime.CoreException;
2627
import org.eclipse.core.runtime.IPath;
2728
import org.eclipse.jdt.core.CompletionContext;
@@ -34,6 +35,7 @@
3435
import org.eclipse.jdt.core.IType;
3536
import org.eclipse.jdt.core.ITypeRoot;
3637
import org.eclipse.jdt.core.JavaModelException;
38+
import org.eclipse.jdt.core.Signature;
3739
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
3840
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalDescriptionProvider;
3941
import org.eclipse.jdt.ls.core.internal.contentassist.GetterSetterCompletionProposal;
@@ -109,6 +111,10 @@ private boolean isTestSource(IJavaProject project, ITypeRoot cu) {
109111

110112
@Override
111113
public void accept(CompletionProposal proposal) {
114+
if (isFiltered(proposal)) {
115+
return;
116+
}
117+
112118
if (!isIgnored(proposal.getKind())) {
113119
if (proposal.getKind() == CompletionProposal.POTENTIAL_METHOD_DECLARATION) {
114120
acceptPotentialMethodDeclaration(proposal);
@@ -265,4 +271,81 @@ public CompletionContext getContext() {
265271
return context;
266272
}
267273

268-
}
274+
/**
275+
* copied from
276+
* org.eclipse.jdt.ui.text.java.CompletionProposalCollector.isFiltered(CompletionProposal)
277+
*/
278+
private boolean isFiltered(CompletionProposal proposal) {
279+
if (isIgnored(proposal.getKind())) {
280+
return true;
281+
}
282+
283+
try {
284+
// Only filter types and constructors from completion.
285+
// Methods from already imported types and packages can still be proposed.
286+
// See https://github.com/eclipse/eclipse.jdt.ls/issues/1212
287+
switch (proposal.getKind()) {
288+
case CompletionProposal.CONSTRUCTOR_INVOCATION:
289+
case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
290+
case CompletionProposal.JAVADOC_TYPE_REF:
291+
case CompletionProposal.TYPE_REF: {
292+
char[] declaringType = getDeclaringType(proposal);
293+
return declaringType != null && org.eclipse.jdt.ls.core.internal.contentassist.TypeFilter.isFiltered(declaringType);
294+
}
295+
default: // do nothing
296+
}
297+
} catch (Exception e) {
298+
// do nothing
299+
}
300+
301+
return false;
302+
}
303+
304+
/**
305+
* copied from
306+
* org.eclipse.jdt.ui.text.java.CompletionProposalCollector.getDeclaringType(CompletionProposal)
307+
*/
308+
private final char[] getDeclaringType(CompletionProposal proposal) {
309+
switch (proposal.getKind()) {
310+
case CompletionProposal.METHOD_DECLARATION:
311+
case CompletionProposal.METHOD_NAME_REFERENCE:
312+
case CompletionProposal.JAVADOC_METHOD_REF:
313+
case CompletionProposal.METHOD_REF:
314+
case CompletionProposal.CONSTRUCTOR_INVOCATION:
315+
case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
316+
case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
317+
case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
318+
case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
319+
case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
320+
case CompletionProposal.FIELD_REF:
321+
case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
322+
case CompletionProposal.JAVADOC_FIELD_REF:
323+
case CompletionProposal.JAVADOC_VALUE_REF:
324+
char[] declaration = proposal.getDeclarationSignature();
325+
// special methods may not have a declaring type: methods defined on arrays etc.
326+
// Currently known: class literals don't have a declaring type - use Object
327+
if (declaration == null) {
328+
return "java.lang.Object".toCharArray(); //$NON-NLS-1$
329+
}
330+
return Signature.toCharArray(declaration);
331+
case CompletionProposal.PACKAGE_REF:
332+
case CompletionProposal.MODULE_REF:
333+
case CompletionProposal.MODULE_DECLARATION:
334+
return proposal.getDeclarationSignature();
335+
case CompletionProposal.JAVADOC_TYPE_REF:
336+
case CompletionProposal.TYPE_REF:
337+
return Signature.toCharArray(proposal.getSignature());
338+
case CompletionProposal.LOCAL_VARIABLE_REF:
339+
case CompletionProposal.VARIABLE_DECLARATION:
340+
case CompletionProposal.KEYWORD:
341+
case CompletionProposal.LABEL_REF:
342+
case CompletionProposal.JAVADOC_BLOCK_TAG:
343+
case CompletionProposal.JAVADOC_INLINE_TAG:
344+
case CompletionProposal.JAVADOC_PARAM_REF:
345+
return null;
346+
default:
347+
Assert.isTrue(false);
348+
return null;
349+
}
350+
}
351+
}

com.microsoft.java.debug.plugin/src/main/java/com/microsoft/java/debug/plugin/internal/CompletionsProvider.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ private CompletionItem convertFromLsp(org.eclipse.lsp4j.CompletionItem lspItem)
115115
if (lspItem.getKind() != null) {
116116
item.type = lspItem.getKind().name().toLowerCase();
117117
}
118+
119+
if (lspItem.getSortText() != null) {
120+
item.sortText = lspItem.getSortText();
121+
}
122+
118123
return item;
119124
}
120125
}

0 commit comments

Comments
 (0)