IntelliJ howto questions

I have three things I want to do with intellij. I've been poking around for a while, and can't seem to find if they're possible. I thought I'd post them here before filing tracker/bug requests. If anyone can answer them, I'd appreciate it...

1. Fully specified class names on Surround With Try-Catch.

If I highlight a statement that throws an exception, and invoke the Surround With Try-Catch action, unless I've previously imported the exception, it puts the fully qualified class name in the catch block:

I'd like it to just import the exception by default, and only qualify it if there's a conflict...

2. Brace-less if statements.

This is more of a code-style issue. I'll occasionally see code like this:

I'd like it to become this when I invoke Layout Code:

Can't seem to figure out how to do this.

3. Move variable declaration out of loop/statement block.

Probably an intention. I'll have code like this:

I'd like to move the variable declaration outside of the loop, like this:

Again, any answers on how to do this would be appreciated. Otherwise, I'll probably be posting requests...

0
13 comments
Avatar
Permanently deleted user

Peter Morelli wrote:

> 2. Brace-less if statements.

option 1: define (and use) a live template like
{ $SELECTION$ }

option 2: the IntentionPowerPack plugin
http://www.intellij.org/twiki/bin/view/Main/IntentionPowerPack


> 3. Move variable declaration out of loop/statement block.

You can use the LineUpAndDownMover plugin. It was designed for this
purpose, and has easy shortcuts ().
http://www.intellij.org/twiki/bin/view/Main/LineUpAndDownMoverPlugin


Alain


0
Avatar
Permanently deleted user

1.

I'd like it to just import the exception by default, and only qualify it

if there's a conflict...

Seems like a bug. Search/post a bug report in the tracker.


0
Avatar
Permanently deleted user

On Tue, 17 Jun 2003 16:17:45 +0000 (UTC), Peter Morelli
<jiveadmin@jetbrains.com> wrote:

I have three things I want to do with intellij. I've been poking around
for a while, and can't seem to find if they're possible. I thought I'd
post them here before filing tracker/bug requests. If anyone can answer
them, I'd appreciate it...

>

1. Fully specified class names on Surround With Try-Catch.

>

If I highlight a statement that throws an exception, and invoke the
Surround With Try-Catch action, unless I've previously imported the
exception, it puts the fully qualified class name in the catch block:

 SimpleDateFormat df = new SimpleDateFormat("MMddyy");
>
> try
> {
> dataObj.setDate(df.parse(postDateStr));
> }
> catch (java.text.ParseException e)
> {
> e.printStackTrace();
> }
> ]]>

I'd like it to just import the exception by default, and only qualify it
if there's a conflict...


ummm, my Idea works as you wish. I'm using the default code style. I
suppose that you have
touch some options from the IDE settings / Code Style / Import tab.

>

2. Brace-less if statements.

>

This is more of a code-style issue. I'll occasionally see code like this:

 if (true)
> System.out.println("true");
> ]]>

I'd like it to become this when I invoke Layout Code:

 if (true)
> {
> System.out.println("true");
> }
> ]]>

Can't seem to figure out how to do this.


I think that Idea cannot do it. You should fill a request

>

3. Move variable declaration out of loop/statement block.

>

Probably an intention. I'll have code like this:

 for (Iterator iter = details.iterator(); iter.hasNext();)
> {     Element detail = (Element)iter.next();
>
> // do  work with detail
> }
> ]]>

I'd like to move the variable declaration outside of the loop, like this:

 Element detail; // = null; (maybe declare null automatically)
>
> for (Iterator iter = details.iterator(); iter.hasNext();)
> {     detail = (Element)iter.next();
>
> // do  work with detail
> }
> ]]>

Again, any answers on how to do this would be appreciated. Otherwise,
I'll probably be posting requests...

>
>
You cannot do it, but i you place the cursor at: Element detail ]]>=
(Element)iter.next();
you have the "Split into declaration && assignamet" intention. Then just
move the assignament out of the loop.

Check for the
http://www.intellij.org/twiki/bin/view/Main/LineUpAndDownMoverPlugin for a
quicker way to do this.

hope it helps;


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

0
Avatar
Permanently deleted user

2. Brace-less if statements.

This is more of a code-style issue. I'll occasionally
see code like this:

 if (true)
>      System.out.println("true");
> ]]>

I'd like it to become this when I invoke Layout
Code:

 if (true)
> {
>      System.out.println("true");
> }
> ]]>

Can't seem to figure out how to do this.



See http://www.intellij.net/tracker/idea/viewSCR?publicId=650

0
Avatar
Permanently deleted user

2. Brace-less if statements.


Other options

1) If my pleading with JetBrains to open the inspection API works, I'm committed to shipping a plugin which will include an inspection for such statements (among about a hundred other inspections). If the opened API is rich enough, I'll include a "Fix it" link. Go to the thread "An Immodest Proposal" to offer your support.

2) Vote for the old, old, old, tracker item http://www.intellij.net/tracker/idea/viewSCR?publicId=650, which would put braces wrapping the code formatting, where it belongs.

3. Move variable declaration out of loop/statement block



This could certainly be made an intention, but I'm wondering what the real use case is behind it. I've played around with a "move variable declaration to first use" intention, which could move variable declarations into loops, but I'm not seeing the much of a reason to move them out, on a regular basis.

0
Avatar
Permanently deleted user

ummm, my Idea works as you wish. I'm using the default code style. I
suppose that you have
touch some options from the IDE settings / Code Style / Import tab.


Hmm. It doesn't work here also (#823).

Posted a new tracker request:
http://www.intellij.net/tracker/idea/viewSCR?publicId=13416


0
Avatar
Permanently deleted user

This could certainly be made an intention, but I'm wondering what the real

use case is behind it. I've played around with a "move variable declaration
to first use" intention, which could move variable declarations into
loops, but I'm not seeing the much of a reason to move them out, on a
regular basis.

This jus't shows you aren't lazy enough ;)

Generate variable (inside loop), move out of loop for use after loop -
usually flags and counters, sometimes search result vars.


0
Avatar
Permanently deleted user

>Generate variable (inside loop), move out of loop for use after loop -
>usually flags and counters, sometimes search result vars.

Gah, that is impressively lazy. Fair enough. Unless someone beats me to it, both this and the "move local variable declaration to first use" intention will be in a coming version of IPP.

0
Avatar
Permanently deleted user

Dave Griffith wrote:
> This could certainly be made an intention, but I'm wondering what
the real use case is behind it.
> I've played around with a "move variable declaration to first use"
> intention, which could move variable declarations into
> loops, but I'm not seeing the much of a reason to move them out, on
> a regular basis.

A somewhat related intention, with more general use is :
"change the scope of a variable"

There are some ideas and UI suggestions in the 3rd message, at
http://www.intellij.net/tracker/idea/viewSCR?publicId=7633

Alain

0
Avatar
Permanently deleted user

Thanks. Your IPP is great, btw. If people didn't have the answers I needed, at least for 3, I was going to email you with a request. Guess I don't need to now. ;o)

--pete

0
Avatar
Permanently deleted user

> 2. Brace-less if statements.

option 1: define (and use) a live template like
{ $SELECTION$ }


Not ideal, since it's usually someone else's code. Sort of a pain in the ass to do them all.


option 2: the IntentionPowerPack plugin



http://www.intellij.org/twiki/bin/view/Main/Intention
owerPack


I've seen this, and have just installed it. I think this is what I want:

Wrap branches of 'if' statement with {}

However, you still have the manual fix problem. It'd be more suited to an inspection, or ideally, code style. Someone posted a tracker for this (code style), so I went and voted for it.

Thanks for your suggestions.

3. Move variable declaration out of

of loop/statement block.

You can use the LineUpAndDownMover plugin. It was
designed for this
purpose, and has easy shortcuts ().



http://www.intellij.org/twiki/bin/view/Main/LineUpAnd
ownMoverPlugin


Cool plugin. While it's still a little more work than I'd like for this issue(Intellij spoils me ;o), I'd find this useful in other situations, too.

0
Avatar
Permanently deleted user

If you have a lot of code to rewrite, use Jalopy, it will allow you to
convert "if (expr) statement" in "if (expr) ".


"Peter Morelli" <jiveadmin@jetbrains.com> a ?crit dans le message de
news:10999215.1055874830981.JavaMail.itn@is.intellij.net...
>

> 2. Brace-less if statements.

>

option 1: define (and use) a live template like
{ $SELECTION$ }

>

Not ideal, since it's usually someone else's code. Sort of a pain in the

ass to do them all.
>

>

option 2: the IntentionPowerPack plugin

>
>
>

http://www.intellij.org/twiki/bin/view/Main/Intention
owerPack

>

>

I've seen this, and have just installed it. I think this is what I want:

>

Wrap branches of 'if' statement with {}

>

However, you still have the manual fix problem. It'd be more suited to an

inspection, or ideally, code style. Someone posted a tracker for this (code
style), so I went and voted for it.
>

Thanks for your suggestions.

>

>

3. Move variable declaration out of

of loop/statement block.

>

You can use the LineUpAndDownMover plugin. It was
designed for this
purpose, and has easy shortcuts ().

>
>
>

http://www.intellij.org/twiki/bin/view/Main/LineUpAnd
ownMoverPlugin

>

Cool plugin. While it's still a little more work than I'd like for this

issue(Intellij spoils me ;o), I'd find this useful in other situations, too.
>


0
Avatar
Permanently deleted user

2. Brace-less if statements.

This is more of a code-style issue. I'll occasionally
see code like this:

 if (true)
>      System.out.println("true");
> ]]>

I'd like it to become this when I invoke Layout
Code:

 if (true)
> {
>      System.out.println("true");
> }
> ]]>

Can't seem to figure out how to do this.


I just posted ReformatPlugin-0.5 ( http://www.intellij.org/twiki/bin/view/Main/ReformatPlugin , see Wrap Single Statements option).

Example:

0

Please sign in to leave a comment.