Question

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.

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top