From ade51588f9a1849e50835ac335354d0ed11eb1d7 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Thu, 13 Jun 2019 09:52:03 +0900 Subject: [PATCH] Insert paired brackets: insert space after right brace of nested lambda (KT-28098) #KT-28098 Fixed --- .../kotlin/idea/editor/KotlinTypedHandler.java | 11 ++++++++++- .../jetbrains/kotlin/idea/editor/TypedHandlerTest.kt | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java index 22be5953bcd..2da7b52cc6a 100644 --- a/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java +++ b/idea/src/org/jetbrains/kotlin/idea/editor/KotlinTypedHandler.java @@ -116,7 +116,8 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { int tokenBeforeBraceOffset = iterator.getStart(); - PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); + Document document = editor.getDocument(); + PsiDocumentManager.getInstance(project).commitDocument(document); PsiElement leaf = file.findElementAt(offset); if (leaf != null) { @@ -130,6 +131,14 @@ public class KotlinTypedHandler extends TypedHandlerDelegate { return Result.STOP; } } + if (leaf.getText().equals("}") + && parent instanceof KtFunctionLiteral + && document.getLineNumber(offset) == document.getLineNumber(parent.getTextRange().getStartOffset()) + ) { + EditorModificationUtil.insertStringAtCaret(editor, "{} ", false, false); + editor.getCaretModel().moveToOffset(offset + 1); + return Result.STOP; + } } return Result.CONTINUE; diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt index aa5b8ef3d54..e3b85d9d607 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt @@ -398,7 +398,13 @@ class TypedHandlerTest : LightCodeInsightTestCase() { " if (some.hello (12) {})\n" + "}" ) - + + fun testInsertSpaceAfterRightBraceOfNestedLambda() = doTypeTest( + '{', + "val t = Array(100) { Array(200) }", + "val t = Array(100) { Array(200) {} }" + ) + fun testAutoInsertParenInStringLiteral() = doTypeTest( '(', """fun f() { println("$dollar{f}") }""",