Skip to content

Commit 928c4e4

Browse files
committed
[formatter] More workarounds for assignment
1 parent 9b4f29b commit 928c4e4

7 files changed

Lines changed: 201 additions & 5 deletions

File tree

core/src/main/java/com/facebook/ktfmt/format/KotlinInputAstVisitor.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,11 +1427,18 @@ open class KotlinInputAstVisitor(
14271427
builder.sync(expression)
14281428
val op = expression.operationToken
14291429

1430-
if (KtTokens.ALL_ASSIGNMENTS.contains(op) && expression.right.isLambdaOrScopingFunction) {
1430+
val right = expression.right
1431+
if (
1432+
KtTokens.ALL_ASSIGNMENTS.contains(op) &&
1433+
right != null &&
1434+
(!forceLineBreakAfterAssignment || right.isLambdaOrScopingFunction)
1435+
) {
14311436
// Assignments are statements in Kotlin; we don't have to worry about compound assignment.
14321437
visit(expression.left)
14331438
builder.spaceThenToken(expression.operationReference.text)
1434-
visitLambdaOrScopingFunction(expression.right)
1439+
// The level keeps the break after the operator out of reach of any forced break the
1440+
// left-hand side brought with it -- an annotation before the statement, say.
1441+
builder.block(ZERO) { emitAssignedExpression(right) }
14351442
return
14361443
}
14371444

@@ -1737,15 +1744,23 @@ open class KotlinInputAstVisitor(
17371744
*/
17381745
private fun emitInitializer(initializer: KtExpression) {
17391746
builder.spaceThenToken("=")
1740-
if (emitExpressionAfterOperator(initializer)) {
1747+
emitAssignedExpression(initializer)
1748+
}
1749+
1750+
/**
1751+
* Lays out the right-hand side of an assignment operator -- the `=` of an initializer or of an
1752+
* assignment statement -- according to the kind of expression it is.
1753+
*/
1754+
private fun emitAssignedExpression(expression: KtExpression) {
1755+
if (emitExpressionAfterOperator(expression)) {
17411756
return
17421757
}
17431758
// A chain gets to keep its receiver on the `=` line when it fits there; everything else
17441759
// breaks after the `=` and is laid out one level in.
1745-
if (!emitChainAfterOperator(initializer)) {
1760+
if (!emitChainAfterOperator(expression)) {
17461761
builder.breakOpThenBlock(" ", expressionBreakIndent) {
17471762
builder.fenceComments()
1748-
visit(initializer)
1763+
visit(expression)
17491764
}
17501765
}
17511766
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
fun singleCallChain() {
2+
reports {
3+
total {
4+
html {
5+
report {
6+
htmlDir = conventionProject.layout.buildDirectory.dir("kover/${project.name}/html")
7+
}
8+
}
9+
}
10+
}
11+
}
12+
13+
fun splittableChain() {
14+
reports {
15+
total {
16+
html {
17+
htmlDir = conventionProject.layout.buildDirectory.dir("kover/html").asFile.absolutePath
18+
}
19+
}
20+
}
21+
}
22+
23+
fun annotatedAssignment() {
24+
var b
25+
@Suppress("UNCHECKED_CAST") b = f(1) as Int
26+
@Suppress("UNCHECKED_CAST")
27+
b = f(1) as Int
28+
}
29+
30+
fun whenAssignment() {
31+
reports {
32+
total {
33+
htmlDirectoryValue = when (someLongConditionValue) {
34+
1 -> firstValue
35+
else -> secondValue
36+
}
37+
}
38+
}
39+
}
40+
41+
fun scopingFunctionAssignment() {
42+
reports {
43+
total {
44+
htmlDirectoryValue = conventionProjectValue.apply { someLongName = otherLongName }
45+
}
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
fun singleCallChain() {
2+
reports {
3+
total {
4+
html {
5+
report {
6+
htmlDir =
7+
conventionProject.layout.buildDirectory.dir("kover/${project.name}/html")
8+
}
9+
}
10+
}
11+
}
12+
}
13+
14+
fun splittableChain() {
15+
reports {
16+
total {
17+
html {
18+
htmlDir = conventionProject.layout.buildDirectory
19+
.dir("kover/html")
20+
.asFile
21+
.absolutePath
22+
}
23+
}
24+
}
25+
}
26+
27+
fun annotatedAssignment() {
28+
var b
29+
@Suppress("UNCHECKED_CAST") b = f(1) as Int
30+
@Suppress("UNCHECKED_CAST")
31+
b = f(1) as Int
32+
}
33+
34+
fun whenAssignment() {
35+
reports {
36+
total {
37+
htmlDirectoryValue = when (someLongConditionValue) {
38+
1 -> firstValue
39+
else -> secondValue
40+
}
41+
}
42+
}
43+
}
44+
45+
fun scopingFunctionAssignment() {
46+
reports {
47+
total {
48+
htmlDirectoryValue = conventionProjectValue.apply { someLongName = otherLongName }
49+
}
50+
}
51+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
fun singleCallChain() {
2+
reports {
3+
total {
4+
html {
5+
report {
6+
htmlDir = conventionProject.layout.buildDirectory.dir("kover/${project.name}/html")
7+
}
8+
}
9+
}
10+
}
11+
}
12+
13+
fun splittableChain() {
14+
reports {
15+
total {
16+
html {
17+
htmlDir = conventionProject.layout.buildDirectory.dir("kover/html").asFile.absolutePath
18+
}
19+
}
20+
}
21+
}
22+
23+
fun annotatedAssignment() {
24+
var b
25+
@Suppress("UNCHECKED_CAST") b = f(1) as Int
26+
@Suppress("UNCHECKED_CAST")
27+
b = f(1) as Int
28+
}
29+
30+
fun whenAssignment() {
31+
reports {
32+
total {
33+
htmlDirectoryValue =
34+
when (someLongConditionValue) {
35+
1 -> firstValue
36+
else -> secondValue
37+
}
38+
}
39+
}
40+
}
41+
42+
fun scopingFunctionAssignment() {
43+
reports {
44+
total {
45+
htmlDirectoryValue = conventionProjectValue.apply { someLongName = otherLongName }
46+
}
47+
}
48+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fun foo() {
2+
extensions.configure<KoverProjectExtension>("kover") {
3+
reports {
4+
total {
5+
html {
6+
htmlDir =
7+
conventionProject.layout.buildDirectory.dir("kover/${project.name}/html")
8+
}
9+
}
10+
}
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fun foo() {
2+
extensions.configure<KoverProjectExtension>("kover") {
3+
reports {
4+
total {
5+
html {
6+
htmlDir =
7+
conventionProject.layout.buildDirectory.dir("kover/${project.name}/html")
8+
}
9+
}
10+
}
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fun foo() {
2+
extensions.configure<KoverProjectExtension>("kover") {
3+
reports {
4+
total {
5+
html {
6+
htmlDir = conventionProject.layout.buildDirectory.dir("kover/${project.name}/html")
7+
}
8+
}
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)