Skip to content

Commit

Permalink
rework import visitation
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 2, 2019
1 parent 28575a0 commit 273a92a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.expr.Expression;
Expand Down Expand Up @@ -85,17 +84,6 @@ default TypeLookupResult lookupType(MethodNode node, VariableScope scope) {
return null;
}

/**
* Determine the type for an import node.
*
* @param node the AST Node to determine the type for
* @param scope the variable scope available at this location
* @return the type for the node and confidence in that type, or null if cannot determine
*/
default TypeLookupResult lookupType(ImportNode node, VariableScope scope) {
return null;
}

/**
* Determine the type for a parameter node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.DynamicVariable;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.MethodNode;
import org.codehaus.groovy.ast.Parameter;
import org.codehaus.groovy.ast.PropertyNode;
Expand Down Expand Up @@ -99,12 +98,6 @@ public TypeLookupResult lookupType(final MethodNode node, final VariableScope sc
return new TypeLookupResult(node.getReturnType(), node.getDeclaringClass(), node, TypeConfidence.EXACT, scope);
}

@Override
public TypeLookupResult lookupType(final ImportNode node, final VariableScope scope) {
ClassNode baseType = Optional.ofNullable(node.getType()).orElse(VariableScope.NULL_TYPE);
return new TypeLookupResult(baseType, baseType, baseType, TypeConfidence.EXACT, scope);
}

@Override
public TypeLookupResult lookupType(final ClassNode node, final VariableScope scope) {
ClassNode type = node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ public void visitPackage(PackageNode node) {
IJavaElement oldEnclosing = enclosingElement;
enclosingElement = unit.getPackageDeclaration(node.getName().substring(0, node.getName().length() - 1));
try {
TypeLookupResult result = new TypeLookupResult(null, null, node, TypeConfidence.EXACT, null);
VisitStatus status = notifyRequestor(node, requestor, result);
TypeLookupResult noLookup = new TypeLookupResult(null, null, node, TypeConfidence.EXACT, null);
VisitStatus status = notifyRequestor(node, requestor, noLookup);
if (status == VisitStatus.STOP_VISIT) {
throw new VisitCompleted(status);
}
Expand Down Expand Up @@ -517,24 +517,11 @@ public void visitImports(ModuleNode node) {
}
}

VariableScope scope = scopes.getLast();
assignmentStorer.storeImport(imp, scope);
try {
TypeLookupResult result = null;
VariableScope scope = scopes.getLast();
scope.setPrimaryNode(false);
assignmentStorer.storeImport(imp, scope);
for (ITypeLookup lookup : lookups) {
TypeLookupResult candidate = lookup.lookupType(imp, scope);
if (candidate != null) {
if (result == null || result.confidence.isLessThan(candidate.confidence)) {
result = candidate;
}
if (result.confidence.isAtLeast(TypeConfidence.INFERRED)) {
break;
}
}
}
VisitStatus status = notifyRequestor(imp, requestor, result);

TypeLookupResult noLookup = new TypeLookupResult(null, null, imp, TypeConfidence.EXACT, scope);
VisitStatus status = notifyRequestor(imp, requestor, noLookup);
switch (status) {
case CONTINUE:
try {
Expand Down Expand Up @@ -670,10 +657,10 @@ public void visitAnnotation(AnnotationNode node) {
case CONTINUE:
// visit annotation label
visitClassReference(type);
// visit attribute values
super.visitAnnotation(node);
// visit attribute labels
visitAnnotationKeys(node);
// visit attribute values
super.visitAnnotation(node);
break;
case CANCEL_BRANCH:
return;
Expand All @@ -692,13 +679,13 @@ private void visitAnnotationKeys(AnnotationNode node) {
ASTNode attr;
if (meth != null) {
attr = meth; // no Groovy AST node exists for name
noLookup = new TypeLookupResult(meth.getReturnType(), type.redirect(), meth, TypeConfidence.EXACT, scope);
noLookup = new TypeLookupResult(meth.getReturnType(), type, meth, TypeConfidence.EXACT, scope);
} else {
attr = new ConstantExpression(name);
// this is very rough; it only works for an attribute that directly follows '('
attr.setStart(type.getEnd() + 1);
attr.setEnd(attr.getStart() + name.length());
noLookup = new TypeLookupResult(VariableScope.VOID_CLASS_NODE, type.redirect(), null, TypeConfidence.UNKNOWN, scope);
noLookup = new TypeLookupResult(VariableScope.VOID_CLASS_NODE, type, null, TypeConfidence.UNKNOWN, scope);
}
noLookup.enclosingAnnotation = node; // set context for requestor
if (notifyRequestor(attr, requestor, noLookup) != VisitStatus.CONTINUE) break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ final class CodeSelectImportsTests extends BrowsingTestSuite {
|Pattern p = ~/123/
|'''.stripMargin()

def elem = assertCodeSelect([source], 'State')
assert elem.inferredElement instanceof ClassNode
assertCodeSelect([source], 'State')
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ private void handleMatch(TypeLookupResult result, IJavaElement enclosingElement)
} else {
requestedNode = ((ConstructorNode) requestedNode).getDeclaringClass();
}
} else if (requestedNode instanceof ImportNode) {
ImportNode importNode = (ImportNode) requestedNode;
requestedNode = Optional.ofNullable(importNode.getType()).map(ClassNode::redirect).get();
}

if (requestedNode != null) {
Expand Down Expand Up @@ -214,8 +217,7 @@ private void handleMatch(TypeLookupResult result, IJavaElement enclosingElement)
requestedElement = root.getPackageFragment(pack);
}

} else if (nodeToLookFor instanceof ImportNode &&
((ImportNode) nodeToLookFor).isStar() && !((ImportNode) nodeToLookFor).isStatic()) {
} else if (nodeToLookFor instanceof ImportNode && ((ImportNode) nodeToLookFor).isStar() && !((ImportNode) nodeToLookFor).isStatic()) {
int start = nodeToLookFor.getStart(), until = selectRegion.getEnd();
if (start < until) {
String pack = gunit.getSource().substring(start, until).replaceFirst("^import\\s+", "");
Expand All @@ -227,7 +229,7 @@ private void handleMatch(TypeLookupResult result, IJavaElement enclosingElement)
}
}
}
requestedNode = nodeToLookFor; // result.declaration should be java.lang.Object here
requestedNode = nodeToLookFor;

} else {
String qualifier = checkQualifiedType(result, enclosingElement);
Expand Down Expand Up @@ -297,12 +299,13 @@ private LocalVariable createLocalVariable(TypeLookupResult result, IJavaElement

private String checkQualifiedType(TypeLookupResult result, IJavaElement enclosingElement) throws JavaModelException {
if (result.declaration instanceof ClassNode ||
result.declaration instanceof ImportNode ||
result.declaration instanceof ConstructorNode /*||
result.declaration instanceof DeclarationExpression*/) {

ClassNode type = result.type;
if (type == null) type = result.declaringType;
if (type == null) type = (ClassNode) result.declaration;
if (result.declaration instanceof ImportNode)
type = ((ImportNode) result.declaration).getType();
int typeStart = startOffset(type), typeEnd = endOffset(type);
type = GroovyUtils.getBaseType(type); // unpack type now that position is known

Expand Down Expand Up @@ -364,6 +367,8 @@ private ClassNode findDeclaringType(TypeLookupResult result) {
declaringType = ((MethodNode) result.declaration).getDeclaringClass();
} else if (result.declaration instanceof PropertyNode) {
declaringType = ((PropertyNode) result.declaration).getDeclaringClass();
} else if (result.declaration instanceof ImportNode) {
declaringType = ((ImportNode) result.declaration).getType();
} else if (result.declaration instanceof DeclarationExpression) {
declaringType = GroovyUtils.getBaseType(((DeclarationExpression) result.declaration).getLeftExpression().getType());
}
Expand Down

0 comments on commit 273a92a

Please sign in to comment.