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 ad6ce9526b8a..256013ec0842 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaCompletionContributor.java @@ -126,7 +126,7 @@ public class JavaCompletionContributor extends CompletionContributor { return ElementClassFilter.CLASS; } - if (psiElement().afterLeaf(psiElement(JavaTokenType.RBRACE).withParents(PsiCodeBlock.class, PsiTryStatement.class)).accepts(position) || + if (isCatchFinallyPosition(position) || JavaKeywordCompletion.START_SWITCH.accepts(position) || JavaKeywordCompletion.isInstanceofPlace(position) || JavaKeywordCompletion.isAfterPrimitiveOrArrayType(position)) { @@ -166,6 +166,15 @@ public class JavaCompletionContributor extends CompletionContributor { return TrueFilter.INSTANCE; } + private static boolean isCatchFinallyPosition(PsiElement position) { + PsiElement leaf = PsiTreeUtil.prevVisibleLeaf(position); + return leaf != null && + leaf.textMatches("}") && + leaf.getParent() instanceof PsiCodeBlock && + leaf.getParent().getParent() instanceof PsiTryStatement && + ((PsiTryStatement)leaf.getParent().getParent()).getResourceList() == null; + } + private static boolean isInsideAnnotationName(PsiElement position) { PsiAnnotation anno = PsiTreeUtil.getParentOfType(position, PsiAnnotation.class, true, PsiMember.class); return anno != null && PsiTreeUtil.isAncestor(anno.getNameReferenceElement(), position, true); diff --git a/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java b/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java index d6f9359019b8..92f3271c4e94 100644 --- a/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java +++ b/java/java-impl/src/com/intellij/codeInsight/completion/JavaKeywordCompletion.java @@ -303,8 +303,9 @@ public class JavaKeywordCompletion { if (statement != null && statement.getTextRange().getStartOffset() == position.getTextRange().getStartOffset()) { if (!psiElement().withSuperParent(2, PsiSwitchStatement.class).afterLeaf("{").accepts(statement)) { PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(prevLeaf, PsiTryStatement.class); - if (tryStatement == null || tryStatement.getCatchSections().length > 0 || tryStatement.getFinallyBlock() != null) { + if (tryStatement == null || tryStatement.getCatchSections().length > 0 || tryStatement.getFinallyBlock() != null || tryStatement.getResourceList() != null) { result.consume(new OverrideableSpace(createKeyword(position, PsiKeyword.FINAL), TailType.HUMBLE_SPACE_BEFORE_WORD)); + return; } } } diff --git a/java/java-tests/testData/codeInsight/completion/normal/AfterTryWithResources.java b/java/java-tests/testData/codeInsight/completion/normal/AfterTryWithResources.java new file mode 100644 index 000000000000..1bcabc1bd045 --- /dev/null +++ b/java/java-tests/testData/codeInsight/completion/normal/AfterTryWithResources.java @@ -0,0 +1,8 @@ +public class Util { + int goo() { + try (Object foo = bar()) { + + } + + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/Normal17CompletionTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/Normal17CompletionTest.groovy index 3f10302dacee..aa8ecaa28156 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/Normal17CompletionTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/Normal17CompletionTest.groovy @@ -56,4 +56,10 @@ public class Normal17CompletionTest extends LightFixtureCompletionTestCase { myFixture.type('\n') checkResultByFile(getTestName(false) + "_after.java") } + + public void testAfterTryWithResources() { + configureByFile(getTestName(false) + ".java") + def strings = myFixture.lookupElementStrings + assert strings.containsAll(['final', 'finally', 'int', 'Util']) + } }