PhpStorm Plugin right-click

I'm trying to make a simple plugin for PhpStorm where an action is taken if I right-click on a directory in the project view and choose my action.  I'm not sure how to attach that action to a right-click, nor how to get the path in the action.

I basically want something like so:

 

    Project project = event.getProject();

String cmd = String.format("/bin/sh -c /usr/bin/sudo /usr/bin/tmutil addexclusion -p '%s'", "path");
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);

StreamGobbler gobbler = new StreamGobbler(process.getInputStream());

process.waitFor();

gobbler.close();
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
4 comments
Comment actions Permalink

Hi! To create custom action, please, perform the next steps:

1. Create action class. This class should extend com.intellij.openapi.actionSystem.AnAction class.

2. Register created action in your plugin.xml (it can be done via quickfix in intellij idea or by hand). Here is the example registration of custom action, that will be located in "run context" group when right click on file/directory will be performed:

<actions>
<action id="com.custom.action.CustomAction" class="com.custom.action.CustomAction" text="Name of the action">
<add-to-group group-id="RunContextGroup" anchor="last"/>
</action>
</actions>

3. Now you have to override update action. In update you can control presentation and visibility of an action. All info about the place from action was invoked contains in AnActionEvent event parameter of the method. In your example, you can get selected file via VirtualFile file = event.getData(CommonKeys.VIRTUAL_FILE) and show action only if directory was selected by the following code: event.getPresentation().setEnabledAndVisible(file != null && file.isDirectory())

4. Same information available in actionPerformed method which will be called when action will be performed.

0
Comment actions Permalink

You may also try using External Tools facility for this simple case + add custom shortcut/file context menu item in settings.

0
Comment actions Permalink

OK, so now my code looks like this, which I guess is complete:

 

@Override
public void actionPerformed(AnActionEvent e) {
VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
if (file == null) {
return;
}

String path = file.getCanonicalPath();

String cmd = String.format("/bin/sh -c /usr/bin/sudo /usr/bin/tmutil addexclusion -p '%s'", path);
Process process = null;
try {
process = Runtime.getRuntime().exec(cmd);

StreamGobbler gobbler = new StreamGobbler(process.getInputStream());

process.waitFor();

gobbler.close();
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}

@Override
public void update(AnActionEvent e) {
VirtualFile file = e.getData(CommonDataKeys.VIRTUAL_FILE);
e.getPresentation().setEnabledAndVisible(file != null && file.isDirectory());
}

 

I've done the Build->Build Project.  Has this built a release build somewhere that I can now open PhpStorm and point it at this plugin by installing from disk?  Not sure which file to point at.

 

0
Comment actions Permalink

To release plugin you can invoke Build | Prepare plugin for deployment. After that you will get .jar file in your working directory that can be installed as plugin from disk in PhpStorm via Preferences | Plugins | Install plugin from disk.

But indeed, if all you want is just run some external tool on directory it will be easier to use External Tools. Here is example of configuration that can be created to match your needs:

After that action called "MyCustomName" will be available in "External tools" group after right click on the file/directory.

0

Please sign in to leave a comment.