Happy SE Life

IT業界で働いている人のブログです

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を選択します。

f:id:it-managers-life:20190525202904j:plain
プロジェクト作成(1)

手順4. プロジェクト名を入力します。

f:id:it-managers-life:20190525203627j:plain
プロジェクト作成(2)

手順5. plugin.xmlを編集します。

PhpStormでもプラグインが利用できる様に、コメントアウトを外します。

<depends>com.intellij.modules.lang</depends>

f:id:it-managers-life:20190525203741p:plain
plugin.xmlの編集

手順6. アクションを作成していきます。

srcディレクトリを選択して、画像のようにクリックします。

f:id:it-managers-life:20190525204003j:plain
アクション作成

手順7. アクションを登録します。

今回、エディターのポップアップにメニューを追加するので、GroupsにEditorPopupMenu、ActionsにEditorPopupMenu1を選択しました。

f:id:it-managers-life:20190525204440j:plain
アクションの登録

手順8. クラスファイルがでるので、いよいよコードを実装します。

actionPerformedメソッドをオーバーライドします。

f:id:it-managers-life:20190525204736j:plain
クラスファイル生成

コードはこんな感じです。

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 をクリックする。

f:id:it-managers-life:20190525210027j:plain
ビルド

手順10. phpStorm にインストールする。

ここからPhpStormでの操作です。設定→プラグイン→ディスクからプラグインをインストールをクリックし、先ほどのjarファイルを選択する。そして、PhpStormの再起動をすれば完了です。

f:id:it-managers-life:20190525210708j:plain
PhpStormへのインストール

手順11. 実行してみる。

エディター上で右クリックすると、追加したメニューが表示されます。実行すると期待する検索結果が得られました。

f:id:it-managers-life:20190525211210j:plain
右クリックでポップアップ表示からの実行

以上となります。 Javaは正直得意ではないですが、なんとか簡単なプラグインの作成に成功しました。 IntelliJ の助けが大きかったです。さすがです。 今後、もっと凝ったことをしたいのですが、開発ドキュメントがネット上に少ないのがツライですね。 また、勉強したら、ブログにします。

ではまた。