IntelliJ, hotkey using the code under selection as a parameter?
One of those little things that tends to slow me down when coding…
How do you refactor the code at the current location to be a parameter for a new method call? Your cursor is |
var foo = “|bar”;
Now foo should be a file, so I thought:
var foo = new File(“bar”);|
How do I type “new File(” without moving the cursor and get the closing parenthesis and semicolon generated by IntelliJ? Are Ctrl-W, cut, type, paste the fastest path?
Solution
You may want to try to complete the current statement operation (Cmd+Shift+Enter on Mac). It completes the current statement by adding any closing parenthesis and semicolon as needed, regardless of where the text cursor is located in the statement. You just need to type new File (
and ) in front of it;
to add it for you without moving the text cursor.
Another option might be to add your own live templates. For example, something like $END$($SELECTION$).
I named it “Surround with method call” and gave it the abbreviation M
. Use it by first selecting a string constant and then calling the surround with a live template. It adds parentheses and positions the text cursor in front of the opening parenthesis, and you can type new File
.