-
Notifications
You must be signed in to change notification settings - Fork 32.8k
CodeAction.disabled #85160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Copying some extension authors and other potentially interested parties: @DanTup, @jrieken, @DonJayamanne, @DustinCampbell, @akaroml, @kieferrm The specifics of the proposed API will likely change. I am mainly interested in your feedback on:
|
Sounds like a good idea, having all refactorings in one place and displayed (
At this stage I don't think so. But it is a change in a positive direction (more exposure). |
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 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 |
I like - the is an easy to adopt and pragmatic change that adds much value. Tho, I also think that |
I've tentatively changed the property name to Here's showing a custom message if the requested code action (such as Here's what disabled code actions look like in the refactor menu: Building on @DanTup's comment that it may be difficult to generate a useful error message, we could change the api to be something like: disabled?: {
reason?: string
} That may be more explicit. If extensions do not provide a |
Should it be: disabled?: boolean | {
reason?: string
} ? Would be weird to use 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? |
Maybe even more compact: |
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. |
For #85160 Using an object is more explict with property names and will let us introduce additional properties in the future if needed
Change the api to: disabled?: {
/**
* Human readable description of why the code action is currently disabled.
*
* This is displayed in the UI.
*/
reason: string;
}; This is more explicit and would allow us the add additional properties related to disablement in the future (say a @DanTup With this proposal, you would always need to provide a reason why a refactoring is disabled. The |
What's the reason to require extensions to provide generic text over VS Code doing it? Having one in VS Code would not only give some consistency, but it could be localised along with the rest of the UI. I suspect 100% of mine will use a generic message (for now at least), as the language server doesn't provide a reason why they're not available. |
@DanTup We really want to encourage extensions to provide a useful reason if they disable a refactoring, even if it's something simple like |
@akaroml The ways We do want to improve this by including the disabled reason in the context menu itself, but are still trying to figure out a clean way to do this |
@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). 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". |
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 |
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 |
Uh oh!
There was an error while loading. Please reload this page.
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 constant
for 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:I propose including
.disabled
on theCodeAction
class because it would let us reuse the existing code action metadata (title
,kind
, ...) without having to introduce new classes (such asInvalidCodeAction
)How
.disabled
would be surfaced to usersLightbulb 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
apply
as part of the keybinding, in this case we would want to show the.error
from 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 asextract constant
) either in a valid or in an error stateThe text was updated successfully, but these errors were encountered: