diff --git a/CHANGELOG.md b/CHANGELOG.md index 25d31dc939..3b3991a846 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 - ? diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/ast/package.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/ast/package.kt index 266cda3542..853b912efa 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/ast/package.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/ast/package.kt @@ -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 @@ -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 } diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt index 80e5fcf88a..d1f758c51b 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/ArgumentListWrappingRuleTest.kt @@ -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() + } }