Skip to content

Commit

Permalink
Stop creating in-memory KtFile when file path is provided (#7250)
Browse files Browse the repository at this point in the history
* Trigger automatic line separator detection in tests

This is required when loading KtFile using standard methods

* Stop creating in-memory KtFile when file path is provided

The new methods will create KtFile with virtualPath.path set correctly

* Reset modifiedText when autocorrecting files

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 6, 2024
1 parent 1d6b738 commit 731735b
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
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
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
Expand Up @@ -4,13 +4,13 @@ import io.github.detekt.psi.absolutePath
import org.intellij.lang.annotations.Language
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt
import org.jetbrains.kotlin.com.intellij.psi.util.PsiUtilCore
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import java.nio.file.Path
import kotlin.io.path.absolute
import kotlin.io.path.isRegularFile
import kotlin.io.path.name
import kotlin.io.path.readText

open class KtCompiler(
protected val environment: KotlinCoreEnvironment = createKotlinCoreEnvironment(printStream = System.err)
Expand All @@ -20,7 +20,11 @@ open class KtCompiler(

fun compile(path: Path): KtFile {
require(path.isRegularFile()) { "Given path '$path' should be a regular file!" }
return createKtFile(path.readText(), path)

val virtualFile = requireNotNull(environment.findLocalFile(path.toString()))
return requireNotNull(PsiUtilCore.getPsiFile(environment.project, virtualFile) as? KtFile) {
"$path is not a Kotlin file"
}
}

fun createKtFile(@Language("kotlin") content: String, path: Path): KtFile {
Expand Down
Expand Up @@ -3,7 +3,6 @@ package io.github.detekt.parser
import io.github.detekt.test.utils.resourceAsPath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.jetbrains.kotlin.com.intellij.psi.PsiErrorElement
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -52,17 +51,11 @@ class KtCompilerSpec {

@Test
fun `parses with errors for non kotlin files`() {
val cssPath = resourceAsPath("css")
val ktFile = ktCompiler.compile(cssPath.resolve("test.css"))
val cssPath = resourceAsPath("css/test.css")

val errors = mutableListOf<PsiErrorElement>()
ktFile.accept(object : KtTreeVisitorVoid() {
override fun visitErrorElement(element: PsiErrorElement) {
errors.add(element)
}
})

assertThat(errors).isNotEmpty()
assertThatIllegalArgumentException()
.isThrownBy { ktCompiler.compile(cssPath) }
.withMessage("$cssPath is not a Kotlin file")
}
}

Expand Down

0 comments on commit 731735b

Please sign in to comment.