Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop creating in-memory KtFile when file path is provided #7250

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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