在开发Toolkit过程中查阅相关资料和阅读其他开源项目总结的一些常用API.
整体内容来源于网络, 以及自己使用开发Toolkit过程中使用到的.
总结的不到位的地方欢迎指正.
AnAction操作
- 创建Action集成
AnAction
并实现其actionPerformed
方法. 在方法中可以获取到AnActionEvent
对象. 代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| public class JsonFormatAction extends AnAction {
@Override public void actionPerformed(AnActionEvent event) {
Project project = event.getData(PlatformDataKeys.PROJECT); PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE); Editor editor = event.getData(CommonDataKeys.EDITOR); PsiClass psiClass = getTargetClass(editor, psiFile); DialogWrapper dialog = new JsonFormat(project, psiFile, editor, psiClass); dialog.show(); }
|
- 其他方式
1 2 3 4 5 6 7 8 9 10 11
| Project project = e.getProject();
DataContext dataContext = e.getDataContext();
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类或者接口
1 2 3 4 5 6 7 8 9 10 11
| @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
源码有注释且比较清楚, 此处仅记录我用到的一部分
1 2 3 4
| String qualifiedName = aClass.getQualifiedName();
PsiField[] fields = aClass.getFields();
|
PsiField操作
1 2
| String name = psiField.getName()
|
PsiElement操作
PsiClass和PsiField都实现了PsiElement
1 2 3 4
| element.delete()
add(PsiElement element)
|
PsiType操作
PsiType支持常用基本类型, 但是当创建对象时则不支持.需要自己创建
1 2 3 4 5 6 7
| PsiElementFactory psiElementFactory = JavaPsiFacade.getElementFactory(project);
PsiType stringPsiType = psiElementFactory.createTypeFromText("java.lang.String", null)
PsiType listPsiType = psiElementFactory.createTypeFromText("java.util.List<String>", null);
PsiType 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 方法要使用复数形式。
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
| public interface Mapper extends DomElement {
@Attribute("namespace") GenericAttributeValue<String> getNamespace();
@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:
1 2
| List<DomFileElement<Mapper>> fileElements = DomService.getInstance().getFileElements(Mapper.class, project, GlobalSearchScope.allScope(project));
|
写入文件
需要调用WriteCommandAction
进行异步写入.
1 2 3
| WriteCommandAction.runWriteCommandAction(project, () -> { doGenerate(psiClass, jsonObject); });
|
通知
在操作成功之后,在 IDEA 右下角通知用户,使用 NotificationGroup 类即可。
1 2 3 4 5 6 7 8 9
| 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);
}
|
也可以定义为工具类,如下
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
|
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 这个插件源码,源码中有一些注释。在其他优秀的插件中,同样可有相关使用方法。
相关资料