Refactoring API

Hi All,

as you may have noticed, there is now a limited form of Refactoring API
availble for your disposal in the latest Pallada builds.

API for refactorings come in two flavours:
1) com.intellij.refactoring.Refactoring, its descendants and
com.intellij.refactoring.RefactoringFactory give you ability
to programmaticaly invoke all "big" (that is, involving usage search)
IDEA refactorings. Basically you just supply the parameters and they do
the work.
(Currently Introduce Parameter, Change Method/Class Signature andf
Replace Inheritance With Delegation are missing - coming soon).
2) com.intellij.refactoring.RefactoringActionHandler and friends lets you
invoke refactoring actions, with all the dialogs and UI that ensue when
click on a menu item in IDEA 'Refactoring' menu.

I admit that this API is definitely incomplete, but this is something
already, isn't it?

Your comments and suggestions are most welcome.

Friendly,
Dmitry

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

15 comments
Comment actions Permalink

Hi,

it looks like the createIntroduceVariableHandler() and
createIntroduceConstantHandler() are missing. I assume this is
also "coming soon", right? :)

Would it be possible to make the RefactoringActionHandler return
a boolean flag that indicates if the action has been completed
successfully? This would make it easier to do a pre-refactoring
modification to a document that can be undone in case the user
cancels the action or it doesn't complete for some reason.

Sascha


Dmitry Lomov (JetBrains) wrote:

Hi All,

as you may have noticed, there is now a limited form of Refactoring API
availble for your disposal in the latest Pallada builds.

API for refactorings come in two flavours:
1) com.intellij.refactoring.Refactoring, its descendants and
com.intellij.refactoring.RefactoringFactory give you ability
to programmaticaly invoke all "big" (that is, involving usage search)
IDEA refactorings. Basically you just supply the parameters and they do
the work.
(Currently Introduce Parameter, Change Method/Class Signature andf
Replace Inheritance With Delegation are missing - coming soon).
2) com.intellij.refactoring.RefactoringActionHandler and friends lets you
invoke refactoring actions, with all the dialogs and UI that ensue when
click on a menu item in IDEA 'Refactoring' menu.

I admit that this API is definitely incomplete, but this is something
already, isn't it?

Your comments and suggestions are most welcome.

Friendly,
Dmitry

0
Comment actions Permalink

Sascha Weinreuter wrote:

Hi,

it looks like the createIntroduceVariableHandler() and
createIntroduceConstantHandler() are missing. I assume this is
also "coming soon", right? :)


Will fix. Thanks.


Would it be possible to make the RefactoringActionHandler return
a boolean flag that indicates if the action has been completed
successfully? This would make it easier to do a pre-refactoring
modification to a document that can be undone in case the user
cancels the action or it doesn't complete for some reason.


I'll see what I can do about this.

Friendly,
Dmitry


Sascha


Dmitry Lomov (JetBrains) wrote:

>> Hi All,
>>
>> as you may have noticed, there is now a limited form of Refactoring API
>> availble for your disposal in the latest Pallada builds.
>>
>> API for refactorings come in two flavours:
>> 1) com.intellij.refactoring.Refactoring, its descendants and
>> com.intellij.refactoring.RefactoringFactory give you ability
>> to programmaticaly invoke all "big" (that is, involving usage search)
>> IDEA refactorings. Basically you just supply the parameters and they
>> do the work.
>> (Currently Introduce Parameter, Change Method/Class Signature andf
>> Replace Inheritance With Delegation are missing - coming soon).
>> 2) com.intellij.refactoring.RefactoringActionHandler and friends lets you
>> invoke refactoring actions, with all the dialogs and UI that ensue
>> when click on a menu item in IDEA 'Refactoring' menu.
>>
>> I admit that this API is definitely incomplete, but this is something
>> already, isn't it?
>>
>> Your comments and suggestions are most welcome.
>>
>> Friendly,
>> Dmitry
>>

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

I think this is a great start! I remember a certain conversation we had 2 years ago... ;)
For the ability to execute refactorings programmatically seems to meet my requirement.
What about more refactoring listeners and extendable refactorings (I want to add my unit test renaming to the default method/class renaming for example)?

Jacques

0
Comment actions Permalink

Hmm, I have a problem that's a little OT but still applies to the subject:
My SmartIntroduce plugin can modify the code before the refactoring handler
e.g. for IntroduceField is being invoked. Something like "A string with a "
becomes "A string with a " + ["message"].

The modification and the invocation of the handler are done in the same
runnable that's being executed by CommandManager.executeCommand() to achieve
that both the refactoring and the pre-modification can be undone in one step
rather than two. This works, but only if nothing is typed into the dialog that
pops up. However, if the default-suggestion for the field-name is edited, one
undo just reverts the refactoring and not the modification before.

Is this combined Undo together with the refactoring handlers possible, and
if it is, how? Any hint is appreciated.

Sascha

0
Comment actions Permalink

Jacques Morel wrote:

I think this is a great start! I remember a certain conversation we had 2
years ago... ;) For the ability to execute refactorings programmatically
seems to meet my requirement. What about more refactoring listeners and
extendable refactorings (I want to add my unit test renaming to the
default method/class renaming for example)?


Mythical morning line-up mantra applies :)

Cheers,
Dmitry
--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

LOL! I did not even entertain the possibility you could add it for 4.1 ;) But I would have gladly taken it.
Certainly I would take it for 5.0 though. Do you think that might be possible?

Jacques

0
Comment actions Permalink

Jacques Morel wrote:

LOL! I did not even entertain the possibility you could add it for 4.1 ;)
But I would have gladly taken it. Certainly I would take it for 5.0
though. Do you think that might be possible?


Yes, I think so.

Cheers,
Dmitry
--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Sascha Weinreuter wrote:

Hmm, I have a problem that's a little OT but still applies to the subject:
My SmartIntroduce plugin can modify the code before the refactoring
handler e.g. for IntroduceField is being invoked. Something like "A string
with a " becomes "A string with a " + ["message"].

The modification and the invocation of the handler are done in the same
runnable that's being executed by CommandManager.executeCommand() to
achieve that both the refactoring and the pre-modification can be undone
in one step rather than two. This works, but only if nothing is typed into
the dialog that pops up. However, if the default-suggestion for the
field-name is edited, one undo just reverts the refactoring and not the
modification before.

Is this combined Undo together with the refactoring handlers possible, and
if it is, how? Any hint is appreciated.


Tricky. I'll see what I can do :)

Friendly,
Dmitry

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Another problem we talked about some time back is the fact that Undoing refactorings cannot be track down. So if I want to listen to refactorings, remember them and redo them later (my old batch refactoring idea ;) I won't know if one of them got undone.
I suppose this another mantra candidate but I would like to get your take on how feasible this is. BTW when are you going to start gathering requirements for 5.0 and release 4.1?

0
Comment actions Permalink

The rename refactoring entry points are a little weird.
If I want to rename multiple elements in one shot I have to do create the refactoring with the first element and add the others with addElement. This is rather suboptimal.
What about having an createRename() or a createRename(Pair<PsiElement, String>[])

Jacques

0
Comment actions Permalink

Here is another problem.
I add multiple elements to a rename refactoring. These elements might be in different files. These files might be read only (checked in VCS). The current refactoring preview doesn't provide any way to change the writability of these files.
The preview seems to be the best place to be able to make the refactoring targets writable since we are doing this already for the usages. Optimally we should be able to select the top level node that lists the refactoring targets and invoke a VCS Check Out action to make them all writable like you can on the top level usage node.

Jacques

0
Comment actions Permalink

On the topic of undo, in the JUnit test plugin after a method is renamed I renamed its tests. Unfortunately if I was doing this from the test class and I want to undo I get the message "Cannot undo, Some files were modified".
Intuitivement I would think that the refactoring listeners would be invoked inside the same action as the refactoring is running in. Shouldn't it?

Jacques

0
Comment actions Permalink

Jacques Morel wrote:

On the topic of undo, in the JUnit test plugin after a method is renamed I
renamed its tests. Unfortunately if I was doing this from the test class
and I want to undo I get the message "Cannot undo, Some files were
modified". Intuitivement I would think that the refactoring listeners
would be invoked inside the same action as the refactoring is running in.
Shouldn't it?


Yes, they are. It is weird that it does not work for you.

Friendly,
Dmitry

--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0
Comment actions Permalink

Hello Dmitry,

Once I am finished the last touch of the new version I will look into it a little more.
Is there anything I could be doing that would trigger this behavior? If an action is already started I cannot do anything to override it right? Any new action will just be encapsulated/grouped into the original one. Am I correct?

Jacques

DL> Jacques Morel wrote:
DL>
>> On the topic of undo, in the JUnit test plugin after a method is
>> renamed I renamed its tests. Unfortunately if I was doing this from
>> the test class and I want to undo I get the message "Cannot undo,
>> Some files were modified". Intuitivement I would think that the
>> refactoring listeners would be invoked inside the same action as the
>> refactoring is running in. Shouldn't it?
>>
DL> Yes, they are. It is weird that it does not work for you.
DL>
DL> Friendly,
DL> Dmitry

0
Comment actions Permalink

Jacques Morel wrote:

Hello Dmitry,

Once I am finished the last touch of the new version I will look into it a
little more. Is there anything I could be doing that would trigger this
behavior? If an action is already started I cannot do anything to override
it right? Any new action will just be encapsulated/grouped into the
original one. Am I correct?


Apparently you are messing up actions and commands.
You should not execute new commands in refactoring listeners, however
you can start write actions (although you do not have to).

Friendly,
Dmitry


Jacques

DL> Jacques Morel wrote:
DL>

>>> On the topic of undo, in the JUnit test plugin after a method is
>>> renamed I renamed its tests. Unfortunately if I was doing this from
>>> the test class and I want to undo I get the message "Cannot undo,
>>> Some files were modified". Intuitivement I would think that the
>>> refactoring listeners would be invoked inside the same action as the
>>> refactoring is running in. Shouldn't it?
>>>

DL> Yes, they are. It is weird that it does not work for you.
DL>
DL> Friendly,
DL> Dmitry


--
Dmitry Lomov
Software Developer
JetBrains Inc.
http://www.jetbrains.com
"Develop with pleasure!"

0

Please sign in to leave a comment.