Skip to content

Commit

Permalink
Fix source offsets for multi-dimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Apr 18, 2018
1 parent cabb416 commit 77a9934
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,7 @@ protected void importDef(AST importNode) {
configureAST(typeNode, packageNode);
}
configureAST(imp, importNode);
ConstantExpression nameExpr = new ConstantExpression(name);
configureAST(nameExpr, nameNode);
imp.setFieldNameExpr(nameExpr);
imp.setFieldNameExpr(literalExpression(nameNode, name));
// GRECLIPSE end
} else {
// import is like "import foo.Bar"
Expand All @@ -586,11 +584,8 @@ protected void importDef(AST importNode) {
// GRECLIPSE end
}
// GRECLIPSE add
// configure alias ast
if (alias != null) {
ConstantExpression aliasExpr = new ConstantExpression(alias);
configureAST(aliasExpr, aliasNode);
imp.setAliasExpr(aliasExpr);
imp.setAliasExpr(literalExpression(aliasNode, alias));
}
// GRECLIPSE end
}
Expand Down Expand Up @@ -2544,7 +2539,8 @@ protected Expression literalExpression(AST node, Object value) {
return constantExpression;
}

protected Expression rangeExpression(AST rangeNode, boolean inclusive) {
// GRECLIPSE Expression->ConstantExpression
protected ConstantExpression rangeExpression(AST rangeNode, boolean inclusive) {
AST node = rangeNode.getFirstChild();
Expression left = expression(node);
Expression right = expression(node.getNextSibling());
Expand Down Expand Up @@ -3058,6 +3054,11 @@ protected Expression constructorCallExpression(AST node) {
List size = arraySizeExpression(expressionNode);
ArrayExpression arrayExpression = new ArrayExpression(type, null, size);
configureAST(arrayExpression, constructorCallNode);
// GRECLIPSE add
Expression name = literalExpression(node, null);
arrayExpression.setNameStart(name.getStart());
arrayExpression.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
return arrayExpression;
}
Expression arguments = arguments(elist);
Expand All @@ -3068,15 +3069,17 @@ protected Expression constructorCallExpression(AST node) {
ret.setUsingAnonymousInnerClass(true);
innerClass.setUnresolvedSuperClass(type);
// GRECLIPSE add
innerClass.setNameStart(type.getStart());
innerClass.setNameEnd(type.getEnd() - 1);
Expression name = literalExpression(node, null);
innerClass.setNameStart(name.getStart());
innerClass.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
}

configureAST(ret, constructorCallNode);
// GRECLIPSE add
ret.setNameStart(type.getStart());
ret.setNameEnd(type.getEnd() - 1);
Expression name = literalExpression(node, null);
ret.setNameStart(name.getStart());
ret.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,9 +563,7 @@ protected void importDef(AST importNode) {
configureAST(typeNode, packageNode);
}
configureAST(imp, importNode);
ConstantExpression nameExpr = new ConstantExpression(name);
configureAST(nameExpr, nameNode);
imp.setFieldNameExpr(nameExpr);
imp.setFieldNameExpr(literalExpression(nameNode, name));
// GRECLIPSE end
} else {
// import is like "import foo.Bar"
Expand All @@ -587,11 +585,8 @@ protected void importDef(AST importNode) {
// GRECLIPSE end
}
// GRECLIPSE add
// configure alias ast
if (alias != null) {
ConstantExpression aliasExpr = new ConstantExpression(alias);
configureAST(aliasExpr, aliasNode);
imp.setAliasExpr(aliasExpr);
imp.setAliasExpr(literalExpression(aliasNode, alias));
}
// GRECLIPSE end
}
Expand Down Expand Up @@ -2556,7 +2551,8 @@ protected Expression literalExpression(AST node, Object value) {
return constantExpression;
}

protected Expression rangeExpression(AST rangeNode, boolean inclusive) {
// GRECLIPSE Expression->ConstantExpression
protected ConstantExpression rangeExpression(AST rangeNode, boolean inclusive) {
AST node = rangeNode.getFirstChild();
Expression left = expression(node);
Expression right = expression(node.getNextSibling());
Expand Down Expand Up @@ -3066,6 +3062,11 @@ protected Expression constructorCallExpression(AST node) {
List size = arraySizeExpression(expressionNode);
ArrayExpression arrayExpression = new ArrayExpression(type, null, size);
configureAST(arrayExpression, constructorCallNode);
// GRECLIPSE add
Expression name = literalExpression(node, null);
arrayExpression.setNameStart(name.getStart());
arrayExpression.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
return arrayExpression;
}
Expression arguments = arguments(elist);
Expand All @@ -3076,15 +3077,17 @@ protected Expression constructorCallExpression(AST node) {
ret.setUsingAnonymousInnerClass(true);
innerClass.setUnresolvedSuperClass(type);
// GRECLIPSE add
innerClass.setNameStart(type.getStart());
innerClass.setNameEnd(type.getEnd() - 1);
Expression name = literalExpression(node, null);
innerClass.setNameStart(name.getStart());
innerClass.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
}

configureAST(ret, constructorCallNode);
// GRECLIPSE add
ret.setNameStart(type.getStart());
ret.setNameEnd(type.getEnd() - 1);
Expression name = literalExpression(node, null);
ret.setNameStart(name.getStart());
ret.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3523,15 +3523,22 @@ public Expression visitCreator(CreatorContext ctx) {
constructorCallExpression.setNameStart(nameNode.getStart());
constructorCallExpression.setNameEnd(nameNode.getEnd() - 1);
return configureAST(constructorCallExpression, ctx);
// GRECLIPSE end
}

if (asBoolean(ctx.LBRACK()) || asBoolean(ctx.dims())) { // create array
ArrayExpression arrayExpression;
List<List<AnnotationNode>> allDimList;
// GRECLIPSE add
List<AnnotationsOptContext> annOptCtxt = new ArrayList<>();
// GRECLIPSE end

if (asBoolean(ctx.arrayInitializer())) {
ClassNode elementType = classNode;
allDimList = this.visitDims(ctx.dims());
// GRECLIPSE add
annOptCtxt.addAll(ctx.dims().annotationsOpt());
// GRECLIPSE end

for (int i = 0, n = allDimList.size() - 1; i < n; i++) {
elementType = elementType.makeArray();
Expand Down Expand Up @@ -3577,10 +3584,27 @@ public Expression visitCreator(CreatorContext ctx) {
Collections.reverse(emptyDimList);
allDimList.addAll(emptyDimList);
Collections.reverse(allDimList);
// GRECLIPSE add
annOptCtxt.addAll(ctx.annotationsOpt());
if (asBoolean(ctx.dimsOpt().dims())) annOptCtxt.addAll(ctx.dimsOpt().dims().annotationsOpt());
// GRECLIPSE end
}

arrayExpression.setType(createArrayType(classNode, allDimList));

// GRECLIPSE edit
//arrayExpression.setType(createArrayType(classNode, allDimList));
ClassNode componentType = arrayExpression.getType();
if (!asBoolean(ctx.dims())) {
configureAST(componentType, ctx);
} else {
configureAST(componentType, ctx, configureAST(new ConstantExpression(""), ctx.dims()));
}
for (int i = annOptCtxt.size() - 1; i > 0; i -= 1) {
componentType = componentType.getComponentType();
configureAST(componentType, ctx, configureAST(new ConstantExpression(""), annOptCtxt.get(i)));
}
ASTNode nameNode = configureAST(new ConstantExpression(""), ctx.createdName().qualifiedClassName());
arrayExpression.setNameStart(nameNode.getStart()); arrayExpression.setNameEnd(nameNode.getEnd() - 1);
// GRECLIPSE end
return configureAST(arrayExpression, ctx);
}

Expand Down Expand Up @@ -4329,6 +4353,12 @@ public ClassNode visitType(TypeContext ctx) {
classNode = this.createArrayType(classNode, dimList);
// GRECLIPSE add
configureAST(classNode, ctx);
ClassNode componentType = classNode;
for (int i = dimList.size() - 1; i > 0; i -= 1) {
componentType = componentType.getComponentType();
AnnotationsOptContext aoc = ctx.dimsOpt().dims().annotationsOpt(i);
configureAST(componentType, ctx, configureAST(new ConstantExpression(""), aoc));
}
// GRECLIPSE end
}

Expand Down Expand Up @@ -4741,10 +4771,10 @@ private Parameter processFormalParameter(GroovyParserRuleContext ctx,
if (asBoolean(ellipsis)) {
// GRECLIPSE edit
//classNode = configureAST(classNode.makeArray(), classNode);
if (asBoolean(typeContext)) {
classNode = configureAST(classNode.makeArray(), typeContext);
} else {
if (!asBoolean(typeContext)) {
classNode = configureAST(classNode.makeArray(), ellipsis);
} else {
classNode = configureAST(classNode.makeArray(), typeContext, configureAST(new ConstantExpression("..."), ellipsis));
}
// GRECLIPSE end
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -653,9 +653,7 @@ protected void importDef(AST importNode) {
configureAST(typeNode, packageNode);
}
configureAST(imp, importNode);
ConstantExpression nameExpr = new ConstantExpression(name);
configureAST(nameExpr, nameNode);
imp.setFieldNameExpr(nameExpr);
imp.setFieldNameExpr(literalExpression(nameNode, name));
// GRECLIPSE end
} else {
// import is like "import foo.Bar"
Expand All @@ -677,11 +675,8 @@ protected void importDef(AST importNode) {
// GRECLIPSE end
}
// GRECLIPSE add
// configure alias ast
if (alias != null) {
ConstantExpression aliasExpr = new ConstantExpression(alias);
configureAST(aliasExpr, aliasNode);
imp.setAliasExpr(aliasExpr);
imp.setAliasExpr(literalExpression(aliasNode, alias));
}
// GRECLIPSE end
}
Expand Down Expand Up @@ -2663,7 +2658,8 @@ protected Expression variableExpression(AST node) {
return variableExpression;
}

protected Expression literalExpression(AST node, Object value) {
// GRECLIPSE Expression->ConstantExpression
protected ConstantExpression literalExpression(AST node, Object value) {
ConstantExpression constantExpression = new ConstantExpression(value, value instanceof Boolean);
configureAST(constantExpression, node);
return constantExpression;
Expand Down Expand Up @@ -3179,6 +3175,11 @@ protected Expression constructorCallExpression(AST node) {
List size = arraySizeExpression(expressionNode);
ArrayExpression arrayExpression = new ArrayExpression(type, null, size);
configureAST(arrayExpression, constructorCallNode);
// GRECLIPSE add
Expression name = literalExpression(node, null);
arrayExpression.setNameStart(name.getStart());
arrayExpression.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
return arrayExpression;
}
Expression arguments = arguments(elist);
Expand All @@ -3189,15 +3190,17 @@ protected Expression constructorCallExpression(AST node) {
ret.setUsingAnonymousInnerClass(true);
innerClass.setUnresolvedSuperClass(type);
// GRECLIPSE add
innerClass.setNameStart(type.getStart());
innerClass.setNameEnd(type.getEnd() - 1);
Expression name = literalExpression(node, null);
innerClass.setNameStart(name.getStart());
innerClass.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
}

configureAST(ret, constructorCallNode);
// GRECLIPSE add
ret.setNameStart(type.getStart());
ret.setNameEnd(type.getEnd() - 1);
Expression name = literalExpression(node, null);
ret.setNameStart(name.getStart());
ret.setNameEnd(name.getEnd() - 1);
// GRECLIPSE end
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.eclipse.jdt.groovy.core.util.DepthFirstVisitor;
import org.eclipse.jdt.groovy.core.util.GroovyUtils;
import org.eclipse.jdt.groovy.search.AccessorSupport;
import org.eclipse.jdt.internal.compiler.ISourceElementRequestor;

Expand Down Expand Up @@ -120,7 +121,7 @@ protected void visitParameter(Parameter parameter) {

@Override
public void visitArrayExpression(ArrayExpression expression) {
if (expression.getType() != expression.getType().redirect()) {
if (expression.getEnd() > 0) {
visitTypeReference(expression.getType(), false, true);
}
super.visitArrayExpression(expression);
Expand Down Expand Up @@ -256,8 +257,7 @@ private void visitTypeReference(ClassNode type, boolean isAnnotation, boolean us
if (isAnnotation) {
requestor.acceptAnnotationTypeReference(splitName(type, useQualifiedName), type.getStart(), type.getEnd());
} else {
ClassNode componentType = type.getComponentType();
requestor.acceptTypeReference(splitName(componentType != null ? componentType : type, useQualifiedName), type.getStart(), type.getEnd());
requestor.acceptTypeReference(splitName(GroovyUtils.getBaseType(type), useQualifiedName), type.getStart(), type.getEnd());
}
visitTypeParameters(type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,9 @@
*/
package org.codehaus.groovy.eclipse.codebrowsing.tests

import static org.eclipse.jdt.groovy.core.tests.GroovyBundle.isParrotParser
import static org.junit.Assume.assumeTrue

import groovy.transform.NotYetImplemented

import org.eclipse.jdt.core.SourceRange
Expand Down Expand Up @@ -670,8 +673,8 @@ final class CodeSelectTypesTests extends BrowsingTestSuite {

@Test
void testSelectPackageOnFullyQualifiedName5() {
String contents = 'def x = new java.util.Date()'
assertCodeSelect([contents], 'x')
String contents = 'def z = new java.util.Date()'
assertCodeSelect([contents], 'z')
assertCodeSelect([contents], 'Date')
assertCodeSelect([contents], 'java', 'java')
assertCodeSelect([contents], 'util', 'java.util')
Expand All @@ -689,6 +692,59 @@ final class CodeSelectTypesTests extends BrowsingTestSuite {

@Test
void testSelectPackageOnFullyQualifiedName6() {
String contents = 'def z = new java.util.Date[9][10]'
assertCodeSelect([contents], 'z')
assertCodeSelect([contents], 'Date')
assertCodeSelect([contents], 'java', 'java')
assertCodeSelect([contents], 'util', 'java.util')
}

@Test
void testSelectPackageOnFullyQualifiedName6a() {
String contents = 'def z = new java.util.regex.Pattern[9][10][]'
assertCodeSelect([contents], 'z')
assertCodeSelect([contents], 'Pattern')
assertCodeSelect([contents], 'java', 'java')
assertCodeSelect([contents], 'util', 'java.util')
assertCodeSelect([contents], 'regex', 'java.util.regex')
}

@Test
void testSelectPackageOnFullyQualifiedName7() {
assumeTrue(isParrotParser())

String contents = 'def z = new java.util.Date[][] {}'
assertCodeSelect([contents], 'z')
assertCodeSelect([contents], 'Date')
assertCodeSelect([contents], 'java', 'java')
assertCodeSelect([contents], 'util', 'java.util')
}

@Test
void testSelectPackageOnFullyQualifiedName7a() {
assumeTrue(isParrotParser())

String contents = 'def z = new java.util.regex.Pattern[][][] { }'
assertCodeSelect([contents], 'z')
assertCodeSelect([contents], 'Pattern')
assertCodeSelect([contents], 'java', 'java')
assertCodeSelect([contents], 'util', 'java.util')
assertCodeSelect([contents], 'regex', 'java.util.regex')
}

@Test
void testSelectPackageOnFullyQualifiedName7b() {
assumeTrue(isParrotParser())

String contents = 'def z = new java.lang.String[] { "a", "b", "c" }'
assertCodeSelect([contents], 'z')
assertCodeSelect([contents], 'String')
assertCodeSelect([contents], 'java', 'java')
assertCodeSelect([contents], 'lang', 'java.lang')
}

@Test
void testSelectPackageOnFullyQualifiedName8() {
String contents = 'def x = java.util.Collections.emptyList()'
assertCodeSelect([contents], 'x')
assertCodeSelect([contents], 'Collections')
Expand Down
Loading

0 comments on commit 77a9934

Please sign in to comment.