IDEAのプラグイン開発の簡単なチュートリアルを見た後、パートナーは、全体のプラグインに自分の手を得るために待つことができないのですか?1つ、2つ、3つと計画していますが、どこから始めればいいのかわかりません。以下に、私が整理した一般的なAPIのいくつかを共有します。
AnAction
public class JsonFormatAction extends AnAction {
@Override
public void actionPerformed(AnActionEvent event) {
// 現在のプロジェクトオブジェクトを取得する
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();
}
- その他のメソッド
// プロジェクトを取得する. 内部呼び出し getData(CommonDataKeys).PROJECT) = getDataContext().getData(CommonDataKeys.PROJECT)
Project project = e.getProject();
// データコンテキストを取得する
DataContext dataContext = e.getDataContext();
// contextPlatformDataKeysで定義されたフィールドに追加情報を取得することもできる。
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);
// カスタムリスト
PsiType typeFromText = psiElementFactory.createTypeFromText("java.util.List<" + className + ">", null);
その他のAPI
XML ファイル操作
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を検索することができます:
// 現在のプロジェクトマッパーのすべての要素、タイプを記入、スコープGlobalSearchScope
List<DomFileElement<Mapper>> fileElements = DomService.getInstance().getFileElements(Mapper.class, project, GlobalSearchScope.allScope(project));
ファイルの書き込み
非同期に書き込むには WriteCommandAction を呼び出す必要があります。
WriteCommandAction.runWriteCommandAction(project, () -> {
doGenerate(psiClass, jsonObject);
});
通知
操作が成功したら、NotificationGroupクラスを使って、IDEAの右下にユーザーに通知します。
// 静的プロパティ
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);
}
また、次のようにツールクラスとして定義することもできます。
/**
*
* メッセージ通知ツールキット
*
*
*
*/
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);
}
}