J.D.
J.D.

Reputation: 1045

Ungroup Java Class kinds in Intellij

In intelliJ, is there a way to ungroup the Java Class templates in the New context menu? I would prefer to not have the second popup with a dropdown if possible. (Trivial, I know, but I like to avoid additional steps when possible)

Upvotes: 0

Views: 122

Answers (1)

Javaru
Javaru

Reputation: 32026

As @vikingsteve indicates, this is not possible shy of writing a plug-in. As a workaround, if it is a case of a few template you use frequently and you want faster access to them, you can record a macro that ends with the New Class Dialog open and the "Kind" field completed with the desired template. Then map that macro to a keyboard shortcut. Here's the steps for recording a macro to Create a Singleton via the Singleton template:

  1. With the directory/package node selected in the project tool window or the navigation bar (Alt+Home / Home), start recording the macro: Edit > Macros > Start Macro Recording
  2. Type Alt+Insert (N, or N) to open the "New" popup dialog
  3. Type Java Class to use inline search to select the "Java Class" entry from the list.
    • This could be an optional step since the Java Class item is the first one in the list and should already be selected. But to ensure future proofing in the event that changes, you probably want to include this step
  4. Hit Enter to select the "Java Class" selection
  5. Type Tab to select the "Kind" field
    • You could also try using the K accelerator key(Alt+K)... however I found this didn't work correctly on playback
  6. Type singleton (or the name of your template) to use inline search to select the template.
  7. Hit Esc to "turn off" the inline search
  8. Type Shift+Tab to return to the Name field
  9. Use the mouse to close the new class dialog (Mouse actions are not recorded, and you need to close the dialog before you can stop the macro recording)
  10. Stop the Macro Recording via Edit > Macros > Stop Macro Recording
  11. Give the Macro a name in the dialog that opens
  12. Go into Settings and go to the Keymap setting. Under Macros, give your macro a shortcut.
    • If you have more than one of these, it might be useful and more intuitive to use a two key sequence. The first one says "Use a new file template" -- and it opens up the entire keyboard so the key used can be more meaningful to the template -- and the second one selects the particular template.

To use the macro, just select the directory/package in the Project tool window or the Navigator bar and use the keyboard shortcut to launch the macro. It should end with the New Class dialog open with the correct template selected and the cursor in the Name field ready for you to type. If the class associated with the template has a particular naming convention -- such as always ending in 'Singleton' -- you can enhance the Macro to pre-populate that as well and have the cursor positioned properly.

EDIT

I should mention, if you have several to do, rather than having to repeat the above to record a new macro each time, after recoding one, you can hack/edit the macro settings file. (Unfortunately. while there is an edit macro option in the macro dialog, there is not a copy macro option so you need to edit the config file directly.) Close IDEA. Backup and then open the file config/options/macros.xml (see Directories used by the IDE to store settings, caches, plugins and logs to locate the config directory.) Find the <macro> element with your macro. Copy & paste it, and edit the name element and typing element where you entered the template name. (You can look in config/fileTemplates for the template names, although there is some minor normalization that can occur between the template name and the file name it is saved under.) Repeat for all templates. Save, and restart IDEA. Add key mappings to the new macros.

Upvotes: 3

Related Questions