IDE scripting console invoke refactoring

Answered

Hello,

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,

Dorin

0
1 comment

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());
0

Please sign in to leave a comment.