Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom code generator

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:

  • if I am working with Java file Code > Generate allows me to insert getter, setter, constructor etc
  • if I am working with HTML file Code > Generate allows me to insert an XML tag
  • if I am working with Jasmine Text file Code > Generate allows me to insert a scaffolding of a test suit or a singe test case

I 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.

like image 277
pawluch4 Avatar asked Sep 04 '13 09:09

pawluch4


People also ask

How do you create a code generator in Python?

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.

Are code generators good?

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.


1 Answers

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 developmentfor 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!

like image 168
vikingsteve Avatar answered Sep 27 '22 22:09

vikingsteve