Skip to content

Commit

Permalink
Fix for issue #478: adjust the enclosing type for type annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Feb 15, 2018
1 parent 3daf1dd commit 5a7f6f5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import static org.eclipse.jdt.ui.PreferenceConstants.TYPEFILTER_ENABLED
import groovy.transform.NotYetImplemented

import org.eclipse.jdt.internal.compiler.impl.CompilerOptions
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jface.text.contentassist.ICompletionProposal
import org.junit.Assert
import org.junit.Before
Expand Down Expand Up @@ -599,11 +600,11 @@ final class AnnotationCompletionTests extends CompletionTestSuite {
|class C {
|}
|'''.stripMargin()
checkProposalApplication(contents, expected, contents.indexOf('Ch') + 2, 'TypeChecked', true)
checkProposalApplication(contents, expected, getIndexOf(contents, 'Ch'), 'TypeChecked', true)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/365
void testQualifierForTypeAnnoScope() {
void testQualifierForTypeAnnoScope1() {
String contents = '''\
@SuppressWarnings(V)
class C {
Expand All @@ -616,7 +617,34 @@ final class AnnotationCompletionTests extends CompletionTestSuite {
public static final String VALUE = 'nls'
}
'''.stripIndent()
checkProposalApplication(contents, expected, contents.indexOf('(V') + 2, 'VALUE', false)
checkProposalApplication(contents, expected, getIndexOf(contents, '(V'), 'VALUE', false)
}

@Test // https://github.com/groovy/groovy-eclipse/issues/478
void testQualifierForTypeAnnoScope2() {
addJavaSource '''\
package a;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
public @interface B {
Class<?> value();
}
''', 'B', 'a'

String contents = '''\
@a.B(Nes)
class C {
static class Nested {}
}
'''.stripIndent()
String expected = '''\
@a.B(C.Nested)
class C {
static class Nested {}
}
'''.stripIndent()
setJavaPreference(PreferenceConstants.CODEASSIST_ADDIMPORT, 'false')
checkProposalApplication(contents, expected, getIndexOf(contents, '(Nes'), 'Nested - C', true)
}

//--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ private ICompletionProposal proposeType(AcceptedType type) {
proposal.setRelevance(computeRelevanceForTypeProposal(type.fullyQualifiedName, type.accessibility, type.modifiers));
proposal.setReplaceRange(completionOffset, context.completionLocation);
proposal.setSignature(Signature.createCharArrayTypeSignature(type.fullyQualifiedName, true));
proposal.setTokenRange(completionOffset, context.completionLocation);
proposal.setTokenRange(completionOffset, context.completionEnd);
proposal.setTypeName(type.qualifiedTypeName);

if (type.qualifiedTypeName.length != type.simpleTypeName.length) {
Expand Down Expand Up @@ -595,7 +595,6 @@ private ICompletionProposal proposeConstructor(AcceptedCtor ctor) {
proposal.setDeclarationTypeName(ctor.qualifiedTypeName);
proposal.setDeclarationSignature(CompletionEngine.createNonGenericTypeSignature(ctor.packageName, ctor.qualifiedTypeName));
proposal.setFlags(Flags.isDeprecated(ctor.typeModifiers) ? ctor.modifiers | Flags.AccDeprecated : ctor.modifiers);
proposal.setAdditionalFlags(ctor.extraFlags);
proposal.setAccessibility(ctor.accessibility);

if (contextOnly) {
Expand Down Expand Up @@ -743,8 +742,8 @@ private void initializeRelevanceRule(JDTResolver resolver) {
ClassNode lhsType = ((Variable) context.lhsNode).getType();
if (VariableScope.CLASS_CLASS_NODE.equals(lhsType) && lhsType.isUsingGenerics()) {
GenericsType target = lhsType.getGenericsTypes()[0];
if (target.getLowerBound() == null && target.getUpperBounds().length == 1 &&
VariableScope.OBJECT_CLASS_NODE.equals(target.getUpperBounds()[0])) {
if (target.getLowerBound() == null && target.getUpperBounds() == null ||
(target.getUpperBounds().length == 1 && VariableScope.OBJECT_CLASS_NODE.equals(target.getUpperBounds()[0]))) {
return;
}
// create a relevance rule that will boost types compatible with the target type
Expand Down Expand Up @@ -890,7 +889,7 @@ private static class AcceptedCtor {
public char[][] parameterNames;
public int typeModifiers;
public char[] packageName;
public int extraFlags;
//public int extraFlags;
public int accessibility;

public final char[] qualifiedTypeName;
Expand All @@ -916,7 +915,7 @@ public AcceptedCtor(
this.parameterNames = parameterNames;
this.typeModifiers = typeModifiers;
this.packageName = packageName;
this.extraFlags = extraFlags;
//this.extraFlags = extraFlags;
this.accessibility = accessibility;

this.qualifiedTypeName = simpleTypeName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.codehaus.groovy.ast.ASTNode;
import org.codehaus.groovy.ast.AnnotatedNode;
import org.codehaus.groovy.ast.AnnotationNode;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.eclipse.codeassist.GroovyContentAssist;
import org.codehaus.groovy.eclipse.codeassist.completions.GroovyExtendedCompletionContext;
Expand Down Expand Up @@ -151,11 +152,18 @@ public IType getEnclosingType() {
}

public ClassNode getEnclosingGroovyType() {
if (containingDeclaration instanceof ClassNode) {
return (ClassNode) containingDeclaration;
ClassNode containingTypeDecl;
if (!(containingDeclaration instanceof ClassNode)) {
containingTypeDecl = containingDeclaration.getDeclaringClass();
} else {
return containingDeclaration.getDeclaringClass();
containingTypeDecl = (ClassNode) containingDeclaration;
// check for type annotation (sits outside of declaration)
if (containingCodeBlock instanceof AnnotationNode &&
containingCodeBlock.getEnd() < containingTypeDecl.getNameStart()) {
containingTypeDecl = containingTypeDecl.getOuterClass();
}
}
return containingTypeDecl;
}

/**
Expand Down

0 comments on commit 5a7f6f5

Please sign in to comment.