Skip to content

Commit

Permalink
Remove code that was inserting newline after = in a chained call (#387)
Browse files Browse the repository at this point in the history
* Remove code that was inserting newline after = except for raw strings
  • Loading branch information
shashachu authored May 23, 2019
1 parent 49dd555 commit 5658473
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,41 +326,18 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (
!node.nextCodeSibling()?.elementType.let {
it == DOT_QUALIFIED_EXPRESSION ||
it == SAFE_ACCESS_EXPRESSION ||
it == BINARY_EXPRESSION ||
it == BINARY_WITH_TYPE
} ||
!node.nextSubstringContains('\n') ||
(
mustBeFollowedByNewline(node) &&
// force """ to be on a separate line
!node.nextCodeLeaf().let { it?.elementType == OPEN_QUOTE && it.text == "\"\"\"" }
)
) {
// force """ to be on a separate line
if (!node.nextCodeLeaf().isRawString()) {
return
}
val nextCodeLeaf = node.nextCodeLeaf()!!
// val v = (...
if (nextCodeLeaf.elementType in lTokenSet) {
return
}
if (!nextCodeLeaf.prevLeaf().isWhiteSpaceWithNewline()) {
requireNewlineAfterLeaf(node, autoCorrect, emit)
}
}

private fun ASTNode.nextSubstringContains(c: Char): Boolean {
var n = this.treeNext
while (n != null) {
if (n.textContains(c)) {
return true
}
n = n.treeNext
}
return false
private fun ASTNode?.isRawString(): Boolean {
return this?.elementType == OPEN_QUOTE && this.text == "\"\"\""
}

private fun mustBeFollowedByNewline(node: ASTNode): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,22 @@ class IndentationRuleTest {
assertThat(IndentationRule().format(ktScript, mapOf("indent_size" to "2")))
.isEqualTo("fun main() {\n return 0\n}")
}

@Test
fun testLintNewlineAfterEqAllowed() {
assertThat(
IndentationRule().lint(
// Previously the IndentationRule would force the line break after the `=`. Verify that it is
// still allowed.
"""
private fun getImplementationVersion() =
javaClass.`package`.implementationVersion
?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
?.let { stream ->
Manifest(stream).mainAttributes.getValue("Implementation-Version")
}
""".trimIndent()
)
).isEmpty()
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class A {
private fun getImplementationVersion() =
javaClass.`package`.implementationVersion
?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
?.let { stream ->
Manifest(stream).mainAttributes.getValue("Implementation-Version")
}
private fun getImplementationVersion() = javaClass.`package`.implementationVersion
?: javaClass.getResourceAsStream("/META-INF/MANIFEST.MF")
?.let { stream ->
Manifest(stream).mainAttributes.getValue("Implementation-Version")
}

fun f() {
x()?.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ class C {
}

fun f() {
val x =
"a" +
"b2"
val x = "a" +
"b2"
val x =
"a" +
"b2"

val x =
paths.flatMap { dir ->
"hello"
} + f0(
"there"
) + f1(
"sssss"
)
val x = paths.flatMap { dir ->
"hello"
} + f0(
"there"
) + f1(
"sssss"
)

fun Exception.toLintError(): LintError = this.let { e ->
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class C {

private val Any.className
get() =
this.javaClass.name
.fn()
get() = this.javaClass.name
.fn()

private fun String.escape() =
this.fn()
Expand Down
3 changes: 2 additions & 1 deletion ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ import picocli.CommandLine.Option
import picocli.CommandLine.Parameters

@Command(
headerHeading = """An anti-bikeshedding Kotlin linter with built-in formatter
headerHeading =
"""An anti-bikeshedding Kotlin linter with built-in formatter
(https://github.com/shyiko/ktlint).
Usage:
Expand Down

0 comments on commit 5658473

Please sign in to comment.