Skip to content

Commit

Permalink
Reset modifiedText when autocorrecting files
Browse files Browse the repository at this point in the history
The same instance of a KtFile will be reused in some circumstances. To
avoid issues the modifiedText must be reset.
  • Loading branch information
3flex committed May 3, 2024
1 parent 8489544 commit d9ee9ff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ class KtFileModifier : FileProcessListener {

override fun onFinish(files: List<KtFile>, result: Detektion) {
files.filter { it.modifiedText != null }
.map { it.absolutePath() to it.unnormalizeContent() }
.forEach { (path, content) ->
.forEach { ktFile ->
val path = ktFile.absolutePath()
result.add(ModificationNotification(path))
path.writeText(content)
path.writeText(ktFile.unnormalizeContent())
// reset modification text after writing as the PsiFile may be reused in tests or an IDE session
ktFile.modifiedText = null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ private fun createConfig(ruleSet: AutoCorrectConfig, rule: AutoCorrectConfig): S

private fun runRule(config: Config): Pair<KtFile, List<Finding>> {
val testFile = loadFile("configTests/fixed.kt")
// reset modification text, otherwise it will be persisted between tests
testFile.modifiedText = null

val ruleSet = loadRuleSet<FormattingProvider>()
val rules = ruleSet.rules
.map { (ruleId, provider) -> provider(config.subConfig(ruleSet.id.value).subConfig(ruleId.value)) }
Expand Down

0 comments on commit d9ee9ff

Please sign in to comment.