Live templates and indentation

Hi,

I have the following class

public class Test {

....


public void methodName(String s) {
tni|
}
}

And the following Live Template:

if ($param$ == null) {
throw new IllegalArgumentException("$COMMENT$");
}
$END$

Where $param$ is set to be valueOfType("")

This is intended to minize the amount of typing for error checking. This is
expanded to

public class Test {

....


public void methodName(String s) {
if (s == null) {
throw new IllegalArgumentException("I typed my message here");
}
|
}
}

Is there a way to magically place the cursor at the right place?

Best regards,

Dirk Dittert

11 comments
Comment actions Permalink

Is there a way to magically place the cursor at the right place?


$END$



0
Comment actions Permalink

Carlos Costa e Silva wrote:

$END$


That is already in my macro. It places the cursor at the beginning of the
correct line, not at the indented location.

Best regrads,

Dirk Dittert

0
Comment actions Permalink

Hi,

$END$ indicates where the cursor will be placed.
So you need to put it between the quotes ("$END$")

Hope this helps.
Best Regards,
M.

0
Comment actions Permalink

Mohamed Hussein wrote:

$END$ indicates where the cursor will be placed.
So you need to put it between the quotes ("$END$")


What I wanted to get was:

public class Test {

....


public void methodName(String s) {
if (s == null) {
throw new IllegalArgumentException("I typed my message here");
}
| <-- cursor
}
}

I deliberately didn't do this. The way the template is setup I can just
enter the comment and press enter to jump below the if block. My problem is
that I didn't know how to make the cursor jump to the correct indented
position. I solved it for now by just indenting the $END$ keywords as much
as the usual indent would be. I'll probably file an SCR for the wrong
indentation later on.

Best regards,

Dirk Dittert

0
Comment actions Permalink

I have some templates like yours. For example, I have one called "try" that looks like this:

Notice that the $END$ tag is there, with NO indentation. I tried it both ways (indented and not) and in both cases the cursor was placed like so:

The only thing I can recommend you check is the "Options" panel of the "Edit Live Template" dialog. There is an option called "Reformat according to style" and I think this may be what you need.

This even works as I expect:

I found out that the spaces around the $END$ were important; if they were missing, the cursor would NOT go where I'd expect, but be stuck next to the open or close brace.

HTH,

-sms

0
Comment actions Permalink

Dirk Dittert wrote:

I solved it for now by just indenting the $END$ keywords as much
as the usual indent would be.


Another temporary workaround is to use ";$END" on the last line (and
make sure you turn on "reformat according to style"). Then when you tab
to the end the semicolon is indented properly, and you can press
backspace to delete it. Your workaround might be less intrusive if you
normally always have the same indent level, though.

0
Comment actions Permalink

Scott Sirovy wrote:

I have some templates like yours. For example, I have one called "try"

that looks like this:


Getting a bit offtopic for this thread, but I have to ask: you do know
about the "Surround with..." feature (CtrlAltT), don't you?

Vil.
--
Vilya Harvey, Consultant
vilya.harvey@digitalsteps.com / digital steps /
(W) +44 (0)1483 469 480
(M) +44 (0)7816 678 457 http://www.digitalsteps.com/

Disclaimer

This e-mail and any attachments may be confidential and/or legally
privileged. If you have received this email and you are not a named
addressee, please inform the sender at Digital Steps Ltd by phone on
+44 (0)1483 469 480 or by reply email and then delete the email from
your system. If you are not a named addressee you must not use,
disclose, distribute, copy, print or rely on this email. Although
Digital Steps Ltd routinely screens for viruses, addressees should
check this email and any attachments for viruses. Digital Steps Ltd
makes no representation or warranty as to the absence of viruses in this
email or any attachments.


0
Comment actions Permalink

Scott Sirovy wrote:

I have some templates like yours. For example, I have one

> called "try" that looks like this:

 try {
> $END$
> }]]>

Notice that the $END$ tag is there, with NO indentation. I tried it

> both ways (indented and not) and in both cases the cursor was placed
> like so:

But in your case $END$ is followed by a closing brace, and then it
apparently works the way it should. Try his example (I just did) and
you'll see that you don't get the expected indentation. Unless I did
something wrong, of course, but I did try with and without "reformat
according to style".

0
Comment actions Permalink

I'll probably file an SCR for the wrong indentation later on.


I think that would make sense.

--
Valentin Kipiatkov
JetBrains, Inc
http://www.intellij.com
"Develop with pleasure!"


"Dirk Dittert" <dittert@despammed.com> wrote in message
news:ats9gf$9jc$1@is.intellij.net...

Mohamed Hussein wrote:

>
>

$END$ indicates where the cursor will be placed.
So you need to put it between the quotes ("$END$")

>

What I wanted to get was:

>

public class Test {

>

....

>
>

public void methodName(String s) {
if (s == null) {
throw new IllegalArgumentException("I typed my message here");
}
| <-- cursor
}
}

>

I deliberately didn't do this. The way the template is setup I can just
enter the comment and press enter to jump below the if block. My problem

is

that I didn't know how to make the cursor jump to the correct indented
position. I solved it for now by just indenting the $END$ keywords as much
as the usual indent would be. I'll probably file an SCR for the wrong
indentation later on.

>

Best regards,

>

Dirk Dittert



0
Comment actions Permalink

Vilya Harvey wrote:
Getting a bit offtopic for this thread, but I have to
ask: you do know
about the "Surround with..." feature (CtrlAltT),
don't you?


Sure, and I use it frequently for many things. However, it seems more natural to create new code by hitting four quick keystrokes ("try" and tab) rather then to type all my code, highlight it, and then do the (somewhat) awkward CTRLALTT gymnastics.

If I'm on a blank line ready to type something that I know will need a try block around it, CTRLALTT will simply highlight the line but NOT offer me any options let alone the one I want which is to "wrap with try".

0
Comment actions Permalink

Fair enough. Just making sure. :)

Vil.

"Scott Sirovy" <ssirovy@deephavenfunds.com> wrote in message
news:6939540.1040305926096.JavaMail.jrun@is.intellij.net...

Vilya Harvey wrote:
Getting a bit offtopic for this thread, but I have to
ask: you do know
about the "Surround with..." feature (CtrlAltT),
don't you?

>

Sure, and I use it frequently for many things. However, it seems more

natural to create new code by hitting four quick keystrokes ("try" and tab)
rather then to type all my code, highlight it, and then do the (somewhat)
awkward CTRLALTT gymnastics.
>

If I'm on a blank line ready to type something that I know will need a try

block around it, CTRLALTT will simply highlight the line but NOT offer me
any options let alone the one I want which is to "wrap with try".


0

Please sign in to leave a comment.