You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于IntelliJ Platform Plugin SDK的插件错误监听与面板更新咨询

解决IntelliJ插件监听IDE错误并更新ToolWindow的方案

针对你需要在IDE检测到错误时更新自定义ToolWindow的需求,有两种实用的实现方式,以下是具体步骤:

方式一:监听Psi语法错误元素(PsiErrorElement)

这种方式直接监听代码语法树中的错误元素变化,适合只关注语法错误的场景:

  1. 在你的MyWindowTools的createToolWindowContent方法中,获取项目的PsiManager并注册监听器:
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
    // 原有的面板初始化代码
    panel.add(imageLabel)
    toolWindow.contentManager.addContent(toolWindow.contentManager.factory.createContent(panel, "", false))

    // 注册Psi树变化监听器
    val psiManager = PsiManager.getInstance(project)
    val psiChangeListener = object : PsiTreeChangeAdapter() {
        // 监听子元素添加事件
        override fun childAdded(event: PsiTreeChangeEvent) {
            checkAndUpdatePanel(event.child)
        }

        // 监听子元素修改事件
        override fun childChanged(event: PsiTreeChangeEvent) {
            checkAndUpdatePanel(event.child)
        }

        // 检查元素是否为错误元素并更新面板
        private fun checkAndUpdatePanel(element: PsiElement?) {
            if (element is PsiErrorElement) {
                // 必须在UI线程更新Swing组件
                ApplicationManager.getApplication().invokeLater {
                    imageLabel.text = "检测到语法错误: ${element.errorDescription}"
                    // 这里可以添加你的面板更新逻辑,比如刷新整个面板内容
                }
            }
        }
    }
    // 将监听器绑定到ToolWindow的生命周期,避免内存泄漏
    psiManager.addPsiTreeChangeListener(psiChangeListener, toolWindow.disposable)
}

方式二:监听所有高亮错误(含静态检查错误)

如果需要监听包括静态检查(如未使用变量、类型不匹配)在内的所有IDE标记错误,可以通过监听高亮信息变化实现:

  1. 同样在createToolWindowContent方法中,通过项目消息总线注册HighlightInfoListener:
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
    // 原有的面板初始化代码
    panel.add(imageLabel)
    toolWindow.contentManager.addContent(toolWindow.contentManager.factory.createContent(panel, "", false))

    // 注册高亮信息监听器
    val messageBusConnection = project.messageBus.connect(toolWindow.disposable)
    messageBusConnection.subscribe(HighlightInfoListener.TOPIC, object : HighlightInfoListener {
        override fun highlightInfosAdded(infos: MutableList<HighlightInfo>, editor: Editor) {
            // 筛选出错误级别的高亮信息
            val errorInfos = infos.filter { it.type.severity == Severity.ERROR }
            if (errorInfos.isNotEmpty()) {
                ApplicationManager.getApplication().invokeLater {
                    imageLabel.text = "当前共${errorInfos.size}个错误"
                    // 可以遍历errorInfos获取每个错误的详细信息,更新面板内容
                }
            }
        }
    })
}

关键注意事项

  • UI线程更新:所有Swing组件的更新必须放在ApplicationManager.getApplication().invokeLater中,因为Psi和高亮监听事件运行在后台线程,直接操作UI会引发异常。
  • 生命周期管理:注册监听器时绑定到toolWindow.disposable,确保ToolWindow关闭时自动移除监听器,避免内存泄漏。
  • 场景选择:如果只需要语法错误,选方式一;如果需要覆盖所有IDE检测的错误(含静态检查),选方式二。

内容的提问来源于stack exchange,提问作者Quentin Emo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 00:25:24