detekt / detekt

Static code analysis for Kotlin
https://detekt.dev
Apache License 2.0
6.07k stars 751 forks source link

Make detekt-formatting autocorrect work with read-only KtFile #7206

Closed 3flex closed 3 weeks ago

3flex commented 4 weeks ago

Breaking change - instead of changing the KtFile contents directly, a rule implementing autocorrect must create a copy of the file text (or the already modified text, in case another rule has applied autocorrect already, so those changes are not overwritten), modify it then save the updated text to KtFile.modifiedText.

They will also need to take a copy of the original file path, since the copy created by createPhysicalFile only allows saving the file name but not the full path.

This is the only way to make autocorrect work with a read only KtFile - by reading and storing separate copies of the file's text.

Why do this? Because it allows for some simplification and refactoring of the detekt core and easier handling of file paths (for the majority of rules that don't implement autocorrect).

This has been tested against the new & updated tests in #7204.

codecov[bot] commented 4 weeks ago

Codecov Report

Attention: Patch coverage is 93.33333% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 84.71%. Comparing base (19b1784) to head (e721278).

Files Patch % Lines
...io/gitlab/arturbosch/detekt/core/KtFileModifier.kt 80.00% 0 Missing and 1 partial :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #7206 +/- ## ========================================= Coverage 84.71% 84.71% - Complexity 3984 3986 +2 ========================================= Files 579 580 +1 Lines 12153 12162 +9 Branches 2494 2496 +2 ========================================= + Hits 10295 10303 +8 Misses 625 625 - Partials 1233 1234 +1 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

schalkms commented 3 weeks ago

This would also help with #2560. Now we will have the initial state of the file and the after so create a patch should be really easy.

The patch should help to implement autocorrect for the compiler plugin.

I had the same thought.