Skip to content

Check completion proposal is compatible or not. #2733

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

Merged
merged 2 commits into from
Jul 4, 2023
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 @@ -45,6 +45,7 @@
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.manipulation.SharedASTProviderCore;
import org.eclipse.jdt.internal.codeassist.CompletionEngine;
import org.eclipse.jdt.internal.codeassist.InternalCompletionProposal;
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;
Expand Down Expand Up @@ -801,6 +802,12 @@ private String[] computeTypeArgumentProposals(CompletionProposal proposal) {

IType expectedType= (IType) expectedTypeBinding.getJavaElement();

if (proposal instanceof InternalCompletionProposal icp && !icp.isCompatibleProposal()) {
// the proposal is not compatible with the expected type
// -> do not add any type arguments
return new String[0];
}

IType[] path= TypeProposalUtils.computeInheritancePath(type, expectedType);
if (path == null) {
// proposed type does not inherit from expected type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -56,7 +57,7 @@

public final class SignatureHelpRequestor extends CompletionRequestor {

private List<CompletionProposal> proposals = new ArrayList<>();
private Map<String, CompletionProposal> proposals = new LinkedHashMap<>();
private List<CompletionProposal> typeProposals = new ArrayList<>();
private final ICompilationUnit unit;
private Map<SignatureInformation, CompletionProposal> infoProposals;
Expand All @@ -83,9 +84,8 @@ public SignatureHelp getSignatureHelp(IProgressMonitor monitor) {
SignatureHelp signatureHelp = new SignatureHelp();

List<SignatureInformation> infos = new ArrayList<>();
for (int i = 0; i < proposals.size(); i++) {
for (CompletionProposal proposal : proposals.values()) {
if (!monitor.isCanceled()) {
CompletionProposal proposal = proposals.get(i);
if (proposal.getKind() != CompletionProposal.METHOD_REF) {
typeProposals.add(proposal);
continue;
Expand Down Expand Up @@ -128,14 +128,14 @@ public void accept(CompletionProposal proposal) {
for (String typeName : this.declaringTypeNames) {
String declaringTypeSimpleName = Signature.getSimpleName(typeName);
if (Objects.equals(proposalTypeSimpleName, declaringTypeSimpleName)) {
proposals.add(proposal);
proposals.putIfAbsent(String.valueOf(proposal.getSignature()), proposal);
return;
}
}
return;
}
}
proposals.add(proposal);
proposals.putIfAbsent(String.valueOf(proposal.getSignature()), proposal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,30 @@ public static void main(String[] args) {
}
}

@Test
public void testCompletion_constructor_innerClass() throws JavaModelException {
ICompilationUnit unit = getWorkingCopy(
"src/java/Foo.java",
"""
import java.util.List;
import java.util.ArrayList;
public class Foo {
public void test() {
List<String> a = new MyC
}
public class MyClass {
static class MyList<E> extends ArrayList<E> { }
}
}
"""
);
CompletionList list = requestCompletions(unit, "new MyC");
assertNotNull(list);
CompletionItem ci = list.getItems().stream().filter(item -> item.getLabel().equals("MyClass - java.Foo")).findFirst().orElse(null);
assertNotNull(ci);
assertEquals("java.Foo.MyClass", ci.getDetail());
}

private ClientPreferences mockClientPreferences(boolean supportCompletionSnippets, boolean supportSignatureHelp, boolean isCompletionListItemDefaultsSupport) {
ClientPreferences mockCapabilies = Mockito.mock(ClientPreferences.class);
Mockito.when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);
Expand Down