@@ -3,8 +3,13 @@ package cc.unitmesh.devti.sketch.lint
3
3
import com.intellij.analysis.AnalysisScope
4
4
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer
5
5
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx
6
+ import com.intellij.codeInsight.daemon.impl.DaemonProgressIndicator
7
+ import com.intellij.codeInspection.InspectionEngine
6
8
import com.intellij.codeInspection.InspectionManager
9
+ import com.intellij.codeInspection.ProblemDescriptor
7
10
import com.intellij.codeInspection.ex.GlobalInspectionContextBase
11
+ import com.intellij.codeInspection.ex.LocalInspectionToolWrapper
12
+ import com.intellij.codeInspection.util.InspectionMessage
8
13
import com.intellij.lang.annotation.HighlightSeverity
9
14
import com.intellij.openapi.Disposable
10
15
import com.intellij.openapi.application.runReadAction
@@ -13,20 +18,51 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
13
18
import com.intellij.openapi.project.Project
14
19
import com.intellij.openapi.util.Disposer
15
20
import com.intellij.openapi.util.TextRange
21
+ import com.intellij.profile.codeInspection.InspectionProjectProfileManager
16
22
import com.intellij.psi.PsiElement
17
23
import com.intellij.psi.PsiErrorElement
18
24
import com.intellij.psi.PsiFile
25
+ import com.intellij.util.PairProcessor
19
26
import com.intellij.util.messages.MessageBusConnection
20
27
import java.util.concurrent.CompletableFuture
21
28
import java.util.concurrent.TimeUnit
22
29
23
30
object PsiErrorCollector {
24
- fun runInspections (project : Project , psiFile : PsiFile ) {
31
+ fun runInspections (project : Project , psiFile : PsiFile ): List <@ InspectionMessage String > {
25
32
val scope = AnalysisScope (psiFile)
26
- val globalContext = InspectionManager .getInstance(project).createNewGlobalContext() as ? GlobalInspectionContextBase
33
+ val globalContext = InspectionManager .getInstance(project).createNewGlobalContext()
34
+ as ? GlobalInspectionContextBase ? : return emptyList()
27
35
28
- globalContext?.currentScope = scope
29
- globalContext?.doInspections(scope)
36
+ val inspectionProfile = InspectionProjectProfileManager .getInstance(project).currentProfile
37
+
38
+ val toolWrappers = inspectionProfile.getInspectionTools(psiFile)
39
+
40
+ globalContext.currentScope = scope
41
+ toolWrappers.forEach {
42
+ it.initialize(globalContext)
43
+ }
44
+
45
+ val toolsCopy: MutableList <LocalInspectionToolWrapper > = ArrayList <LocalInspectionToolWrapper >(toolWrappers.size)
46
+ for (tool in toolWrappers) {
47
+ if (tool is LocalInspectionToolWrapper ) {
48
+ toolsCopy.add(tool.createCopy())
49
+ }
50
+ }
51
+
52
+ if (toolsCopy.isEmpty()) {
53
+ return emptyList()
54
+ }
55
+
56
+ val indicator = DaemonProgressIndicator ()
57
+ return runReadAction {
58
+ val result: Map <LocalInspectionToolWrapper , List <ProblemDescriptor >> = InspectionEngine .inspectEx(
59
+ toolsCopy, psiFile, psiFile.textRange, psiFile.textRange, false , false , true ,
60
+ indicator, PairProcessor .alwaysTrue<LocalInspectionToolWrapper ?, ProblemDescriptor ?>()
61
+ )
62
+
63
+ val problems = result.values.flatten()
64
+ return @runReadAction problems.map { it.descriptionTemplate }
65
+ }
30
66
}
31
67
32
68
/* *
0 commit comments