-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Added skipDestructiveCodeActions argument to organize imports server command #43184
Added skipDestructiveCodeActions argument to organize imports server command #43184
Conversation
I think this is probably okay, but part of me feels like there's a difference between a user-specified import cleanup and one that was triggered incidentally by organize-imports-on-save. |
Err, does such a preference exist in the services API? I don't see anything in |
The combination of codefixes on save and save on blur is so horrifying to me; to pick up on Daniel’s point, I think it merits a broader discussion about what kinds of actions should be allowed through that sequence. My feeling is we should never run anything that deletes code without an explicit user action, and blurring your editor tab doesn’t count as an explicit user action. |
Doesn't "do X on save" require that the use take explicit action in the first place (add the configuration to vscode settings.json)? |
Yeah, but I’m talking about direct synchronous action. I’m not saying “this is dangerous so it needs to be opt-in”; I’m saying “we should not have a feature that deletes code when users aren’t looking, no matter how badly they think they want it.” |
And to clarify, I’m not talking about codefixes-on-save alone. That’s ok because the direct causal action is Ctrl+S. If it messes up, you’re actively observing the mistake and you can undo it. The problem is save-on-blur triggering codefixes-on-save. Because in that case, the you’re explicitly moving your attention away from the place where we’re performing the edits. Anything done via that chain reaction needs to be totally non-destructive (e.g., formatting). |
Thanks for the clarification. Does this make sense as a user preference, something like "remove unused imports when organizing imports" (only for the explicitly requested case)? |
@JoshuaKGoldberg we discussed this a bit with VSCode and we think it makes sense to add a user preference to control if code is deleted on save. I propose The syntax error would then be checked after this option. |
Super. Is that something I'd be well positioned to fix? I'm wary of taking on big projects that are still in early stages cross-team as an external contributor, but if you have a solid vision I could contribute to that'd make me very happy. |
The work on our (TypeScript) side should be pretty simple. You would just need to add the option to the existing You don't necessarily need to make changes on the vscode side to get this change in but you're welcome to try. If you're not comfortable doing that we can just let them know about the new option. I'm not familiar enough with the vscode side logic to give you pointers anyway 😅. |
Note that |
Oh right. So for this particular issue, |
Thanks! 3a61904 adds the option here. I've dabbled in VS Code before; it'd be fun to try to add it there too. 😄 |
7fd22f8
to
1637e1a
Compare
Thanks @JoshuaKGoldberg! Let’s get a review from @jessetrinity too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: a word
Co-authored-by: Jesse Trinity <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. We should make a separate issue for adding a UserPreference
for cases in which the command is explicitly requested.
Why didn’t @typescript-bot issue the pinging-everyone-because-you-changed-protocol.ts comment? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a breaking change to the protocol? Am I missing something?
src/server/protocol.ts
Outdated
export interface OrganizeImportsRequestArgs { | ||
scope: OrganizeImportsScope; | ||
export interface OrganizeImportsRequestArgs extends OrganizeImportsScope { | ||
allowDestructiveCodeActions?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm, we expect the editor to want to make this decision per-request and that's why it's not a UserPreference?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also i would name this flag as skipDestructiveCodeActions
so that you would need to check only for truthiness instead of defaults when not set..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To confirm, we expect the editor to want to make this decision per-request and that's why it's not a UserPreference?
Yes, the editor would need to specify the option based on whether the command was sent as a part of compileOnSave
or similar.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dacfd7e renames to skipDestructiveCodeActions
and defaults it to falsy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to get a second opinion on the flag name, but LGTM otherwise.
@@ -681,6 +681,7 @@ namespace ts.server.protocol { | |||
|
|||
export interface OrganizeImportsRequestArgs { | |||
scope: OrganizeImportsScope; | |||
skipDestructiveCodeActions?: boolean; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we have an actual convention, but it looks like we have a pattern of using positive names and, if necessary, treating undefined as true. e.g. UserPreferences.includeAutomaticOptionalChainCompletions
or CompilerOptions.AllowUnreachableCode
. I think @andrewbranch has also found this frustrating and might have suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I don’t really have a preference here. This looks fine to me.
Fixes #43051