IDE scripting console invoke refactoring



I was wondering if it's possible to invoke refactoring methods within PyCharm's IDE Scripting console. What I would like to achieve is to be able to invoke an "Extract Method" refactoring programmatically. For example to be able to select given lines of code from a given file and trigger "Extract Method".

Is it possible? If yes, could you please point me to some SDK documentation?


Thank you,


1 comment
Comment actions Permalink

Hi! You can call `com.jetbrains.python.refactoring.extractmethod.PyExtractMethodHandler#invoke(com.intellij.openapi.project.Project, com.intellij.openapi.editor.Editor, com.intellij.psi.PsiFile, com.intellij.openapi.actionSystem.DataContext)` method, which will call refactoring for selected text in editor.

Also you can check how it's used, for example, inside tests (`com.jetbrains.python.refactoring.PyExtractMethodTest`):

final RefactoringSupportProvider provider = LanguageRefactoringSupport.INSTANCE.forLanguage(PythonLanguage.getInstance());
final RefactoringActionHandler handler = provider.getExtractMethodHandler();
handler.invoke(project, editor, file, ((EditorEx)editor).getDataContext());

Please sign in to leave a comment.