1
1
package cc.unitmesh.devti.sketch.lint
2
2
3
+ import com.intellij.analysis.AnalysisScope
3
4
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
4
5
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
6
8
import com.intellij.lang.annotation.HighlightSeverity
7
9
import com.intellij.openapi.Disposable
8
10
import com.intellij.openapi.application.runReadAction
9
11
import com.intellij.openapi.editor.Document
10
12
import com.intellij.openapi.fileEditor.FileDocumentManager
11
- import com.intellij.openapi.fileEditor.FileEditorManager
12
13
import com.intellij.openapi.project.Project
13
14
import com.intellij.openapi.util.Disposer
14
15
import com.intellij.openapi.util.TextRange
15
16
import com.intellij.openapi.vfs.VirtualFile
16
- import com.intellij.psi.PsiDocumentManager
17
17
import com.intellij.psi.PsiElement
18
18
import com.intellij.psi.PsiErrorElement
19
19
import com.intellij.psi.PsiFile
@@ -22,16 +22,12 @@ import java.util.concurrent.CompletableFuture
22
22
import java.util.concurrent.TimeUnit
23
23
24
24
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
33
28
34
- return errors
29
+ globalContext?.currentScope = scope
30
+ globalContext?.doInspections(scope)
35
31
}
36
32
37
33
/* *
@@ -40,12 +36,10 @@ object PsiErrorCollector {
40
36
*
41
37
* @param sourceFile The PSI file from which syntax errors need to be collected.
42
38
* @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.
44
39
* @param project The project to which the files belong.
45
40
*/
46
41
fun collectSyntaxError (
47
42
sourceFile : PsiFile ,
48
- outputFile : VirtualFile ,
49
43
project : Project ,
50
44
runAction : ((errors: List <String >) -> Unit )? ,
51
45
) {
@@ -54,15 +48,11 @@ object PsiErrorCollector {
54
48
return runAction?.invoke(collectPsiError) ? : Unit
55
49
}
56
50
57
- val document = runReadAction { FileDocumentManager .getInstance().getDocument(outputFile ) } ? : return
51
+ val document = runReadAction { FileDocumentManager .getInstance().getDocument(sourceFile.virtualFile ) } ? : return
58
52
59
53
val range = TextRange (0 , document.textLength)
60
54
val errors = mutableListOf<String >()
61
55
62
- runReadAction {
63
- DaemonCodeAnalyzerEx .getInstanceEx(project).restart(sourceFile)
64
- }
65
-
66
56
val hintDisposable = Disposer .newDisposable()
67
57
val busConnection: MessageBusConnection = project.messageBus.connect(hintDisposable)
68
58
val future: CompletableFuture <List <String >> = CompletableFuture ()
@@ -72,6 +62,10 @@ object PsiErrorCollector {
72
62
}
73
63
)
74
64
65
+ runReadAction {
66
+ DaemonCodeAnalyzerEx .getInstanceEx(project).restart(sourceFile)
67
+ }
68
+
75
69
future.get(30 , TimeUnit .SECONDS )
76
70
}
77
71
0 commit comments