【问题标题】:Intellij plugin development print in console windowIntellij 插件开发打印在控制台窗口中
【发布时间】:2018-08-22 17:21:07
【问题描述】:

我是 Intellij Idea 插件开发的新手。所以我正在开发一个简单的插件来在工具窗口中打印一个字符串值(类似于控制台窗口)!我在网上搜索的例子少了!我对 Intellij 动作系统有一点了解,但无法弄清楚如何在 plugin.xml 中注册必要的动作以在工具窗口中打印字符串!

以下是我的代码

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;

    public class A extends AnAction {

        @Override
        public void actionPerformed(AnActionEvent e) {
           String x="Hello how are you?";
        }
    }

如何在工具窗口中打印字符串 x?

【问题讨论】:

  • 哪个工具窗口?你的动作是否被触发了?
  • @Meo 不!我不知道如何为工具窗口注册操作以及在 A 类中调用哪个方法来打印字符串 x
  • 哪个工具窗口,您到底想在哪里执行该操作? ...阅读 intellij-sources 并找到类似的东西。
  • @Meo 我想在运行插件并在其中打印字符串 x 的值时拥有类似控制台的东西! eg-showMessageDialog() 可用于在对话框中打印字符串,但我需要一个类似控制台的东西

标签: gradle intellij-idea plugins window action


【解决方案1】:

控制台窗口不能单独存在,它们必须绑定到工具窗口。这是一个简单的例子。

首先在 XML 中为您的插件创建一个 ToolWindow:

<extensions defaultExtensionNs="com.intellij">
  <!-- Add your extensions here -->
  <toolWindow id="MyPlugin" 
              anchor="bottom"
              icon="iconfile.png"
              factoryClass="com.intellij.execution.dashboard.RunDashboardToolWindowFactory"></toolWindow>
</extensions>

然后在您的操作中,您可以抓住该工具窗口的句柄并懒惰地创建一个控制台视图,然后在那里添加您的文本:

  ToolWindow toolWindow = ToolWindowManager.getInstance(e.getProject()).getToolWindow("MyPlugin");
  ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(e.getProject()).getConsole();
  Content content = toolWindow.getContentManager().getFactory().createContent(consoleView.getComponent(), "MyPlugin Output", false);
  toolWindow.getContentManager().addContent(content);
  consoleView.print("Hello from MyPlugin!", ConsoleViewContentType.NORMAL_OUTPUT);

几点说明:

  1. 默认情况下您的新工具窗口可能不可见,因此您可能需要从“查看”->“工具窗口”菜单激活它。

  2. 我们使用RunDashboardToolWindowFactory 来创建我们的新工具窗口,因此它将采用运行窗口的布局。您可以使用ToolWindowFactory 的任何实现(包括您自己的自定义类)来代替它。

【讨论】:

【解决方案2】:

RunDashboardToolWindowFactory 不再存在于最新的智能社区代码库中。我唯一的参考是https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/tool_window

【讨论】:

    【解决方案3】:

    动作应该以这种方式注册(在plugin.xml中):

        <actions>
        <group id="MyPlugin.TopMenu"
               text="_MyPlugin"
               description="MyPlugin Toolbar Menu">
            <add-to-group group-id="MainMenu" anchor="last"/>
            <action id="MyAction"
                    class="actions.MyAction"
                    text="_MyAction"
                    description="MyAction"/>
        </group>
    </actions>
    

    另外,请确保您的操作在包内,否则可能无法找到/调用它。

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 2016-07-08
      • 2018-06-28
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 2012-06-22
      • 2019-05-06
      • 2010-12-26
      相关资源
      最近更新 更多