Move Refactorings
Basics
The Move refactorings allow you to move classes, functions, modules, files and directories within a project. So doing, PyCharm tracks these movements and automatically corrects all references to the moved symbols in the source code.
The following Move refactorings are available:
- The Move File refactoring moves a file to another directory.
- The Move Directory refactoring moves a directory to another directory.
- The Move Module Members refactoring moves top-level symbols of a Python module.
- The Make local function/method top-level refactoring converts a method or a local function to a top-level function and moves it to the specified file.
Performing Move refactoring
To perform a Move refactoring, follow these general steps:
- Select the symbol to be moved and do one of the following:
- On the main menu, or on the context menu, point to , and then choose .
- Press F6.
- In the Project Tool Window, drag the symbol to the new location.
The dialog that opens depends on the type of the selected symbol.
- Specify the move options according to the type of the item to be moved. See option descriptions in the Move dialog box reference.
- Preview and apply the changes.
Moving JavaScript and TypeScript top-level symbols
The Move Symbol Refactoring for JavaScript and TypeScript works for classes, functions, and variables in ES6 modules. To invoke this refactoring:
- Select the symbol to move.
- Choose on the main menu or on the context menu of the selection or just press F6.
- In the dialog box that opens, specify the destination file.
In the example below, the function changeSelectedPlaylists
is moved from the PlayerActions.js
file to the PlaylistsActions.js
file. Note that an import statement for the types that changeSelectedPlaylists
requires is added to PlaylistsActions.js
. Also all the imports of changeSelectedPlaylists
in the other files are updated.
Moving top-level symbols
To move a member, follow these steps:
- Place the caret at a top-level symbol, for example:
- Press F6. The dialog box Move Module Members opens:
Refer to the dialog reference for the detailed description of controls.
- In this dialog box, select the members to be moved, and specify the target file.
- Preview and apply the changes.
Moving function/method to the top-level
This refactoring moves local functions or methods to the top-level by converting references to instance attributes or variables from enclosing scopes to parameters and updating existing usages accordingly.
To move a function or a method to top-level, follow these steps:
- Place the caret at the local function or method name.
- On the main menu, or on the context menu of the editor, choose , or press F6.
- In the Make Method Top-Level dialog box that opens, specify the destination of move. You can type it manually, or click the browse button and locate the destination file in the Choose Destination File dialog.
- Click Refactor to perform the refactoring, or Preview, to shows the preview in the Find tool window. If satisfied with the preview results, confirm move by clicking Do Refactor.