Skip to content

Commit 6ce29ba

Browse files
committed
refactor(lint): simplify error collection and add inspections #288
- Replace `collectSyntaxError` with `runInspections` for better error handling. - Remove unnecessary parameters and streamline error collection logic. - Add `showErrors` method to display lint issues in the UI.
1 parent a460231 commit 6ce29ba

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

core/src/main/kotlin/cc/unitmesh/devti/sketch/lint/PsiErrorCollector.kt

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
package cc.unitmesh.devti.sketch.lint
22

3+
import com.intellij.analysis.AnalysisScope
34
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
45
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx
5-
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerImpl
6+
import com.intellij.codeInspection.InspectionManager
7+
import com.intellij.codeInspection.ex.GlobalInspectionContextBase
68
import com.intellij.lang.annotation.HighlightSeverity
79
import com.intellij.openapi.Disposable
810
import com.intellij.openapi.application.runReadAction
911
import com.intellij.openapi.editor.Document
1012
import com.intellij.openapi.fileEditor.FileDocumentManager
11-
import com.intellij.openapi.fileEditor.FileEditorManager
1213
import com.intellij.openapi.project.Project
1314
import com.intellij.openapi.util.Disposer
1415
import com.intellij.openapi.util.TextRange
1516
import com.intellij.openapi.vfs.VirtualFile
16-
import com.intellij.psi.PsiDocumentManager
1717
import com.intellij.psi.PsiElement
1818
import com.intellij.psi.PsiErrorElement
1919
import com.intellij.psi.PsiFile
@@ -22,16 +22,12 @@ import java.util.concurrent.CompletableFuture
2222
import java.util.concurrent.TimeUnit
2323

2424
object PsiErrorCollector {
25-
fun collectSyntaxError(
26-
psiFile: PsiFile,
27-
project: Project,
28-
): List<String> {
29-
var errors: List<String> = listOf()
30-
collectSyntaxError(psiFile, psiFile.virtualFile, project) {
31-
errors = it
32-
}
25+
fun runInspections(project: Project, psiFile: PsiFile) {
26+
val scope = AnalysisScope(psiFile)
27+
val globalContext = InspectionManager.getInstance(project).createNewGlobalContext() as? GlobalInspectionContextBase
3328

34-
return errors
29+
globalContext?.currentScope = scope
30+
globalContext?.doInspections(scope)
3531
}
3632

3733
/**
@@ -40,12 +36,10 @@ object PsiErrorCollector {
4036
*
4137
* @param sourceFile The PSI file from which syntax errors need to be collected.
4238
* @param runAction A callback function that takes a list of errors as input and performs some action.
43-
* @param outputFile The virtual file where the errors will be collected.
4439
* @param project The project to which the files belong.
4540
*/
4641
fun collectSyntaxError(
4742
sourceFile: PsiFile,
48-
outputFile: VirtualFile,
4943
project: Project,
5044
runAction: ((errors: List<String>) -> Unit)?,
5145
) {
@@ -54,15 +48,11 @@ object PsiErrorCollector {
5448
return runAction?.invoke(collectPsiError) ?: Unit
5549
}
5650

57-
val document = runReadAction { FileDocumentManager.getInstance().getDocument(outputFile) } ?: return
51+
val document = runReadAction { FileDocumentManager.getInstance().getDocument(sourceFile.virtualFile) } ?: return
5852

5953
val range = TextRange(0, document.textLength)
6054
val errors = mutableListOf<String>()
6155

62-
runReadAction {
63-
DaemonCodeAnalyzerEx.getInstanceEx(project).restart(sourceFile)
64-
}
65-
6656
val hintDisposable = Disposer.newDisposable()
6757
val busConnection: MessageBusConnection = project.messageBus.connect(hintDisposable)
6858
val future: CompletableFuture<List<String>> = CompletableFuture()
@@ -72,6 +62,10 @@ object PsiErrorCollector {
7262
}
7363
)
7464

65+
runReadAction {
66+
DaemonCodeAnalyzerEx.getInstanceEx(project).restart(sourceFile)
67+
}
68+
7569
future.get(30, TimeUnit.SECONDS)
7670
}
7771

core/src/main/kotlin/cc/unitmesh/devti/sketch/ui/patch/SingleFileDiffSketch.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ class SingleFileDiffSketch(
133133
mainPanel.add(myHeaderPanel)
134134
mainPanel.add(contentPanel)
135135

136-
// runin thread
137136
ApplicationManager.getApplication().executeOnPooledThread {
138137
lintCheckForNewCode()
139138
}
@@ -228,11 +227,14 @@ class SingleFileDiffSketch(
228227

229228
val newFile = LightVirtualFile(currentFile.name, newCode)
230229
val psiFile = runReadAction { PsiManager.getInstance(myProject).findFile(newFile) } ?: return
231-
val errors = PsiErrorCollector.collectSyntaxError(psiFile, myProject)
232-
233-
if (errors.isEmpty()) return
230+
PsiErrorCollector.collectSyntaxError(psiFile, myProject) { errors ->
231+
PsiErrorCollector.runInspections(myProject, psiFile)
232+
if (errors.isEmpty()) return@collectSyntaxError
233+
showErrors(errors)
234+
}
235+
}
234236

235-
// show error size and hover to show error message
237+
private fun showErrors(errors: List<String>) {
236238
val errorPanel = JPanel(VerticalLayout(5)).apply {
237239
val errorLabel = JBLabel("Found Lint issue: ${errors.size}").apply {
238240
border = BorderFactory.createEmptyBorder(5, 5, 5, 5)

0 commit comments

Comments
 (0)