From d65b7a5b4b362045feae3c0f5b396da0dabc1ea4 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 6 Apr 2017 20:42:16 +0300 Subject: [PATCH] Better indent in else-if editing (KT-14601) #KT-14601 Fixed (cherry picked from commit 25d6d61) --- .../idea/formatter/KotlinCommonBlock.kt | 7 +++ .../kotlin/idea/editor/TypedHandlerTest.kt | 47 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index 54cd5eed396..11e90bb22dc 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -157,6 +157,13 @@ abstract class KotlinCommonBlock( return ChildAttributes(Indent.getNoneIndent(), null) } + if (type == KtNodeTypes.IF) { + val elseBlock = mySubBlocks?.getOrNull(newChildIndex) + if (elseBlock is ASTBlock && elseBlock.node.elementType == KtTokens.ELSE_KEYWORD) { + return ChildAttributes.DELEGATE_TO_NEXT_CHILD + } + } + return when (type) { in CODE_BLOCKS, KtNodeTypes.WHEN, KtNodeTypes.IF, KtNodeTypes.FOR, KtNodeTypes.WHILE, KtNodeTypes.DO_WHILE -> ChildAttributes(Indent.getNormalIndent(), null) diff --git a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt index d0e26ebcb0e..4adfc8f4301 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/editor/TypedHandlerTest.kt @@ -557,6 +557,53 @@ class TypedHandlerTest : LightCodeInsightTestCase() { ) } + fun testIndentBeforeElseWithBlock() { + doCharTypeTest( + '\n', + """ + |fun test(b: Boolean) { + | if (b) { + | } + | else if (!b) { + | } + |} + """, + """ + |fun test(b: Boolean) { + | if (b) { + | } + | + | else if (!b) { + | } + |} + """ + ) + } + + fun testIndentBeforeElseWithoutBlock() { + doCharTypeTest( + '\n', + """ + |fun test(b: Boolean) { + | if (b) + | foo() + | else { + | } + |} + """, + """ + |fun test(b: Boolean) { + | if (b) + | foo() + | + | else { + | } + |} + """ + ) + } + + fun testMoveThroughGT() { LightPlatformCodeInsightTestCase.configureFromFileText("a.kt", "val a: List>>") EditorTestUtil.performTypingAction(LightPlatformCodeInsightTestCase.getEditor(), '>')