diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java index 97c47eadb64b..d8dbe8026baf 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionData.java @@ -310,49 +310,6 @@ public class JavaCompletionData extends JavaAwareCompletionData { registerVariant(variant); } -// Catch/Finally completion - { - final ElementFilter position = AFTER_TRY_BLOCK; - - final CompletionVariant variant = new CompletionVariant(position); - variant.includeScopeClass(PsiCodeBlock.class, true); - variant.addCompletion(PsiKeyword.CATCH, TailTypes.CATCH_LPARENTH); - variant.addCompletion(PsiKeyword.FINALLY, TailTypes.FINALLY_LBRACE); - registerVariant(variant); - } - -// Catch/Finally completion - { - final ElementFilter position = new LeftNeighbour(new AndFilter( - new TextFilter("}"), - new ParentElementFilter(new AndFilter( - new LeftNeighbour(new NotFilter(new TextFilter(PsiKeyword.TRY))), - new OrFilter( - new ParentElementFilter(new ClassFilter(PsiTryStatement.class)), - new ParentElementFilter(new ClassFilter(PsiCatchSection.class))) - )))); - - final CompletionVariant variant = new CompletionVariant(position); - variant.includeScopeClass(PsiCodeBlock.class, false); - variant.addCompletion(PsiKeyword.CATCH, TailTypes.CATCH_LPARENTH); - variant.addCompletion(PsiKeyword.FINALLY, TailTypes.FINALLY_LBRACE); - registerVariant(variant); - } - -// Completion for else expression -// completion - { - final ElementFilter position = new LeftNeighbour( - new OrFilter( - new AndFilter(new TextFilter("}"),new ParentElementFilter(new ClassFilter(PsiIfStatement.class), 3)), - new AndFilter(new TextFilter(";"),new ParentElementFilter(new ClassFilter(PsiIfStatement.class), 2)) - )); - final CompletionVariant variant = new CompletionVariant(PsiMethod.class, position); - variant.addCompletion(PsiKeyword.ELSE); - - registerVariant(variant); - } - } private static TailType getReturnTail(PsiElement position) { @@ -381,7 +338,7 @@ public class JavaCompletionData extends JavaAwareCompletionData { } } - private static void addStatementKeywords(Consumer variant, PsiElement position) { + private static void addStatementKeywords(Consumer variant, PsiElement position, PsiElement prevLeaf) { variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.SWITCH), TailTypes.SWITCH_LPARENTH)); variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.WHILE), TailTypes.WHILE_LPARENTH)); variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.DO), TailTypes.DO_LBRACE)); @@ -402,11 +359,22 @@ public class JavaCompletionData extends JavaAwareCompletionData { ret = new OverrideableSpace(ret, returnTail); } variant.consume(ret); + + if (psiElement().withText(";").withSuperParent(2, PsiIfStatement.class).accepts(prevLeaf) || + psiElement().withText("}").withSuperParent(3, PsiIfStatement.class).accepts(prevLeaf)) { + variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.ELSE), TailTypes.SYNCHRONIZED_LPARENTH)); + } + + if (psiElement().withText("}").withParent(psiElement(PsiCodeBlock.class).withParent(or(psiElement(PsiTryStatement.class), psiElement(PsiCatchSection.class)))).accepts(prevLeaf)) { + variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.CATCH), TailTypes.CATCH_LPARENTH)); + variant.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.FINALLY), TailTypes.FINALLY_LBRACE)); + } + } public void fillCompletions(CompletionParameters parameters, final Consumer result) { final PsiElement position = parameters.getPosition(); - if (PsiTreeUtil.getParentOfType(position, PsiComment.class, false) != null) { + if (PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class) != null) { return; } @@ -420,7 +388,7 @@ public class JavaCompletionData extends JavaAwareCompletionData { } addBreakContinue(result, position); - addStatementKeywords(result, position); + addStatementKeywords(result, position, prevLeaf); } addThisSuper(result, position); @@ -764,10 +732,6 @@ public class JavaCompletionData extends JavaAwareCompletionData { } private static boolean isStatementPosition(PsiElement position) { - if (PsiTreeUtil.getNonStrictParentOfType(position, PsiLiteralExpression.class, PsiComment.class) != null) { - return false; - } - if (psiElement().withSuperParent(2, PsiConditionalExpression.class).andNot(psiElement().insideStarting(psiElement(PsiConditionalExpression.class))).accepts(position)) { return false; } diff --git a/java/java-tests/testData/codeInsight/completion/keywords/elseAfterRBrace.java b/java/java-tests/testData/codeInsight/completion/keywords/elseAfterRBrace.java new file mode 100644 index 000000000000..76e3300b3d97 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/keywords/elseAfterRBrace.java @@ -0,0 +1,6 @@ +public class A { + public void method() { + if (true) { foo(); } + + } +} diff --git a/java/java-tests/testData/codeInsight/completion/keywords/elseAfterSemicolon.java b/java/java-tests/testData/codeInsight/completion/keywords/elseAfterSemicolon.java new file mode 100644 index 000000000000..6e83759fe4e8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/keywords/elseAfterSemicolon.java @@ -0,0 +1,6 @@ +public class A { + public void method() { + if (true) foo(); + + } +} diff --git a/java/java-tests/testData/codeInsight/completion/keywords/secondCatch.java b/java/java-tests/testData/codeInsight/completion/keywords/secondCatch.java new file mode 100644 index 000000000000..75a14cc3318c --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/keywords/secondCatch.java @@ -0,0 +1,26 @@ +import java.lang.Exception; + +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class A{ + public void method(){ + try{ + String str = ""; + } + catch (Exception e) {} + + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java index c5c00a12c9ad..34f1f3d7348c 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/KeywordCompletionTest.java @@ -76,6 +76,8 @@ public class KeywordCompletionTest extends LightCompletionTestCase { public void testMethodScope3() throws Exception { doTest(1, "final", "public", "static", "volatile", "abstract", "throws", "instanceof"); } public void testMethodScope4() throws Exception { doTest(6, "final", "try", "for", "while", "return", "throw"); } public void testMethodScope5() throws Exception { doTest(false); } + public void testElseAfterSemicolon() throws Exception { doTest(1, "else"); } + public void testElseAfterRBrace() throws Exception { doTest(1, "else"); } public void testExtraBracketAfterFinally1() throws Exception { doTest(false); } public void testExtraBracketAfterFinally2() throws Exception { doTest(false); } public void testExtendsInCastTypeParameters() throws Exception { doTest(false); } @@ -89,6 +91,7 @@ public class KeywordCompletionTest extends LightCompletionTestCase { public void testInstanceOf2() throws Exception { doTest(false); } public void testInstanceOf3() throws Exception { doTest(false); } public void testCatchFinally() throws Exception { doTest(2, "catch", "finally"); } + public void testSecondCatch() throws Exception { doTest(2, "catch", "finally"); } public void testSuper1() throws Exception { doTest(1, "super"); } public void testSuper2() throws Exception { doTest(0, "super"); } public void testSuper3() throws Exception { doTest(true); }