[kotlin] code style: add line break after multiline when entry

^KTIJ-17326 Fixed

GitOrigin-RevId: 86eff5a4d2e3ca08b7e31943f4047b8d1f2d5851
This commit is contained in:
Dmitry Gridin
2022-03-10 14:22:49 +00:00
committed by intellij-monorepo-bot
parent 823b766a13
commit 6f2d358b2d
54 changed files with 204 additions and 34 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.core.formatter;
@@ -35,6 +35,7 @@ public class KotlinCodeStyleSettings extends CustomCodeStyleSettings {
public boolean SPACE_AFTER_EXTEND_COLON = true;
public boolean INSERT_WHITESPACES_IN_SIMPLE_ONE_LINE_METHOD = true;
public boolean ALIGN_IN_COLUMNS_CASE_BRANCH = false;
public boolean LINE_BREAK_AFTER_MULTILINE_WHEN_ENTRY = true;
public boolean SPACE_AROUND_FUNCTION_TYPE_ARROW = true;
public boolean SPACE_AROUND_WHEN_ARROW = true;
public boolean SPACE_BEFORE_LAMBDA_ARROW = true;
@@ -529,14 +529,31 @@ 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.requireNode().psi as KtWhenEntry
val rightEntry = right.requireNode().psi as KtWhenEntry
val blankLines = if (leftEntry.expression is KtBlockExpression || rightEntry.expression is KtBlockExpression)
kotlinCustomSettings.BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES
else
0
val blankLines = kotlinCustomSettings.BLANK_LINES_AROUND_BLOCK_WHEN_BRANCHES
if (blankLines != 0) {
val leftEntry = left.requireNode().psi as KtWhenEntry
val rightEntry = right.requireNode().psi as KtWhenEntry
if (leftEntry.expression is KtBlockExpression || rightEntry.expression is KtBlockExpression) {
return@customRule createSpacing(0, minLineFeeds = blankLines + 1)
}
}
createSpacing(0, minLineFeeds = blankLines + 1)
if (kotlinCustomSettings.LINE_BREAK_AFTER_MULTILINE_WHEN_ENTRY) {
builderUtil.createLineFeedDependentSpacing(
minSpaces = 0,
maxSpaces = 0,
minimumLineFeeds = 1,
keepLineBreaks = commonCodeStyleSettings.KEEP_LINE_BREAKS,
keepBlankLines = commonCodeStyleSettings.KEEP_BLANK_LINES_IN_CODE,
dependency = left.textRange,
rule = DependentSpacingRule(DependentSpacingRule.Trigger.HAS_LINE_FEEDS).registerData(
DependentSpacingRule.Anchor.MIN_LINE_FEEDS,
2,
),
)
} else {
createSpacing(0, minLineFeeds = 1)
}
}
inPosition(parent = WHEN_ENTRY, right = BLOCK).customRule(leftBraceRule())
@@ -520,6 +520,7 @@ formatter.text.use.defaults.from=Use defaults from:
formatter.title.after.colon.before.declaration.type=After colon, before declaration type
formatter.title.after.colon.in.new.type.definition=After colon in new type definition
formatter.title.align.when.branches.in.columns=Align 'when' branches in columns
formatter.title.line.break.after.multiline.when.entry=New line after multiline entry
formatter.title.around.arrow.in.function.types=Around arrow in function types
formatter.title.around.arrow.in=Around arrow in "when" clause
formatter.title.around.when.branches.with=Around 'when' branches with {}
@@ -1,4 +1,4 @@
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package org.jetbrains.kotlin.idea.formatter
@@ -195,6 +195,12 @@ class KotlinLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvide
codeStyleSettingsCustomizableOptions.WRAPPING_SWITCH_STATEMENT
)
showCustomOption(
KotlinCodeStyleSettings::LINE_BREAK_AFTER_MULTILINE_WHEN_ENTRY,
KotlinBundle.message("formatter.title.line.break.after.multiline.when.entry"),
codeStyleSettingsCustomizableOptions.WRAPPING_SWITCH_STATEMENT
)
showCustomOption(
KotlinCodeStyleSettings::LBRACE_ON_NEXT_LINE,
KotlinBundle.message("formatter.title.put.left.brace.on.new.line"),
@@ -55,6 +55,7 @@
"keep_indents_on_empty_lines": false,
"keep_line_breaks": true,
"lbrace_on_next_line": false,
"line_break_after_multiline_when_entry": true,
"line_comment_add_space": false,
"line_comment_add_space_on_reformat": false,
"line_comment_at_first_column": true,
@@ -55,6 +55,7 @@
"keep_indents_on_empty_lines": false,
"keep_line_breaks": true,
"lbrace_on_next_line": false,
"line_break_after_multiline_when_entry": true,
"line_comment_add_space": false,
"line_comment_add_space_on_reformat": false,
"line_comment_at_first_column": true,
@@ -45,3 +45,4 @@ val a = when {
}
// SET_FALSE: ALIGN_IN_COLUMNS_CASE_BRANCH
// SET_TRUE: LINE_BREAK_AFTER_MULTILINE_WHEN_ENTRY
@@ -40,8 +40,10 @@ fun some(x: Any) {
val a = when {
true && true && true -> {
}
else -> {
}
}
// SET_FALSE: ALIGN_IN_COLUMNS_CASE_BRANCH
// SET_TRUE: LINE_BREAK_AFTER_MULTILINE_WHEN_ENTRY
@@ -50,4 +50,5 @@ val a = when {
}
}
// SET_FALSE: ALIGN_IN_COLUMNS_CASE_BRANCH
// SET_FALSE: ALIGN_IN_COLUMNS_CASE_BRANCH
// SET_TRUE: LINE_BREAK_AFTER_MULTILINE_WHEN_ENTRY
@@ -3,6 +3,7 @@ fun a() {
true && true -> {}
false -> {
}
else -> {}
}
}
@@ -3,6 +3,7 @@ fun a() {
true && true -> {}
false -> {
}
else -> {}
}
}
@@ -2,10 +2,13 @@ fun some(x: Any) {
when (x) {
is Int ->
0
3 ->
2
in 0..3 ->
2
else ->
1
}
@@ -13,12 +16,15 @@ fun some(x: Any) {
is Int -> {
0
}
3 -> {
2
}
in 0..3 -> {
2
}
else -> {
1
}
@@ -27,12 +33,15 @@ fun some(x: Any) {
is Int -> {
0
}
3 -> {
2
}
in 0..3 -> {
2
}
else -> {
1
}
@@ -43,19 +52,22 @@ fun some(x: Any) {
-> {
0
}
3,
-> {
2
}
in
0..3,
-> {
2
}
else
-> {
1
}
}
}
// SET_TRUE: ALLOW_TRAILING_COMMA
// SET_TRUE: ALLOW_TRAILING_COMMA
@@ -2,6 +2,7 @@ fun f() {
when (c) {
a -> {
}
b -> j
else -> {
}
@@ -10,6 +11,7 @@ fun f() {
when (c) {
a -> {
}
b -> j
else -> {
}
@@ -18,6 +20,7 @@ fun f() {
when (c) {
a -> {
}
b -> j
else -> {
}
@@ -28,6 +31,7 @@ fun f() {
a -> //eol comment
{
}
b -> j
else -> //eol comment
{
@@ -5,6 +5,7 @@ fun f()
a ->
{
}
b -> j
else ->
{
@@ -16,6 +17,7 @@ fun f()
a ->
{
}
b -> j
else ->
{
@@ -27,6 +29,7 @@ fun f()
a ->
{
}
b -> j
else ->
{
@@ -38,6 +41,7 @@ fun f()
a -> //eol comment
{
}
b -> j
else -> //eol comment
{
@@ -19,9 +19,11 @@ abstract class TemplateGroupBase : TemplateGroup {
when {
it.returnType.isSubtypeOf(typeMemberTemplate) ->
yield(it.call(this) as MemberTemplate)
it.returnType.isSubtypeOf(typeIterableOfMemberTemplates) ->
@Suppress("UNCHECKED_CAST")
yieldAll(it.call(this) as Iterable<MemberTemplate>)
else ->
error("Member $it violates naming convention")
}
@@ -1,6 +1,7 @@
fun foo(x: Any) = when (x) {
Comparable::class, Iterable::class, String::class, // trailing comma
-> println(1)
else -> println(3)
}
@@ -13,6 +14,7 @@ fun foo(x: Any) {
when (x) {
Comparable::class, Iterable::class,
String::class /*// trailing comma*/ -> println(1)
else -> println(3)
}
@@ -20,6 +22,7 @@ fun foo(x: Any) {
Comparable::class, Iterable::class,
String::class, /*// trailing comma*/
-> println(1)
else -> println(3)
}
@@ -32,6 +35,7 @@ fun foo(x: Any) {
1 -> {
}
else -> println(3)
}
@@ -39,6 +43,7 @@ fun foo(x: Any) {
1 -> {
}
else -> println(3)
}
@@ -47,6 +52,7 @@ fun foo(x: Any) {
-> {
}
else -> println(3)
}
@@ -55,6 +61,7 @@ fun foo(x: Any) {
3 /**/ -> {
}
else -> println(3)
}
@@ -63,6 +70,7 @@ fun foo(x: Any) {
3 /**/ -> {
}
else -> println(3)
}
@@ -71,8 +79,9 @@ fun foo(x: Any) {
-> {
}
else -> println(3)
}
}
// SET_TRUE: ALLOW_TRAILING_COMMA
// SET_TRUE: ALLOW_TRAILING_COMMA
@@ -1,6 +1,7 @@
fun foo(x: Any) = when (x) {
Comparable::class, Iterable::class, String::class, // trailing comma
-> println(1)
else -> println(3)
}
@@ -14,6 +15,7 @@ fun foo(x: Any) {
Comparable::class, Iterable::class,
String::class, /*// trailing comma*/
-> println(1)
else -> println(3)
}
@@ -21,6 +23,7 @@ fun foo(x: Any) {
Comparable::class, Iterable::class,
String::class, /*// trailing comma*/
-> println(1)
else -> println(3)
}
@@ -33,6 +36,7 @@ fun foo(x: Any) {
1 -> {
}
else -> println(3)
}
@@ -40,6 +44,7 @@ fun foo(x: Any) {
1 -> {
}
else -> println(3)
}
@@ -48,6 +53,7 @@ fun foo(x: Any) {
-> {
}
else -> println(3)
}
@@ -57,6 +63,7 @@ fun foo(x: Any) {
-> {
}
else -> println(3)
}
@@ -66,6 +73,7 @@ fun foo(x: Any) {
-> {
}
else -> println(3)
}
@@ -74,8 +82,9 @@ fun foo(x: Any) {
-> {
}
else -> println(3)
}
}
// SET_TRUE: ALLOW_TRAILING_COMMA
// SET_TRUE: ALLOW_TRAILING_COMMA
@@ -7,9 +7,10 @@ fun foo(a: Any) {
is String -> {
println(a)
}
is List<*> -> {
@Suppress("UNCHECKED_CAST")
println(a as List<String>)
}
}
}
}
@@ -11,6 +11,7 @@ suspend fun myFunction(context: CoroutineContext, switch: Int): Int {
val x = 123
x * x
}
1 -> return withContext(context) { -1 }
else -> return withContext(Dispatchers.Default) { 9 }
}
@@ -9,6 +9,7 @@ fun test(n: Int): String {
doSomething("***")
"one"
}
else -> {
doSomething("***")
"two"
@@ -5,9 +5,10 @@ fun test(i: Int) {
println(1)
1
}
else -> {
println(3)
3
}
}
}
}
@@ -4,8 +4,9 @@ fun test(i: Int) {
i == 1 -> {
println(1)
}
else -> {
println(3)
}
}
}
}
@@ -5,7 +5,8 @@ fun test(x: Int, b: Boolean) {
if (b) 1 else {
2 // comment
}
else ->
0
}
}
}
@@ -3,6 +3,7 @@ fun toInt(s: Number): Int {
is Int -> {
foo()
}
else -> {
return -1
}
@@ -8,12 +8,14 @@ fun testIf(xs: List<Any>) {
continue // do not change
}
}
is Int -> {
break
}
else -> {
println(x)
}
}
}
}
}
@@ -9,8 +9,9 @@ fun testIf(x: Any) {
break // do not change
}
}
else -> {
println(x)
}
}
}
}
@@ -20,6 +20,7 @@ private fun __dummyTestFun__(a: Int, b: Int) {
a > 0 -> {
bar(a)
}
else -> {
b
}
@@ -25,6 +25,7 @@ private fun pair(a: Int, b: Int, c: Int): Pair<Int, Int> {
a > 0 -> {
b1 += a
}
else -> {
c1 -= a
}
@@ -32,4 +33,4 @@ private fun pair(a: Int, b: Int, c: Int): Pair<Int, Int> {
println(b1)
println(c1)
return Pair(b1, c1)
}
}
@@ -17,10 +17,11 @@ private fun i(a: Int, b: Int): Int {
a > 0 -> {
b1 = b1 + 1
}
a < 0 -> {
b1 = b1 - 1
}
}
println(b1)
return b1
}
}
@@ -17,13 +17,15 @@ private fun i(a: Int, b: Int): Int {
a > 0 -> {
b1 = b1 + 1
}
a < 0 -> {
b1 = b1 - 1
}
else -> {
b1 = a
}
}
println(b1)
return b1
}
}
@@ -22,5 +22,6 @@ private fun i(o: Any) = when (o) {
is A -> {
if (o is T) o.a + o.t else o.a
}
else -> o.hashCode()
}
}
@@ -11,6 +11,7 @@ object Test {
b = 1
a = b
}
else -> {
b = -1
a = b
@@ -10,6 +10,7 @@ object SwitchDemo {
print(5)
5
}
2 -> {
print(2)
print(3)
@@ -17,21 +18,25 @@ object SwitchDemo {
print(5)
5
}
3 -> {
print(3)
print(4)
print(5)
5
}
4 -> {
print(4)
print(5)
5
}
5 -> {
print(5)
5
}
6 -> {
print(6)
print(7)
@@ -42,6 +47,7 @@ object SwitchDemo {
monthString = "December"
13
}
7 -> {
print(7)
print(8)
@@ -51,6 +57,7 @@ object SwitchDemo {
monthString = "December"
13
}
8 -> {
print(8)
print(9)
@@ -59,6 +66,7 @@ object SwitchDemo {
monthString = "December"
13
}
9 -> {
print(9)
print(10)
@@ -66,21 +74,25 @@ object SwitchDemo {
monthString = "December"
13
}
10 -> {
print(10)
print(11)
monthString = "December"
13
}
11 -> {
print(11)
monthString = "December"
13
}
12 -> {
monthString = "December"
13
}
else -> {
print(4)
print(5)
@@ -5,11 +5,13 @@ fun foo(a: Int): Int {
println(x)
1
}
2 -> {
val x = 2
println(x)
2
}
3 -> {
println(3)
}
@@ -16,6 +16,7 @@ object SwitchDemo {
print(5)
5
}
2 -> {
print(2)
print(3)
@@ -23,21 +24,25 @@ object SwitchDemo {
print(5)
5
}
3 -> {
print(3)
print(4)
print(5)
5
}
4 -> {
print(4)
print(5)
5
}
5 -> {
print(5)
5
}
6 -> {
print(6)
print(7)
@@ -48,6 +53,7 @@ object SwitchDemo {
monthString = "December"
12
}
7 -> {
print(7)
print(8)
@@ -57,6 +63,7 @@ object SwitchDemo {
monthString = "December"
12
}
8 -> {
print(8)
print(9)
@@ -65,6 +72,7 @@ object SwitchDemo {
monthString = "December"
12
}
9 -> {
print(9)
print(10)
@@ -72,21 +80,25 @@ object SwitchDemo {
monthString = "December"
12
}
10 -> {
print(10)
print(11)
monthString = "December"
12
}
11 -> {
print(11)
monthString = "December"
12
}
12 -> {
monthString = "December"
12
}
else -> {
monthString = "Invalid month"
13
@@ -6,8 +6,9 @@ fun foo() {
println("2")
return
}
3 -> break@Loop
}
println()
}
}
}
@@ -15,6 +15,7 @@ object C {
print("2")
}
}
2 -> {
val a = 2
print("2")
@@ -10,50 +10,62 @@ object SwitchDemo {
monthString = "January"
1
}
2 -> {
monthString = "February"
2
}
3 -> {
monthString = "March"
3
}
4 -> {
monthString = "April"
4
}
5 -> {
monthString = "May"
5
}
6 -> {
monthString = "June"
6
}
7 -> {
monthString = "July"
7
}
8 -> {
monthString = "August"
8
}
9 -> {
monthString = "September"
9
}
10 -> {
monthString = "October"
10
}
11 -> {
monthString = "November"
11
}
12 -> {
monthString = "December"
12
}
else -> {
monthString = "Invalid month"
13
@@ -5,6 +5,7 @@ fun foo(i: Int, j: Int) {
} else {
"2"
}
1 -> "3"
else -> "4"
}
@@ -5,6 +5,7 @@ fun foo(i: Int, j: Int): String {
"1"
} else "2"
}
1 -> "2"
else -> "3"
}
@@ -4,6 +4,7 @@ fun foo(i: Int, j: Int) {
1 -> "0, 1"
else -> "0, x"
}
1 -> "1, x"
else -> "x, x"
}
@@ -7,6 +7,7 @@ fun foo(i: Int, j: Int) {
}
"1, x"
}
1 -> "1, x"
else -> "x, x"
}
@@ -9,10 +9,12 @@ object NonDefault {
valueString = "ONE"
1
}
2 -> {
valueString = "TWO"
2
}
3 -> {
valueString = "THREE"
3
@@ -4,14 +4,17 @@ fun foo(a: Int): Int {
println("1")
1
}
2 -> {
println("2")
2
}
3 -> {
println("3")
throw RuntimeException()
}
else -> {
println("default")
0
@@ -9,21 +9,25 @@ object SwitchDemo {
print(4)
print(5)
}
2 -> {
print(2)
print(3)
print(4)
print(5)
}
3 -> {
print(3)
print(4)
print(5)
}
4 -> {
print(4)
print(5)
}
5 -> print(5)
6 -> {
print(6)
@@ -34,6 +38,7 @@ object SwitchDemo {
print(11)
monthString = "December"
}
7 -> {
print(7)
print(8)
@@ -42,6 +47,7 @@ object SwitchDemo {
print(11)
monthString = "December"
}
8 -> {
print(8)
print(9)
@@ -49,21 +55,25 @@ object SwitchDemo {
print(11)
monthString = "December"
}
9 -> {
print(9)
print(10)
print(11)
monthString = "December"
}
10 -> {
print(10)
print(11)
monthString = "December"
}
11 -> {
print(11)
monthString = "December"
}
12 -> monthString = "December"
else -> {
print(4)
@@ -79,4 +89,4 @@ object SwitchDemo {
else -> -0x9a0000
}
}
}
}
@@ -4,12 +4,14 @@ fun foo() {
val x = 1
println(x)
}
2 -> {
val x = 2
println(x)
}
3 -> {
println(3)
}
}
}
}
@@ -15,21 +15,25 @@ object SwitchDemo {
print(4)
print(5)
}
2 -> {
print(2)
print(3)
print(4)
print(5)
}
3 -> {
print(3)
print(4)
print(5)
}
4 -> {
print(4)
print(5)
}
5 -> print(5)
6 -> {
print(6)
@@ -40,6 +44,7 @@ object SwitchDemo {
print(11)
monthString = "December"
}
7 -> {
print(7)
print(8)
@@ -48,6 +53,7 @@ object SwitchDemo {
print(11)
monthString = "December"
}
8 -> {
print(8)
print(9)
@@ -55,21 +61,25 @@ object SwitchDemo {
print(11)
monthString = "December"
}
9 -> {
print(9)
print(10)
print(11)
monthString = "December"
}
10 -> {
print(10)
print(11)
monthString = "December"
}
11 -> {
print(11)
monthString = "December"
}
12 -> monthString = "December"
else -> monthString = "Invalid month"
}
@@ -80,4 +90,4 @@ object SwitchDemo {
fun main(args: Array<String>) {
for (i in 1..12) test(i)
}
}
}
@@ -6,8 +6,9 @@ fun foo() {
println("2")
return
}
3 -> break@Loop
}
println()
}
}
}
@@ -12,10 +12,11 @@ object C {
print("2")
}
}
2 -> {
val a = 2
print("2")
}
}
}
}
}
@@ -5,7 +5,8 @@ fun foo(i: Int, j: Int): String {
} else {
"2"
}
1 -> "3"
else -> "4"
}
}
}
@@ -5,7 +5,8 @@ fun foo(i: Int, j: Int): String {
"1"
} else "2"
}
1 -> "2"
else -> "3"
}
}
}
@@ -4,7 +4,8 @@ fun foo(i: Int, j: Int): String {
1 -> "0, 1"
else -> "0, x"
}
1 -> "1, x"
else -> "x, x"
}
}
}
@@ -7,7 +7,8 @@ fun foo(i: Int, j: Int): String {
}
"1, x"
}
1 -> "1, x"
else -> "x, x"
}
}
}
@@ -4,17 +4,20 @@ fun foo(a: Int): Int {
println("1")
1
}
2 -> {
println("2")
2
}
3 -> {
println("3")
throw RuntimeException()
}
else -> {
println("default")
0
}
}
}
}