Incorrect reporting of Indentation issues for IntelliJ highlighting
See original GitHub issueExpected Behavior
Intellij IDEA highlights a line (or first symbol in a line), where Indentation issue is found. Hoovering mouse over highlighted area shows tooltip with the issue description.
Below is screenshot with correct highlighting and issue description from SpacingAroundParens rule.
Observed Behavior
Whole file content is highlighted, making it impossible to find violation (including other types of rules). Hoovering mouse over random place of file shows correct violation description, but its impossible to find its location.
Below is screenshot with the problem from testing project to reproduce the issue.
Steps to Reproduce
- Download test project: https://github.com/godiale/detekt-indentation-highlighting-issue
- Build it with
./gradlew.bat build
, which will result in following output
1 kotlin file was analyzed.
formatting - 10min debt
Indentation - [] at C:\Users\godin\Projects\detekt-indentation-highlighting-issue\src\main\kotlin\MainWithFormattingErrors.kt:4:1
SpacingAroundParens - [] at C:\Users\godin\Projects\detekt-indentation-highlighting-issue\src\main\kotlin\MainWithFormattingErrors.kt:5:20
Overall debt: 10min
- Install detekt IntelliJ plugin and configure it as below:
- Enable Detekt = Y
- Enable Formatting rules = Y
- Build upon the default configuration = N
- Enable all experimental rules = N
- Treat detekt findings as errors = N
- Configuration file = https://github.com/godiale/detekt-indentation-highlighting-issue/blob/main/config/detekt.yml
- Baseline File = <empty>
- Plugin jars = <empty>
- Open
MainWithFormattingErrors.kt
in editor
Context
The issue duplicates detekt-intellij-plugin#105, with more reproduction details.
However I think placing it to detekt project is more correct, because seems the problem is not in visualizing code smell, but rather in the way how its reported.
I built detekt itself to investigate and found that location
in FormattingRule for Indentation is created with values like TextLocation(0, 1165)
where node.psi.endOffset
is equal to index of last character in source file.
val location = Location(
SourceLocation(line, column),
TextLocation(node.startOffset, node.psi.endOffset),
root.absolutePath().toString()
)
SourceLocation
is created with correct line and column is reported as 1.
Maybe this could be fixed by customizing TextLocation
creation for Indentation
, so that only affected line is put to TextLocation
, not whole file?
Your Environment
- Version of detekt used: 1.15.0
- Version of detekt IntelliJ plugin used: 1.6.1
- Version of Gradle used (if applicable): 6.7
- Operating System and version: Windows 10 Pro
- Version of IntelliJ IDEA: Community 2020.3.1 Build #IC-203.6682.168, built on December 29, 2020
- Runtime version: 11.0.9.1+11-b1145.63 amd64
- Link to your project (if it’s a public repository): https://github.com/godiale/detekt-indentation-highlighting-issue
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:11 (6 by maintainers)
Top GitHub Comments
It is a
detekt-formatting
’s bug.IndentationRule
ofktlint
isRestrictToRoot
rule, it is working for the whole file only. So its output is treated by FormattingRule as output for the file node and location of the file is used in TextLocation. This behaviour affects above mentionedIndentationRule
,FilenameRule
(fairly),FinalNewlineRule
(detekt’sNewLineAtEndOfFile
also highlights all file, so I think it is fairly too). And not only plugin affected, html output is also incorrect, because is is always will show error til the end of the snippet.I am working on fix.
This doesn’t appear to be an issue with detekt, it seems to just be an issue with the plugin.
When I run using the detekt cli, e.g.
detekt -c default-detekt-config.yml -i ./src/main/kotlin/nz/govt/linz/landonline/survey/controller/MyFirmController.kt --plugins ../detekt-formatting-1.17.1.jar
I get result:Which is indicating the correct line numbers for this issue (line 29 and 36). So Detekt is working.
But in the plugin, it seems to fail to figure out these lines, instead saying it’s on line 1:
So I’m not sure what’s going on here. I’ve looked through the plugin code, and I can’t figure out where it’s going wrong.