Formatter: fix indent for multiline expression in string template

#KT-35359 Fixed
This commit is contained in:
Dmitry Gridin
2020-04-09 23:51:59 +07:00
parent decbe99391
commit 585177bcf7
17 changed files with 112 additions and 9 deletions
@@ -906,7 +906,17 @@ private val INDENT_RULES = arrayOf(
strategy("Indent for block content") strategy("Indent for block content")
.within(BLOCK, CLASS_BODY, FUNCTION_LITERAL) .within(BLOCK, CLASS_BODY, FUNCTION_LITERAL)
.notForType(RBRACE, LBRACE, BLOCK) .notForType(RBRACE, LBRACE, BLOCK)
.set(Indent.getNormalIndent(false)), .set(Indent.getNormalIndent()),
strategy("Indent for template content")
.within(LONG_STRING_TEMPLATE_ENTRY)
.notForType(LONG_TEMPLATE_ENTRY_START, LONG_TEMPLATE_ENTRY_END)
.set(Indent.getNormalIndent()),
strategy("No indent for braces in template")
.within(LONG_STRING_TEMPLATE_ENTRY)
.forType(LONG_TEMPLATE_ENTRY_START, LONG_TEMPLATE_ENTRY_END)
.set(Indent.getNoneIndent()),
strategy("Indent for property accessors") strategy("Indent for property accessors")
.within(PROPERTY).forType(PROPERTY_ACCESSOR) .within(PROPERTY).forType(PROPERTY_ACCESSOR)
@@ -1006,7 +1016,7 @@ private val INDENT_RULES = arrayOf(
strategy("Colon of delegation list") strategy("Colon of delegation list")
.within(CLASS, OBJECT_DECLARATION) .within(CLASS, OBJECT_DECLARATION)
.forType(COLON) .forType(COLON)
.set(Indent.getNormalIndent(false)), .set(Indent.getNormalIndent()),
strategy("Delegation list") strategy("Delegation list")
.within(SUPER_TYPE_LIST) .within(SUPER_TYPE_LIST)
@@ -240,6 +240,9 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
else else
null null
} }
inPosition(parent = LONG_STRING_TEMPLATE_ENTRY, right = LONG_TEMPLATE_ENTRY_END).lineBreakIfLineBreakInParent(0)
inPosition(parent = LONG_STRING_TEMPLATE_ENTRY, left = LONG_TEMPLATE_ENTRY_START).lineBreakIfLineBreakInParent(0)
} }
simple { simple {
@@ -396,9 +399,6 @@ fun createSpacingBuilder(settings: CodeStyleSettings, builderUtil: KotlinSpacing
betweenInside(LPAR, DESTRUCTURING_DECLARATION, FOR).spaces(0) betweenInside(LPAR, DESTRUCTURING_DECLARATION, FOR).spaces(0)
betweenInside(LOOP_RANGE, RPAR, FOR).spaces(0) betweenInside(LOOP_RANGE, RPAR, FOR).spaces(0)
after(LONG_TEMPLATE_ENTRY_START).spaces(0)
before(LONG_TEMPLATE_ENTRY_END).spaces(0)
afterInside(ANNOTATION_ENTRY, ANNOTATED_EXPRESSION).spaces(1) afterInside(ANNOTATION_ENTRY, ANNOTATED_EXPRESSION).spaces(1)
before(SEMICOLON).spaces(0) before(SEMICOLON).spaces(0)
+8
View File
@@ -0,0 +1,8 @@
fun test() {
val message1 = "${
"sd"
}"
val message2 = {
"sd"
}()
}
+8
View File
@@ -0,0 +1,8 @@
fun test() {
val message1 = "${
"sd"
}"
val message2 = {
"sd"
}()
}
@@ -0,0 +1,4 @@
fun test() {
val message1 = "${"sd"}"
val message2 = { "sd" }()
}
+4
View File
@@ -0,0 +1,4 @@
fun test() {
val message1 = "${"sd"}"
val message2 = {"sd"}()
}
@@ -0,0 +1,3 @@
val message = "${
"sd"
}"
+2
View File
@@ -0,0 +1,2 @@
val message = "${"sd"
}"
@@ -0,0 +1,5 @@
fun test() {
val message = "start ${
"sd"
} end"
}
+4
View File
@@ -0,0 +1,4 @@
fun test() {
val message = "start ${
"sd"} end"
}
@@ -0,0 +1,5 @@
fun test() {
"start ${
println("sd")
} end"
}
+4
View File
@@ -0,0 +1,4 @@
fun test() {
"start ${
println("sd")} end"
}
@@ -0,0 +1,7 @@
fun test() {
"start ${
println(
"sd"
)
} end"
}
+5
View File
@@ -0,0 +1,5 @@
fun test() {
"start ${println(
"sd"
)} end"
}
@@ -1,2 +1,4 @@
val v = "${2 + val v = "${
2}" 2 +
2
}"
@@ -1,4 +1,6 @@
fun foo() { fun foo() {
val y = "x=${"""ab val y = "x=${
c"""}" """ab
c"""
}"
} }
@@ -545,6 +545,36 @@ public class FormatterTestGenerated extends AbstractFormatterTest {
runTest("idea/testData/formatter/MultilineFunctionLiteralWithParams.after.kt"); runTest("idea/testData/formatter/MultilineFunctionLiteralWithParams.after.kt");
} }
@TestMetadata("MultilineStringEntry.after.kt")
public void testMultilineStringEntry() throws Exception {
runTest("idea/testData/formatter/MultilineStringEntry.after.kt");
}
@TestMetadata("MultilineStringEntry2.after.kt")
public void testMultilineStringEntry2() throws Exception {
runTest("idea/testData/formatter/MultilineStringEntry2.after.kt");
}
@TestMetadata("MultilineStringEntry3.after.kt")
public void testMultilineStringEntry3() throws Exception {
runTest("idea/testData/formatter/MultilineStringEntry3.after.kt");
}
@TestMetadata("MultilineStringEntry4.after.kt")
public void testMultilineStringEntry4() throws Exception {
runTest("idea/testData/formatter/MultilineStringEntry4.after.kt");
}
@TestMetadata("MultilineStringEntry5.after.kt")
public void testMultilineStringEntry5() throws Exception {
runTest("idea/testData/formatter/MultilineStringEntry5.after.kt");
}
@TestMetadata("MultilineStringEntry6.after.kt")
public void testMultilineStringEntry6() throws Exception {
runTest("idea/testData/formatter/MultilineStringEntry6.after.kt");
}
@TestMetadata("NewLineForRBrace.after.kt") @TestMetadata("NewLineForRBrace.after.kt")
public void testNewLineForRBrace() throws Exception { public void testNewLineForRBrace() throws Exception {
runTest("idea/testData/formatter/NewLineForRBrace.after.kt"); runTest("idea/testData/formatter/NewLineForRBrace.after.kt");