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 2c4ac23db0f..1e3002fdb6e 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 @@ -43,6 +43,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings { public boolean CONTINUATION_INDENT_IN_PARAMETER_LISTS = true; public boolean CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = true; public boolean CONTINUATION_INDENT_FOR_CHAINED_CALLS = true; + public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0; public KotlinCodeStyleSettings(CodeStyleSettings container) { super("JetCodeStyleSettings", container); diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt index e19d2b349f8..2c445b7be7f 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/kotlinSpacingRules.kt @@ -31,10 +31,7 @@ import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings import org.jetbrains.kotlin.idea.formatter.KotlinSpacingBuilder.CustomSpacingBuilder import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.lexer.KtTokens.* -import org.jetbrains.kotlin.psi.KtClass -import org.jetbrains.kotlin.psi.KtFunction -import org.jetbrains.kotlin.psi.KtPrimaryConstructor -import org.jetbrains.kotlin.psi.KtPropertyAccessor +import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.textRangeWithoutComments val MODIFIERS_LIST_ENTRIES = TokenSet.orSet(TokenSet.create(ANNOTATION_ENTRY, ANNOTATION), MODIFIER_KEYWORDS) @@ -256,8 +253,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing betweenInside(REFERENCE_EXPRESSION, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1) betweenInside(TYPE_ARGUMENT_LIST, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1) - between(WHEN_ENTRY, WHEN_ENTRY).lineBreakInCode() - around(COLONCOLON).spaces(0) around(BY_KEYWORD).spaces(1) @@ -373,6 +368,17 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing inPosition(right = CLASS_BODY).customRule(leftBraceRule(blockType = CLASS_BODY)) + inPosition(left = WHEN_ENTRY, right = WHEN_ENTRY).customRule { _, left, right -> + val leftEntry = left.node.psi as KtWhenEntry + val rightEntry = right.node.psi as KtWhenEntry + val blankLines = if (leftEntry.expression is KtBlockExpression || rightEntry.expression is KtBlockExpression) + settings.kotlinSettings.BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES + else + 0 + + createSpacing(0, minLineFeeds = blankLines + 1) + } + inPosition(parent = WHEN_ENTRY, right = BLOCK).customRule(leftBraceRule()) inPosition(parent = WHEN, right = LBRACE).customRule { parent, _, _ -> diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt index d554289d835..ffcf5466352 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinLanguageCodeStyleSettingsProvider.kt @@ -76,6 +76,24 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide field1 } + + when(field1) { + 1 -> println("1") + 2 -> println("2") + 3 -> + println("3" + + "4") + } + + when(field2) { + 1 -> { + println("1") + } + + 2 -> { + println("2") + } + } } @@ -231,10 +249,15 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide "Put left brace on new line", CodeStyleSettingsCustomizable.WRAPPING_BRACES) } - LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> consumer.showStandardOptions( - "KEEP_BLANK_LINES_IN_CODE", - "KEEP_BLANK_LINES_IN_DECLARATIONS" - ) + LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> { + consumer.showStandardOptions( + "KEEP_BLANK_LINES_IN_CODE", + "KEEP_BLANK_LINES_IN_DECLARATIONS" + ) + showCustomOption(KotlinCodeStyleSettings::BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES, + "Around 'when' branches with {}", + CodeStyleSettingsCustomizable.BLANK_LINES) + } else -> consumer.showStandardOptions() } } diff --git a/idea/testData/formatter/WhenBlockBlankLines.after.kt b/idea/testData/formatter/WhenBlockBlankLines.after.kt new file mode 100644 index 00000000000..7acceb7d10c --- /dev/null +++ b/idea/testData/formatter/WhenBlockBlankLines.after.kt @@ -0,0 +1,13 @@ +fun f(x: Int) = when (x) { + 1 -> "Foo" + 2 -> "Bar" + + 3 -> { + "Foo" + } + + 4 -> "Bar" + else -> "Xyzzy" +} + +// SET_INT: BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 1 diff --git a/idea/testData/formatter/WhenBlockBlankLines.kt b/idea/testData/formatter/WhenBlockBlankLines.kt new file mode 100644 index 00000000000..35f37f45dff --- /dev/null +++ b/idea/testData/formatter/WhenBlockBlankLines.kt @@ -0,0 +1,11 @@ +fun f(x: Int) = when(x) { + 1 -> "Foo" + 2 -> "Bar" + 3 -> { + "Foo" + } + 4 -> "Bar" + else -> "Xyzzy" +} + +// SET_INT: BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 1 diff --git a/idea/testData/formatter/WhenEntryExpr.after.kt b/idea/testData/formatter/WhenEntryExpr.after.kt index 9820936150c..c85aa745fa3 100644 --- a/idea/testData/formatter/WhenEntryExpr.after.kt +++ b/idea/testData/formatter/WhenEntryExpr.after.kt @@ -57,4 +57,4 @@ fun some(x: Any) { 1 } } -} \ No newline at end of file +} diff --git a/idea/testData/formatter/WhenEntryExpr.kt b/idea/testData/formatter/WhenEntryExpr.kt index 75958910934..55010a37421 100644 --- a/idea/testData/formatter/WhenEntryExpr.kt +++ b/idea/testData/formatter/WhenEntryExpr.kt @@ -65,4 +65,4 @@ else 1 } } -} \ No newline at end of file +} diff --git a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java index 8f0dbe47df3..64bb1a8a9e5 100644 --- a/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/formatter/FormatterTestGenerated.java @@ -764,6 +764,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest { doTest(fileName); } + @TestMetadata("WhenBlockBlankLines.after.kt") + public void testWhenBlockBlankLines() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/WhenBlockBlankLines.after.kt"); + doTest(fileName); + } + @TestMetadata("WhenEntryExpr.after.kt") public void testWhenEntryExpr() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/WhenEntryExpr.after.kt");