From 8e31113a4f215650342e138ccb03dbd690a678ef Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Thu, 21 Jun 2018 20:47:06 +0300 Subject: [PATCH] Ident elvis with normal ident in Kotlin code style (KT-25008) #KT-25008 Fixed --- .../core/formatter/KotlinCodeStyleSettings.java | 1 + .../kotlin/idea/formatter/KotlinCommonBlock.kt | 8 +++++++- .../idea/formatter/KotlinStyleGuideCodeStyle.kt | 1 + .../KotlinLanguageCodeStyleSettingsProvider.kt | 5 +++++ .../ElvisContinuationIndentOptions.after.inv.kt | 6 ++++++ .../ElvisContinuationIndentOptions.after.kt | 6 ++++++ .../formatter/ElvisContinuationIndentOptions.kt | 6 ++++++ idea/testData/formatter/ElvisIndent.after.inv.kt | 14 ++++++++++++++ .../kotlin/formatter/FormatterTestGenerated.java | 15 +++++++++++++++ 9 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 idea/testData/formatter/ElvisContinuationIndentOptions.after.inv.kt create mode 100644 idea/testData/formatter/ElvisContinuationIndentOptions.after.kt create mode 100644 idea/testData/formatter/ElvisContinuationIndentOptions.kt create mode 100644 idea/testData/formatter/ElvisIndent.after.inv.kt diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java b/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java index 90b095433bf..33134dbe54d 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java @@ -53,6 +53,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings { public boolean CONTINUATION_INDENT_FOR_CHAINED_CALLS = true; public boolean CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = true; public boolean CONTINUATION_INDENT_IN_IF_CONDITIONS = true; + public boolean CONTINUATION_INDENT_IN_ELVIS = true; public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0; public int WRAP_EXPRESSION_BODY_FUNCTIONS = 0; public int WRAP_ELVIS_EXPRESSIONS = 1; 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 34af9ff8c36..d3edc0b35bd 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -193,9 +193,15 @@ abstract class KotlinCommonBlock( private fun splitSubBlocksOnElvis(nodeSubBlocks: List): List { val elvisIndex = nodeSubBlocks.indexOfBlockWithType(ELVIS_SET) if (elvisIndex >= 0) { + val indent = if (settings.kotlinCustomSettings.CONTINUATION_INDENT_IN_ELVIS) { + Indent.getContinuationIndent() + } else { + Indent.getNormalIndent() + } + return nodeSubBlocks.splitAtIndex( elvisIndex, - Indent.getContinuationIndent(), + indent, null ) } diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinStyleGuideCodeStyle.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinStyleGuideCodeStyle.kt index 694c5601e2c..87c84e20279 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinStyleGuideCodeStyle.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinStyleGuideCodeStyle.kt @@ -48,6 +48,7 @@ class KotlinStyleGuideCodeStyle : PredefinedCodeStyle("Kotlin style guide", Kotl CONTINUATION_INDENT_FOR_CHAINED_CALLS = false CONTINUATION_INDENT_IN_SUPERTYPE_LISTS = false CONTINUATION_INDENT_IN_IF_CONDITIONS = false + CONTINUATION_INDENT_IN_ELVIS = false WRAP_EXPRESSION_BODY_FUNCTIONS = CodeStyleSettings.WRAP_AS_NEEDED IF_RPAREN_ON_NEW_LINE = true } diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt index ac9907b024e..94b1500ac36 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt @@ -339,6 +339,11 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide CodeStyleSettingsCustomizable.WRAP_VALUES_FOR_SINGLETON ) ) + showCustomOption( + KotlinCodeStyleSettings::CONTINUATION_INDENT_IN_ELVIS, + title = "Use continuation indent", + groupName = "Elvis expressions" + ) @Suppress("InvalidBundleOrProperty") showCustomOption( KotlinCodeStyleSettings::IF_RPAREN_ON_NEW_LINE, diff --git a/idea/testData/formatter/ElvisContinuationIndentOptions.after.inv.kt b/idea/testData/formatter/ElvisContinuationIndentOptions.after.inv.kt new file mode 100644 index 00000000000..2e81732285e --- /dev/null +++ b/idea/testData/formatter/ElvisContinuationIndentOptions.after.inv.kt @@ -0,0 +1,6 @@ +fun test(some: Any?, error: Int) { + val test = some + ?: error +} + +// SET_FALSE: CONTINUATION_INDENT_IN_ELVIS \ No newline at end of file diff --git a/idea/testData/formatter/ElvisContinuationIndentOptions.after.kt b/idea/testData/formatter/ElvisContinuationIndentOptions.after.kt new file mode 100644 index 00000000000..a286e091e87 --- /dev/null +++ b/idea/testData/formatter/ElvisContinuationIndentOptions.after.kt @@ -0,0 +1,6 @@ +fun test(some: Any?, error: Int) { + val test = some + ?: error +} + +// SET_FALSE: CONTINUATION_INDENT_IN_ELVIS \ No newline at end of file diff --git a/idea/testData/formatter/ElvisContinuationIndentOptions.kt b/idea/testData/formatter/ElvisContinuationIndentOptions.kt new file mode 100644 index 00000000000..d4133b3b169 --- /dev/null +++ b/idea/testData/formatter/ElvisContinuationIndentOptions.kt @@ -0,0 +1,6 @@ +fun test(some: Any?, error: Int) { + val test = some + ?: error +} + +// SET_FALSE: CONTINUATION_INDENT_IN_ELVIS \ No newline at end of file diff --git a/idea/testData/formatter/ElvisIndent.after.inv.kt b/idea/testData/formatter/ElvisIndent.after.inv.kt new file mode 100644 index 00000000000..5c7f185b052 --- /dev/null +++ b/idea/testData/formatter/ElvisIndent.after.inv.kt @@ -0,0 +1,14 @@ +fun foo() { + val topLevelDeclaration = + DescriptorUtils.getParentOfType(referencedDescriptor, PropertyDescriptor::class.java, false) as DeclarationDescriptor? + ?: DescriptorUtils.getParentOfType( + referencedDescriptor, + TypeAliasConstructorDescriptor::class.java, + false + )?.typeAliasDescriptor + ?: DescriptorUtils.getParentOfType(referencedDescriptor, FunctionDescriptor::class.java, false) + ?: return emptyList() +} + +// SET_FALSE: CONTINUATION_INDENT_IN_ARGUMENT_LISTS +// SET_FALSE: CONTINUATION_INDENT_FOR_EXPRESSION_BODIES diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index 71ee403ecac..bc9a05841f3 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -216,6 +216,11 @@ public class FormatterTestGenerated extends AbstractFormatterTest { runTest("idea/testData/formatter/Elvis.after.kt"); } + @TestMetadata("ElvisContinuationIndentOptions.after.kt") + public void testElvisContinuationIndentOptions() throws Exception { + runTest("idea/testData/formatter/ElvisContinuationIndentOptions.after.kt"); + } + @TestMetadata("ElvisIndent.after.kt") public void testElvisIndent() throws Exception { runTest("idea/testData/formatter/ElvisIndent.after.kt"); @@ -1136,6 +1141,16 @@ public class FormatterTestGenerated extends AbstractFormatterTest { runTest("idea/testData/formatter/Elvis.after.inv.kt"); } + @TestMetadata("ElvisContinuationIndentOptions.after.inv.kt") + public void testElvisContinuationIndentOptions() throws Exception { + runTest("idea/testData/formatter/ElvisContinuationIndentOptions.after.inv.kt"); + } + + @TestMetadata("ElvisIndent.after.inv.kt") + public void testElvisIndent() throws Exception { + runTest("idea/testData/formatter/ElvisIndent.after.inv.kt"); + } + @TestMetadata("EmptyBlocks.after.inv.kt") public void testEmptyBlocks() throws Exception { runTest("idea/testData/formatter/EmptyBlocks.after.inv.kt");