-->

IntelliJ live template for logging a selected vari

2019-07-06 04:54发布

问题:

I would like to add a new line right after the line where I selected a variable name. This line should than contain the selection.

Example:

var variable = getValue();

I select the word "variable", press CMD+J to apply a live template or CMD+Alt+T to surround the selection with a live template and get something like this:

var value = getValue();
console.log('[value]', value);

回答1:

This doesn't work exactly as you expect, but the result is the same. Create a new Live Template (Settings → Editor → Live Templates) and add an abbreviation (e.g. logv). Set the applicable context to Javascript. Use this as a template:

console.log("$EXPR_COPY$ = " + $EXPR$);

Now click the Edit variables button and copy this:

The order is important: first EXPR, then EXPR_COPY.

Now you can type logv (the abbreviation you chose) to paste the template. You will have to enter the variable name (using autocompletion), but the variable name will be copied into the string.