Skip to content

Commit

Permalink
Fix for #411: boost relevance for exact, prefix, camel case or substring
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Aug 13, 2018
1 parent ac5b068 commit 15ebd7c
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,23 @@ import org.junit.Test
final class RelevanceTests extends CompletionTestSuite {

@Test
void testParamThenFieldThenMethodThenDGM() {
void testExactMatchThenPrefixMatchThenIgnoreCaseThenCamelCaseThenSubstring() {
String contents = '''\
class Outer {
def f
def m(p) {
null
}
def aToZ() {}
def toz() {}
def Toz() {}
def toZAbc() {}
def tozXyz() {}
}
new Outer().toz
'''.stripIndent()
ICompletionProposal[] proposals = orderByRelevance(createProposalsAtOffset(contents, getIndexOf(contents, 'null\n')))
assertProposalOrdering(proposals, 'p', 'f', 'getF', 'm', 'find')
ICompletionProposal[] proposals = orderByRelevance(createProposalsAtOffset(contents, getLastIndexOf(contents, 'toz')))
assertProposalOrdering(proposals, 'toz', 'tozXyz', 'Toz', 'toZAbc', 'aToZ')
}

@Test
void testLocalVarThenFieldThenMethodThenDGM() {
void testLocalThenFieldThenMethodThenDGM() {
String contents = '''\
class Outer {
def f
Expand All @@ -52,6 +54,20 @@ final class RelevanceTests extends CompletionTestSuite {
assertProposalOrdering(proposals, 'p', 'f', 'getF', 'm', 'find')
}

@Test
void testParamThenFieldThenMethodThenDGM() {
String contents = '''\
class Outer {
def f
def m(p) {
null
}
}
'''.stripIndent()
ICompletionProposal[] proposals = orderByRelevance(createProposalsAtOffset(contents, getIndexOf(contents, 'null\n')))
assertProposalOrdering(proposals, 'p', 'f', 'getF', 'm', 'find')
}

@Test
void testObjectMethods() {
String contents = '''\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,6 @@ public static boolean matches(String pattern, String candidate, boolean camelCas
if (camelCaseMatch && SearchPattern.camelCaseMatch(pattern, candidate)) {
return true;
}
return substringMatch ? candidate.toLowerCase().contains(pattern) : candidate.startsWith(pattern);
return substringMatch ? CharOperation.substringMatch(pattern, candidate) : candidate.startsWith(pattern);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.eclipse.codeassist.relevance.Relevance;
import org.codehaus.groovy.eclipse.codeassist.requestor.ContentAssistContext;
import org.codehaus.groovy.runtime.StringGroovyMethods;
import org.eclipse.jdt.core.compiler.CharOperation;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.groovy.core.util.GroovyUtils;
import org.eclipse.jdt.groovy.search.VariableScope;
import org.eclipse.jdt.internal.codeassist.RelevanceConstants;

public abstract class AbstractGroovyProposal implements IGroovyProposal {

Expand Down Expand Up @@ -82,17 +86,44 @@ protected int computeRelevance(ContentAssistContext context) {
int relevance = getRelevanceClass().getRelevance(getRelevanceMultiplier());

AnnotatedNode node = getAssociatedNode();
if (node != null && context.lhsType != null) {
ClassNode type = null;
if (node instanceof FieldNode) {
type = ((FieldNode) node).getType();
} else if (node instanceof MethodNode) {
type = ((MethodNode) node).getReturnType();
} else if (node instanceof PropertyNode) {
type = ((PropertyNode) node).getType();
if (node != null) {
if (context.lhsType != null) {
ClassNode type = null;
if (node instanceof FieldNode) {
type = ((FieldNode) node).getType();
} else if (node instanceof MethodNode) {
type = ((MethodNode) node).getReturnType();
} else if (node instanceof PropertyNode) {
type = ((PropertyNode) node).getType();
}
if (type != null && GroovyUtils.isAssignable(type, context.lhsType)) {
relevance = Math.max(relevance, Relevance.HIGH.getRelevance());
}
}
if (type != null && GroovyUtils.isAssignable(type, context.lhsType)) {
relevance = Math.max(relevance, Relevance.HIGH.getRelevance());

if (StringGroovyMethods.asBoolean(context.completionExpression)) {
String name = null;
if (node instanceof FieldNode) {
name = ((FieldNode) node).getName();
} else if (node instanceof MethodNode) {
name = ((MethodNode) node).getName();
} else if (node instanceof PropertyNode) {
name = ((PropertyNode) node).getName();
}
if (StringGroovyMethods.asBoolean(name)) {
String expr = context.getPerceivedCompletionExpression();
if (name.equals(expr)) {
relevance += RelevanceConstants.R_EXACT_NAME + RelevanceConstants.R_CASE;
} else if (name.equalsIgnoreCase(expr)) {
relevance += RelevanceConstants.R_EXACT_NAME;
} else if (/*name.startsWithIgnoreCase(expr)*/CharOperation.prefixEquals(expr.toCharArray(), name.toCharArray(), false)) {
if (name.startsWith(expr)) relevance += RelevanceConstants.R_CASE;
} else if (/*options.camelCaseMatch &&*/ SearchPattern.camelCaseMatch(expr, name)) {
relevance += RelevanceConstants.R_CAMEL_CASE;
} else if (/*options.substringMatch &&*/ CharOperation.substringMatch(expr, name)) {
relevance += RelevanceConstants.R_SUBSTRING;
}
}
}
}

Expand Down

0 comments on commit 15ebd7c

Please sign in to comment.