Intellij plugin development print in console window

倖福魔咒の 提交于 2021-02-07 09:49:44

问题


I am new to Intellij Idea plugin development. So I am developing a simple plugin to print a string value in a tool window(similar to console window)! There are less examples when I searched the web! I have a slight understanding about the Intellij action system but is unable to figure out how to register the necessary action in the plugin.xml to print the string in a tool window!

Following is my code

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?";
        }
    }

How can I print String x in a tool window?


回答1:


Console windows can't just exist on their own, they have to be tied to a tool window. Here's a quick example.

First create a ToolWindow for your plugin in XML:

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

Then in your action, you can grab a handle to that tool window and lazily create a console view, then add your text there:

  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);

A couple of notes:

  1. Your new tool window may not be visible by default so you may need to activate it from the View -> Tool Windows menu.

  2. We used RunDashboardToolWindowFactory to create our new tool window, so it will take on the layout of a run window. You can use any implementation of ToolWindowFactory (including your own custom class) in its place.




回答2:


Actions should be registered this way (inside in 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>

Also, make sure your action is inside a package, otherwise it might not be found/called.



来源:https://stackoverflow.com/questions/51972122/intellij-plugin-development-print-in-console-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!