Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ArgumentListWrappingRule: Fix false positive for method after string template #936

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) ([#859](https://github.com/pinterest/ktlint/issues/859))

### 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,53 @@ 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()
}
}