Skip to content

Commit

Permalink
Fixes to completing types
Browse files Browse the repository at this point in the history
Focusing on the `CompletionTests.testType*` tests

- Fix relevance calculations: the R_CLASS / R_INTERFACE / R_ANNOTATION
  / etc. are only used when completing an `extends` or `implements`
  entry
- Complete keyword types in cases where types are suggested

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Dec 18, 2024
1 parent 2470931 commit d1182e8
Showing 1 changed file with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public class DOMCompletionEngine implements Runnable {

private static final String FAKE_IDENTIFIER = new String(RecoveryScanner.FAKE_IDENTIFIER);
private static final char[] VOID = PrimitiveType.VOID.toString().toCharArray();
private static final List<char[]> TYPE_KEYWORDS_EXCEPT_VOID = List.of(
PrimitiveType.BOOLEAN.toString().toCharArray(),
PrimitiveType.BYTE.toString().toCharArray(),
PrimitiveType.SHORT.toString().toCharArray(),
PrimitiveType.INT.toString().toCharArray(),
PrimitiveType.LONG.toString().toCharArray(),
PrimitiveType.DOUBLE.toString().toCharArray(),
PrimitiveType.FLOAT.toString().toCharArray(),
PrimitiveType.CHAR.toString().toCharArray());

private final int offset;
private final CompilationUnit unit;
Expand Down Expand Up @@ -342,6 +351,7 @@ public void run() {
ITypeBinding typeDeclBinding = typeDecl.resolveBinding();
findOverridableMethods(typeDeclBinding, this.modelUnit.getJavaProject(), context);
ExtendsOrImplementsInfo extendsOrImplementsInfo = isInExtendsOrImplements(this.toComplete);
suggestTypeKeywords(true);
if (!this.requestor.isIgnored(CompletionProposal.TYPE_REF)) {
findTypes(this.prefix, IJavaSearchConstants.TYPE, null)
// don't care about annotations
Expand Down Expand Up @@ -688,13 +698,16 @@ public void run() {
scrapeAccessibleBindings(defaultCompletionBindings);

if (suggestDefaultCompletions) {
ExtendsOrImplementsInfo extendsOrImplementsInfo = isInExtendsOrImplements(this.toComplete);
statementLikeKeywords();
if (!this.prefix.isEmpty() && extendsOrImplementsInfo == null) {
suggestTypeKeywords(DOMCompletionUtil.findParent(this.toComplete, new int[] { ASTNode.BLOCK }) == null);
}
publishFromScope(defaultCompletionBindings);
if (!completeAfter.isBlank()) {
final int typeMatchRule = this.toComplete.getParent() instanceof Annotation
? IJavaSearchConstants.ANNOTATION_TYPE
: IJavaSearchConstants.TYPE;
ExtendsOrImplementsInfo extendsOrImplementsInfo = isInExtendsOrImplements(this.toComplete);
if (!this.requestor.isIgnored(CompletionProposal.TYPE_REF)) {
findTypes(completeAfter, typeMatchRule, null)
.filter(type -> {
Expand All @@ -721,6 +734,17 @@ public void run() {
}
}

private void suggestTypeKeywords(boolean includeVoid) {
for (char[] keyword : TYPE_KEYWORDS_EXCEPT_VOID) {
if (!this.isFailedMatch(this.prefix.toCharArray(), keyword)) {
this.requestor.accept(createKeywordProposal(keyword, -1, -1));
}
}
if (includeVoid && !this.isFailedMatch(this.prefix.toCharArray(), VOID)) {
this.requestor.accept(createKeywordProposal(VOID, -1, -1));
}
}

private void scrapeAccessibleBindings(Bindings scope) {
ASTNode current = this.toComplete;
while (current != null) {
Expand Down Expand Up @@ -1142,6 +1166,9 @@ private void completeConstructor(ITypeBinding typeBinding, ASTNode referencedFro
ILog.get().error("Unable to compute type hierarchy while performing completion", e); //$NON-NLS-1$
}
} else if (enclosingTypeElement != null) {
if (isArray) {
suggestTypeKeywords(false);
}
// for some reason the enclosing type is almost always suggested
if (!this.isFailedMatch(this.prefix.toCharArray(), enclosingTypeElement.getElementName().toCharArray())) {
List<CompletionProposal> proposals = toConstructorProposals(enclosingTypeElement, referencedFrom, true);
Expand Down Expand Up @@ -1710,15 +1737,20 @@ private CompletionProposal toProposal(IType type) {
+ RelevanceConstants.R_NON_RESTRICTED
+ computeRelevanceForQualification(!nodeInImports && !fromCurrentCU && !inSamePackage && !typeIsImported);
relevance += computeRelevanceForCaseMatching(this.prefix.toCharArray(), simpleName, this.assistOptions);
try {
if (type.isAnnotation()) {
relevance += RelevanceConstants.R_ANNOTATION;
}
if (type.isInterface()) {
relevance += RelevanceConstants.R_INTERFACE;
if (isInExtendsOrImplements(this.toComplete) != null) {
try {
if (type.isAnnotation()) {
relevance += RelevanceConstants.R_ANNOTATION;
}
if (type.isInterface()) {
relevance += RelevanceConstants.R_INTERFACE;
}
if (type.isClass()) {
relevance += RelevanceConstants.R_CLASS;
}
} catch (JavaModelException e) {
// do nothing
}
} catch (JavaModelException e) {
// do nothing
}
res.setRelevance(relevance);
if (parentType != null) {
Expand Down

0 comments on commit d1182e8

Please sign in to comment.