关于java:小伙伴想写个-IDEA-插件么这些-API-了解一下

8次阅读

共计 4682 个字符,预计需要花费 12 分钟才能阅读完成。

前言

在看完 IDEA 插件开发繁难教程后,小伙伴们是否急不可待的想本人上手整一个插件了?心里布局好了一二三,然而却不晓得从哪里开始下手。上面我分享下本人整顿的一些罕用的 API。

公众号:『刘志航』,记录工作学习中的技术、开发及源码笔记;时不时分享一些生存中的见闻感悟。欢送大佬来领导!

AnAction 操作

  1. 创立 Action 集成 AnAction 并实现其 actionPerformed 办法. 在办法中能够获取到 AnActionEvent 对象. 代码如下:
public class JsonFormatAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent event) {

        // 获取以后 project 对象
        Project project = event.getData(PlatformDataKeys.PROJECT);
        // 获取以后编辑的文件, 能够进而获取 PsiClass, PsiField 对象
        PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
        Editor editor = event.getData(CommonDataKeys.EDITOR);
        // 获取 Java 类或者接口
        PsiClass psiClass = getTargetClass(editor, psiFile);
        // 创立并调起 DialogWrapper
        DialogWrapper dialog = new JsonFormat(project, psiFile, editor, psiClass);
        dialog.show();}
  1. 其余形式
// 获取 project. 外部调用 getData(CommonDataKeys.PROJECT) = getDataContext().getData(CommonDataKeys.PROJECT)
Project project = e.getProject();
// 获取数据上下文
DataContext dataContext = e.getDataContext();
// context 能够也获取到其余信息, 入参为 PlatformDataKeys 定义的字段
Project project1 = dataContext.getData(PlatformDataKeys.PROJECT);
Editor editor = dataContext.getData(PlatformDataKeys.EDITOR);
PsiFile psiFile = dataContext.getData(PlatformDataKeys.PSI_FILE);
PsiElement psiElement = dataContext.getData(PlatformDataKeys.PSI_ELEMENT);
// 虚构文件
VirtualFile virtualFile = dataContext.getData(PlatformDataKeys.VIRTUAL_FILE);

获取 PsiClass

PsiClass 为 java 类或者接口

@Nullable
protected PsiClass getTargetClass(Editor editor, PsiFile file) {int offset = editor.getCaretModel().getOffset();
    PsiElement element = file.findElementAt(offset);
    if (element == null) {return null;} else {PsiClass target = PsiTreeUtil.getParentOfType(element, PsiClass.class);
        return target instanceof SyntheticElement ? null : target;
    }
}

Psixxx 操作

PsiClass 操作 API

源码有正文且比较清楚, 此处仅记录我用到的一部分

// 获取全类名
String qualifiedName = aClass.getQualifiedName();
// 获取所有字段
PsiField[] fields = aClass.getFields();

PsiField 操作

// 获取字段名
String name = psiField.getName()

PsiElement 操作

PsiClass 和 PsiField 都实现了 PsiElement

// 删除
element.delete()
// 增加元素, 向一个类中增加办法, 字段等, 也能够调用 addBefore, addAfter
add(PsiElement element)

PsiType 操作

PsiType 反对罕用根本类型, 然而当创建对象时则不反对. 须要本人创立

PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(project);
// String 类型
PsiType stringPsiType = psiElementFactory.createTypeFromText("java.lang.String", null)
// list
PsiType listPsiType = psiElementFactory.createTypeFromText("java.util.List<String>", null);
// 自定义 list
PsiType typeFromText = psiElementFactory.createTypeFromText("java.util.List<" + className + ">", null);

其余 API

XML 文件操作

参考地址:https://jetbrains.org/intelli…

以 Mapper.xml 举例申明接口,继承 DomElement,并配合 @Attribute、@SubTag、@SubTagsList 注解定义一个 xml model,其中须要留神 @SubTagsList 办法要应用复数模式。

public interface Mapper extends DomElement {

    /**
     * namespace
     *
     * @return
     */
    @Attribute("namespace")
    GenericAttributeValue<String> getNamespace();

    /**
     *
     * 增删改查对应的节点
     *
     * @return
     */
    @SubTagsList({"select", "insert", "update", "delete"})
    List<Statement> getStatements();
​
    @SubTagList("select")
    List<Select> getSelects();

    @SubTagList("insert")
    List<Insert> getInserts();

    @SubTagList("update")
    List<Update> getUpdates();

    @SubTagList("delete")
    List<Delete> getDeletes();}

搜寻文件

比方想搜寻我的项目中的所有 xml 文件,下面应用 Mapper 接口定义了 Mapper.xml 的构造,就能够利用 DomService 搜寻所有的 Mapper.xml:

// 以后我的项目的所有元素 mapper, 别离填入类型, 作用域 GlobalSearchScope
List<DomFileElement<Mapper>> fileElements = DomService.getInstance().getFileElements(Mapper.class, project, GlobalSearchScope.allScope(project));

写入文件

须要调用 WriteCommandAction 进行异步写入.

WriteCommandAction.runWriteCommandAction(project, () -> {doGenerate(psiClass, jsonObject);
});

告诉

在操作胜利之后,在 IDEA 右下角告诉用户,应用 NotificationGroup 类即可。

// 动态属性
private static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup("Java2Json.NotificationGroup", NotificationDisplayType.BALLOON, true);

public void actionPerformed(@NotNull AnActionEvent e) {
    // 在办法中调用
    Notification success = NOTIFICATION_GROUP.createNotification(message, NotificationType.INFORMATION);
    Notifications.Bus.notify(success, project);

}

也能够定义为工具类,如下

/**
 *
 * 进行音讯告诉工具类
 *
 * @author liuzhihang
 * @date 2020/2/28 18:52
 */
public class NotificationUtils {private static NotificationGroup notificationGroup = new NotificationGroup("ApiDoc.NotificationGroup", NotificationDisplayType.BALLOON, true);

    public static void warnNotify(String message, Project project) {Notifications.Bus.notify(notificationGroup.createNotification(message, NotificationType.WARNING), project);
    }

    public static void infoNotify(String message, Project project) {Notifications.Bus.notify(notificationGroup.createNotification(message, NotificationType.INFORMATION), project);
    }

    public static void errorNotify(String message, Project project) {Notifications.Bus.notify(notificationGroup.createNotification(message, NotificationType.ERROR), project);
    }

}

总结

基本上罕用的就是这些了,也能够查找官网文档,官网文档当初还是比拟全面的,地址在相干材料中。也能够 Clone Toolkit 这个插件源码,源码中有一些正文。在其余优良的插件中,同样可有相干应用办法。

相干材料

  • Toolkit: https://github.com/liuzhihang/toolkit
  • copy-as-json: https://github.com/liuzhihang/copy-as-json
  • IntelliJ Platform SDK: https://jetbrains.org/intellij/sdk/docs/intro/welcome.html
正文完
 0