Surround template - is this possible?


Say I have a line in my javascript file as follows:

var foo = "bar";

I want to be able to surround foo with console.log(); and have it added to a new line, whilst maintaining the structure of the original line above.

Is this possible?

I've used console.log($SELECTION$); but that uses the actual var and results in

var console.log(foo); = "bar";

Can anyone shed some light on how to do this, if it's possible?


Original message URL:

Comment actions Permalink

A surround template will not work since it cannot isolate the variable and copy just that. Instead you'll need to use a conventional template. If you look at the "soutv" template in the "output" group, you will see an example of doing this in Java code. When the template is run, it asks what variable you want to output (based on the template variable using the variableOfType("") function). The nice thing is it will default to the closest variable. So if you execute the template just below your line, it will default to having the variable above it selected and it only requires an extra hit of ENTER.

Unfortunately, it looks like there is not a corresponding 'valueOfType()' template function for JavaScript. However, you can use the 'completeSmart()' to accomplish the same thing. So to get:


use the template


with the template variable $VAR$ set as

Name Expression Default Value Skip if defined
VAR completeSmart() {blank} {unchecked}

if you want to get a bit more fancy (and perhpas make the log message a bit clearer), to get an output of

foo = 'bar'

via the code

console.log("foo = '" + foo + "'");

use the template

console.log("$VAR_NAME$ = '" + $VAR$ + "'");

Name Expression Default Value Skip if defined
VAR completeSmart() {blank} {unchecked}
VAR_NAME VAR {blank}

Be aware that the order of variable definitions is important. You want IDEA to prompt you for the value of VAR first so it's result can be used as the value of VAR_NAME

Comment actions Permalink

Thank you so much for the quick and concise reply, it's really helped me to understand the live templates.

I've used your solution and combined with a macro that takes care of putting the output in the right place.

My productivity just increased a lot (over a lifetime anyway)...

Thanks so much! :D


Please sign in to leave a comment.