-
Notifications
You must be signed in to change notification settings - Fork 59
Code Actions
When a Code Action is available for the current selection / cursor position, this is indicated by a light bulb to the left of the editor:
To show the available actions, either click the light bulb or press Ctrl+.. This will open a popup menu:
In case you want to change the default shortcut, the command
for this keybinding is "editor.action.quickFix"
.
Additionally, there's also an "Auto Fix" command ("editor.action.autoFix"
) that directly applies the "preferred" code action if one exists (usually the first one), without opening a menu. The default shortcut is Shift+Alt+..
Here is an overview over all Code Actions that currently exist:
In some cases, simple compiler errors can be fixed with a Code Action:
Replaces all occurrences of a string literal with a constant.
Source Actions are special code actions that apply to the whole document, so they are not available contextually. You can select "Source Action..." from the context menu anywhere in the document to get a complete list:
Sorts imports and usings in a module alphabetically.
Combination of "Sort imports/usings" and "Remove all unused imports/usings".
VSCode also has a built-in "Organize Imports" command that is availbale from the command palette or with its default shortcut Shift+Alt+O.
-
"editor.codeActionsOnSave"
- All Source Actions can be applied on save.To enable autofixes on file save for basic compiler errors (Fix All command), you can use the following:
"editor.codeActionsOnSave": { "source.fixAll": true }
To organize imports on save, you can use the following:
"editor.codeActionsOnSave": { "source.organizeImports": true }
Or if you only want to sort the imports without removing unused ones:
"editor.codeActionsOnSave": { "source.sortImports": true }
Note: this works well together with format on save.
Setup
Features
- Tasks
- Debugging
- Commands
- Dependency Explorer
- Auto Indentation
- Completion
- Postfix Completion
- Snippets
- Code Generation
- Signature Help
- Hover Hints
- Go to Definition
- Go to Type Definition
- Go to Implementations
- Peek Definition
- Find All References
- Rename Symbol
- Document Symbols
- Workspace Symbols
- Outline
- Folding
- Diagnostics
- Code Actions
- Code Lens
- Formatting
- Extension API
- Inlay Hints
Noteworthy Releases