Skip to content

Commit e1a97f7

Browse files
committed
Fix for #1526: qualified this and super
1 parent 403f3fc commit e1a97f7

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

base/org.eclipse.jdt.groovy.core/src/org/eclipse/jdt/groovy/search/SimpleTypeLookup.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,12 @@ protected TypeLookupResult findType(final Expression node, final ClassNode decla
226226
} else {
227227
return new TypeLookupResult(VariableScope.VOID_CLASS_NODE, null, null, TypeConfidence.UNKNOWN, scope);
228228
}
229+
} else if (scope.getEnclosingNode() instanceof PropertyExpression) {
230+
if (("this".equals(node.getText()) || "super".equals(node.getText())) && isStaticObjectExpression) {
231+
// "Type.this" in inner class or "Type.super" in implementer
232+
ClassNode type = declaringType.getGenericsTypes()[0].getType();
233+
return new TypeLookupResult(type, null, type, confidence, scope);
234+
}
229235
}
230236

231237
if ("new".equals(node.getText()) && isStaticObjectExpression && isStaticReferenceToInstanceMethod(scope)) {
@@ -373,9 +379,6 @@ protected TypeLookupResult findTypeForNameWithKnownObjectExpression(final String
373379
resolvedType = VariableScope.OBJECT_CLASS_NODE;
374380
resolvedDeclaringType = VariableScope.CLOSURE_CLASS_NODE;
375381
declaration = resolvedDeclaringType.getMethods("call").get(0);
376-
} else if ("this".equals(name) && declaringType.equals(VariableScope.CLASS_CLASS_NODE)) {
377-
// "Type.this" (aka ClassExpression.ConstantExpression) within inner class
378-
declaration = resolvedType = resolvedDeclaringType = declaringType.getGenericsTypes()[0].getType();
379382
} else {
380383
resolvedType = VariableScope.OBJECT_CLASS_NODE;
381384
resolvedDeclaringType = declaringType;

ide-test/org.codehaus.groovy.eclipse.codebrowsing.test/src/org/codehaus/groovy/eclipse/codebrowsing/tests/CodeSelectKeywordsTests.groovy

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ final class CodeSelectKeywordsTests extends BrowsingTestSuite {
181181
assertCodeSelect([contents], 'this', null)
182182
}
183183

184+
@Test
185+
void testCodeSelectKeywordThis3() {
186+
// Java Editor doesn't code select on 'this' property expression
187+
String contents = 'class C { class D { def x() { C.this } } }'
188+
assertCodeSelect([contents], 'this', null)
189+
}
190+
184191
@Test // GRECLIPSE-548
185192
void testCodeSelectKeywordSuper1() {
186193
String contents = '''\
@@ -203,6 +210,30 @@ final class CodeSelectKeywordsTests extends BrowsingTestSuite {
203210
assertCodeSelect([contents], 'super', 'Super')
204211
}
205212

213+
@Test
214+
void testCodeSelectKeywordSuper3() {
215+
addJavaSource '''\
216+
|package p;
217+
|public interface A {
218+
| default void m() {}
219+
|}
220+
|'''.stripMargin(), 'A', 'p'
221+
addJavaSource '''\
222+
|package p;
223+
|public interface B {
224+
| default void m() {}
225+
|}
226+
|'''.stripMargin(), 'B', 'p'
227+
228+
String contents = '''\
229+
|import p.*
230+
|class C implements A,B {
231+
| void m() { A.super.m() }
232+
|}
233+
|'''.stripMargin()
234+
assertCodeSelect([contents], 'super', null)
235+
}
236+
206237
@Test
207238
void testCodeSelectKeywordReturn1() {
208239
String contents = 'def meth() { return null }'

ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/ui/SemanticHighlightingTests.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5219,6 +5219,44 @@ final class SemanticHighlightingTests extends GroovyEclipseTestSuite {
52195219
new HighlightedTypedPosition(contents.lastIndexOf('getFoo'), 6, METHOD_CALL))
52205220
}
52215221

5222+
@Test // https://github.com/groovy/groovy-eclipse/issues/1526
5223+
void testInterface() {
5224+
assumeTrue(isParrotParser() && isAtLeastGroovy(40))
5225+
5226+
String contents = '''\
5227+
|interface I {
5228+
| static bar() {}
5229+
| private baz() {}
5230+
| default foo() {
5231+
| I.bar()
5232+
| I.this.baz() // required qualifier w/o static compilation
5233+
| }
5234+
|}
5235+
|class C implements I{
5236+
| @Override
5237+
| def foo() {
5238+
| I.super.foo()
5239+
| }
5240+
|}
5241+
|'''.stripMargin()
5242+
5243+
assertHighlighting(contents,
5244+
new HighlightedTypedPosition(contents.indexOf('I'), 1, INTERFACE),
5245+
new HighlightedTypedPosition(contents.indexOf('bar'), 3, STATIC_METHOD),
5246+
new HighlightedTypedPosition(contents.indexOf('baz'), 3, METHOD),
5247+
new HighlightedTypedPosition(contents.indexOf('foo'), 3, METHOD),
5248+
new HighlightedTypedPosition(contents.indexOf('I.'), 1, INTERFACE),
5249+
new HighlightedTypedPosition(contents.lastIndexOf('bar'), 3, STATIC_CALL),
5250+
new HighlightedTypedPosition(contents.indexOf('I.this.'), 1, INTERFACE),
5251+
new HighlightedTypedPosition(contents.lastIndexOf('baz'), 3, METHOD_CALL),
5252+
new HighlightedTypedPosition(contents.indexOf('C'), 1, CLASS),
5253+
new HighlightedTypedPosition(contents.indexOf('I{'), 1, INTERFACE),
5254+
new HighlightedTypedPosition(contents.lastIndexOf('foo() {'), 3, METHOD),
5255+
new HighlightedTypedPosition(contents.lastIndexOf('I.'), 1, INTERFACE),
5256+
//new HighlightedTypedPosition(contents.lastIndexOf('super'), 5, UNKNOWN),
5257+
new HighlightedTypedPosition(contents.lastIndexOf('foo'), 3, METHOD_CALL))
5258+
}
5259+
52225260
//
52235261
private int counter
52245262

ide/org.codehaus.groovy.eclipse.codebrowsing/src/org/codehaus/groovy/eclipse/codebrowsing/requestor/CodeSelectHelper.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ protected static boolean isKeyword(final ASTNode node, final char[] contents, fi
178178
}
179179

180180
@Override public void visitConstantExpression(final ConstantExpression expr) {
181-
// "null" or "true" or "false"
182-
keyword[0] = (expr.isNullExpression() || expr.isTrueExpression() || expr.isFalseExpression());
181+
// "null" or "true" or "false" or "Type.this" or "Type.super"
182+
keyword[0] = (expr.isNullExpression() || expr.isTrueExpression() || expr.isFalseExpression() ||
183+
(length == 4 && expr.getText().equals("this")) || (length == 5 && expr.getText().equals("super")));
183184
}
184185

185186
@Override public void visitConstructorCallExpression(final ConstructorCallExpression expr) {
@@ -236,7 +237,7 @@ protected static boolean isKeyword(final ASTNode node, final char[] contents, fi
236237
}
237238

238239
protected static boolean isStringLiteral(final ASTNode node, final char[] contents, final int start, final int length) {
239-
if (node instanceof ConstantExpression && ClassHelper.STRING_TYPE.equals(((ConstantExpression) node).getType())) {
240+
if (node instanceof ConstantExpression && ((ConstantExpression) node).getType().equals(ClassHelper.STRING_TYPE)) {
240241
return (start > node.getStart() && length < node.getLength());
241242
} else if (node instanceof MethodNode) {
242243
return (start > ((MethodNode) node).getNameStart() && start + length <= ((MethodNode) node).getNameEnd());

0 commit comments

Comments
 (0)