diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt index bc47fb1557d..1b5cfb6e5fa 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/KeywordCompletion.kt @@ -188,13 +188,16 @@ object KeywordCompletion { is KtBlockExpression -> { var prefixText = "fun foo() { " if (prevParent is KtExpression) { - val tryExpression = prevParent.prevSiblingOfSameType() as? KtTryExpression - if (tryExpression != null && tryExpression.finallyBlock == null) { - prefixText += when { - tryExpression.catchClauses.isEmpty() -> "try {}\n" - else -> "try {} catch (e: E) {}\n" + // check that we are right after a try-expression without finally-block + val prevLeaf = prevParent.prevLeaf { it !is PsiWhiteSpace && it !is PsiComment && it !is PsiErrorElement } + if (prevLeaf?.node?.elementType == KtTokens.RBRACE) { + val blockParent = (prevLeaf?.parent as? KtBlockExpression)?.parent + when (blockParent) { + is KtTryExpression -> prefixText += "try {}\n" + is KtCatchClause -> prefixText += "try {} catch (e: E) {}\n" } } + return buildFilterWithContext(prefixText, prevParent, position) } else { diff --git a/idea/idea-completion/testData/keywords/AfterTryInAssignment.kt b/idea/idea-completion/testData/keywords/AfterTryInAssignment.kt new file mode 100644 index 00000000000..731e8440265 --- /dev/null +++ b/idea/idea-completion/testData/keywords/AfterTryInAssignment.kt @@ -0,0 +1,13 @@ +fun foo() { + val v = try { + bar() + } + +} + +// EXIST: catch +// EXIST: finally +// EXIST: false +// EXIST: null +// EXIST: true +// NOTHING_ELSE diff --git a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java index 66a288e23ed..95d3052ed91 100644 --- a/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java +++ b/idea/idea-completion/tests/org/jetbrains/kotlin/idea/completion/test/KeywordCompletionTestGenerated.java @@ -85,6 +85,12 @@ public class KeywordCompletionTestGenerated extends AbstractKeywordCompletionTes doTest(fileName); } + @TestMetadata("AfterTryInAssignment.kt") + public void testAfterTryInAssignment() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/idea-completion/testData/keywords/AfterTryInAssignment.kt"); + doTest(fileName); + } + public void testAllFilesPresentInKeywords() throws Exception { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/idea-completion/testData/keywords"), Pattern.compile("^(.+)\\.kt$"), false); }