Options for blank lines between 'when' branches

#KT-18607 Fixed
This commit is contained in:
Dmitry Jemerov
2017-06-22 14:59:09 +02:00
parent 8fe2858c6a
commit 6a96ade02c
8 changed files with 72 additions and 12 deletions
@@ -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);
@@ -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, _, _ ->
@@ -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()
}
}
+13
View File
@@ -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
View File
@@ -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
+1 -1
View File
@@ -57,4 +57,4 @@ fun some(x: Any) {
1
}
}
}
}
+1 -1
View File
@@ -65,4 +65,4 @@ else
1
}
}
}
}
@@ -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");