question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect reporting of Indentation issues for IntelliJ highlighting

See original GitHub issue

Expected 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. correct_spacing_highlighting

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. incorrect_indentation_highlighting

Steps to Reproduce

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

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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Yundincommented, Aug 8, 2021

It is a detekt-formatting’s bug. IndentationRule of ktlint is RestrictToRoot 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 mentioned IndentationRule, FilenameRule (fairly), FinalNewlineRule (detekt’s NewLineAtEndOfFile 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.

Screenshot 2021-08-08 at 11 04 19

I am working on fix.

1reaction
flapjackfritzcommented, Jun 18, 2021

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:

1 kotlin file was analyzed.
/Users/tommyfritz/workspace/linz/landonline-survey/src/main/kotlin/nz/govt/linz/landonline/survey/controller/MyFirmController.kt - 10min debt
	Indentation - [] at /Users/tommyfritz/workspace/linz/landonline-survey/src/main/kotlin/nz/govt/linz/landonline/survey/controller/MyFirmController.kt:29:34
	Indentation - [] at /Users/tommyfritz/workspace/linz/landonline-survey/src/main/kotlin/nz/govt/linz/landonline/survey/controller/MyFirmController.kt:36:9

Overall debt: 10min

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: Screen Shot 2021-06-18 at 1 14 09 PM

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Incorrect reporting of Indentation issues for IntelliJ highlighting
Intellij IDEA highlights a line (or first symbol in a line), where Indentation issue is found. Hoovering mouse over highlighted area shows ...
Read more >
Indentation | IntelliJ IDEA Documentation - JetBrains
Open indentation settings in code style scheme · Click the widget and select Configure Indents for 'Language'. · In the dialog that opens,...
Read more >
Java error highlighting showing errors that are incorrect
We have a large Java project using gradle and the error highlighting keeps getting in a bad state on Mac. If I clear...
Read more >
Disable and suppress inspections | IntelliJ IDEA Documentation
Inspections have severities according to which they highlight code problems in the editor. You can quickly disable code highlighting for an ...
Read more >
LexerEditorHighlighter$InvalidStateException
I managed to figure out what was going on. The LexerEditorHighlighter has a loop detection that checks if the lexer is progressing on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found