Correct keyword completion after try in assignment

This commit is contained in:
Valentin Kipyatkov
2016-04-19 16:28:04 +03:00
parent 4f9f5c205f
commit 364a8801a3
3 changed files with 27 additions and 5 deletions
@@ -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 {
@@ -0,0 +1,13 @@
fun foo() {
val v = try {
bar()
}
<caret>
}
// EXIST: catch
// EXIST: finally
// EXIST: false
// EXIST: null
// EXIST: true
// NOTHING_ELSE
@@ -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);
}