IntelliJ IDEA插件开发实战

最近接触了Android中新的架构模式,偶然想到能不能开发一个Android Studio插件来辅助该架构模式,由于Android Studio是基于idea开发的,所以尝试了一下idea 插件的开发。

idea 插件开发采用的ide即是idea,下面一步一步展示如何创建到运行一个项目。

idea插件开发需要启用idea中的一个插件,如下图:

启用插件

启用改插件,如果已经默认启用了,则就不用管了。

然后新建一个项目,如下图:

新建项目

选中左侧的项目类型,Project SDK如果是空的,就点击New,会弹出一个对话框,该对话框将路径定位在idea的安装目录,我们直接choose(选择)就行了,不用更改目录。这时就会有了一个Project SDK,下面的Groovy和SQL Support不用选,然后点击下一步,命名项目,就完成了。

完整的项目目录结构如下:

项目目录结构

当然新创建的项目,src下面是空的。

下面介绍一下idea插件开发中的一个重要文件位于META-INF下的plugin.xml,该文件为开发的插件的配置文件,代码如下:

<idea-plugin version="2">
    <id>com.tbc.hsx.plugin.</id>
    <name>Plugin display name here</name>
    <version>1.0</version>
    <vendor email="support@yourcompany.com" url="http://www.yourcompany.com">YourCompany</vendor>

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

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

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

    <!-- please see https://confluence.jetbrains.com/display/IDEADEV/Plugin+Compatibility+with+IntelliJ+Platform+Products
         on how to target different products -->
    <!-- uncomment to enable mvpplugin in all products
    <depends>com.intellij.modules.lang</depends>
    -->

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

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

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

    <actions>
        <group id="MVPSupport.Generate" text="MVPSupport" description="MVPSupport">
            <add-to-group group-id="GenerateGroup" anchor="last"/>
            <action id="MVPSupportPlugin.MVPSupportAction"
                    class="com.tbc.hsx.mvpplugin.action.MVPSupportAction"
                    text="MVPSupport" description="MVPSupport">
            </action>
        </group>
    </actions>

</idea-plugin>

上面的一些配置基本没写,直接看下面actions节点下的一些配置,group节点中的key没什么需要具体说明的。

add-to-group中的group-id很重要,它指定了你的插件在整个idea界面上的入口,这个值可以指定的很多,可参看:这里的源码 ;具体的每个group-id的含义可参看:这里

而add-to-group中的anchor是指这个插件相对group-id中的其他插件的位置,有4种选项,分别first,last,before,after 具体的含义可参看这里

action节点中重要的是class和text,class是指定插件的入口action,一般情况下,一个插件定义一个就行了,相当于main函数吧,而text是指定插件的具体的名称,就是点击这个text然后执行class指定的类。

下面具体来看一下class指定的action

package com.tbc.hsx.plugin.mvp.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilBase;
import com.tbc.hsx.plugin.mvp.constants.AppConstant;
import com.tbc.hsx.plugin.mvp.dialog.HintDialog;
import com.tbc.hsx.plugin.mvp.util.StringUtil;

/**
 * Created by hsx on 16/4/29.
 */
public class MVPSupportAction extends AnAction {

     /**
     * 执行插件的入口,相当于java中的main方法
     *
     * @param anActionEvent
     */
    @Override
    public void actionPerformed(AnActionEvent anActionEvent) {

        Project project = anActionEvent.getData(PlatformDataKeys.PROJECT);
        Editor editor = anActionEvent.getData(PlatformDataKeys.EDITOR);
        PsiFile currentEditorFile = PsiUtilBase.getPsiFileInEditor(editor, project);

        String currentEditorFileName = currentEditorFile.getName();
        /**
         * 文件名的前缀
         */
        String prefixOfGenerateFileName = currentEditorFileName;
        if (currentEditorFileName.endsWith(AppConstant.ACTIVITY)) {
            prefixOfGenerateFileName = StringUtil.cutLastSubContent(currentEditorFileName, AppConstant.ACTIVITY);
        } else if (currentEditorFileName.endsWith(AppConstant.FRAGMENT)) {
            prefixOfGenerateFileName = StringUtil.cutLastSubContent(currentEditorFileName, AppConstant.FRAGMENT);
        }

        showHintDialog(currentEditorFile, prefixOfGenerateFileName, project);
    }

    /**
     * 显示提示对话框
     *
     * @param file
     * @param prefix
     * @param project
     */
    private void showHintDialog(PsiFile file, String prefix, Project project) {
        HintDialog dialog = new HintDialog(file, prefix, project);
        dialog.setSize(600, 400);
        dialog.setLocationRelativeTo(null);
        dialog.setVisible(true);
        dialog.requestFocus();
    }

}

就2个方法,入口方法和显示对话框。说一下执行流程,当时user点击了插件之后,开始执行入口方法的代码,代码获取了当前正在editor中的文件,然后就是该插件的一些业务逻辑的处理,最后显示一个提示对话框,该对话框的大概样式如下图:

对话框

一个提示label,一个开始生成的按钮,该对话是要用到swing来绘制,可自行学习swing的知识点来了解怎么绘制各种swing控件。

当点击开始生成后,执行如下的代码:

private void createFileInWriteCommandAction() {
    new WriteCommandAction.Simple(project, mPsiFile) {
        @Override
        protected void run() throws Throwable {
            createFile();
        }
    }.execute();
}

因为用到了写文件操作,所以要采用上面的写法,在线程中操作,不然会报如下错误:

[ 116639] ERROR - plication.impl.ApplicationImpl - Assertion failed: Write access is allowed inside write-action only (see com.intellij.openapi.application.Application.runWriteAction())

java.lang.Throwable

大意就是说写的权限只能在Application.runWriteAction()里面是允许的。

以上就是开发一个idea插件的大致流程了,若有遗漏有机会再补上。

完...

参考网址:

1.http://git.jetbrains.org/?p=idea/community.git;a=tree;f=platform/platform-api/src/com/intellij/openapi;hb=HEAD

2.http://keithlea.com/idea-actions/

3.http://www.jetbrains.org/intellij/sdk/docs/index.html

4.http://www.jetbrains.org/intellij/sdk/docs/faq.html

5.https://intellij-support.jetbrains.com/hc/en-us/community/topics/200366979-IntelliJ-IDEA-Open-API-and-Plugin-Development

6.http://www.jetbrains.org/intellij/sdk/docs/basics/action_system.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容