现今的IDE尽管有如“洪水猛兽”般强大,但要知道再强大的IDE也没法提供给使用者想要的一切功能,
所以IDE一般都提供有API接口供开发者自行扩展。下面以Intellij IDEA 12下的插件开发为例,来看一下如何进一步增强IDE以适应开发者的需求。



1.创建Plugin工程

如果Module SDK中没有可选的SDK,那么点击New新添加一个SDK,目录就选择Intellij的安装位置即可。



创建出的Plugin项目结构很简单,只是在META-INF下多了一个plugin.xml配置文件,后文会介绍到它的用处。





2.让插件Say哈喽

2.1添加Component

在src目录上Alt+Insert,可以看到New对话框中列出有三种组件,分别对应三种级别:Application、Project、Module Component。
这里我们选择Application Component作为实例,在弹出框中输入一个名字例如MyComponent,这样一个组件就创建出来了。



然后在MyComponent中添加一个SayHello的方法,其他方法暂不实现,源代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.cdai.plugin.rapidg;
 
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.ui.Messages;
import org.jetbrains.annotations.NotNull;
 
/**
 * My Component
 * User: cdai
 * Date: 13-11-4
 * Time: 上午10:08
 */
public class MyComponent implements ApplicationComponent {
    public MyComponent() {
    }
 
    public void initComponent() {
        // TODO: insert component initialization logic here
    }
 
    public void disposeComponent() {
        // TODO: insert component disposal logic here
    }
 
    @NotNull
    public String getComponentName() {
        return "MyComponent";
    }
 
    public void sayHello() {
        // Show dialog with message
        Messages.showMessageDialog(
                "Hello World!",
                "Sample",
                Messages.getInformationIcon()
        );
    }
}

2.2添加Action

现在需要添加一个Action让使用我们插件的用户可以通过菜单或其他方式点击到插件。



Action主要工作是创建一个Application和MyComponent对象,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.cdai.plugin.rapidg;
 
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
 
/**
 * Say Hello Action
 * User: cdai
 * Date: 13-11-4
 * Time: 上午10:16
 */
public class SayHelloAction extends AnAction {
 
    @Override
    public void actionPerformed(AnActionEvent e) {
        Application application = ApplicationManager.getApplication();
        MyComponent myComponent = application.getComponent(MyComponent.class);
        myComponent.sayHello();
    }
 
}

2.3配置文件

其实前面两步新建Component和Action的同时,IDEA在帮我们自动将它们注册到META-INF/plugin.xml中。
我们刚才添加的Application Component和Action会在<application-components>结点下,plugin.xml最终是下面的样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<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>



3.运行调试

打开Run/Debug配置对话框,新加一个Plugin类型的,Use classpath of module选择刚才的示例项目。



运行起来就会发现,原来会启动一个新的Intellij IDEA实例,重新走一遍启动配置过程,可以看到插件的名字就是plugin.xml中<name>中的值。
我们可以只选中我们刚开发的插件,忽略掉其他的。现在通过Window->Say Hello!就可以触发我们的插件了,效果就是会弹出个对话框。




有趣的是,plugin.xml中其他的一些描述会在插件崩溃时显示给用户,将问题报告给插件作者。





4.插件配置面板

很多插件都是在Settings中有配置页的,现在简单介绍一下如何为我们的插件添加一个配置页。

首先改造一下MyComponent类,主要变化就是多实现了一个Configurable接口。这个接口中有一个createComponent方法,
这个方法返回Swing的JComponent对象就会显示到Settings里。另外使用IDEA提供的Swing Designer设计器还是挺方便的,
自动生成的样式和布局代码为了避免被修改,也不会被我们看到(与NetBeans不同),所以最终代码很简洁。



最终效果就是这样的了,我们在设计器里设计的面板嵌入到了右边。





5.带对话框的插件

一种常见的插件就是点击插件对应的菜单项后,弹出一个对话框(例如搜索工作空间里的类、提交SVN前的代码确认等等)。
其实很简单,实现方法就是先创建一个Dialog,然后在Swing设计器中设计好Dialog中的控件布局,最后在Action中显示出对话框。
具体代码就不列举了,有需求可以找我索取。



参考资料

We have tried to answer some of these questions in the new demo: GoogleSearch
Plugin for IntelliJ IDEA
. This demo explains the basics of plugin writing and publishing.

More experienced plugin developers can find answers in these two articles:

1.   The
Basics of Plugin Development for IntelliJ IDEA by Alexey Efimov
 (for beginners)

2.   Developing
Custom Language Plugins for IntelliJ IDEA 5.0 by Dmitry Jemerov
 (for more advanced developers) 



Also IntelliJ IDEA tries to ease the plugin development by providing specific code generation, completion, etc. Just look what was done for 5.0
and 5.1 releases for Open
API & Plugins
.

Intellij IDEA插件开发入门的更多相关文章

  1. Chrome插件开发入门(二)——消息传递机制

    Chrome插件开发入门(二)——消息传递机制   由于插件的js运行环境有区别,所以消息传递机制是一个重要内容.阅读了很多博文,大家已经说得很清楚了,直接转一篇@姬小光 的博文,总结的挺好.后面附一 ...

  2. IntelliJ IDEA 插件开发视频教程

    IntelliJ IDEA 插件开发视频教程 学习了:http://wiki.jikexueyuan.com/project/intellij-idea-tutorial/plugins-develo ...

  3. Intellij IDEA 插件开发秘籍

    来这里找志同道合的小伙伴! 这里总结一下 Intellij IDEA 插件开发的知识,供大家参考,本篇文章包含以下内容: 开发环境搭建 Component 介绍 Extension Point And ...

  4. Vue插件开发入门

    相对组件来说,Vue 的插件开发受到的关注要少一点.但是插件的功能是十分强大的,能够完成许多 Vue 框架本身不具备的功能. 大家一般习惯直接调用现成的插件,比如官方推荐的 vue-router.vu ...

  5. discuzX3后台管理插件开发入门

    discuz官方有完整的插件开发文档,详见: http://open.discuz.net/?ac=document&page=dev_plugin 关于discuz前台插件入门可以见这篇文: ...

  6. jQuery插件开发入门

    扩展jQuery插件和方法的作用是非常强大的,它可以节省大量开发时间.这篇文章将概述jQuery插件开发的基本知识,最佳做法和常见的陷阱. 入门 编写一个jQuery插件开始于给jQuery.fn加入 ...

  7. intellij idea 插件开发--快速定位到mybatis mapper文件中的sql

    intellij idea 提供了openApi,通过openApi我们可以自己开发插件,提高工作效率.这边直接贴个链接,可以搭个入门的demo:http://www.jianshu.com/p/24 ...

  8. Maven3.x 插件开发入门

    Maven工具有很多插件,各种各样的插件,让我们开发调试过程中非常方便,但是终究是有你想要的但是现目前插件不能满足的(可能性非常非常低),这个时候就需要使用其他的替代工具,或者是自己来开发一个Mave ...

  9. openfire插件开发入门1

    .案例插件的功能 这个插件很简单,就是在openfire Server启动时,和关闭时,在控制台打印出消息. 3.插件开发的目录结构设计 先来看一下当前openfire在eclipse中的目录结构: ...

随机推荐

  1. [BZOJ]1017 魔兽地图DotR(JSOI2008)

    BZOJ第一页做着做着就能碰到毒题,做到BZOJ1082小C就忍了,没想到下一题就是这种东西.这种题目不拖出来枭首示众怎么对得起小C流逝的青春啊. Description DotR (Defense ...

  2. Python中byte与str

    原文传送门:请点击 现在计算机中,在内存中采用unicode编码方式. 可以看到上图中,字节型数据t并没有像想象中的一样显示0,1字符串.显示仍然是b,这是因为t是采用utf-8来编码,而utf-8与 ...

  3. python中str常用操作

    1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + str2) >> ...

  4. C++ 智能指针 auto_ptr 和 shared_ptr

    首先,如果你不知道什么是智能指针,请先移步:C++智能指针简单剖析 1.auto_ptr #ifndef AUTO_PTR_H #define AUTO_PTR_H template<typen ...

  5. linux修改root账户的用户名所得的教训

    之前linux服务器的密码被别人改过, 然后叫服务器相关的负责人重置了root账户(服务器负责人在客户所在公司), 重置好之后, 领导叫更改下root 用户名和密码, 于是我二话不说就开始找方法, 找 ...

  6. angular-cli学习笔记 快速创建代码模板

    组件: ng g component component/demo 服务: ng g service service/news 然后在app.module.ts里引入 ng g service ser ...

  7. 一个使用 Web Components 的音乐播放器: MelodyPlayer

    先上效果预览: Web Components 首先,什么是 Web Components ? MDN 给出的定义是: Web Components 是一套不同的技术,允许您创建可重用的定制元素(它们的 ...

  8. 排序分析函数中对null的处理

    --排序分析函数中对null的处理 --分析:对于null在分析函数中是升序默认是nulls last,降序默认是nulls first.如果不指定排序,那么是升序 )); ,'测试1'); ,'测试 ...

  9. R语言do.call 函数用法详解

    虽然R语言有类型很丰富的数据结构,但是很多时候数据结构比较复杂,那么基本就会用到list这种结构的数据类型.但是list对象很难以文本的形式导出,因此需要一个函数能快速将复杂的list结构扁平化成da ...

  10. OpenResty 执行阶段的概念和用途

    主要还是 Nginx 的执行阶段知识了,都是因为 OR 才会那么深刻, 它有些自己的阶段. 主要还是参照 春哥的 Nginx 教程 请多读几遍,如果不清楚nginx的执行阶段就无法充分利用 openr ...