欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

Intellij IDEA 插件开发入门详解 - 如何添加 Application 和 Project Component,并创建 Action? 在本文中,我们将详细介绍如何在 IntelliJ IDEA 中添加 Application 和 Project Component,并且通过这些组件来创建一个简单的 Action。 首先,我们将在 src 目录上使用 Alt+Insert 快捷键打开 New 对话框,然后从中选择 Application Component 并输入名称如 MyComponent。接下来,我们在 MyComponent 类中添加一个 sayHello 方法并编写相关逻辑。 然后,我们需要为我们的插件添加一个 Action,使用户可以通过菜单或其它方式访问它。为此,我们将创建一个新的类 SayHelloAction 继承自 AnAction 类,并在 actionPerformed 方法中获取 Application 和 MyComponent 对象,最后调用 MyC

最编程 2024-08-04 20:31:50
...

其实前面两步新建Component和Action的同时,IDEA在帮我们自动将它们注册到META-INF/plugin.xml中。

我们刚才添加的Application Component和Action会在<application-components>结点下,plugin.xml最终是下面的样子:

<idea-plugin version="2">
 <id>com.cdai.plugin.rapidg</id>
 <name>CDai's Rapid Generator Plugin</name>
 <version>1.0</version>
 <vendor email="dc_726@163.com" url="http://www.yourcompany.com">CDai</vendor> 

 <description><![CDATA[
   Enter short description for your plugin here.<br>
   <small>most HTML tags may be used</small>
   ]]></description> 

 <change-notes><![CDATA[
   Add change notes here.<br>
   <small>most HTML tags may be used</small>
   ]]>

 </change-notes>

 <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Build+Number+Ranges for description -->
 <idea-version since-build="107.105"/>

 <!-- please see http://confluence.jetbrains.net/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products

    on how to target different products -->
 <!-- uncomment to enable plugin in all products
 <depends>com.intellij.modules.lang</depends>
 -->

 <application-components>
  <!-- Add your application components here -->
   <component>
     <implementation-class>com.cdai.plugin.rapidg.MyComponent</implementation-class>
   </component>
 </application-components>

 <project-components>
  <!-- Add your project components here -->
 </project-components>

 <actions>
  <!-- Add your actions here -->
   <action id="SayHello" class="com.cdai.plugin.rapidg.SayHelloAction" text="Say Hello!">
     <add-to-group group-id="WindowMenu" anchor="first"/>
   </action>
 </actions>

 <extensions defaultExtensionNs="com.intellij">
  <!-- Add your extensions here -->
 </extensions>
</idea-plugin>

推荐阅读