Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix type completion when type name conflicts #2232

Merged
merged 2 commits into from
Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.ASTRequestor;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.IBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.manipulation.SharedASTProviderCore;
import org.eclipse.jdt.internal.codeassist.CompletionEngine;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.template.java.SignatureUtil;
import org.eclipse.jdt.ls.core.internal.ChangeUtil;
Expand Down Expand Up @@ -1021,7 +1024,12 @@ private String computeJavaTypeReplacementString(CompletionProposal proposal) {

/* Add imports if the preference is on. */
if (importRewrite != null) {
return importRewrite.addImport(qualifiedTypeName, null);
CompilationUnit cu = SharedASTProviderCore.getAST(compilationUnit, SharedASTProviderCore.WAIT_NO, new NullProgressMonitor());
ContextSensitiveImportRewriteContext rewriteContext = null;
if (cu != null) {
rewriteContext = new ContextSensitiveImportRewriteContext(cu, this.offset, this.importRewrite);
}
return importRewrite.addImport(qualifiedTypeName, rewriteContext);
}

// fall back for the case we don't have an import rewrite (see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,17 @@ protected IFile getFile(String path) {
}

protected int[] findCompletionLocation(ICompilationUnit unit, String completeBehind) throws JavaModelException {
return findCompletionLocation(unit, completeBehind, 0);
}

protected int[] findCompletionLocation(ICompilationUnit unit, String completeBehind, int fromIndex) throws JavaModelException {
String str= unit.getSource();
int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
int cursorLocation;
if (fromIndex > 0) {
cursorLocation = str.indexOf(completeBehind, fromIndex) + completeBehind.length();
} else {
cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
}
return JsonRpcHelpers.toLine(unit.getBuffer(), cursorLocation);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3499,8 +3499,41 @@ public void testCompletion_QualifiedName2() throws Exception {
assertEquals("java.util.ArrayList()", list.getItems().get(0).getFilterText());
}

@Test
public void testCompletion_withConflictingTypeNames() throws Exception{
getWorkingCopy("src/java/List.java",
"package util;\n" +
"public class List {\n" +
"}\n");
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"package util;\n" +
"public class Foo {\n"+
" void foo() {\n"+
" Object list = new List();\n" +
" List \n"+
" }\n"+
"}\n");
CoreASTProvider.getInstance().setActiveJavaElement(unit);
CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);

CompletionList list = requestCompletions(unit, "List", unit.getSource().indexOf("List()") + 6);
assertNotNull(list);
assertFalse("No proposals were found",list.getItems().isEmpty());

List<CompletionItem> items = list.getItems().stream().filter(p -> "java.util.List".equals(p.getDetail()))
.collect(Collectors.toList());
assertFalse("java.util.List not found",items.isEmpty());
assertEquals("java.util.List", items.get(0).getTextEdit().getLeft().getNewText());
}


private CompletionList requestCompletions(ICompilationUnit unit, String completeBehind) throws JavaModelException {
int[] loc = findCompletionLocation(unit, completeBehind);
return requestCompletions(unit, completeBehind, 0);
}

private CompletionList requestCompletions(ICompilationUnit unit, String completeBehind, int fromIndex) throws JavaModelException {
int[] loc = findCompletionLocation(unit, completeBehind, fromIndex);
return server.completion(JsonMessageHelper.getParams(createCompletionRequest(unit, loc[0], loc[1]))).join().getRight();
}

Expand Down