From 45eba8f54abd9cb91097217cb965966c95c1c0c3 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 20 Sep 2011 17:26:37 +0200 Subject: [PATCH] don't suggest to call methods in anonymous class bodies --- .../PsiJavaCodeReferenceElementImpl.java | 38 +++++++++++-------- .../completion/normal/PrivateInAnonymous.java | 10 +++++ .../normal/PrivateInAnonymous_after.java | 10 +++++ .../completion/NormalCompletionTest.groovy | 1 + 4 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous_after.java diff --git a/java/java-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java index 5557de518d1b..9e441705a70c 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/PsiJavaCodeReferenceElementImpl.java @@ -743,21 +743,7 @@ public class PsiJavaCodeReferenceElementImpl extends CompositePsiElement impleme public void processVariants(final PsiScopeProcessor processor) { final OrFilter filter = new OrFilter(); - PsiElement superParent = getParent(); - boolean smartCompletion = true; - if (isQualified()) { - smartCompletion = false; - } - else { - while (superParent != null) { - if (superParent instanceof PsiCodeBlock || superParent instanceof PsiLocalVariable) { - smartCompletion = false; - break; - } - superParent = superParent.getParent(); - } - } - if (!smartCompletion && !isCodeFragmentType(getTreeParent().getElementType()) && !(getParent() instanceof PsiAnnotation)) { + if (isInCode()) { filter.addFilter(new AndFilter(ElementClassFilter.METHOD, new NotFilter(new ConstructorFilter()))); filter.addFilter(ElementClassFilter.VARIABLE); } @@ -805,6 +791,28 @@ public class PsiJavaCodeReferenceElementImpl extends CompositePsiElement impleme PsiScopesUtil.resolveAndWalk(proc, this, null, true); } + private boolean isInCode() { + if (isCodeFragmentType(getTreeParent().getElementType()) || getParent() instanceof PsiAnnotation) { + return false; + } + + if (isQualified()) { + return true; + } + + PsiElement superParent = getParent(); + while (superParent != null) { + if (superParent instanceof PsiCodeBlock || superParent instanceof PsiLocalVariable) { + return true; + } + if (superParent instanceof PsiClass) { + return false; + } + superParent = superParent.getParent(); + } + return false; + } + private void addClassFilter(final OrFilter filter) { if (getParent() instanceof PsiAnnotation) { filter.addFilter(new AnnotationTypeFilter()); diff --git a/java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous.java b/java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous.java new file mode 100644 index 000000000000..8b5f2006ad3e --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous.java @@ -0,0 +1,10 @@ +class A { + A() { + new Runnable() { + pri + }; + + } + + public void prepareImage() {} +} diff --git a/java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous_after.java b/java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous_after.java new file mode 100644 index 000000000000..b9c77cc06935 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/PrivateInAnonymous_after.java @@ -0,0 +1,10 @@ +class A { + A() { + new Runnable() { + private + }; + + } + + public void prepareImage() {} +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy index 5ac71e462daf..f684c51723a6 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy @@ -427,6 +427,7 @@ public class NormalCompletionTest extends LightFixtureCompletionTestCase { configure() assertStringItems 'final', 'finalize' } + public void testPrivateInAnonymous() throws Throwable { doTest() } public void testMethodParenthesesSpaces() throws Throwable { final settings = CodeStyleSettingsManager.getSettings(getProject())