Skip to content

Commit

Permalink
Use proper symbols for interfaces, enums, enum values and constants
Browse files Browse the repository at this point in the history
One noticeable change is that, for methods, we no longer return
CompletionItemKind.Function but CompletionItemKind.Method instead.

Fixes eclipse-jdtls#1012

Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed Apr 29, 2019
1 parent c7967e7 commit 15edef8
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jdt.core.CompletionContext;
import org.eclipse.jdt.core.CompletionProposal;
import org.eclipse.jdt.core.CompletionRequestor;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
Expand Down Expand Up @@ -50,12 +51,16 @@ public final class CompletionProposalRequestor extends CompletionRequestor {
// @formatter:off
public static final Set<CompletionItemKind> SUPPORTED_KINDS = ImmutableSet.of(CompletionItemKind.Constructor,
CompletionItemKind.Class,
CompletionItemKind.Constant,
CompletionItemKind.Interface,
CompletionItemKind.Enum,
CompletionItemKind.EnumMember,
CompletionItemKind.Module,
CompletionItemKind.Field,
CompletionItemKind.Keyword,
CompletionItemKind.Reference,
CompletionItemKind.Variable,
CompletionItemKind.Function,
CompletionItemKind.Method,
CompletionItemKind.Text);
// @formatter:on

Expand Down Expand Up @@ -116,7 +121,7 @@ public List<CompletionItem> getCompletionItems() {

public CompletionItem toCompletionItem(CompletionProposal proposal, int index) {
final CompletionItem $ = new CompletionItem();
$.setKind(mapKind(proposal.getKind()));
$.setKind(mapKind(proposal));
Map<String, String> data = new HashMap<>();
// append data field so that resolve request can use it.
data.put(CompletionResolveHandler.DATA_FIELD_URI, JDTUtils.toURI(unit));
Expand All @@ -137,22 +142,38 @@ public void acceptContext(CompletionContext context) {
}


private CompletionItemKind mapKind(final int kind) {
private CompletionItemKind mapKind(final CompletionProposal proposal) {
//When a new CompletionItemKind is added, don't forget to update SUPPORTED_KINDS
int kind = proposal.getKind();
int flags = proposal.getFlags();
switch (kind) {
case CompletionProposal.ANONYMOUS_CLASS_CONSTRUCTOR_INVOCATION:
case CompletionProposal.CONSTRUCTOR_INVOCATION:
return CompletionItemKind.Constructor;
case CompletionProposal.ANONYMOUS_CLASS_DECLARATION:
case CompletionProposal.TYPE_REF:
if (Flags.isInterface(flags)) {
return CompletionItemKind.Interface;
} else if (Flags.isEnum(flags)) {
return CompletionItemKind.Enum;
}
return CompletionItemKind.Class;
case CompletionProposal.FIELD_IMPORT:
case CompletionProposal.METHOD_IMPORT:
case CompletionProposal.METHOD_NAME_REFERENCE:
case CompletionProposal.PACKAGE_REF:
case CompletionProposal.TYPE_IMPORT:
case CompletionProposal.MODULE_DECLARATION:
case CompletionProposal.MODULE_REF:
return CompletionItemKind.Module;
case CompletionProposal.FIELD_REF:
if (Flags.isEnum(flags)) {
return CompletionItemKind.EnumMember;
}
if (Flags.isStatic(flags) && Flags.isFinal(flags)) {
return CompletionItemKind.Constant;
}
return CompletionItemKind.Field;
case CompletionProposal.FIELD_REF_WITH_CASTED_RECEIVER:
return CompletionItemKind.Field;
case CompletionProposal.KEYWORD:
Expand All @@ -166,7 +187,7 @@ private CompletionItemKind mapKind(final int kind) {
case CompletionProposal.METHOD_REF:
case CompletionProposal.METHOD_REF_WITH_CASTED_RECEIVER:
case CompletionProposal.POTENTIAL_METHOD_DECLARATION:
return CompletionItemKind.Function;
return CompletionItemKind.Method;
//text
case CompletionProposal.ANNOTATION_ATTRIBUTE_REF:
case CompletionProposal.JAVADOC_BLOCK_TAG:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMember;
import org.eclipse.jdt.core.IParent;
Expand Down Expand Up @@ -230,31 +231,51 @@ private boolean isSyntheticElement(IJavaElement element) {

public static SymbolKind mapKind(IJavaElement element) {
switch (element.getElementType()) {
case IJavaElement.TYPE:
try {
IType type = (IType)element;
if (type.isInterface()) {
return SymbolKind.Interface;
}
else if (type.isEnum()) {
return SymbolKind.Enum;
}
} catch (JavaModelException ignore) {
}
return SymbolKind.Class;
case IJavaElement.ANNOTATION:
return SymbolKind.Property; // TODO: find a better mapping
case IJavaElement.CLASS_FILE:
case IJavaElement.COMPILATION_UNIT:
return SymbolKind.File;
case IJavaElement.FIELD:
IField field = (IField) element;
try {
if (field.isEnumConstant()) {
return SymbolKind.EnumMember;
}
int flags = field.getFlags();
if (Flags.isStatic(flags) && Flags.isFinal(flags)) {
return SymbolKind.Constant;
}
} catch (JavaModelException ignore) {
}
return SymbolKind.Field;
case IJavaElement.IMPORT_CONTAINER:
case IJavaElement.IMPORT_DECLARATION:
//should we return SymbolKind.Namespace?
case IJavaElement.JAVA_MODULE:
return SymbolKind.Module;
case IJavaElement.INITIALIZER:
return SymbolKind.Constructor;
case IJavaElement.LOCAL_VARIABLE:
case IJavaElement.TYPE_PARAMETER:
return SymbolKind.Variable;
case IJavaElement.TYPE_PARAMETER:
return SymbolKind.TypeParameter;
case IJavaElement.METHOD:
return SymbolKind.Method;
case IJavaElement.PACKAGE_DECLARATION:
return SymbolKind.Package;
case IJavaElement.TYPE:
try {
return (((IType)element).isInterface() ? SymbolKind.Interface : SymbolKind.Class);
} catch (JavaModelException e) {
return SymbolKind.Class;
}
}
return SymbolKind.String;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ public static class MyClass {

void bar() {}
}

public static final String EMPTY = "";


public enum Foo {
Bar, Zoo
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public void testCompletion_import_static() throws JavaModelException{
// Check completion item
assertEquals("SECONDS", secondsFieldItem.getInsertText());
assertEquals("SECONDS : TimeUnit", secondsFieldItem.getLabel());
assertEquals(CompletionItemKind.Field, secondsFieldItem.getKind());
assertEquals(CompletionItemKind.EnumMember, secondsFieldItem.getKind());
assertEquals("999999210", secondsFieldItem.getSortText());
assertNull(secondsFieldItem.getTextEdit());

Expand Down Expand Up @@ -521,7 +521,7 @@ public void testCompletion_method_withLSPV2() throws JavaModelException{
assertNotNull(ci);

assertEquals("put", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999019", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -556,7 +556,7 @@ public void testCompletion_method_withLSPV3() throws JavaModelException{
assertNotNull(ci);

assertEquals("put", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999019", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -606,7 +606,7 @@ private void testCompletion_method_guessMethodArguments(boolean guessMethodArgum
assertNotNull(ci);

assertEquals("test", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999163", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -641,7 +641,7 @@ public void testCompletion_method_guessMethodArguments2() throws JavaModelExcept
assertNotNull(ci);

assertEquals("test", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999163", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -677,7 +677,7 @@ public void testCompletion_method_guessMethodArguments3() throws JavaModelExcept
assertNotNull(ci);

assertEquals("test", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999163", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -780,7 +780,7 @@ public void testCompletion_import_type() throws JavaModelException{
assertNotNull(list);
assertEquals(1, list.getItems().size());
CompletionItem item = list.getItems().get(0);
assertEquals(CompletionItemKind.Class, item.getKind());
assertEquals(CompletionItemKind.Interface, item.getKind());
assertEquals("Map", item.getInsertText());
assertNull(item.getAdditionalTextEdits());
assertNull(item.getTextEdit());
Expand Down Expand Up @@ -1282,7 +1282,7 @@ public void testCompletion_getter() throws Exception {
assertNotNull(ci);

assertEquals("getStrField", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999979", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -1317,7 +1317,7 @@ public void testCompletion_booleangetter() throws Exception {
assertNotNull(ci);

assertEquals("isBoolField", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999979", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -1351,7 +1351,7 @@ public void testCompletion_setter() throws Exception {
assertNotNull(ci);

assertEquals("setStrField", ci.getInsertText());
assertEquals(CompletionItemKind.Function, ci.getKind());
assertEquals(CompletionItemKind.Method, ci.getKind());
assertEquals("999999979", ci.getSortText());
assertNull(ci.getTextEdit());

Expand Down Expand Up @@ -2043,7 +2043,7 @@ public void testStaticImports2() throws Exception {
" public void foo(int x) {\n" + // conflicting method, no static import possible
" }\n" +
"}\n");
//@formatter:on
//@formatter:on

int[] loc = findCompletionLocation(unit, "/* */fo");
CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
Expand Down Expand Up @@ -2117,6 +2117,49 @@ public void testCompletion_additionalTextEdit() throws Exception {
assertNull(resolvedItem.getAdditionalTextEdits());
}

@Test
public void testCompletion_Enum() throws JavaModelException {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java",
//@formatter:off
"package org.sample;\n"
+ "public class Test {\n\n"
+ " enum Zenum{A,B}\n"
+ " void test() {\n\n"
+ " Zenu\n"
+ " }\n"
+ "}\n");
//@formatter:on
int[] loc = findCompletionLocation(unit, " Zenu");

CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
assertNotNull(list);
assertEquals(1, list.getItems().size());
CompletionItem item = list.getItems().get(0);
assertEquals(CompletionItemKind.Enum, item.getKind());
assertEquals("Zenum", item.getInsertText());
}

@Test
public void testCompletion_Constant() throws JavaModelException {
ICompilationUnit unit = getWorkingCopy("src/org/sample/Test.java",
//@formatter:off
"package org.sample;\n"
+ "public class Test {\n\n"
+ " void test() {\n\n"
+ " char c = java.io.File.pathSeparatorC \n"
+ " }\n"
+ "}\n");
//@formatter:on
int[] loc = findCompletionLocation(unit, "pathSeparatorC");

CompletionList list = server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
assertNotNull(list);
assertEquals(1, list.getItems().size());
CompletionItem item = list.getItems().get(0);
assertEquals(CompletionItemKind.Constant, item.getKind());
assertEquals("pathSeparatorChar", item.getInsertText());
}

private String createCompletionRequest(ICompilationUnit unit, int line, int kar) {
return COMPLETION_TEMPLATE.replace("${file}", JDTUtils.toURI(unit))
.replace("${line}", String.valueOf(line))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ public void testTypes() throws Exception {
assertHasSymbol("foo()", "MyInterface", SymbolKind.Method, symbols);
assertHasSymbol("MyClass", "Bar", SymbolKind.Class, symbols);
assertHasSymbol("bar()", "MyClass", SymbolKind.Method, symbols);
assertHasSymbol("Foo", "Bar", SymbolKind.Enum, symbols);
assertHasSymbol("Bar", "Foo", SymbolKind.EnumMember, symbols);
assertHasSymbol("Zoo", "Foo", SymbolKind.EnumMember, symbols);
assertHasSymbol("EMPTY", "Bar", SymbolKind.Constant, symbols);

}

@Test
Expand Down

0 comments on commit 15edef8

Please sign in to comment.