Skip to content

Commit

Permalink
Add source offsets for path exprs, for/catch params, ctor/method calls,
Browse files Browse the repository at this point in the history
anonymous inner classes and fields/properties w/o initializers

#412
  • Loading branch information
eric-milles committed Mar 17, 2018
1 parent b5aac4b commit 56ed572
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.PropertyNode;
import org.codehaus.groovy.ast.Variable;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
Expand Down Expand Up @@ -381,8 +382,9 @@ public VisitStatus acceptASTNode(ASTNode visitorNode, TypeLookupResult visitorRe
// might have AST nodes with overlapping locations, so result may not be null
if (this.result == null &&
visitorNode.getStart() == start && visitorNode.getEnd() == end &&
!(visitorNode instanceof MethodNode /* ignore the run() method*/) &&
!(visitorNode instanceof Statement /* ignore all statements */) &&
!(visitorNode instanceof Statement /* ignore any statement */) &&
!(visitorNode instanceof TupleExpression /* ignore wrapper */) &&
!(visitorNode instanceof MethodNode /* ignore the run() method */) &&
!(visitorNode instanceof ClassNode && ((ClassNode) visitorNode).isScript() /* ignore the script */)) {
if (ClassHelper.isPrimitiveType(visitorResult.type)) {
this.result = new TypeLookupResult(ClassHelper.getWrapper(visitorResult.type), visitorResult.declaringType, visitorResult.declaration, visitorResult.confidence, visitorResult.scope, visitorResult.extraDoc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public void testSpecialConstructor1() {
"}";

int start = contents.indexOf("this()");
int end = start + "this(".length();
int end = start + "this()".length();
assertType(contents, start, end, "java.lang.Object");
assertDeclaringType(contents, start, end, "C");
}
Expand All @@ -834,7 +834,7 @@ public void testSpecialConstructor2() {
"}";

int start = contents.indexOf("super()");
int end = start + "super(".length();
int end = start + "super()".length();
assertType(contents, start, end, "java.lang.Object");
assertDeclaringType(contents, start, end, "java.util.HashMap");
}
Expand All @@ -855,8 +855,8 @@ public void testStaticMethodCall2() {

@Test
public void testStaticMethodCall3() {
String contents = "class Two {\n def other() { \n x() } \n static String x() {\n \"\" } } ";
String expr = "x() "; // extra space b/c static method call expression end offset is wrong
String contents = "class Two {\n def other() { \n x(); } \n static String x() {\n \"\" } } ";
String expr = "x()";
assertType(contents, contents.indexOf(expr), contents.indexOf(expr)+expr.length(), "java.lang.String");
}

Expand Down Expand Up @@ -2005,7 +2005,7 @@ public void testNothingIsUnknownWithCategories() {

@Test // GRECLIPSE-1304
public void testNoGString() {
assertNoUnknowns("'$'\n'${}\n'${a}'\n'$a'");
assertNoUnknowns("'$'\n'${}'\n'${a}'\n'$a'");
}

@Test // GRECLIPSE-1341
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,68 +505,66 @@ public void testParsingRecovery_GRE1213_2() {
@Test
public void testParsingRecovery_GRE1107_1() {
runNegativeTest(new String[] {
"X.groovy",
"package foo\n"+
"\n"+
"class X {\n"+
" public void foo() {\n"+
" def blah = this\n"+
" do {\n"+
" } while (blah != null)\n"+
"\n"+
" return null\n"+
" }\n"+
"foo/X.groovy",
"package foo\n" +
"\n" +
"class X {\n" +
" public void foo() {\n" +
" def blah = this\n" +
" do {\n" +
" } while (blah != null)\n" +
" }\n" +
"}\n",
},
Boolean.getBoolean("groovy.antlr4") ? "" :
"----------\n" +
"1. ERROR in X.groovy (at line 6)\n" +
"1. ERROR in foo\\X.groovy (at line 6)\n" +
"\tdo {\n" +
"\t^\n" +
"Groovy:unexpected token: do @ line 6, column 5.\n" +
"----------\n");

checkGCUDeclaration("X.groovy",
"package foo;\n"+
"public class X {\n"+
" public X() {\n"+
" }\n"+
" public void foo() {\n"+
" }\n"+
"package foo;\n" +
"public class X {\n" +
" public X() {\n" +
" }\n" +
" public void foo() {\n" +
" }\n" +
"}\n");
}

@Test
public void testParsingRecovery_GRE1107_2() {
runNegativeTest(new String[] {
"X.groovy",
"package foo\n"+
"import java.io.Serializable;\n"+
"\n"+
"class X {\n"+
" public void foo() {\n"+
" def blah = this\n"+
" do {\n"+
" } while (blah != null)\n"+
"\n"+
" return null\n"+
" }\n"+
"foo/X.groovy",
"package foo\n" +
"import java.io.Serializable;\n" +
"\n" +
"class X {\n" +
" public void foo() {\n" +
" def blah = this\n" +
" do {\n" +
" } while (blah != null)\n" +
" }\n" +
"}\n",
},
Boolean.getBoolean("groovy.antlr4") ? "" :
"----------\n" +
"1. ERROR in X.groovy (at line 7)\n" +
"1. ERROR in foo\\X.groovy (at line 7)\n" +
"\tdo {\n" +
"\t^\n" +
"Groovy:unexpected token: do @ line 7, column 5.\n" +
"----------\n");

checkGCUDeclaration("X.groovy",
"package foo;\n"+
"import java.io.Serializable;\n"+
"public class X {\n"+
" public X() {\n"+
" }\n"+
" public void foo() {\n"+
" }\n"+
"package foo;\n" +
"import java.io.Serializable;\n" +
"public class X {\n" +
" public X() {\n" +
" }\n" +
" public void foo() {\n" +
" }\n" +
"}\n");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ public void testPrimitiveLikeTypeNames_GRE891() {
},
"works");
// return type is a single char type (not in package and not primitive)
assertEquals("[[LZ;",getReturnTypeOfMethod("Z.groovy","zzz"));
assertEquals("[[LZ;", getReturnTypeOfMethod("Z.groovy", "zzz"));
}

@Test
Expand All @@ -666,7 +666,7 @@ public void testPrimitiveLikeTypeNames_GRE891_2() {
},
"works");
// return type is a primitive
assertEquals("[[I",getReturnTypeOfMethod("Z.groovy","zzz"));
assertEquals("[[I", getReturnTypeOfMethod("Z.groovy", "zzz"));
}

@Test
Expand All @@ -687,7 +687,7 @@ public void testPrimitiveLikeTypeNames_GRE891_3() {
},
"works");
// return type is a qualified java built in type
assertEquals("[[Ljava.lang.String;",getReturnTypeOfMethod("Z.groovy","zzz"));
assertEquals("[[Ljava.lang.String;", getReturnTypeOfMethod("Z.groovy", "zzz"));
}

@Test
Expand All @@ -696,19 +696,16 @@ public void testAnonymousClasses_GE1531() {
"Foo.groovy",
"class Foo {\n"+
" def foo () {\n"+
" new java.lang.Runnable () {}//<--quick fix here\n"+
" new java.lang.Runnable() {}\n"+
" }\n"+
"}\n",
},
"----------\n" +
"1. ERROR in Foo.groovy (at line 3)\n" +
"\tnew java.lang.Runnable () {}//<--quick fix here\n" +
"\t ^^^^^^^^^^^^^^^^^^^\n" +
"Groovy:Can\'t have an abstract method in a non-abstract class. The class \'Foo$1\' must be declared abstract or the method \'void run()\' must be implemented.\n" +
"\tnew java.lang.Runnable() {}\n" +
"\t ^^^^^^^^^^^^^^^^^^\n" +
"Groovy:Can\'t have an abstract method in a non-abstract class. The class 'Foo$1' must be declared abstract or the method 'void run()' must be implemented.\n" +
"----------\n");
// return type is a qualified java built in type
ModuleNode mn = getModuleNode("Foo$1.class");
System.out.println(mn);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ public void testTypeChecked1() {
"----------\n" +
"2. ERROR in Foo.groovy (at line 5)\n" +
"\tprintln \"Did you spot the error in this ${message.toUppercase()}?\"\n" +
"\t ^^^^^^^^^^^^^^^^^^^^^^^\n" +
"\t " + (Boolean.getBoolean("groovy.antlr4") ? " ^^^^^^^^^^^^^^^^^^^^^" : "^^^^^^^^^^^^^^^^^^^^^^^") + "\n" +
"Groovy:[Static type checking] - Cannot find matching method java.lang.String#toUppercase(). Please check if the declared type is correct and if the method exists.\n" +
"----------\n");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.codehaus.groovy.ast.expr.ListExpression;
import org.codehaus.groovy.ast.expr.MapEntryExpression;
import org.codehaus.groovy.ast.expr.MapExpression;
import org.codehaus.groovy.ast.expr.MethodCall;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.MethodPointerExpression;
import org.codehaus.groovy.ast.expr.NamedArgumentListExpression;
Expand Down Expand Up @@ -2921,6 +2922,10 @@ protected Expression specialConstructorCallExpression(AST methodCallNode, ClassN
// GRECLIPSE add
expression.setNameStart(expression.getStart());
expression.setNameEnd(expression.getStart() + keywordLength - 1);

expression.setEnd(arguments.getEnd() + 1);
int[] row_col = locations.getRowCol(expression.getEnd());
expression.setLastLineNumber(row_col[0]); expression.setLastColumnNumber(row_col[1]);
// GRECLIPSE end
return expression;
}
Expand Down Expand Up @@ -3656,7 +3661,7 @@ protected void configureAST(ASTNode node, AST ast) {
node instanceof MapEntryExpression ||
node instanceof MapExpression ||
node instanceof CastExpression ||
node instanceof MethodCallExpression) &&
node instanceof MethodCall) &&
node.getStart() <= startoffset && node.getEnd() >= endoffset) {
// sloc has already been set and it is larger than this one
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.codehaus.groovy.ast.expr.ListExpression;
import org.codehaus.groovy.ast.expr.MapEntryExpression;
import org.codehaus.groovy.ast.expr.MapExpression;
import org.codehaus.groovy.ast.expr.MethodCall;
import org.codehaus.groovy.ast.expr.MethodCallExpression;
import org.codehaus.groovy.ast.expr.MethodPointerExpression;
import org.codehaus.groovy.ast.expr.NamedArgumentListExpression;
Expand Down Expand Up @@ -2924,6 +2925,10 @@ protected Expression specialConstructorCallExpression(AST methodCallNode, ClassN
// GRECLIPSE add
expression.setNameStart(expression.getStart());
expression.setNameEnd(expression.getStart() + keywordLength - 1);

expression.setEnd(arguments.getEnd() + 1);
int[] row_col = locations.getRowCol(expression.getEnd());
expression.setLastLineNumber(row_col[0]); expression.setLastColumnNumber(row_col[1]);
// GRECLIPSE end
return expression;
}
Expand Down Expand Up @@ -3679,7 +3684,7 @@ protected void configureAST(ASTNode node, AST ast) {
node instanceof MapEntryExpression ||
node instanceof MapExpression ||
node instanceof CastExpression ||
node instanceof MethodCallExpression) &&
node instanceof MethodCall) &&
node.getStart() <= startoffset && node.getEnd() >= endoffset) {
// sloc has already been set and it is larger than this one
return;
Expand Down
Loading

0 comments on commit 56ed572

Please sign in to comment.