I am developing an Inspection plugin, and I hope to execute a global Inspection programmatically and obtain the results. I am currently using com.intellij.codeInsight.daemon.impl.MainPassesRunner
, but this approach is too slow for large projects.
You may check sources for Run Inspection by Name
action in GitHub - JetBrains/intellij-community: IntelliJ IDEA Community Edition & IntelliJ Platform
It is the standard functionality of all IDEs
Thanks for your answer!
I have already read the relevant code in com.intellij.codeInspection.ex.GlobalInspectionContextImpl#doInspections
, but I don’t know how to obtain the results after the Inspection has been executed asynchronously and then analyze them.
What about this code:
val diagnostics: List<HighlightInfo> by
lazy(LazyThreadSafetyMode.NONE) {
/*
HighlightInfo contains metadata regarding highlighted lint info such as severity level,
highlighted code (and associated offsets), lint message, associated Quick Fix Action, etc.
*/
val warningsAndErrorsWithinSelectedCode: List<HighlightInfo> = mutableListOf()
/*
This seems to be the only way to get the same diagnostics as the ones displayed in the
"Problems" tool window (linked to the "TrafficLightRenderer") for the selected code.
Moreover, this is fetched lazily because the Daemon takes a while to complete, so the
additional delay helps improve the probability that we actually have some warnings listed.
However, we do not want to block on this because it's not valuable enough to delay the
request issued by a user.
*/
DaemonCodeAnalyzerEx.processHighlights(
editor.document,
project,
HighlightSeverity.WEAK_WARNING,
selectedOffsetStart,
selectedOffsetEnd,
Processors.cancelableCollectProcessor(warningsAndErrorsWithinSelectedCode))
return@lazy warningsAndErrorsWithinSelectedCode
}
Plug it into:
val msgBusConnection = project.messageBus.connect()
msgBusConnection.subscribe(
DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC,
object : DaemonCodeAnalyzer.DaemonListener {
override fun daemonFinished() {
println("DaemonListener: daemonFinished() called")
super.daemonFinished()
}
})
If you find a better way, do let me know
(Because I’m trying to achieve something somewhat similar. See this post.)