Repository navigation
Description
Problems
A user selects some text in a JS file and tries to run extract constant (though a keyboard shortcut). The selected text is not extractable. However, there is currently no feedback in the editor about why the selected text could not be extracted.
Also, unless the already user know that JS/TS supports extract constant, they may never discover this refactoring exists at all; it will only show up in the refactor menu when the current selection is extractable.
Desired UX
-
If the user explicitly requests to apply a refactoring that cannot be applied (such as
extract constantfor an invalid selection) let extensions surface a human readable error message describing why the refactoring cannot be applied. -
Provide a way for extensions to show refactorings that they support but are currently not supported in the refactor menu. This would help users understand what refactorings are available, while also seeing that those refactorings are not possible (ideally we'd also find a way to surface the error message here too)
Proposal
With microsoft/TypeScript#35098, we are requesting that TypeScript return the reason why a given JS/TS refactoring is not possible. This issue tracks the VS Code side API required and how information from this new API would be surfaced to users.
My API proposal is to let extensions set an disabled property on the code actions they return:
export class CodeAction {
...
disabled?: {
/**
* Human readable description of why the code action is currently disabled.
*
* This is displayed in the UI.
*/
reason: string;
};
}I propose including .disabled on the CodeAction class because it would let us reuse the existing code action metadata (title, kind, ...) without having to introduce new classes (such as InvalidCodeAction)
How .disabled would be surfaced to users
Lightbulb menu
This automatic lightbulb menu would drop all error code actions. It would likely be overwhelming to show them all.
I like keeping the lightbulb as showing: here is everything that is possible right now.
Keybinding for code action
There are a few cases here:
-
All returned code actions for the keybinding are errors.
If the user has enabled
applyas part of the keybinding, in this case we would want to show the.errorfrom the code action that would have been applied if it were valid. -
A mix of error and valid actions are returned, and the user has not selected to apply the code actions.
Here we would want to show a full list of actions, including the invalid actions disabled.
-
A mix of code error and valid actions are returned, and the user has selected to apply code actions.
We would apply the code actions as if the error code actions were not there. For example, use
"apply": "ifOnly"would apply the valid code action if it is the only valid code action (ignoring the errored ones)
Refactoring menu
We'd show the errored actions in the refactoring menu but as disabled. The goal of this is to improve discoverability.
With the current proposal it would be up to extensions to say which code actions they return in the error state and which ones they simply drop. For example, it may be overwhelming if JS/TS to showed all of its refactorings in the refactor menu all the time (including rarely used ones such as rewrite export). Instead, for the generic code action menu, JS/TS would likely always return the most common actions (such as extract constant) either in a valid or in an error state
Activity
mjbvz commentedon Nov 19, 2019
Copying some extension authors and other potentially interested parties: Danny Tuppeny (@DanTup), Johannes Rieken (@jrieken), Don Jayamanne (@DonJayamanne), Dustin Campbell (@DustinCampbell), Rome Li (@akaroml), Kai Maetzel (@kieferrm)
The specifics of the proposed API will likely change. I am mainly interested in your feedback on:
Is surfacing "here's why this refactoring is not possible" something that would be helpful for your users?
Could your language server / extension generate this error information? Could it do so efficiently?
How would you expect the error information be used by VS Code? Do the current proposed use cases make sense? Are there any other ones that we should consider?
DonJayamanne commentedon Nov 20, 2019
Sounds like a good idea, having all refactorings in one place and displayed (
almost) always does improve discoverability.At this stage I don't think so. But it is a change in a positive direction (more exposure).
DanTup commentedon Nov 20, 2019
Sounds like a good idea to me (it also seems like #45383 would be more reasonable of this is done?).
I think the naming of the
.errorproperty is a little weird (the name makes it sound like "we tried this and it failed" rather than "this is the reason this one is not available") but I'm not sure I have anything better.Is there value in having a way of flagging code actions as unavailable without having to provide a string? I think in a majority of cases it's going to be the same reason ("it's just not valid at this location" - for example if you open the menu in the middle of whitespace, or on a language keyword) so having VS Code provide a consistent (and localised) message without us having to would be nice (I think being able to generally provide refactors that are unavailable is more significant than the specific message shown). Maybe the property could be a
boolean | stringfor ex., so we can flag that it is unavailable, or provide a specific reason it is unavailable.jrieken commentedon Nov 20, 2019
I like - the is an easy to adopt and pragmatic change that adds much value. Tho, I also think that
errorisn't a great name.[-]CodeAction.error[/-][+]CodeAction.disabled[/+]Add CodeAction.disabled
Hook up basic alert of why a code action could not be applied
mjbvz commentedon Nov 21, 2019
I've tentatively changed the property name to
disabledinstead and wired it up in a few places.Here's showing a custom message if the requested code action (such as
extract constant) is disabled:Here's what disabled code actions look like in the refactor menu:
Building on Danny Tuppeny (@DanTup)'s comment that it may be difficult to generate a useful error message, we could change the api to be something like:
That may be more explicit. If extensions do not provide a
reason, we could fall back to the a generic oneDanTup commentedon Nov 21, 2019
Should it be:
? Would be weird to use
disabled: {}to indicate disabled. Otherwise, I like it :-)When disabled items are shown in the menu that do have reasons (eg. if there are multiple, so they don't show in the bubble), will the reasons show in tooltips or something?
jrieken commentedon Nov 21, 2019
Maybe even more compact:
disabled?: true | string. Or we make this not only negative but also have a way to explain why a code action is there. Maybe an over-delivery and not really needed but it could then look like thisreasoning: { enabled:boolean, description:string }akaroml commentedon Dec 3, 2019
I just noticed this proposal. It will definitely serve the purpose of reporting reasons. Besides, we could use this as a way of exposing features from the Java language server. In this way, we can provide a full list of code actions that are supported and enable/disable them according to the current context. This way we make the trigger of code actions more deterministic and it's also great for feature discoverability.
Jinbo Wang (@testforstephen) Yan Zhang (@Eskibear) Sheng Chen (@jdneo)
13 remaining items
alexr00 commentedon Feb 27, 2020
Matt Bierner (@mjbvz) I can't get my disabled action to show anywhere. I added this to the code-actions-sample extension:
It doesn't show in the light bulb (which appears to be correct based on the details in the original issue).
It doesn't show when I run ctrl+., which based on the details above it should show during.
I tried making all of the code actions in the sample refactors. The disabled one still wouldn't show. Even when I ran Refactor... from the command palette. In fact, even the enabled code actions didn't show. They only showed in the lightbulb.
Finally, I added this keyboard shortcut:
Same result. It just says "No code actions for 'refactor' available".
Improve docs for `CodeAction.disabled`
mjbvz commentedon Feb 27, 2020
I've update the documentation to clarify when disabled action should/should not show up. Please review the updated docs and see if you can get them working as expected (in the example extension, use
CodeActionKind.Refactorinstead ofCodeActionKind.QuickFix)alexr00 commentedon Feb 28, 2020
I've already tried that :)
mjbvz commentedon Feb 28, 2020
You also need to update the metadata in the example (or just delete metadata entirely). Otherwise the provider is never called for refactorings)
Note you should also be able to verify this with the js/ts extension since it uses this api for its extract refactorings