Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use variable value in live templates in Intellij IDEA?

I want to create live template for setter.

I've created this template enter image description here

How can I use value of par variable to generate value of var variable? Basically, I want to avoid redundancy here and put name of variable only once and other one will be generated automatically by some algorithm.

UPDATE

I want to clarify a little bit what I want to achieve.

Suppose I want to create setter with name setTime which has parameter time.

public void setTime(long time) {     // ... } 

I don't want to type "time" twice - capitalized and non-capitalized. I want to type just parameter name so method name will be generated automatically.

UPDATE (Answer)

Turned out that variable order is important. This is final result of what I want

enter image description here

like image 679
Vitalii Korsakov Avatar asked Dec 27 '12 10:12

Vitalii Korsakov


People also ask

What is live template in IntelliJ?

Use live templates to insert common constructs into your code, such as loops, conditions, various declarations, or print statements. To expand a code snippet, type the corresponding template abbreviation and press Tab . Keep pressing Tab to jump from one variable in the template to the next one.

How do I use IntelliJ templates?

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.

How do you add a live template?

Press Ctrl+Alt+S to open the IDE settings and select Editor | Live Templates. Select the template group where you want to create a new live template (for example, other). If you do not select a template group, the live template will be added to the user group. and select Live Template.

How do I find variables in IntelliJ?

To navigate to the code where the variable is declared, right-click a variable and select Jump to Source F4 . To navigate to the class declaration of the variable type, right-click a variable and select Jump to Type Source F4 .


1 Answers

You can use the soutv as an example, notice how it defines a copy of a variable:

copy

It's also possible to define custom expression for live templates via plugins or Groovy code: How can i add custom expression functions for Live templates in Intellij.

like image 63
CrazyCoder Avatar answered Sep 29 '22 08:09

CrazyCoder