Skip to content

Commit

Permalink
Fix for issue #355: handle naming collisions in code select
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 1, 2017
1 parent 4b641c8 commit 366827e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@

public final class InferencingTests extends InferencingTestSuite {

private void assertNoUnknowns(String contents) {
GroovyCompilationUnit unit = createUnit("Search", contents);
private void assertExprType(String source, String target, String type) {
final int offset = source.lastIndexOf(target);
assertType(source, offset, offset + target.length(), type);
}

private void assertNoUnknowns(String source) {
GroovyCompilationUnit unit = createUnit("Search", source);

TypeInferencingVisitorWithRequestor visitor = factory.createVisitor(unit);
visitor.DEBUG = true;
Expand Down Expand Up @@ -2844,16 +2849,24 @@ public void testFieldAndPropertyWithSameName() {
" private Wrapper<String> foo = new Wrapper<>(\"foo\");\n" +
" public String getFoo() { return foo.getWrapped(); }\n" +
"}");

String contents =
"class GroovyTest {\n" +
" static void main(String[] args) {\n" +
" def b = new MyBean()\n" +
" println b.foo.toUpperCase()\n" +
" }\n" +
"}";
assertExprType(contents, "foo", "java.lang.String");
}

int start = contents.lastIndexOf("foo");
int end = start + "foo".length();
assertType(contents, start, end, "java.lang.String");
@Test // https://github.com/groovy/groovy-eclipse/issues/355
public void testLocalTypeAndDefaultImportCollision() {
createJavaUnit("domain", "Calendar",
"public class Calendar { public static Calendar instance() { return null; } }");

String contents = "def cal = domain.Calendar.instance()";
assertExprType(contents, "instance", "domain.Calendar");
assertExprType(contents, "cal", "domain.Calendar");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ final class CodeSelectMethodsTests extends BrowsingTestSuite {
void testCodeSelectClosure() {
String contents = 'def x = { t -> print t }\nx("hello")'
IJavaElement elem = assertCodeSelect([contents], 'x')
assert elem.typeSignature == 'QClosure;'
assert elem.typeSignature =~ 'groovy.lang.Closure'
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ final class CodeSelectVariablesTests extends BrowsingTestSuite {
assertCodeSelect([contents], 'yyy')
}

@Test // https://github.com/groovy/groovy-eclipse/issues/355
void testSelectLocalVar5() {
addJavaSource 'public class Calendar { public static Calendar instance() { return null; } }', 'Calendar', 'domain'
String contents = 'def cal = domain.Calendar.instance()'
def elem = assertCodeSelect([contents], 'cal')
assert elem.typeSignature =~ 'domain.Calendar'
}

@Test // GRECLIPSE-1330
void testSelectLocalVarInGString1() {
String contents = 'def i\n"$i"'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,15 @@ private void handleMatch(TypeLookupResult result, IJavaElement enclosingElement)
}

private LocalVariable createLocalVariable(TypeLookupResult result, IJavaElement enclosingElement, Variable var) {
ClassNode type = result.type != null ? result.type : var.getType();

int start;
if (var instanceof Parameter) {
start = ((Parameter) var).getNameStart();
} else {
start = ((VariableExpression) var).getStart();
}
int until = start + var.getName().length() - 1;
String signature = GroovyUtils.getTypeSignature(type, false, false);
ClassNode type = result.type != null ? result.type : var.getType();
String signature = GroovyUtils.getTypeSignature(type, /*fully-qualified:*/ true, false);
return new LocalVariable((JavaElement) enclosingElement, var.getName(), start, until, start, until, signature, null, 0, false);
}

Expand Down

0 comments on commit 366827e

Please sign in to comment.