Skip to content

Commit

Permalink
Fix for #1562: promote exact-match type proposals and demote prefix miss
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Mar 31, 2024
1 parent 0e249b3 commit 2a39112
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* Copyright 2009-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,9 @@ final class TypeCompletionTests extends CompletionTestSuite {

@Test
void testCompletionTypesInScript() {
String contents = 'HTML'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'HTML'))
proposalExists(proposals, 'HTML - javax.swing.text.html', 1)
String contents = 'List'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'List'))
assertProposalOrdering(orderByRelevance(proposals), 'List - java.util', 'ListenerList - groovy.beans', 'ListIterator - java.util')
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ private ICompletionProposal proposeType(AcceptedType type) {
proposal.setDeclarationSignature(type.packageName);
proposal.setFlags(type.modifiers);
proposal.setPackageName(type.packageName);
proposal.setRelevance(computeRelevanceForTypeProposal(type.fullyQualifiedName, type.accessibility, type.modifiers));
proposal.setRelevance(computeRelevanceForTypeProposal(type.fullyQualifiedName, type.simpleTypeName, type.accessibility, type.modifiers));
proposal.setReplaceRange(completionOffset, context.completionLocation);
proposal.setSignature(Signature.createCharArrayTypeSignature(type.fullyQualifiedName, true));
proposal.setTokenRange(completionOffset, context.completionEnd);
Expand Down Expand Up @@ -697,7 +697,7 @@ private ICompletionProposal proposeConstructor(AcceptedCtor ctor) {

// TODO: Leverage IRelevanceRule for this?
float relevanceMultiplier = (ctor.accessibility == IAccessRule.K_ACCESSIBLE ? 3 : 0);
relevanceMultiplier += computeRelevanceForCaseMatching(completionExpressionChars, ctor.simpleTypeName);
relevanceMultiplier += computeRelevanceForCaseMatching(CharOperation.lastSegment(completionExpressionChars, '.'), ctor.simpleTypeName);
proposal.setRelevance(Relevance.MEDIUM_HIGH.getRelevance(relevanceMultiplier));

GroovyJavaMethodCompletionProposal lazyProposal = new GroovyJavaMethodCompletionProposal(proposal, getProposalOptions(), javaContext, null);
Expand Down Expand Up @@ -789,9 +789,20 @@ private int computeRelevanceForCaseMatching(char[] token, char[] proposalName) {
return 0;
}

private int computeRelevanceForTypeProposal(char[] fullyQualifiedName, int accessibility, int modifiers) {
private int computeRelevanceForTypeProposal(char[] fullyQualifiedName, char[] simpleTypeName, int accessibility, int modifiers) {
IRelevanceRule rule = Optional.ofNullable(relevanceRule).orElse(IRelevanceRule.DEFAULT);
return rule.getRelevance(fullyQualifiedName, allTypesInUnit, accessibility, modifiers);
int r = rule.getRelevance(fullyQualifiedName, allTypesInUnit, accessibility, modifiers);

final char[] expression = context.completionExpression.toCharArray();
if (CharOperation.equals(expression, simpleTypeName, /*case*/true)) {
r += RelevanceConstants.R_CASE + RelevanceConstants.R_EXACT_NAME;
} else if (CharOperation.equals(expression, simpleTypeName, false)) {
r += RelevanceConstants.R_EXACT_NAME;
} else if (!CharOperation.prefixEquals(expression, simpleTypeName)) {
r -= 5;
}

return Math.max(1, r);
}

private void initializeRelevanceRule(JDTResolver resolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package org.codehaus.groovy.eclipse.codeassist.processors;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
Expand Down Expand Up @@ -197,6 +195,5 @@ protected static boolean isBeforeTypeName(ContentAssistContext context) {
return !FIELD_MODIFIERS.contains(nameAndLocation.name.trim());
}

protected static final Set<String> FIELD_MODIFIERS = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList("private", "protected", "public", "static", "final")));
protected static final Set<String> FIELD_MODIFIERS = Set.of("private", "protected", "public", "static", "final");
}

0 comments on commit 2a39112

Please sign in to comment.