John Smith
John Smith

Reputation: 33

Intellij plugin development print in console window

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?

Upvotes: 2

Views: 3403

Answers (3)

Jakku Sakura
Jakku Sakura

Reputation: 61

RunDashboardToolWindowFactory no longer exists in the lastest intellij-community codebase. The only reference I have is https://github.com/JetBrains/intellij-sdk-docs/tree/master/code_samples/tool_window

Upvotes: 1

Achraf Amil
Achraf Amil

Reputation: 1375

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.

Upvotes: 0

Mike
Mike

Reputation: 5142

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.

enter image description here

Upvotes: 5

Related Questions