Refactoring
Source code refactoring can improve the quality and maintainability of your project by restructuring your code, while not modifying the runtime behavior. Visual Studio Code supports refactoring operations (refactorings) such as Extract Method and Extract Variable to improve your codebase from within the editor.
For example, a common refactoring used to avoid duplicating code (a maintenance headache) is the Extract Method refactoring, where you select source code and pull it out into its own shared method, so that can reuse the code elsewhere.
Refactorings are provided by a language service. VS Code has built-in support for TypeScript and JavaScript refactoring through the TypeScript language service. Refactoring support for other programming languages is enabled through VS Code extensions that contribute language services.
The UI elements and VS Code commands for refactoring are the same across the different languages. This article demonstrates refactoring support in VS Code with the TypeScript language service.
Code Actions = Quick Fixes and refactorings
In VS Code, Code Actions can provide both refactorings and Quick Fixes for detected issues (highlighted with red squiggles). When the cursor is on a squiggle or on a selected text region, VS Code shows a light bulb icon in the editor to indicate that a Code Action is available. If you select the Code Action light bulb or use the Quick Fix command kb(editor.action.quickFix)
, the Quick Fixes and refactorings control is presented.
If you prefer to only see refactorings without Quick Fixes, then you can use the Refactor command (kb(editor.action.refactor)
).
Note: You can completely disable Code Action lightbulbs in the editor with the
editor.lightbulb.enable
setting. You can still open Quick Fixes through Quick Fix command andkb(editor.action.quickFix)
keyboard shortcut.
Code Actions on save
With the editor.codeActionsOnSave
setting, you can configure a set of Code Actions that are automatically applied when you save a file, for example to organize imports. Based on your workspace files and active extensions, IntelliSense provides a list of available Code Actions.
You can configure one or more Code Actions for editor.codeActionsOnSave
. The Code Actions are then run in the order they're listed.
The following example shows how to configure multiple Code Actions on save:
// On explicit save, run sortImports source action. On auto save (window or focus change), run organizeImports source action.
"editor.codeActionsOnSave": {
"source.organizeImports": "always",
"source.sortImports": "explicit",
},
The following values are supported for each Code Action:
explicit
(default): Triggers Code Actions when explicitly savedalways
: Triggers Code Actions when explicitly saved and on Auto Saves from window or focus changesnever
: Never triggers Code Actions on save
[!NOTE] Although
true
andfalse
are still valid configuration values at the moment, they will be deprecated in favor ofexplicit
,always
, andnever
.
Refactoring actions
Extract Method
Select the source code you'd like to extract and then select the light bulb in the gutter or press (kb(editor.action.quickFix)
) to see available refactorings. Source code fragments can be extracted into a new method, or into a new function at various different scopes. During the extract refactoring, you are prompted to provide a meaningful name.
Extract Variable
The TypeScript language service provides Extract to const refactoring to create a new local variable for the currently selected expression:
When working with classes, you can also extract a value to a new property.