Skip to content

Commit

Permalink
Merge pull request #472 from groovy/issue470
Browse files Browse the repository at this point in the history
Fix #470
  • Loading branch information
eric-milles authored Feb 13, 2018
2 parents 39aa4de + ba7f699 commit 9962980
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>()'
assertLocation(contents, contents.indexOf('(') + 1, ContentAssistLocation.METHOD_CONTEXT)
assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT)
}

@Test
void testMethodContext15() {
String contents = 'new ArrayList<String>(a)'
assertLocation(contents, contents.indexOf('(') + 1, ContentAssistLocation.METHOD_CONTEXT)
assertLocation(contents, getLastIndexOf(contents, '('), ContentAssistLocation.METHOD_CONTEXT)
}

@Test
void testMethodContext16() {
String contents = 'new ArrayList<String>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -752,23 +753,24 @@ 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,
declarationStack.getLast(),
completionEnd,
methodExpr,
methodName,
methodExpr.getEnd());
methodNameEnd);
throw new VisitCompleteException();
}

Expand Down

0 comments on commit 9962980

Please sign in to comment.