Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add custom expression functions for Live templates in Intellij

How can add new custom functions for Live Templates in Idea Intellij.

For example i need a custom function which can convert a live template variable from Camel Case to Spaced. i.e in Live Template on variable has to be inserted at multiple places with & without space.

Eg. "MyVar" and "My Var". So i want to insert an expression to convert 'MyVar' to 'My Var'.

No expression available by default can be used for this.

Thanks.

like image 818
Aman Avatar asked Aug 28 '12 09:08

Aman


People also ask

How do I add custom code to completion in IntelliJ?

Invoke type-matching completion By default, IntelliJ IDEA displays the code completion popup automatically as you type. If automatic completion is disabled, press Ctrl+Shift+Space or choose Code | Code Completion | Type-Matching from the main menu. If necessary, press Ctrl+Shift+Space once again.

What is live template in IntelliJ?

Live Templates reduce the amount of code you need to type for the more common code constructs. You can use psvm or main to create the main Java method, for example. There are lots of live templates including fori for a for i loop and nn for not null, plus many more.

How do I use code templates in IntelliJ?

Press Ctrl+Alt+S to open the IDE settings and select Editor | File and Code Templates. and specify the template name, file extension, name of the resulting file, and body of the template. Apply the changes and close the dialog.


1 Answers

There is OpenAPI for providing Live Template functions. One can create IntelliJ IDEA plug-in that will add more functions.

See the Macro abstract class. Plug-in should define extension point, like this one:

<liveTemplateMacro implementation="com.intellij.codeInsight.template.macro.CapitalizeMacro"/>

Search IntelliJ IDEA Community code base for sample implementations.

like image 191
CrazyCoder Avatar answered Sep 23 '22 04:09

CrazyCoder