Repository navigation
Description
Motivation
JavaScript currently implements organize imports as a command. So does Java. Each extension also has its own keybinding for organize imports.
With common actions like organize imports that will be implemented by multiple extensions, we risk making each extension reinvent the wheel and possibly creating conflicts other extensions. It is already very easy to forget to properly scope your organize imports command when defining it, which may cause vscode to trigger the wrong language's organize imports for a file.
Proposal
Provide an API that allows extensions to register file-level actions, such as organize imports, in a standard way. VS Code can use this API to provide a standard keybinding for common actions such as organize imports, and also ensure that we correctly handle cases where multiple organize imports actions exist for a single file
Proposed Implementation
Leverage the existing CodeAction class to create the concept of a source code action. Introduce a new scope source.* scope to define source code actions.
We can then either leverage the existing CodeActionProvider api or introduce a new SourceCodeActionProvider class that allows extensions to return source code actions.
The expected ux in either case:
-
Do not show source code actions in the light bulb menu or when using the
editor.action.quickFixkeyboard shortcut -
Provide a right context click menu that shows a list of all source code actions for the current file. Do not show an entry in this context menu if no source code action providers are registered for the file
-
Also provide a way to setup a keybinding for specific source code actions. This could either reuse the existing
editor.action.codeActioncommand or introduce a neweditor.action.sourceCodeActioncommand
Johannes Rieken (@jrieken) Let me know if you have any concerns or ideas about the proposal or implementation. We can also cover this at the API sync. Overall, I'm leaning towards reusing our the existing CodeActionProvider class for this. The main downside of this approach is that I don't think we could make the source context menu only show up if these is an actual source code action provider since we could not know whether a registered CodeActionProvider will returns source actions or not
Activity
jrieken commentedon Apr 11, 2018
I am 👍 on re-using the same provider. We could extend the
registerCodeActionProvider-function to accept a set of code action kinds it will work on (similar to trigger characters and completion items).mjbvz commentedon Apr 11, 2018
Added basic
CodeActionKind.Sourceimplementation as outlined here. A bit unsure about the nameSourcebut I don't really have a better suggestion. MaybeDocument Actions? But then where would an action likeformat selectionfit?DanTup commentedon Apr 24, 2018
Matt Bierner (@mjbvz) Is this going to come with support for running it on Save? (we currently have our own setting, but if you're going to ship with native support, I'll remove the setting on the branch where I've implemented this).
mjbvz commentedon Apr 24, 2018
Yes, already implemented by #42092. Your extension can contribute a language specific setting that does something like:
in order to enable organize imports on save just for dart
DanTup commentedon Apr 25, 2018
Perfect, thanks!