From 0c7b6dbf35ca2ba308a7d74f3552903667e05247 Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Sun, 3 Dec 2023 11:40:16 +0100 Subject: [PATCH] Fix offset for violation when final newline is missing Closes #2391 --- .../pinterest/ktlint/ruleset/standard/rules/FinalNewlineRule.kt | 2 +- .../ktlint/ruleset/standard/rules/FinalNewlineRuleTest.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRule.kt index a4f839efa7..f68717d1c2 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRule.kt @@ -36,7 +36,7 @@ public class FinalNewlineRule : val lastNode = lastChildNodeOf(node) if (insertFinalNewline) { if (lastNode !is PsiWhiteSpace || !lastNode.textContains('\n')) { - emit(0, "File must end with a newline (\\n)", true) + emit(node.textLength - 1, "File must end with a newline (\\n)", true) if (autoCorrect) { node.addChild(PsiWhiteSpaceImpl("\n"), null) } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRuleTest.kt index ee0e8bf9a6..08c4f980ed 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FinalNewlineRuleTest.kt @@ -32,7 +32,7 @@ class FinalNewlineRuleTest { """.trimIndent() finalNewlineRuleAssertThat(code) .withEditorConfigOverride(FINAL_NEW_LINE_REQUIRED) - .hasLintViolation(1, 1, "File must end with a newline (\\n)") + .hasLintViolation(2, 1, "File must end with a newline (\\n)") .isFormattedAs(formattedCode) }