# How do i run a inspection programmatically and get result?

**URL:** <https://platform.jetbrains.com/t/how-do-i-run-a-inspection-programmatically-and-get-result/852>\
**Category:** IntelliJ Platform\
**Tags:** inspections\
**Created:** [March 11, 2025, 9:22am UTC](https://platform.jetbrains.com/t/how-do-i-run-a-inspection-programmatically-and-get-result/852 "2025-03-11T09:22:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ccc1994](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/ccc1994/32/759_2.png) [@ccc1994](https://platform.jetbrains.com/u/ccc1994)\
**Post date:** [March 11, 2025, 9:22am UTC](https://platform.jetbrains.com/t/how-do-i-run-a-inspection-programmatically-and-get-result/852/1 "2025-03-11T09:22:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![yuriy.artamonov](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/yuriy.artamonov/32/48_2.png) [@yuriy.artamonov](https://platform.jetbrains.com/u/yuriy.artamonov)\
**Post date:** [March 11, 2025, 10:45am UTC](https://platform.jetbrains.com/t/how-do-i-run-a-inspection-programmatically-and-get-result/852/2 "2025-03-11T10:45:54Z")

</div>

You may check sources for `Run Inspection by Name` action in [GitHub - JetBrains/intellij-community: IntelliJ IDEA Community Edition & IntelliJ Platform](https://github.com/JetBrains/intellij-community/)

It is the standard functionality of all IDEs

---

<div class="post-metadata">

**Author:** ![ccc1994](https://sea1.discourse-cdn.com/flex001/user_avatar/platform.jetbrains.com/ccc1994/32/759_2.png) [@ccc1994](https://platform.jetbrains.com/u/ccc1994)\
**Post date:** [March 11, 2025, 11:22am UTC](https://platform.jetbrains.com/t/how-do-i-run-a-inspection-programmatically-and-get-result/852/3 "2025-03-11T11:22:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jeremibg](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@jeremibg](https://platform.jetbrains.com/u/jeremibg)\
**Post date:** [March 11, 2025, 3:05pm UTC](https://platform.jetbrains.com/t/how-do-i-run-a-inspection-programmatically-and-get-result/852/4 "2025-03-11T15:05:38Z")

</div>

What about this code:

```auto
  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:

```auto
    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](https://platform.jetbrains.com/t/inserting-lightbulb-icon-in-gutter-next-to-lint-issues/771/7).)
