Surround with - use as param

is the following possible:

you select an expression, e.g.
node.getId()
and you want to turn it into
Long.toHexString(node.getId())

this means, I want to surround it with Long.toHexString($exp$)
then use the selection as param

So, optimally, it should work like this:
1. select "node.getId()"
2. invoke "surround with and use as param"
3. let me write code, for example Long.toHexString, allowing ctrl-space etc.
4. hit enter, then the previous selection is used as param

So, I think this could work generally, not only with a custom live template for the described case.

Is it reasonable? ;)

18 comments

On 2007-03-20 20:58:08 +0300, Michael Damberger
<michael.damberger@t-online.de> said:

is the following possible:

you select an expression, e.g.
node.getId()
and you want to turn it into
Long.toHexString(node.getId())

this means, I want to surround it with Long.toHexString($exp$)
then use the selection as param

So, optimally, it should work like this:
1. select "node.getId()"
2. invoke "surround with and use as param"
3. let me write code, for example Long.toHexString, allowing ctrl-space etc.
4. hit enter, then the previous selection is used as param

So, I think this could work generally, not only with a custom live
template for the described case.

Is it reasonable? ;)


Not sure if that's what you're asking for but have you tried 'Surround
with live template'? CtrlAltJ

0

No, that doesn't work because you want to replace with an arbitrary
expression, not a pre-defined one.

I have actually quite often wanted to do this and always resort to
putting the cursor before the expression to type my method name, then
manually adding the closing parenthesis. It seems quite painful when
you're very used to "introduce variable" to do what seems like the converse.

R

0

Sorry, I'm not understanding you... does a 'surround' template of the form:

$METHODNAME$($SELECTION$)$END$;

not work for you?

Maybe I'm not understanding how exactly you'd want it to work.
N.

Robert Gibson wrote:

No, that doesn't work because you want to replace with an arbitrary
expression, not a pre-defined one.

I have actually quite often wanted to do this and always resort to
putting the cursor before the expression to type my method name, then
manually adding the closing parenthesis. It seems quite painful when
you're very used to "introduce variable" to do what seems like the
converse.

R

0

Hi Nathan,
Yes it does, thanks! Time for me to revisit my "Idea for dummies" ;)
R

0

you could also use a macro which does the part after ctrl+space (adding braces & stuff)

0

I will evaluate your hints... thank you.

Right now I have another idea:
I just have to replace usage of Hashtable with a List in a class,
specifically two hashtable fields, which are used in half a dozen subclasses too.

-> this could be done by a refactoring, "replace hashtable with list",
or maybe even more generally "replace Dictionary with List"
The refactoring could let me choose which implementation of List to use.

Yes... I consider typing-laziness as an art-form ;)

0

how do you expect the refactoring to replace "map.put(a,b)"? list.add(new Object[]{a,b})?

0

You are right - this is not really a refactoring, or a "logic-modifying refactoring", don't know how this is officially called ;)

My problem, in that specific case, was that I needed to keep the ordering in my list,
therefore a hashtable/hashmap was not suited any more.

So, I would want a way to quickly, easily change the type of the field, let's call it
Hashtable myCollection;
into
List myCollection;

So maybe this is just a "change field type"-refactoring?

IDEA then could inspect the code, and tell me that we have a problem with
myCollection.put(a,b)
and
myCollection.get(a)

and allow me to "refactor" it like this
myCollection.put(a,b) -> myCollection.add(a)
myCollection.get(a) -> just keep, will have to be fixed by hand

In my opinion, this would ROCK! :)

I have the vague feeling, that it should be no problem to code this,
via IDEAs open APIs - but you know how it is... if I only had time... ;)

Or maybe someone coded something like this already???

0

On 2007-03-26 20:07:48 +0400, Michael Damberger
<michael.damberger@t-online.de> said:

You are right - this is not really a refactoring, or a "logic-modifying
refactoring", don't know how this is officially called ;)


Structural Search & Replace?

0

structural search & replace with a usable interface :)

0

Unfortunately, the old-school way of doing it would probably be best: Manually change the type of the field and then manually fix all the errors that subsequently light up from attempting to use Hashtable methods with a List object.

One of the tenets of refactoring is that there should be no change in behavior. The change you're proposing is a behavioral change -- List behaves differently to Hashtable. That sort of thing you have to do the old-fashioned way ;)

0

you could implement a logic changing refactoring like this:
+ change the type of field (or whatever you like to change)
+ idea lists all new errors, groups them by "type" (like "method add(theoldobjectfromadd)" not found)
+ you tell idea what to do with each error type (replace add with put(oldobject,null))

what i currently do in such situations is to use a dummy interface that implements all the old methods and gives me no compile errors. then i can use idea's refactoring to change the method signatures and names. finally i replace the interface by the class that i actually want to use.

0

will investigate your approach with the dummy interface...

I have yet another idea... even more esoteric ;)

Right now, I do a code modification where I need an additional parameter in a constructor, and the affected class has half a dozen of subclasses again...
Yes, sometimes you do CODE MODIFICATION, not refactor it :)

So, what I dream of is... I modify the first constructor in class A.
I introduce a new parameter of type int, and just pass this parameter to the superclass constructor (where I have already introduced this param, but that's another story).
So, now I do just the same with class A's next constructor.

How lame...

My dream is specifically to just say
"do what I just did - with all the other constructors and also with the constructors of all subclasses".

If at least two people would find this useful, than I volunteer to
start writing such a plugin - provided that it is possible with current open api.

Or maybe I start writing it even if nobody finds it useful :)

0

this is called "propagate parameters" and is already implemented. click on the constructor and press ctrl+f6
you just have to add the parameter at a central method or constructor and then progagate it up the call hierachy as long as you like. the magic button is hidden in the upper right

if you have no central point, create a dummy method with the new parameter and add it to your constrcutors temporarily

Message was edited by:
HamsterofDeath

0

No, propagate parameters is something else.
But thanks for the hint, I did not know this feature :)

Prop-Params adds the new parameter to methods which call the modified constructor.
Nice, but it does not add the parameter to the other constructors of modified class A, nor to overriden constructors in subclasses of A.

What my "magic plugin" should do is
a) recognize modifications as structural changes, e.g. "you introduced a new param to a constructor"
b) allow to apply this modifications structurally to other methods/classes,
e.g. introduce new param of type int, default val = "xyz" to all other constructors of A, and to all constructors of its subclasses

This is some kind of "struct search/replace by example", which has already been suggested by me in 2004 ;)
see http://www.intellij.net/forums/thread.jspa?messageID=3420478&#3420478

0

"Prop-Params adds the new parameter to methods which call the modified constructor."
exactly - and if you call the super constructor from all subclasses (which you should be doing, otherwise it makes no sense not to do every change manually), propagate params will solve your problem.

0

Well... ok, you are partly right.

PP does indeed propagate to overridden constructors, but...

1. I have to identify the overridden constructors of A within a large list of methods
and in my case there is also another constructor in the list (let's say of class B),
which is not in the same inheritance hierarchy, but this B constructor constructs instances of A ;)

Nevertheless, this list is really useful, because it certainly makes sense
to propagate also to some methods.

In my special case a button "select all overriden constructors of A" would help,
then union it with "select all other constructors in A, and the overridden ones"
Structural candidate selection for refactorings? ;)

2. problem: It does not at all repeat the parameter introduction to the other constructors of A,
which especially is a problem for those constructors in A's subclasses that override the first one, BUT call different super-constructors.

Yes... I know what you will say now: "there's something wrong with your constructor hierarchy" :)

Message was edited by:
Michael Damberger (typos)

0

there's something wrong with your constructor hierarchy. ;)

i just realized that i really got used to code/design everything in a way which makes it easy to refactor and even debug it with idea. curious.

0

Please sign in to leave a comment.