From 4b10f1fb6f088959592cdb2e9fd7682e8d904aa9 Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Mon, 20 Feb 2012 17:04:00 +0400 Subject: [PATCH] IDEA-49425 Groovy: completion for special property ('.@') and method ('.&') access syntax shows irrelevant choices --- .../CompleteReferenceExpression.java | 27 ++++++++--- .../completion/GroovyCompletionTest.groovy | 45 +++++++++++++++++-- 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/CompleteReferenceExpression.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/CompleteReferenceExpression.java index 2c4a6af49cfb..cba92f066030 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/CompleteReferenceExpression.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/impl/statements/expressions/CompleteReferenceExpression.java @@ -342,6 +342,8 @@ public class CompleteReferenceExpression { private final Set myPropertyNames = new HashSet(); private final Set myLocalVars = new HashSet(); private final Set myProcessedMethodWithOptionalParams = new HashSet(); + private final boolean myFieldPointerOperator; + private final boolean myMethodPointerOperator; protected CompleteReferenceProcessor(GrReferenceExpression place, Consumer consumer, @NotNull PrefixMatcher matcher, CompletionParameters parameters) { super(null, EnumSet.allOf(ResolveKind.class), place, PsiType.EMPTY_ARRAY); @@ -352,6 +354,9 @@ public class CompleteReferenceExpression { mySkipPackages = PsiImplUtil.getRuntimeQualifier(place) == null; myEventListener = JavaPsiFacade.getInstance(place.getProject()).findClass("java.util.EventListener", place.getResolveScope()); myPropertyNames.addAll(addAllRestrictedProperties(place)); + + myFieldPointerOperator = place.hasAt(); + myMethodPointerOperator = place.getDotTokenType() == GroovyTokenTypes.mMEMBER_POINTER; } @Override @@ -396,17 +401,25 @@ public class CompleteReferenceExpression { result.isInvokedOnProperty()); } + if (myFieldPointerOperator && !(element instanceof PsiVariable)) { + return; + } + if (myMethodPointerOperator && !(element instanceof PsiMethod)) { + return; + } addCandidate(result); - if (element instanceof PsiMethod) { - processProperty((PsiMethod)element, result); - } - else if (element instanceof GrField) { - if (((GrField)element).isProperty()) { - processPropertyFromField((GrField)element, result); + if (!myFieldPointerOperator && !myMethodPointerOperator) { + if (element instanceof PsiMethod) { + processProperty((PsiMethod)element, result); + } + else if (element instanceof GrField) { + if (((GrField)element).isProperty()) { + processPropertyFromField((GrField)element, result); + } } } - else if (element instanceof GrVariable) { + if (element instanceof GrVariable && !(element instanceof GrField)) { myLocalVars.add(((GrVariable)element).getName()); } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy index fe133175c2fb..fb4b05585d9d 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/completion/GroovyCompletionTest.groovy @@ -18,13 +18,13 @@ package org.jetbrains.plugins.groovy.completion; import com.intellij.codeInsight.CodeInsightSettings +import com.intellij.codeInsight.completion.CompletionType import com.intellij.codeInsight.lookup.LookupElement import com.intellij.codeInsight.lookup.LookupElementPresentation -import org.jetbrains.plugins.groovy.GroovyFileType -import org.jetbrains.plugins.groovy.util.TestUtils import com.intellij.psi.codeStyle.CodeStyleSettingsManager +import org.jetbrains.plugins.groovy.GroovyFileType import org.jetbrains.plugins.groovy.formatter.GroovyCodeStyleSettings -import com.intellij.codeInsight.completion.CompletionType +import org.jetbrains.plugins.groovy.util.TestUtils /** * @author Maxim.Medvedev @@ -1133,4 +1133,43 @@ Util.foo()''' } public void testPropertyChain() { doBasicTest() } + + public void testMethodPointer() { + myFixture.configureByText('_a.groovy', '''\ +class Base { + def prefixMethod(){} + def prefixField +} + +new Base().&prefix''') + myFixture.completeBasic() + assertNull(myFixture.lookupElements) + myFixture.checkResult('''\ +class Base { + def prefixMethod(){} + def prefixField +} + +new Base().&prefixMethod''') + } + + public void testFieldPointer() { + myFixture.configureByText('_a.groovy', '''\ +class Base { + def prefixMethod(){} + def prefixField +} + +new Base().@prefix''') + myFixture.completeBasic() + assertNull(myFixture.lookupElements) + myFixture.checkResult('''\ +class Base { + def prefixMethod(){} + def prefixField +} + +new Base().@prefixField''') + } + } \ No newline at end of file