if statement - Live Template cursor position
Hi Scott,
I have some questions about the design of the cursor position of the if statement Live Template as below.
https://www.illuminatedcloud.com/features/code-completion-and-live-templates/live-templates
When I typed “iter” and “if”, I got the code below.
for (Account account : accounts)
{
if (true)
{
}
}
Question 1: When I completed ‘true’ at line 3 and pressed Tab key, the cursor didn’t move to line 5, instead, it inserted 4 spaces. Was it by design?
Question 2: When I completed ‘true’ at line 3 and pressed the Down Arrow key twice, the cursor rested at 5:1. Is it possible to automatically insert 8 spaces so that after pressing two Down Arrow key twice, the cursor will rest at 5:9? (My walkaround was that after I complete ‘true’ at line 3, I press Ctrl-Shift-Enter, and press Delete key. Was my walkaround the expected design?)
Thanks for this excellent IC2!!
Comments (4)
-
repo owner -
repo owner - changed status to resolved
Resolving as this is working as designed at present, and multiple options have been offered for how to get the desired behavior.
-
reporter Thank you so much, Scott! I will use the “hit the End key once“ option which is very good. I like this simple way of typing.
-
reporter I was not able to understand how to use
$SELECTION$
until I saw the video link in #2045 and Ctrl+Alt+J. Thanks for the great video! - Log in to comment
Hi. The behavior you're seeing is because when the IDE inserts the live template, it also formats it. By default the formatter eliminates trailing whitespace from a line, and in this case line 5 is all whitespace if there was no previous selection. Here's the actual live template definition:
Note that
$SELECTION$
is indented, and if there was a selection that you wanted to surround with anif
statement, it would be indented according to the code style settings. But without one the whitespace is removed leaving the caret in the first column when you cursor down.There are two ways to get to the correct indentation easily here. One is to cursor down to that line and then hit the End key once which will take you immediately to the beginning/end of the line according to the correct indentation at that point. The second is to use the statement completion feature and type Ctrl+Shift+Enter after you finish the condition expression (in this example,
true
) which will open a new line in theif
statement's block at the correct location. You'll end up with one extra line in that block, but that's easy enough to remove if you want.Alternatively you could edit the
if
live template for Apex to remove the statement block altogether, e.g.,and after you type the condition expression use statement completion which would add the statement block with the caret on the one empty line in between the braces properly indented. The downside is that you lose the "surround with" ability of the current live template.
Let me know if that doesn't help.