问题描述
我是Intellij Idea插件开发的新手.因此,我正在开发一个简单的插件,以便在工具窗口(类似于控制台窗口)中打印字符串值!当我在网上搜索时,例子更少了!我对Intellij操作系统有一点了解,但是无法弄清楚如何在plugin.xml中注册必要的操作以在工具窗口中打印字符串!
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!
以下是我的代码
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?";
}
}
如何在工具窗口中打印String x?
How can I print String x in a tool window?
推荐答案
控制台窗口不能仅自身存在,而必须与工具窗口绑定.这是一个简单的例子.
Console windows can't just exist on their own, they have to be tied to a tool window. Here's a quick example.
首先使用XML为您的插件创建一个ToolWindow:
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);
一些注意事项:
-
默认情况下,您的新工具窗口可能不可见,因此您可能需要从查看"->工具窗口"菜单中将其激活.
Your new tool window may not be visible by default so you may need to activate it from the View -> Tool Windows menu.
我们使用了 RunDashboardToolWindowFactory
来创建我们的新工具窗口,因此它将采用运行窗口的布局.您可以在其位置使用 ToolWindowFactory
的任何实现(包括您自己的自定义类).
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.
这篇关于Intellij插件开发在控制台窗口中打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!