diff --git a/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/ContentAssistLocationTests.groovy b/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/ContentAssistLocationTests.groovy index c7a86f614c..59f0733cdf 100644 --- a/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/ContentAssistLocationTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.codeassist.completion.test/src/org/codehaus/groovy/eclipse/codeassist/tests/ContentAssistLocationTests.groovy @@ -256,77 +256,125 @@ final class ContentAssistLocationTests extends CompletionTestSuite { @Test void testMethodContext10() { String contents = 'new ArrayList()' - assertLocation(contents, contents.indexOf('(') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext11() { String contents = 'new ArrayList(a)' - assertLocation(contents, contents.indexOf('(') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext12() { String contents = 'new ArrayList(a,b)' - assertLocation(contents, contents.indexOf(',') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, ','), ContentAssistLocation.METHOD_CONTEXT) } @Test // see https://github.com/groovy/groovy-eclipse/issues/331 void testMethodContext13() { String contents = 'new ArrayList(a,b)' - assertLocation(contents, contents.indexOf('b') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, 'b'), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext14() { String contents = 'new ArrayList()' - assertLocation(contents, contents.indexOf('(') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext15() { String contents = 'new ArrayList(a)' - assertLocation(contents, contents.indexOf('(') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext16() { String contents = 'new ArrayList(a,b)' - assertLocation(contents, contents.indexOf(',') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, ','), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext17() { String contents = 'foo \nh' - assertLocation(contents, contents.indexOf('foo ') + 4, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, 'foo '), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext18() { String contents = 'foo a, \nh' - assertLocation(contents, contents.indexOf(', ') + 2, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, ', '), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext19() { String contents = 'foo a, b \nh' - assertLocation(contents, contents.indexOf('b ') + 2, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, 'b '), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext20() { String contents = 'foo (a, b )\nh' - assertLocation(contents, contents.indexOf('b ') + 2, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, 'b '), ContentAssistLocation.METHOD_CONTEXT) } @Test void testMethodContext21() { String contents = 'foo (a, )\nh' - assertLocation(contents, contents.indexOf(', ') + 1, ContentAssistLocation.METHOD_CONTEXT) + assertLocation(contents, getLastIndexOf(contents, ','), ContentAssistLocation.METHOD_CONTEXT) } - @Test // https://github.com/groovy/groovy-eclipse/issues/409 + @Test void testMethodContext22() { + String contents = '''\ + import static java.util.regex.Pattern.compile + compile() + '''.stripIndent() + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) + } + + @Test + void testMethodContext23() { + String contents = '''\ + import static java.util.regex.Pattern.compile + compile(/[a-z0-9]/) + '''.stripIndent() + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) + } + + @Test + void testMethodContext24() { + String contents = '''\ + import static java.util.regex.Pattern.compile + def regexp = /[a-z0-9]/ + compile(regexp) + '''.stripIndent() + assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT) + } + + @Test + void testMethodContext25() { + String contents = '''\ + import static java.util.regex.Pattern.compile + def regexp = /[a-z0-9]/ + compile(regexp, ) + '''.stripIndent() + assertLocation(contents, getLastIndexOf(contents, ','), ContentAssistLocation.METHOD_CONTEXT) + } + + @Test + void testMethodContext26() { + String contents = '''\ + import static java.util.regex.Pattern.compile + def regexp = /[a-z0-9]/ + compile(regexp, 0) + '''.stripIndent() + assertLocation(contents, getLastIndexOf(contents, ','), ContentAssistLocation.METHOD_CONTEXT) + } + + @Test // https://github.com/groovy/groovy-eclipse/issues/409 + void testMethodContext27() { String contents = '''\ class Bean { private String foo diff --git a/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/requestor/CompletionNodeFinder.java b/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/requestor/CompletionNodeFinder.java index 8483a3c6b4..9bfa0e736d 100644 --- a/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/requestor/CompletionNodeFinder.java +++ b/ide/org.codehaus.groovy.eclipse.codeassist.completion/src/org/codehaus/groovy/eclipse/codeassist/requestor/CompletionNodeFinder.java @@ -615,10 +615,8 @@ public void visitMethodCallExpression(MethodCallExpression expression) { // if we get here, then we still want to do the context // we are either at a paren, at a comma, or at a start of an expression - createContextForCallContext(expression, methodExpression, - /*call.isImplicitThis() ? methodExpression : objectExpression,*/ + createContextForCallContext(expression, methodExpression, methodExpression.getText()); // this is not exactly right since it will fail on funky kinds of method calls, like those that are called by a GString - methodExpression.getText()); } @Override @@ -657,10 +655,13 @@ public void visitStaticMethodCallExpression(StaticMethodCallExpression expressio visitArguments(expression.getArguments(), expression); // the method itself is not an expression, but only a string - - if (check(expression)) { + Expression methodName = new ConstantExpression(expression.getMethod()); + methodName.setStart(expression.getNameStart()); + methodName.setEnd(expression.getNameEnd()); + if (check(methodName)) { createContext(expression, blockStack.getLast(), expressionOrStatement()); } + createContextForCallContext(expression, expression, expression.getMethod(), expression.getNameEnd()); } @Override @@ -752,15 +753,16 @@ private void createContext(ASTNode completionNode, ASTNode containingNode, Conte throw new VisitCompleteException(); } - /** - * In this case, we are really completing on the method name and not inside the parens so change the information. - */ - private void createContextForCallContext(Expression origExpression, AnnotatedNode methodExpr, String methodName) { + private void createContextForCallContext(Expression expression, AnnotatedNode methodExpr, String methodName) { + createContextForCallContext(expression, methodExpr, methodName, methodExpr.getEnd()); + } + + private void createContextForCallContext(Expression expression, AnnotatedNode methodExpr, String methodName, int methodNameEnd) { context = new MethodInfoContentAssistContext( completionOffset, completionExpression, fullCompletionExpression, - origExpression, + expression, blockStack.getLast(), lhsNode, unit, @@ -768,7 +770,7 @@ private void createContextForCallContext(Expression origExpression, AnnotatedNod completionEnd, methodExpr, methodName, - methodExpr.getEnd()); + methodNameEnd); throw new VisitCompleteException(); }