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.
You can create a generator model with the YAKINDU Statechart generator model wizard by selecting File → New → Code generator model. The code generation is performed automatically whenever the statechart or the generator file is modified. See also chapter Running a generator for more information.
Code generators are not bad, but sometimes they are used in situations when another solution exists. The other situation is when they are used incorrectly, or coded badly. A code generator is a tool or resource that generates a particular sort of code or computer programming language.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With