[ANN] Smart Introduce Variable Plugin


Inspired by the discussion here: http://www.intellij.net/tracker/idea/viewSCR?publicId=14308
I've decided to write a little and very simple plugin that can be used to evaluate the proposed
functionality and to allow everybody who is interested to make up their minds about this feature.

The plugin intercepts the Introduce Variable action if there is no selection and tries to select the
most complete expression, stopping at parameter lists in method calls. Simple reference expressions
like foo, Class.FIELD and literals are ignored in argument lists and the search for the
largest
expression continues.

I don't know yet if it's useful... I'll have to use it for some time to make up my mind myself.

Examples (| = caret, resulting selection in []):
JFrame frame = new JFrame(["Greatest app in world - " + project.getName()]|);

return ;|

final PsiElement startElement = psiFile.findElementAt(|);

final Project project = (Project)dataContext.getData(DataConstants.PROJECT;


The plugin can be temporarily disabled with Options->Smart Introduce Variable

NOTE: The plugin was developed to work with IDEA build #873 and uses the unofficial PSI-API.
The plugin isn't heavily tested. If you are using it and experience crashes with Introduce
Variable, please check the stack trace first before submitting an SCR to the tracker.

Have fun.
Sascha





Attachment(s):
smarterIntroduceVariable.jar
8 comments
Comment actions Permalink

Tried it, liked it!

I have a suggestion that will make it even more flexible.

In many cases there are several options, for example take this grotesque
example:
foo1(foo2(foo3()+1));

If the cursor is inside the foo3 call, then there are several possible
options that could be in another drop down box instead of assuming that the
biggest expression is the right one.

Amnon

"Sascha Weinreuter" <sascha.weinreuter@cit.de> wrote in message
news:bfmcij$54d$1@is.intellij.net...
>

Inspired by the discussion here:

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

I've decided to write a little and very simple plugin that can be used to

evaluate the proposed

functionality and to allow everybody who is interested to make up their

minds about this feature.
>

The plugin intercepts the Introduce Variable action if there is no

selection and tries to select the

most complete expression, stopping at parameter lists in method calls.

Simple reference expressions

like foo, Class.FIELD and literals are ignored in argument lists

and the search for the

largest
expression continues.

>

I don't know yet if it's useful... I'll have to use it for some time to

make up my mind myself.
>

Examples (| = caret, resulting selection in []):
JFrame frame = new JFrame(["Greatest app in world - " +

project.getName()]|);
>

return ;|

>

final PsiElement startElement =

psiFile.findElementAt(|);
>

final Project project =

(Project)dataContext.getData(DataConstants.PROJECT;
>
>

The plugin can be temporarily disabled with Options->Smart Introduce

Variable
>

NOTE: The plugin was developed to work with IDEA build #873 and uses the

unofficial PSI-API.

The plugin isn't heavily tested. If you are using it and experience

crashes with Introduce

Variable, please check the stack trace first before submitting an SCR to

the tracker.
>

Have fun.
Sascha

>
>
>


0
Comment actions Permalink


"Amnon I. Govrin" <agovrin@freshwater.com> wrote:

Tried it, liked it!


Cool. Thanks.

I have a suggestion that will make it even more flexible.

>

In many cases there are several options, for example take this grotesque
example:
foo1(foo2(foo3()+1));

>

If the cursor is inside the foo3 call, then there are several possible
options that could be in another drop down box instead of assuming that the
biggest expression is the right one.


Hmm, sounds good and like some work. It should be possible to show a list
of possible expressions before the 'native' refactoring dialog is shown. But this
will then require at least one additional keypress/click. If that was integrated
into the 'native' dialog(s) it would be more convenient to use - but this is impossible
for a plugin to achieve, I guess.

Sascha


0
Comment actions Permalink

Based on the feedback here:
http://www.intellij.net/forums/thread.jsp?forum=13&thread=35713&message=517753#517753
I've attached a slightly new version that behaves a little better when the cursor is
at a comma in a parameter list.

I still think that it doesn't make much sense (apart from consistency) to select simple reference
expressions like

final Project project = (Project)dataContext.getData(DataConstants.PROJECT);

because one usually doesn't want to introduce a variable for another one.

Comments?

Sascha





Attachment(s):
smarterIntroduceVariable.jar
0
Comment actions Permalink

I like it!
And I have a suggestion for improvement:-) This example (| is caret)
still makes IDEA complain "Selected expression has void type":

System.out.println("foo");|
System.out.println("f|oo");

I think the plugin should select the literal "foo".

Thanks for your plugin,
Bas

Sascha Weinreuter wrote:

Based on the feedback here:
http://www.intellij.net/forums/thread.jsp?forum=13&thread=35713&message=517753#517753
I've attached a slightly new version that behaves a little better when the cursor is
at a comma in a parameter list.

I still think that it doesn't make much sense (apart from consistency) to select simple reference
expressions like

final Project project = (Project)dataContext.getData(DataConstants.PROJECT);

because one usually doesn't want to introduce a variable for another one.

Comments?

Sascha


0
Comment actions Permalink

Is this on the wiki? My work firewall won't let me access the attached jar.
:(

"Sascha Weinreuter" <sascha.weinreuter@cit.de> wrote in message
news:bfmcij$54d$1@is.intellij.net...
>

Inspired by the discussion here:

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

I've decided to write a little and very simple plugin that can be used to

evaluate the proposed

functionality and to allow everybody who is interested to make up their

minds about this feature.
>

The plugin intercepts the Introduce Variable action if there is no

selection and tries to select the

most complete expression, stopping at parameter lists in method calls.

Simple reference expressions

like foo, Class.FIELD and literals are ignored in argument lists

and the search for the

largest
expression continues.

>

I don't know yet if it's useful... I'll have to use it for some time to

make up my mind myself.
>

Examples (| = caret, resulting selection in []):
JFrame frame = new JFrame(["Greatest app in world - " +

project.getName()]|);
>

return ;|

>

final PsiElement startElement =

psiFile.findElementAt(|);
>

final Project project =

(Project)dataContext.getData(DataConstants.PROJECT;
>
>

The plugin can be temporarily disabled with Options->Smart Introduce

Variable
>

NOTE: The plugin was developed to work with IDEA build #873 and uses the

unofficial PSI-API.

The plugin isn't heavily tested. If you are using it and experience

crashes with Introduce

Variable, please check the stack trace first before submitting an SCR to

the tracker.
>

Have fun.
Sascha

>
>
>


0
Comment actions Permalink

Is this on the wiki? My work firewall won't let me access the attached jar.
:(


Sorry, not yet. In the meantime, you should be able to get it from here:
http://www.intellij.net/forums/thread.jsp?forum=18&thread=36101&tstart=0&trange=15#518089

Sorry for inconvenience.
Sascha


0
Comment actions Permalink

What about replacing the native dialog completely by an improved one and not
even showing the native one?

"Sascha Weinreuter" <sascha.weinreuter@cit.de> wrote in message
news:bfo5na$e3r$1@is.intellij.net...
>

"Amnon I. Govrin" <agovrin@freshwater.com> wrote:

Tried it, liked it!

>

Cool. Thanks.

>

I have a suggestion that will make it even more flexible.

>

In many cases there are several options, for example take this grotesque
example:
foo1(foo2(foo3()+1));

>

If the cursor is inside the foo3 call, then there are several possible
options that could be in another drop down box instead of assuming that

the

biggest expression is the right one.

>

Hmm, sounds good and like some work. It should be possible to show a list
of possible expressions before the 'native' refactoring dialog is shown.

But this

will then require at least one additional keypress/click. If that was

integrated

into the 'native' dialog(s) it would be more convenient to use - but this

is impossible

for a plugin to achieve, I guess.

>

Sascha

>
>


0
Comment actions Permalink


"Amnon I. Govrin" <agovrin@freshwater.com> wrote:

What about replacing the native dialog completely by an improved one and not
even showing the native one?


Hmm, I'm afraid that won't work because then the plugin needed be able to invoke IDEA's
inner refactoring code, which it isn't. The other possibility would be to re-write the whole
thing, which clearly wouldn't be worth the effort I think.

I'm currently working on a version that lets the user select from different possible expressions
before the dialog is shown. I'll try to get it finished and posted on the Wiki by friday evening
(MEST).

Sascha


0

Please sign in to leave a comment.