From 7ea7daa29ae837e2efca5ea284952d33ff0177bb Mon Sep 17 00:00:00 2001 From: peter Date: Fri, 15 May 2015 17:04:06 +0200 Subject: [PATCH] IDEA-140294 If you declare a variable and initialize it on the same line, autocomplete will suggest the new variable in that line --- .../codeInsight/completion/JavaCompletionContributor.java | 7 +++---- .../completion/normal/ExcludeVariableBeingDeclared.java | 6 ++++++ .../completion/normal/ExcludeVariableBeingDeclared2.java | 6 ++++++ .../codeInsight/completion/NormalCompletionTest.groovy | 2 ++ 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared.java create mode 100644 java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared2.java diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java index d2e5db9b75a9..b154c38f9f29 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java @@ -161,10 +161,9 @@ public class JavaCompletionContributor extends CompletionContributor { return createAnnotationFilter(position); } - if (psiElement().afterLeaf("=").inside(PsiVariable.class).accepts(position)) { - return new OrFilter( - new ClassFilter(PsiVariable.class, false), - new ExcludeDeclaredFilter(new ClassFilter(PsiVariable.class))); + PsiVariable var = PsiTreeUtil.getParentOfType(position, PsiVariable.class, false, PsiClass.class); + if (var != null && PsiTreeUtil.isAncestor(var.getInitializer(), position, false)) { + return new ExcludeDeclaredFilter(new ClassFilter(PsiVariable.class)); } if (SWITCH_LABEL.accepts(position)) { diff --git a/java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared.java b/java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared.java new file mode 100644 index 000000000000..4980472b67ca --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared.java @@ -0,0 +1,6 @@ +public class MyFirstTestClassFoo { + + { + String newVar = String.valueOf(newV) + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared2.java b/java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared2.java new file mode 100644 index 000000000000..6e36dde1ec63 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/ExcludeVariableBeingDeclared2.java @@ -0,0 +1,6 @@ +public class MyFirstTestClassFoo { + + { + String newVar = "" + newV) + } +} \ No newline at end of file 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 9e51e2a5d79a..b95761cefba3 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/NormalCompletionTest.groovy @@ -676,6 +676,8 @@ public class ListUtils { public void testNothingAfterNumericLiteral() throws Throwable { doAntiTest(); } public void testNothingAfterTypeParameterQualifier() { doAntiTest(); } + public void testExcludeVariableBeingDeclared() { doAntiTest(); } + public void testExcludeVariableBeingDeclared2() { doAntiTest(); } public void testSpacesAroundEq() throws Throwable { doTest('='); }