Reputation: 133
When I work with certain types of files, such as: Java file, HTML file or Jasmine Test file I can generate some useful code snippets using Code > Generate
option, for example:
Code > Generate
allows me to insert getter, setter, constructor etcCode > Generate
allows me to insert an XML tagCode > Generate
allows me to insert a scaffolding of a test suit or a singe test caseI was wondering if (and how) I can add my own 'generator'. I know I can use Live Templates, but I like the fact that Code > Generate
gives me a quick list of all available generators.
Upvotes: 8
Views: 4186
Reputation: 40388
Yes, you can do it by writing an IntelliJ plugin and extending this class:
com.intellij.openapi.actionSystem.Action
If you create an intelliJ plugin project (just google intellij plugin development
for information on how to get started), hit alt-enter somewhere in your project source tree and select Action
, you will get a dialog which allows you to configure where your action should appear.
You want to place it in relation to another action which already exists, for example right below it. In your case - have a look at the menu group named GenerateGroup (Generate)
.
Once your action is defined in this manner in your plugin.xml, build and run your plugin in the sandbox.
Now, when your action is triggered, the AnActionEvent
will be fired which contains references to all the necessary information you need (current project, file, position of cursor within file, psi tree, etc).
Try to get this working so far and come back with any specific questions.
Good luck!
Upvotes: 10