在开发Toolkit过程中查阅相关资料和阅读其他开源项目总结的一些常用API. 整体内容来源于网络, 以及自己使用开发Toolkit过程中使用到的. 总结的不到位的地方欢迎指正.
AnAction操作
创建Action集成
AnAction并实现其actionPerformed方法. 在方法中可以获取到AnActionEvent对象. 代码如下:public class JsonFormatAction extends AnAction {@Overridepublic 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);// 创建并调起 DialogWrapperDialogWrapper dialog = new JsonFormat(project, psiFile, editor, psiClass);dialog.show();}
其他方式
// 获取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类或者接口
@Nullableprotected 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, addAfteradd(PsiElement element)
PsiType操作
PsiType支持常用基本类型, 但是当创建对象时则不支持.需要自己创建
PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(project);// String 类型PsiType stringPsiType = psiElementFactory.createTypeFromText("java.lang.String", null)// listPsiType listPsiType = psiElementFactory.createTypeFromText("java.util.List<String>", null);// 自定义listPsiType typeFromText = psiElementFactory.createTypeFromText("java.util.List<" + className + ">", null);
其他API
XML 文件操作
参考地址:https://jetbrains.org/intellij/sdk/docs/reference_guide/frameworks_and_external_apis/xml_dom_api.html
以 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, 分别填入类型, 作用域 GlobalSearchScopeList<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
