Simplify internal parsing to KtFile's (#2875)

* Simplify parsing to KtFile's

* Do not mark KtFiles as generated
This commit is contained in:
Artur Bosch
2020-07-20 19:17:26 +02:00
committed by GitHub
parent 95ea613f2f
commit 9874528fa6
4 changed files with 28 additions and 21 deletions

View File

@@ -5,10 +5,8 @@ import io.github.detekt.psi.LINE_SEPARATOR
import io.github.detekt.psi.RELATIVE_PATH
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.PsiFileFactory
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import java.nio.file.Files
import java.nio.file.Path
@@ -16,7 +14,7 @@ open class KtCompiler(
protected val environment: KotlinCoreEnvironment = createKotlinCoreEnvironment()
) {
protected val psiFileFactory: PsiFileFactory = PsiFileFactory.getInstance(environment.project)
protected val psiFileFactory = KtPsiFactory(environment.project, markGenerated = false)
fun compile(basePath: Path, path: Path): KtFile {
require(Files.isRegularFile(path)) { "Given sub path ($path) should be a regular file!" }
@@ -33,14 +31,12 @@ open class KtCompiler(
val absolutePath = path.toAbsolutePath().normalize()
val lineSeparator = content.determineLineSeparator()
val psiFile = psiFileFactory.createFileFromText(
val psiFile = psiFileFactory.createPhysicalFile(
path.fileName.toString(),
KotlinLanguage.INSTANCE,
StringUtilRt.convertLineSeparators(content),
true, true, false,
LightVirtualFile(path.toString())
StringUtilRt.convertLineSeparators(content)
)
return (psiFile as? KtFile ?: error("kotlin file expected")).apply {
return psiFile.apply {
putUserData(LINE_SEPARATOR, lineSeparator)
putUserData(RELATIVE_PATH, relativePath.toString())
putUserData(ABSOLUTE_PATH, absolutePath.toString())

View File

@@ -3,9 +3,11 @@ package io.github.detekt.parser
import io.github.detekt.psi.LINE_SEPARATOR
import io.github.detekt.psi.RELATIVE_PATH
import io.github.detekt.test.utils.resource
import io.github.detekt.test.utils.resourceAsPath
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalArgumentException
import org.assertj.core.api.Assertions.assertThatIllegalStateException
import org.jetbrains.kotlin.com.intellij.psi.PsiErrorElement
import org.jetbrains.kotlin.psi.KtTreeVisitorVoid
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
import java.nio.file.Paths
@@ -32,10 +34,18 @@ class KtCompilerTest : Spek({
.withMessageEndingWith(") should be a regular file!")
}
it("throws an exception for a css file") {
val cssPath = Paths.get(resource("css"))
assertThatIllegalStateException()
.isThrownBy { ktCompiler.compile(cssPath, cssPath.resolve("test.css")) }
it("parses with errors for non kotlin files") {
val cssPath = resourceAsPath("css")
val ktFile = ktCompiler.compile(cssPath, cssPath.resolve("test.css"))
val errors = mutableListOf<PsiErrorElement>()
ktFile.accept(object : KtTreeVisitorVoid() {
override fun visitErrorElement(element: PsiErrorElement) {
errors.add(element)
}
})
assertThat(errors).isNotEmpty()
}
}

View File

@@ -0,0 +1,3 @@
body {
margin: 1rem;
}

View File

@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.com.intellij.openapi.util.text.StringUtilRt
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.idea.KotlinLanguage
import org.jetbrains.kotlin.psi.KtFile
import java.io.File
import java.nio.file.Path
@@ -28,12 +27,11 @@ internal object KtTestCompiler : KtCompiler() {
fun compile(path: Path) = compile(root, path)
fun compileFromContent(@Language("kotlin") content: String, filename: String = TEST_FILENAME): KtFile {
val file = psiFileFactory.createFileFromText(
val file = psiFileFactory.createPhysicalFile(
filename,
KotlinLanguage.INSTANCE,
StringUtilRt.convertLineSeparators(content)) as? KtFile
file?.putUserData(ABSOLUTE_PATH, filename)
return file ?: error("kotlin file expected")
StringUtilRt.convertLineSeparators(content))
file.putUserData(ABSOLUTE_PATH, filename)
return file
}
/**