问题
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:
Your new tool window may not be visible by default so you may need to activate it from the View -> Tool Windows menu.
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 ofToolWindowFactory
(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