From b0ec097776f659721a1cd50628023ba38734f6ac Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Tue, 23 Apr 2024 11:51:18 +0200 Subject: [PATCH] Handle trailing space on preceding line in call to `lineLengthWithoutNewlinePrefix` --- .../rule/engine/core/api/ASTNodeExtension.kt | 2 +- .../engine/core/api/ASTNodeExtensionTest.kt | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt b/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt index f37b022576..da78cdda89 100644 --- a/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt +++ b/ktlint-rule-engine-core/src/main/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtension.kt @@ -502,7 +502,7 @@ public fun ASTNode.lineLength(excludeEolComment: Boolean = false): Int = leavesO */ public fun Sequence.lineLengthWithoutNewlinePrefix(): Int { val first = firstOrNull() ?: return 0 - require(first.text.startsWith('\n') || first.prevLeaf() == null) { + require(first.textContains('\n') || first.prevLeaf() == null) { "First node in non-empty sequence must be a whitespace containing a newline" } return joinToString(separator = "") { it.text } diff --git a/ktlint-rule-engine-core/src/test/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtensionTest.kt b/ktlint-rule-engine-core/src/test/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtensionTest.kt index 56a5e7aa11..657fa0f6ff 100644 --- a/ktlint-rule-engine-core/src/test/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtensionTest.kt +++ b/ktlint-rule-engine-core/src/test/kotlin/com/pinterest/ktlint/rule/engine/core/api/ASTNodeExtensionTest.kt @@ -18,7 +18,9 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_REFERENCE import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHITE_SPACE +import com.pinterest.ktlint.test.SPACE import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatNoException import org.assertj.core.api.Assertions.entry import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.lang.FileASTNode @@ -761,6 +763,30 @@ class ASTNodeExtensionTest { " val foo4".length, ) } + + @Suppress("DEPRECATION") + @Test + fun `xxGiven some lines containing identifiers at different indentation levels then get line length exclusive the leading newline characters until and including the identifier`() { + val code = + """ + val foo1 = "foo1"$SPACE + val foo2 = "foo2" + """.trimIndent() + + assertThatNoException() + .isThrownBy { + transformCodeToAST(code) + .firstChildLeafOrSelf() + .leaves() + .filter { it.elementType == IDENTIFIER } + .map { identifier -> + identifier + .leavesOnLine() + .takeWhile { it.prevLeaf() != identifier } + .lineLengthWithoutNewlinePrefix() + }.toList() + } + } } @Nested