Repository: cqfn/diKTat
Pull request #1024 (2021-08-03 09:55:42)

Incorrect pointing of warning in WRONG_INDENTATION Rule
### What's done:
* Fix pointing of warning in WRONG_INDENTATION Rule
* Add tests

codecov[bot]: # [Codecov](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) Report
> Merging [#1024](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) (78d7c2f) into [master](https://codecov.io/gh/cqfn/diKTat/commit/88f5d644549845e1d8c001d0a7c06210a3c29249?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) (88f5d64) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head 78d7c2f differs from pull request most recent head 7186c4a. Consider uploading reports for the commit 7186c4a to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/cqfn/diKTat/pull/1024/graphs/tree.svg?width=650&height=150&src=pr&token=P2quRDffpt&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)

```diff
@@             Coverage Diff              @@
##             master    #1024      +/-   ##
============================================
+ Coverage     83.77%   83.80%   +0.03%     
- Complexity     2419     2424       +5     
============================================
  Files           102      102              
  Lines          6107     6108       +1     
  Branches       1808     1809       +1     
============================================
+ Hits           5116     5119       +3     
  Misses          273      273              
+ Partials        718      716       -2     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `83.80% <100.00%> (+0.03%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn) | Coverage Δ | |
|---|---|---|
| [...at/ruleset/rules/chapter3/files/IndentationRule.kt](https://codecov.io/gh/cqfn/diKTat/pull/1024/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn#diff-ZGlrdGF0LXJ1bGVzL3NyYy9tYWluL2tvdGxpbi9vcmcvY3Fmbi9kaWt0YXQvcnVsZXNldC9ydWxlcy9jaGFwdGVyMy9maWxlcy9JbmRlbnRhdGlvblJ1bGUua3Q=) | `87.41% <100.00%> (+1.49%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn). Last update [88f5d64...7186c4a](https://codecov.io/gh/cqfn/diKTat/pull/1024?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=cqfn).


Patch:
diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt
index 5d976b27..42f7e6b8 100644
--- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt
+++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt
@@ -138,7 +138,12 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
             if (lastChild.elementType != WHITE_SPACE || numBlankLinesAfter != 1) {
                 val warnText = if (lastChild.elementType != WHITE_SPACE || numBlankLinesAfter == 0) "no newline" else "too many blank lines"
                 val fileName = filePath.substringAfterLast(File.separator)
-                WRONG_INDENTATION.warnAndFix(configRules, emitWarn, isFixMode, "$warnText at the end of file $fileName", node.startOffset + node.textLength, node) {
+                // In case, when last child is newline, visually user will see blank line at the end of file,
+                // however, the text length does not consider it, since it's blank and line appeared only because of `\n`
+                // But ktlint synthetically increase length in aim to have ability to point to this line, so in this case
+                // offset will be `node.textLength`, otherwise we will point to the last symbol, i.e `node.textLength - 1`
+                val offset = if (lastChild.elementType == WHITE_SPACE && lastChild.textContains('\n')) node.textLength else node.textLength - 1
+                WRONG_INDENTATION.warnAndFix(configRules, emitWarn, isFixMode, "$warnText at the end of file $fileName", offset, node) {
                     if (lastChild.elementType != WHITE_SPACE) {
                         node.addChild(PsiWhiteSpaceImpl("\n"), null)
                     } else {


Test patch:
diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt
index 2d1690d0..adb26a80 100644
--- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt
+++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt
@@ -65,6 +65,47 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) {
         )
     }
 
+    @Test
+    @Tag(WarningNames.WRONG_INDENTATION)
+    fun `should warn if no new line at the end of file`() {
+        lintMethod(
+            """
+                    |class Example {
+                    |    val zero = 0
+                    |}
+                """.trimMargin(),
+            LintError(3, 1, ruleId, "${WRONG_INDENTATION.warnText()} no newline at the end of file TestFileName.kt", true)
+        )
+    }
+
+    @Test
+    @Tag(WarningNames.WRONG_INDENTATION)
+    fun `should warn if no new line at the end of file, last child whitespace`() {
+        lintMethod(
+            """
+                    |class Example {
+                    |    val zero = 0
+                    |} 
+                """.trimMargin(),
+            LintError(3, 2, ruleId, "${WRONG_INDENTATION.warnText()} no newline at the end of file TestFileName.kt", true)
+        )
+    }
+
+    @Test
+    @Tag(WarningNames.WRONG_INDENTATION)
+    fun `should warn if too many blank lines at the end of file`() {
+        lintMethod(
+            """
+                    |class Example {
+                    |    val zero = 0
+                    |}
+                    |
+                    |
+                """.trimMargin(),
+            LintError(5, 1, ruleId, "${WRONG_INDENTATION.warnText()} too many blank lines at the end of file TestFileName.kt", true)
+        )
+    }
+
     @Test
     @Tag(WarningNames.WRONG_INDENTATION)
     fun `valid indentation - example 1`() {