дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 17:21:13 +02:00
Refactor > Move (F6) in a .py file moves top-level functions, classes and variables to another module. The dialog has From file, To file (a .py path in the project, new or existing) and Elements to Move. An embedded ast-based helper, run with the pylsp-rope environment's Python, moves the code with its decorators and attached comments, places it by import-time dependencies, writes the imports both modules need and rewrites module.name uses; from ... import statements across the project go through the existing module-move rewriter. Unsaved buffers are read, a file changed meanwhile is refused, and every changed file is parsed again before the edit is returned. A name already defined at the destination is refused; risky cases are confirmed first. The tests needing pylsp are ignored by default. Refs #901 Co-authored-by: wildcatDownstairs <[email protected]>
110 рядки
11 KiB
Plaintext
110 рядки
11 KiB
Plaintext
---
|
||
title: "Refactoring"
|
||
description: "Rename, introduce and extract, inline, move, change signature and safe delete, and which ones depend on the language server."
|
||
---
|
||
|
||
The **Refactor** menu has IntelliJ IDEA's refactorings with their usual shortcuts. Most of them are carried out by the file's language server, so what is available depends on the language: Rename works almost everywhere, while Extract Method, Inline or Change Signature appear only where the server offers them. When a refactoring cannot run at the caret, a hint says why (for example "Cannot perform refactoring. Select the code to extract").
|
||
|
||
## Refactor This
|
||
|
||
| Action | macOS | Windows / Linux |
|
||
| ------------------ | ----- | ------------------ |
|
||
| **Refactor This…** | `⌃T` | `Ctrl+Alt+Shift+T` |
|
||
|
||
**Refactor This…** lists only the refactorings that apply at the caret or selection: first the ones below, then any other refactorings the language server offers there (for example "Convert to arrow function" in TypeScript).
|
||
|
||
## Available refactorings
|
||
|
||
| Refactoring | macOS | Windows / Linux | Works through |
|
||
| ------------------------ | ----- | --------------- | ---------------------------------------------------------- |
|
||
| **Rename…** | `⇧F6` | `Shift+F6` | Language server; local variables also without one |
|
||
| **Change Signature…** | `⌘F6` | `Ctrl+F6` | Java and Python (dialog); other languages: language server |
|
||
| **Introduce Variable…** | `⌥⌘V` | `Ctrl+Alt+V` | Language server; simple cases also without one |
|
||
| **Introduce Constant…** | `⌥⌘C` | `Ctrl+Alt+C` | Language server; simple cases also without one |
|
||
| **Introduce Field…** | `⌥⌘F` | `Ctrl+Alt+F` | Language server |
|
||
| **Introduce Parameter…** | `⌥⌘P` | `Ctrl+Alt+P` | Language server |
|
||
| **Extract Method…** | `⌥⌘M` | `Ctrl+Alt+M` | Language server |
|
||
| **Extract Interface…** | — | — | Java (dialog); TypeScript: language server |
|
||
| **Extract Type Alias…** | — | — | TypeScript, JavaScript (`@typedef`): language server |
|
||
| **Inline…** | `⌥⌘N` | `Ctrl+Alt+N` | Language server |
|
||
| **Move…** | `F6` | `F6` | Language server or Python (symbols); the IDE (files) |
|
||
| **Safe Delete…** | `⌘⌦` | `Alt+Delete` | The IDE, using the server's references |
|
||
| **Pull Members Up…** | — | — | Java; PHP with Phoxite |
|
||
| **Push Members Down…** | — | — | Java; PHP with Phoxite |
|
||
|
||
## Rename
|
||
|
||
<Steps>
|
||
<Step title="Start the rename">
|
||
Put the caret on a name and press `⇧F6` / `Shift+F6`. The name becomes editable in place, and its other occurrences
|
||
in the file change as you type.
|
||
</Step>
|
||
<Step title="Finish or cancel">
|
||
Press `↵` / `Enter` or `⇥` / `Tab` to apply the rename across the project. `Esc` restores the old name.
|
||
</Step>
|
||
<Step title="Use the dialog for more options">
|
||
Press `⇧F6` / `Shift+F6` a second time to open the rename dialog instead.
|
||
</Step>
|
||
</Steps>
|
||
|
||
When the change touches more than one file, or renames or moves files, a preview lists the affected files before anything is written; confirm with **Do Refactor**. If the server may have missed usages (for example while it is still loading the project), the preview says so and the button reads **Rename Anyway**.
|
||
|
||
Without a running language server, only local variables are renamed, from the syntax tree. Members, types and other cross-file symbols wait for the server.
|
||
|
||
## Introduce and extract
|
||
|
||
**Introduce Variable**, **Introduce Constant**, **Introduce Field**, **Introduce Parameter** and **Extract Method** work like this:
|
||
|
||
1. Select the expression or statements. With only a caret, a chooser lists the expressions around it; each is highlighted while you move through the list.
|
||
2. If the expression occurs several times, choose whether to replace this occurrence or all of them (**Multiple occurrences found**).
|
||
3. The new name is then edited in place. `↑` / `↓` cycle through suggested names; `↵` / `Enter` accepts.
|
||
|
||
When the language server cannot help, Introduce Variable and Introduce Constant still work from the syntax tree in clear-cut cases, and refuse with a hint otherwise.
|
||
|
||
## Move, copy and rename files
|
||
|
||
In the Project view, **Rename…** (`⇧F6` / `Shift+F6`), **Move…** (`F6`, drag and drop, or cut and paste) and **Copy…** (`F5`, copy and paste, or `⌥`-drag / `Alt`-drag) open a dialog. With **Search for references** on, references to the file are updated through the language servers that support it (for example imports in TypeScript); Python imports are updated by the IDE itself; **Search in comments and strings** also looks there. The changes are shown in a preview first.
|
||
|
||
In the editor, **Move…** on a symbol runs the server's move refactoring, where the language has one. In Java it asks where to: a top-level class goes to another package (its package declaration, imports and every reference follow), a static member or nested class to another class, an instance method to the class of one of its fields or parameters; warnings are confirmed in **Problems Detected** first.
|
||
|
||
In Python, **Move…** (`F6`) in a `.py` file moves top-level functions, classes and variables to another module. The dialog lists them (the one at the caret is checked) and takes the destination **To file**, a `.py` path inside the project that may not exist yet. Decorators and the comments above move along, both modules get the imports they need, and `from … import` statements and `module.name` uses across the project, unsaved editors included, point to the new module. A name already defined at the destination is refused; import cycles, `import *`, strings naming the old module and similar cases are listed in a confirmation first. Class methods are not moved. It runs with the Python of **pylsp-rope (Python refactorings)**.
|
||
|
||
## Safe Delete
|
||
|
||
<Steps>
|
||
<Step title="Start">
|
||
Put the caret on a declaration and press `⌘⌦` / `Alt+Delete`, or use **Safe Delete…** on a file in the Project view.
|
||
</Step>
|
||
<Step title="Confirm">Confirm the deletion. You can also search comments and strings.</Step>
|
||
<Step title="Review usages">
|
||
If the symbol is still used, **Usages Detected** offers **View Usages**, **Delete Anyway** or **Cancel**. Otherwise
|
||
the declaration is removed together with its doc comment and annotations, as one undo step.
|
||
</Step>
|
||
</Steps>
|
||
|
||
## Pull Members Up and Push Members Down
|
||
|
||
These move fields and methods between a class and its superclasses or subclasses, in Java and, with Phoxite as the PHP server, in PHP (methods, properties and constants). A dialog lets you pick the members and the destination; problems the move would cause are listed in **Problems Detected** before you continue. For Java they are provided by the extension bundled with OpenIDEA, for PHP by Phoxite; they are not available for other languages or other PHP servers.
|
||
|
||
## Extract Interface
|
||
|
||
In Java, **Refactor › Extract Interface…** creates an interface from the class of the current file. In the dialog, enter the interface name, choose its package and check the methods (and constants) it should declare. **Refactor** creates the interface, makes the class implement it, replaces the class with the interface wherever that is possible, and opens the new file; **Preview** lists the changes first.
|
||
|
||
In TypeScript, select an object type literal (for example `{ name: string; age: number }`) and choose **Refactor › Extract Interface…**: the language server declares an `interface` for it and uses it in its place. **Refactor › Extract Type Alias…** does the same with a `type` declaration for any selected type, and in JavaScript adds a JSDoc `@typedef`. Afterwards the new name is renamed in place, as after **Introduce Variable…**. When the server has nothing to extract at the selection, the editor says so.
|
||
|
||
## Change Signature
|
||
|
||
In Java, **Change Signature…** (`⌘F6`) on a method opens a dialog to change its visibility, return type and name, and to add, remove and reorder its parameters. A new parameter needs a default value, which is passed at every existing call; **Delegate via overloading method** keeps the old signature as an overload that calls the new one. The preview line shows the resulting declaration. **Refactor** updates the method, its overrides and all calls in one step; **Preview** lists the changes first. In other languages it appears when the server offers a signature rewrite as a code action.
|
||
|
||
In Python, **Change Signature…** on a function, a method or a class (its `__init__`) opens the same dialog with a **Name** field and a table of **Name** and **Default value**. A row can be `*args`, `**kwargs`, or the markers `*` (keyword-only follows) and `/` (positional-only before it); the preview shows the `def`. A new parameter needs a default value, so existing calls stay valid. Renaming a parameter renames it in the body and in keyword arguments. Annotations, return types and decorators are kept. The change reaches the function, the methods that override it and every call, including calls in unsaved editors, in one undoable step; calls rope and the type checker cannot match (`*args` unpacking, calls with comments between arguments) are listed in a confirmation and left as they are. It runs on rope from **pylsp-rope (Python refactorings)** in **Settings › Languages & Frameworks › Language Servers**; the IDE offers to install it when it is missing.
|
||
|
||
## Undo
|
||
|
||
A refactoring is one undo step, even when it changed several files. After a multi-file refactoring, a notification offers **Undo** (and then **Redo**).
|
||
|
||
## Limits
|
||
|
||
- Refactorings other than Rename, Safe Delete and the syntax-based fallbacks need a running language server that offers them. Check the **Language Services** widget in the status bar if one is missing.
|
||
- Outside Java and Python, Change Signature appears only when the server offers a signature rewrite as a code action. In Python, a subclass constructor call (`Child()` with no `__init__` of its own) and calls through names the type checker cannot follow may not be updated.
|
||
- There is no structural search-and-replace refactoring in this menu; see **Edit › Find › Replace Structurally…** in [Inspections](/en/guide/inspections).
|