Skip to content

Commit 5658473

Browse files
authored
Remove code that was inserting newline after = in a chained call (#387)
* Remove code that was inserting newline after = except for raw strings
1 parent 49dd555 commit 5658473

File tree

6 files changed

+40
-48
lines changed

6 files changed

+40
-48
lines changed

ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRule.kt

+4-27
Original file line numberDiff line numberDiff line change
@@ -326,41 +326,18 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
326326
autoCorrect: Boolean,
327327
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
328328
) {
329-
if (
330-
!node.nextCodeSibling()?.elementType.let {
331-
it == DOT_QUALIFIED_EXPRESSION ||
332-
it == SAFE_ACCESS_EXPRESSION ||
333-
it == BINARY_EXPRESSION ||
334-
it == BINARY_WITH_TYPE
335-
} ||
336-
!node.nextSubstringContains('\n') ||
337-
(
338-
mustBeFollowedByNewline(node) &&
339-
// force """ to be on a separate line
340-
!node.nextCodeLeaf().let { it?.elementType == OPEN_QUOTE && it.text == "\"\"\"" }
341-
)
342-
) {
329+
// force """ to be on a separate line
330+
if (!node.nextCodeLeaf().isRawString()) {
343331
return
344332
}
345333
val nextCodeLeaf = node.nextCodeLeaf()!!
346-
// val v = (...
347-
if (nextCodeLeaf.elementType in lTokenSet) {
348-
return
349-
}
350334
if (!nextCodeLeaf.prevLeaf().isWhiteSpaceWithNewline()) {
351335
requireNewlineAfterLeaf(node, autoCorrect, emit)
352336
}
353337
}
354338

355-
private fun ASTNode.nextSubstringContains(c: Char): Boolean {
356-
var n = this.treeNext
357-
while (n != null) {
358-
if (n.textContains(c)) {
359-
return true
360-
}
361-
n = n.treeNext
362-
}
363-
return false
339+
private fun ASTNode?.isRawString(): Boolean {
340+
return this?.elementType == OPEN_QUOTE && this.text == "\"\"\""
364341
}
365342

366343
private fun mustBeFollowedByNewline(node: ASTNode): Boolean {

ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/IndentationRuleTest.kt

+18
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,22 @@ class IndentationRuleTest {
304304
assertThat(IndentationRule().format(ktScript, mapOf("indent_size" to "2")))
305305
.isEqualTo("fun main() {\n return 0\n}")
306306
}
307+
308+
@Test
309+
fun testLintNewlineAfterEqAllowed() {
310+
assertThat(
311+
IndentationRule().lint(
312+
// Previously the IndentationRule would force the line break after the `=`. Verify that it is
313+
// still allowed.
314+
"""
315+
private fun getImplementationVersion() =
316+
javaClass.`package`.implementationVersion
317+
?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
318+
?.let { stream ->
319+
Manifest(stream).mainAttributes.getValue("Implementation-Version")
320+
}
321+
""".trimIndent()
322+
)
323+
).isEmpty()
324+
}
307325
}

ktlint-ruleset-experimental/src/test/resources/spec/indent/format-dot-qualified-expression-expected.kt.spec

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class A {
2-
private fun getImplementationVersion() =
3-
javaClass.`package`.implementationVersion
4-
?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
5-
?.let { stream ->
6-
Manifest(stream).mainAttributes.getValue("Implementation-Version")
7-
}
2+
private fun getImplementationVersion() = javaClass.`package`.implementationVersion
3+
?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
4+
?.let { stream ->
5+
Manifest(stream).mainAttributes.getValue("Implementation-Version")
6+
}
87

98
fun f() {
109
x()?.apply {

ktlint-ruleset-experimental/src/test/resources/spec/indent/format-eq-expected.kt.spec

+9-11
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ class C {
55
}
66

77
fun f() {
8-
val x =
9-
"a" +
10-
"b2"
8+
val x = "a" +
9+
"b2"
1110
val x =
1211
"a" +
1312
"b2"
1413

15-
val x =
16-
paths.flatMap { dir ->
17-
"hello"
18-
} + f0(
19-
"there"
20-
) + f1(
21-
"sssss"
22-
)
14+
val x = paths.flatMap { dir ->
15+
"hello"
16+
} + f0(
17+
"there"
18+
) + f1(
19+
"sssss"
20+
)
2321

2422
fun Exception.toLintError(): LintError = this.let { e ->
2523
//

ktlint-ruleset-experimental/src/test/resources/spec/indent/format-property-accessor-expected.kt.spec

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
class C {
22

33
private val Any.className
4-
get() =
5-
this.javaClass.name
6-
.fn()
4+
get() = this.javaClass.name
5+
.fn()
76

87
private fun String.escape() =
98
this.fn()

ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ import picocli.CommandLine.Option
5353
import picocli.CommandLine.Parameters
5454

5555
@Command(
56-
headerHeading = """An anti-bikeshedding Kotlin linter with built-in formatter
56+
headerHeading =
57+
"""An anti-bikeshedding Kotlin linter with built-in formatter
5758
(https://github.com/shyiko/ktlint).
5859
5960
Usage:

0 commit comments

Comments
 (0)