Programmatically perform extract method on multiple method invocations



I am currently working on a plugin that programmatically performs an inline method, performs some operations (or doesn't), and then extracts the method later. I am using the extract method processor to undo the inline method currently, however I am running into some issues with it. If there are multiple invocations that are inlined, I am only able to extract one of them. I am also unable to get the same signature that I had before I inlined the method. If I manually extract the method, I can extract all of the necessary invocations with a mostly correct signature (I need to perform a change signature refactoring to get the signature textually correct).

I'm pretty sure the method signature is user error and that's something I can figure out. Do you have any suggestions to perform the extract method on all of the method invocations in the source method?



Comment actions Permalink

It can be dangerous to use the extract method to undo the inline refactoring. Both these refactorings do some code optimizations and thus inline+refactoring may fail or result in different code sometimes.

As far as I can understand the question is how to extract method from several code fragments simultaneously (with suggested change signature). The most similar case is extract method + replace duplicates. There is no straight API for this but you can use the test code as an example (github):, boolean, com.intellij.openapi.editor.Editor, com.intellij.psi.PsiFile, com.intellij.openapi.project.Project, boolean, com.intellij.psi.PsiType, boolean, java.lang.String, com.intellij.psi.PsiClass, java.lang.String, int...)
Comment actions Permalink

Hi Alexandr,

Thank you for the quick reply. Is there by chance any way to disable the code optimizations? 

I'm aware that if I perform an inline method using the editor that I can undo it. Is the operation stored on a stack that I could use to undo it programmatically instead of performing extract method on it?

Thank you for directing me to the extract method + replace duplicates test code, that does appear to be what I'm looking for.

Comment actions Permalink

It's possible to invoke undo programmatically via `com.intellij.openapi.command.undo.UndoManager#undo`


Please sign in to leave a comment.