28

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

Vitalii Korsakov
  • 45,737
  • 20
  • 72
  • 90

1 Answers1

25

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.

Community
  • 1
  • 1
CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
  • 1
    But how can I combine two functions in Expression field? Suppose I enter *var* variable from my template and I want my *par* variable be lowercase. I see *decapitalize* function. I tried to put **decapitalize(variableOfType(""))** inside Expression, but it didn't work. – Vitalii Korsakov Dec 27 '12 at 13:29
  • I've tried `capitalize(variableOfType(""))` and it worked for `soutv` template: `EXPR` defined from `test` became `Test`. – CrazyCoder Dec 27 '12 at 14:08
  • Do you mean, that you typed *"test"* only once and whole line became *"System.out.println("Test = " + test);"*? – Vitalii Korsakov Dec 27 '12 at 16:17
  • I didn't type anything, `test` was taken from the variable name above `String test = "";` using `variableOfType("")` and transformed to `Test` with `capitalize()`, so it became `System.out.println("Test = " + Test);` as `EXPR_COPY` = `EXPR` in this example. – CrazyCoder Dec 27 '12 at 16:19
  • Not sure what is the problem, [this works fine for me](http://img22.imageshack.us/img22/3196/20121227203707.png). `set`, `Tab`, type `long`, `Tab`, type `time`, you should get `public void setTime(long time) {...};` – CrazyCoder Dec 27 '12 at 16:38
  • Is it possible to use the same for JavaScript? – Kr1 Mar 24 '21 at 15:45