Skip to content

Commit

Permalink
fix(kotlin-provider): add PsiErrorElement handling and collect syntax…
Browse files Browse the repository at this point in the history
… errors

This commit fixes an issue where PsiErrorElements were not being handled properly, leading to incorrect behavior when parsing Kotlin files. The commit adds a new method to the RunService interface, `PsiFile.collectPsiError()`, which collects all syntax errors from a PsiErrorElement. This method is used to ensure that only valid Kotlin files are tested. Additionally, the `JavaAutoTestService` now checks for syntax errors before creating a configuration, ensuring that only valid Java files are tested.
  • Loading branch information
phodal committed Apr 2, 2024
1 parent 2b912f3 commit 1a67593
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Expand Up @@ -30,6 +30,12 @@ class JavaAutoTestService : AutoTestService() {
override fun isApplicable(element: PsiElement): Boolean = element.language is JavaLanguage

override fun createConfiguration(project: Project, virtualFile: VirtualFile): RunConfiguration? {
val psiFile = PsiManager.getInstance(project).findFile(virtualFile) as? PsiJavaFile ?: return null

if(psiFile.collectPsiError().isNotEmpty()) {
return null
}

return createConfigForGradle(virtualFile, project)
}

Expand Down Expand Up @@ -148,6 +154,7 @@ class JavaAutoTestService : AutoTestService() {
val document = FileDocumentManager.getInstance().getDocument(testFile)
document?.setText(testFileContent)

// OptimizeImportsFix
testFile
}
}
Expand Down
Expand Up @@ -219,6 +219,9 @@ class KotlinAutoTestService : AutoTestService() {
val document = FileDocumentManager.getInstance().getDocument(testFile)
document?.setText(testFileContent)

// TODO: fix import
// org.jetbrains.kotlin.idea.quickfix.ImportFix

testFile
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/cc/unitmesh/devti/provider/RunService.kt
Expand Up @@ -6,12 +6,15 @@ import com.intellij.execution.RunnerAndConfigurationSettings
import com.intellij.execution.actions.ConfigurationContext
import com.intellij.execution.configurations.RunConfiguration
import com.intellij.execution.configurations.RunProfile
import com.intellij.openapi.application.runReadAction
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProgressManager
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiErrorElement
import com.intellij.psi.PsiFile

interface RunService {
private val logger: Logger get() = logger<RunService>()
Expand Down Expand Up @@ -88,6 +91,29 @@ interface RunService {
return settings
}

fun PsiFile.collectPsiError(): MutableList<String> {
val errors = mutableListOf<String>()
val visitor = object : JavaSyntaxCheckingVisitor() {
override fun visitElement(element: PsiElement) {
if (element is PsiErrorElement) {
errors.add("Syntax error at position ${element.textRange.startOffset}: ${element.errorDescription}")
}
super.visitElement(element)
}
}

this.accept(visitor)
return errors
}

abstract class JavaSyntaxCheckingVisitor : com.intellij.psi.PsiElementVisitor() {
override fun visitElement(element: PsiElement) {
runReadAction {
element.children.forEach { it.accept(this) }
}
}
}

private fun createDefaultTestConfigurations(project: Project, element: PsiElement): RunnerAndConfigurationSettings? {
return ConfigurationContext(element).configurationsFromContext?.firstOrNull()?.configurationSettings
}
Expand Down

0 comments on commit 1a67593

Please sign in to comment.