From 640bf22e4f542879c129b9b4c5c8f29f1a21b69a Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 13 Dec 2017 16:26:02 +0100 Subject: [PATCH] Refactoring: extract 'continuationIf' method --- .../idea/formatter/KotlinCommonBlock.kt | 50 ++++++++----------- 1 file changed, 20 insertions(+), 30 deletions(-) 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 c5aba0da1a1..fc6a948a4d1 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -525,6 +525,21 @@ fun needWrapArgumentList(psi: PsiElement): Boolean { return args?.singleOrNull()?.getArgumentExpression() !is KtObjectLiteralExpression } +fun NodeIndentStrategy.PositionStrategy.continuationIf( + option: (KotlinCodeStyleSettings) -> Boolean, + indentFirst: Boolean = false +): NodeIndentStrategy { + return set { settings -> + if (option(settings.kotlinCustomSettings)) { + if (indentFirst) + Indent.getContinuationIndent() + else + Indent.getContinuationWithoutFirstIndent() + } else + Indent.getNormalIndent() + } +} + private val INDENT_RULES = arrayOf( strategy("No indent for braces in blocks") .within(KtNodeTypes.BLOCK, KtNodeTypes.CLASS_BODY, KtNodeTypes.FUNCTION_LITERAL) @@ -557,12 +572,7 @@ private val INDENT_RULES = arrayOf( .forElement { it.psi is KtExpression && it.psi !is KtBlockExpression } - .set { settings -> - if (settings.kotlinCustomSettings.CONTINUATION_INDENT_FOR_EXPRESSION_BODIES) - Indent.getContinuationIndent() - else - Indent.getNormalIndent() - }, + .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES, indentFirst = true), strategy("Property accessor expression body") .within(KtNodeTypes.PROPERTY_ACCESSOR) @@ -576,12 +586,7 @@ private val INDENT_RULES = arrayOf( .forElement { it.psi is KtExpression } - .set { settings -> - if (settings.kotlinCustomSettings.CONTINUATION_INDENT_FOR_EXPRESSION_BODIES) - Indent.getContinuationWithoutFirstIndent() - else - Indent.getNormalIndent() - }, + .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES), strategy("Indent for parts") .within(KtNodeTypes.PROPERTY, KtNodeTypes.FUN, KtNodeTypes.DESTRUCTURING_DECLARATION, KtNodeTypes.SECONDARY_CONSTRUCTOR) @@ -592,12 +597,7 @@ private val INDENT_RULES = arrayOf( .within(QUALIFIED_EXPRESSIONS) .notForType(KtTokens.DOT, KtTokens.SAFE_ACCESS) .forElement { it.treeParent.firstChildNode != it } - .set { settings -> - if (settings.kotlinCustomSettings.CONTINUATION_INDENT_FOR_CHAINED_CALLS) - Indent.getContinuationWithoutFirstIndent() - else - Indent.getNormalIndent() - }, + .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_CHAINED_CALLS), strategy("Colon of delegation list") .within(KtNodeTypes.CLASS, KtNodeTypes.OBJECT_DECLARATION) @@ -606,12 +606,7 @@ private val INDENT_RULES = arrayOf( strategy("Delegation list") .within(KtNodeTypes.SUPER_TYPE_LIST, KtNodeTypes.INITIALIZER_LIST) - .set { settings -> - if (settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_SUPERTYPE_LISTS) - Indent.getContinuationIndent(false) - else - Indent.getNormalIndent() - }, + .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_SUPERTYPE_LISTS, indentFirst = true), strategy("Indices") .within(KtNodeTypes.INDICES) @@ -655,12 +650,7 @@ private val INDENT_RULES = arrayOf( strategy("Parameter list") .within(KtNodeTypes.VALUE_PARAMETER_LIST) .forElement { it.elementType == KtNodeTypes.VALUE_PARAMETER && it.psi.prevSibling != null } - .set { settings -> - if (settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_PARAMETER_LISTS) - Indent.getContinuationIndent() - else - Indent.getNormalIndent() - }, + .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_PARAMETER_LISTS, indentFirst = true), strategy("Where clause") .within(KtNodeTypes.CLASS, KtNodeTypes.FUN, KtNodeTypes.PROPERTY)