Skip to content

Commit

Permalink
Fix false positive for method after string template in argument-list-…
Browse files Browse the repository at this point in the history
…wrapping
  • Loading branch information
romtsn committed Oct 1, 2020
1 parent bde5101 commit 8d4dccd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Allow an inline block comment in `argument-list-wrapping` ([#926](https://github.com/pinterest/ktlint/issues/926))
- Fix false positive for line-breaks inside lambdas in `argument-list-wrapping` ([#861](https://github.com/pinterest/ktlint/issues/861)) ([#870](https://github.com/pinterest/ktlint/issues/870))
- Fix wrong indentation inside an if-condition in `argument-list-wrapping` ([#854](https://github.com/pinterest/ktlint/issues/854)) ([#864](https://github.com/pinterest/ktlint/issues/864))
- Fix false positive for method after string template in `argument-list-wrapping` ([#842](https://github.com/pinterest/ktlint/issues/842))

### Changed
- ?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pinterest.ktlint.core.ast

import com.pinterest.ktlint.core.ast.ElementType.REGULAR_STRING_PART
import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import kotlin.reflect.KClass
Expand Down Expand Up @@ -239,7 +240,7 @@ val ASTNode.column: Int
var leaf = this.prevLeaf()
var offsetToTheLeft = 0
while (leaf != null) {
if (leaf.elementType == WHITE_SPACE && leaf.textContains('\n')) {
if ((leaf.elementType == WHITE_SPACE || leaf.elementType == REGULAR_STRING_PART) && leaf.textContains('\n')) {
offsetToTheLeft += leaf.textLength - 1 - leaf.text.lastIndexOf('\n')
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,52 @@ class ArgumentListWrappingRuleTest {
)
).isEmpty()
}

@Test
fun `test lint column is correctly calculated for string templates`() {
assertThat(
ArgumentListWrappingRule().lint(
"""
class Test {
fun someMethod(str: String) = Unit
val someString = someMethod(
""${'"'}
longtext longtext longtext longtext longtext longtext longtext longtext
longtext longtext longtext longtext longtext longtext longtext longtext
longtext longtext longtext longtext longtext longtext longtext longtext
longtext longtext longtext longtext longtext longtext longtext longtext
longtext longtext longtext longtext longtext longtext longtext longtext
""${'"'}.trimIndent('|')
)
val someString2 = someMethod(""${'"'}stuff""${'"'})
}
fun foo() {
function("arg1", "arg2") {
""${'"'}
WORDS
WORDS c.property = ${"interpolation".length}
${'$'}{FUNCTION_2(TestClassA::startDateTime, descending = true)}
""${'"'}
}
}
fun bar() {
json(
""${'"'}
{
"array": [
${'$'}{function(arg1, arg2, arg3)}
]
}
""${'"'}.trimIndent()
)
}
""".trimIndent(), userData = mapOf("max_line_length" to "100")
)
).isEmpty()
}
}

0 comments on commit 8d4dccd

Please # to comment.