Skip to content

Commit e78eca4

Browse files
committed
fix(java): add PsiErrorElement handling and collect syntax errors #129
This commit introduces the handling of PsiErrorElements and the collection of syntax errors in the JavaAutoTestService.kt file. The changes include the addition of a new import for com.intellij.codeInsight.daemon.DaemonCodeAnalyzer and the use of CommandProcessor.getInstance().runUndoTransparentAction to optimize imports at the cursor in the editor. This enhancement ensures that the auto-test service can handle syntax errors gracefully and provide a better user experience.
1 parent bbc691a commit e78eca4

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

java/src/main/kotlin/cc/unitmesh/idea/service/JavaAutoTestService.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ import cc.unitmesh.devti.context.ClassContext
44
import cc.unitmesh.devti.context.ClassContextProvider
55
import cc.unitmesh.devti.provider.AutoTestService
66
import cc.unitmesh.devti.provider.context.TestFileContext
7+
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
78
import com.intellij.execution.RunManager
89
import com.intellij.execution.configurations.RunConfiguration
910
import com.intellij.execution.configurations.RunProfile
1011
import com.intellij.lang.java.JavaLanguage
12+
import com.intellij.openapi.actionSystem.CommonDataKeys
1113
import com.intellij.openapi.application.ReadAction
1214
import com.intellij.openapi.application.runReadAction
15+
import com.intellij.openapi.command.CommandProcessor
1316
import com.intellij.openapi.command.WriteCommandAction
1417
import com.intellij.openapi.diagnostic.logger
1518
import com.intellij.openapi.fileEditor.FileDocumentManager
19+
import com.intellij.openapi.fileEditor.FileEditorManager
1620
import com.intellij.openapi.project.Project
1721
import com.intellij.openapi.project.guessProjectDir
1822
import com.intellij.openapi.roots.ProjectFileIndex
@@ -155,6 +159,14 @@ class JavaAutoTestService : AutoTestService() {
155159
document?.setText(testFileContent)
156160

157161
// OptimizeImportsFix
162+
CommandProcessor.getInstance().runUndoTransparentAction {
163+
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return@runUndoTransparentAction
164+
DaemonCodeAnalyzer.getInstance(project).autoImportReferenceAtCursor(
165+
editor,
166+
sourceFile
167+
)
168+
}
169+
158170
testFile
159171
}
160172
}

0 commit comments

Comments
 (0)