Adding a custom code generator

When I work with certain types of files, such as: a Java file, an HTML file, or a Jasmine test file, I can generate useful code snippets using the Code > Generate option, for example:

  • If I work with Java Code > Generate file, I can insert getter, setter, constructor, etc.
  • If I work with HTML Code > Generate file, I can insert an XML tag
  • If I work with a Jasmine Code > Generate text file, I can insert a test suit or single test stand.

I was wondering if (and how) I can add my own β€œgenerator”. I know that I can use Live Templates, but I like the fact that Code > Generate gives me a quick list of all available generators.

+6
source share
1 answer

Yes, you can do this by writing the IntelliJ plugin and extending this class:

 com.intellij.openapi.actionSystem.Action 

If you are creating an intelliJ plug-in project (just Google intellij plugin development for information on how to get started), press alt-enter somewhere in the project source tree and select Action , you will get a dialog that allows you to configure where it should your action will appear.

You want to place it in relation to another action that already exists, for example, directly below it. In your case, look at a menu group named GenerateGroup (Generate) .

Once your action is defined this way in your plugin.xml, create and run the plugin in the sandbox.

Now, when your action is launched, AnActionEvent will be launched, which contains links to all the information you need (current project, file, cursor position inside the file, psi tree, etc.).

Try to do this so far and come back with any specific questions.

Good luck

+8
source

Source: https://habr.com/ru/post/953138/


All Articles