PhpStormのプラグインを初めて開発してみた。
こんばんわ。
今日の午後は暇だったので(昼寝はしましたが)、PhpStormのプラグインを作ってみました。
会社のLaravelプロジェクトなんですが、ビューファイルの指定方法が独自の仕様になっているので、ビューファイルから呼び出し元を特定して、そこを開くのに苦労していました。それを何とか簡単にできないかと考えていました。どうやら、IntelliJ IDEAを使ってJavaで作れるらしいことはわかったので、ググりながら作業をしました。
手順1. JDKをインストールする。
既にJAVA8が入っていたので、それを使用することにしました。 一応リンク貼っておきます。
Java SE - Downloads | Oracle Technology Network | Oracle
手順2. IntelliJ IDEAをインストールする。
無償のコミュニティバージョンでOKです。
IntelliJ IDEA: The Java IDE for Professional Developers by JetBrains
手順3. プロジェクトを作成していきます。
IntelliJ Platform Pluginを選択します。
手順4. プロジェクト名を入力します。
手順5. plugin.xmlを編集します。
PhpStormでもプラグインが利用できる様に、コメントアウトを外します。
<depends>com.intellij.modules.lang</depends>
手順6. アクションを作成していきます。
srcディレクトリを選択して、画像のようにクリックします。
手順7. アクションを登録します。
今回、エディターのポップアップにメニューを追加するので、GroupsにEditorPopupMenu、ActionsにEditorPopupMenu1を選択しました。
手順8. クラスファイルがでるので、いよいよコードを実装します。
actionPerformedメソッドをオーバーライドします。
コードはこんな感じです。
import com.intellij.find.FindManager; import com.intellij.find.FindModel; import com.intellij.find.findInProject.FindInProjectManager; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.vfs.VirtualFile; public class WhereIsThisViewCalledFrom extends AnAction { @Override public void actionPerformed(AnActionEvent e) { // プロジェクトディレクトリからの相対パス final String viewRelativePath = "/resources/views/"; // プロジェクトの取得 Project project = e.getData(PlatformDataKeys.PROJECT); // viewファイルのパス String viewBasePath = project.getBasePath() + viewRelativePath; // アクティブウィンドウのファイルを取得 final VirtualFile[] vFiles = e.getData(PlatformDataKeys.VIRTUAL_FILE_ARRAY); if (vFiles == null || vFiles.length == 0) { Messages.showWarningDialog("There is no active file.", "Error"); return; } VirtualFile target = vFiles[0]; // viewファイルでない場合はエラー if (!target.getName().contains(".blade.php")) { Messages.showWarningDialog("This is not a view file.", "Error"); return; } /* * 想定しているviewのフルパス * C:/Users/someone/PhpstormProjects/sample/resources/views/XXX/Application/table/detail/sample.blade.php * 検索する文字列 * ないしょ */ // フルパスから不要な文字列を取り除く String filePath = target.getCanonicalPath() .replace(viewBasePath,"") .replace(".blade.php","") .replace("/", "."); // ないしょ String searchText = 独自の処理; // 検索を実行する FindManager findManager = FindManager.getInstance(project); FindModel findModel = findManager.getFindInProjectModel().clone(); findModel.setStringToFind(searchText); findModel.setRegularExpressions(false); // 正規表現 FindInProjectManager.getInstance(project).startFindInProject(findModel); } }
手順9. ビルドして、jarファイルを作成する。
Build→Prepare Plugin Module "" For Deployment をクリックする。
手順10. phpStorm にインストールする。
ここからPhpStormでの操作です。設定→プラグイン→ディスクからプラグインをインストールをクリックし、先ほどのjarファイルを選択する。そして、PhpStormの再起動をすれば完了です。
手順11. 実行してみる。
エディター上で右クリックすると、追加したメニューが表示されます。実行すると期待する検索結果が得られました。
以上となります。 Javaは正直得意ではないですが、なんとか簡単なプラグインの作成に成功しました。 IntelliJ の助けが大きかったです。さすがです。 今後、もっと凝ったことをしたいのですが、開発ドキュメントがネット上に少ないのがツライですね。 また、勉強したら、ブログにします。
ではまた。