Skip to content

Commit 5153833

Browse files
datho7561mickaelistria
authored andcommitted
Complete super constructor invocation
eg. ```java public class MyClass extends MyOtherClass { public MyClass() { sup| } } class MyOtherClass { public MyOtherClass(int i) {} protected MyOtherClass(Object i) {} MyOtherClass(boolean b) {} private MyOtherClass(String s) {} } ``` Requires #1206 in order to work properly if you have substring matching enabled in your IDE. Fixes #1205 Signed-off-by: David Thompson <[email protected]>
1 parent 274c77c commit 5153833

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/codeassist/DOMCompletionEngine.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
14451445
suggestTypeKeywords(DOMCompletionUtil.findParent(this.toComplete, new int[] { ASTNode.BLOCK }) == null);
14461446
}
14471447
publishFromScope(defaultCompletionBindings);
1448+
suggestSuperConstructors();
14481449
if (!completeAfter.isBlank()) {
14491450
String currentPackage = this.unit.getPackage() == null ? "" : this.unit.getPackage().getName().toString();
14501451
AbstractTypeDeclaration typeDecl = DOMCompletionUtil.findParentTypeDeclaration(context);
@@ -1496,6 +1497,27 @@ public void complete(org.eclipse.jdt.internal.compiler.env.ICompilationUnit sour
14961497
}
14971498
}
14981499

1500+
private void suggestSuperConstructors() {
1501+
if (this.requestor.isIgnored(CompletionProposal.METHOD_REF) || this.requestor.isIgnored(CompletionProposal.CONSTRUCTOR_INVOCATION)) {
1502+
return;
1503+
}
1504+
ASTNode methodOrTypeDeclaration = DOMCompletionUtil.findParent(toComplete,
1505+
new int[] { ASTNode.METHOD_DECLARATION, ASTNode.TYPE_DECLARATION, ASTNode.ANNOTATION_TYPE_DECLARATION, ASTNode.ENUM_DECLARATION, ASTNode.RECORD_DECLARATION });
1506+
if (this.pattern.matchesName(this.prefix.toCharArray(),
1507+
Keywords.SUPER) && methodOrTypeDeclaration instanceof MethodDeclaration methodDecl && methodDecl.isConstructor()) {
1508+
AbstractTypeDeclaration parentType = DOMCompletionUtil.findParentTypeDeclaration(toComplete);
1509+
ITypeBinding parentTypeBinding = parentType.resolveBinding();
1510+
ITypeBinding superclassBinding = parentTypeBinding.getSuperclass();
1511+
if (superclassBinding != null) {
1512+
for (IMethodBinding superclassMethod : superclassBinding.getDeclaredMethods()) {
1513+
if (superclassMethod.isConstructor() && isVisible(superclassMethod)) {
1514+
this.requestor.accept(toSuperConstructorProposal(superclassMethod));
1515+
}
1516+
}
1517+
}
1518+
}
1519+
}
1520+
14991521
/**
15001522
* Returns true if the given type can be accessed from the given context
15011523
*
@@ -2839,6 +2861,22 @@ private static boolean matchHostType(ASTNode host, Object targetAnnotationElemen
28392861
return false;
28402862
}
28412863

2864+
private CompletionProposal toSuperConstructorProposal(IMethodBinding superConstructor) {
2865+
CompletionProposal res = toProposal(superConstructor);
2866+
res.setName(Keywords.SUPER);
2867+
res.setCompletion(CharOperation.concat(Keywords.SUPER, new char[] {'(', ')'}));
2868+
res.setTokenRange(res.getReplaceStart(), res.getReplaceEnd());
2869+
2870+
res.setRelevance(RelevanceConstants.R_DEFAULT +
2871+
RelevanceConstants.R_RESOLVED +
2872+
RelevanceConstants.R_INTERESTING +
2873+
RelevanceUtils.computeRelevanceForCaseMatching(this.prefix.toCharArray(), Keywords.SUPER, this.assistOptions) +
2874+
RelevanceConstants.R_NON_RESTRICTED
2875+
);
2876+
2877+
return res;
2878+
}
2879+
28422880
private CompletionProposal toNewMethodProposal(ITypeBinding parentType, String newMethodName) {
28432881
DOMInternalCompletionProposal res = createProposal(CompletionProposal.POTENTIAL_METHOD_DECLARATION);
28442882
res.setDeclarationSignature(DOMCompletionEngineBuilder.getSignature(parentType));
@@ -3576,7 +3614,7 @@ private boolean isVisible(IBinding binding) {
35763614
return false;
35773615
}
35783616
if (Modifier.isProtected(binding.getModifiers())) {
3579-
return declaringClass.isSubTypeCompatible(DOMCompletionUtil.findParentTypeDeclaration(this.toComplete).resolveBinding());
3617+
return findInSupers(DOMCompletionUtil.findParentTypeDeclaration(this.toComplete).resolveBinding(), declaringClass);
35803618
}
35813619
return declaringClass.getPackage().isEqualTo(DOMCompletionUtil.findParentTypeDeclaration(this.toComplete).resolveBinding().getPackage());
35823620
}

0 commit comments

Comments
 (0)