Options for blank lines between 'when' branches
#KT-18607 Fixed
This commit is contained in:
+1
@@ -43,6 +43,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
|
|||||||
public boolean CONTINUATION_INDENT_IN_PARAMETER_LISTS = true;
|
public boolean CONTINUATION_INDENT_IN_PARAMETER_LISTS = true;
|
||||||
public boolean CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = true;
|
public boolean CONTINUATION_INDENT_FOR_EXPRESSION_BODIES = true;
|
||||||
public boolean CONTINUATION_INDENT_FOR_CHAINED_CALLS = true;
|
public boolean CONTINUATION_INDENT_FOR_CHAINED_CALLS = true;
|
||||||
|
public int BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES = 0;
|
||||||
|
|
||||||
public KotlinCodeStyleSettings(CodeStyleSettings container) {
|
public KotlinCodeStyleSettings(CodeStyleSettings container) {
|
||||||
super("JetCodeStyleSettings", container);
|
super("JetCodeStyleSettings", container);
|
||||||
|
|||||||
@@ -31,10 +31,7 @@ import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
|||||||
import org.jetbrains.kotlin.idea.formatter.KotlinSpacingBuilder.CustomSpacingBuilder
|
import org.jetbrains.kotlin.idea.formatter.KotlinSpacingBuilder.CustomSpacingBuilder
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens.*
|
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||||
import org.jetbrains.kotlin.psi.KtClass
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.KtFunction
|
|
||||||
import org.jetbrains.kotlin.psi.KtPrimaryConstructor
|
|
||||||
import org.jetbrains.kotlin.psi.KtPropertyAccessor
|
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.textRangeWithoutComments
|
import org.jetbrains.kotlin.psi.psiUtil.textRangeWithoutComments
|
||||||
|
|
||||||
val MODIFIERS_LIST_ENTRIES = TokenSet.orSet(TokenSet.create(ANNOTATION_ENTRY, ANNOTATION), MODIFIER_KEYWORDS)
|
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(REFERENCE_EXPRESSION, LAMBDA_ARGUMENT, CALL_EXPRESSION).spaces(1)
|
||||||
betweenInside(TYPE_ARGUMENT_LIST, 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(COLONCOLON).spaces(0)
|
||||||
|
|
||||||
around(BY_KEYWORD).spaces(1)
|
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(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_ENTRY, right = BLOCK).customRule(leftBraceRule())
|
||||||
inPosition(parent = WHEN, right = LBRACE).customRule {
|
inPosition(parent = WHEN, right = LBRACE).customRule {
|
||||||
parent, _, _ ->
|
parent, _, _ ->
|
||||||
|
|||||||
+27
-4
@@ -76,6 +76,24 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
|
|||||||
|
|
||||||
field1
|
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",
|
"Put left brace on new line",
|
||||||
CodeStyleSettingsCustomizable.WRAPPING_BRACES)
|
CodeStyleSettingsCustomizable.WRAPPING_BRACES)
|
||||||
}
|
}
|
||||||
LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> consumer.showStandardOptions(
|
LanguageCodeStyleSettingsProvider.SettingsType.BLANK_LINES_SETTINGS -> {
|
||||||
"KEEP_BLANK_LINES_IN_CODE",
|
consumer.showStandardOptions(
|
||||||
"KEEP_BLANK_LINES_IN_DECLARATIONS"
|
"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()
|
else -> consumer.showStandardOptions()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
+11
@@ -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
|
||||||
@@ -764,6 +764,12 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
|
|||||||
doTest(fileName);
|
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")
|
@TestMetadata("WhenEntryExpr.after.kt")
|
||||||
public void testWhenEntryExpr() throws Exception {
|
public void testWhenEntryExpr() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/WhenEntryExpr.after.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/formatter/WhenEntryExpr.after.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user