-
Notifications
You must be signed in to change notification settings - Fork 29.4k
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
[Api] Auto fix on save and fix all #62110
Comments
Not sure that I understood all the implications outlined above but making auto-fix a property and not a kind sound more correct, e.g. I can have all kinds of code actions that can be auto-fixed/applied. |
Reading through it I prefer |
Documenting a few other proposals
|
I'm in favor of the last proposal — @dbaeumer From the LSP point of view, |
A more complete writeup on the last proposal: ProblemES Lint, TS Lint, and other extensions have the concept of
ProposalThere are two related but separate problems with this proposal: an action that auto fixes problems in a file, and auto fixing individual errors. Auto-fixing all auto-fixable errors in a fileMotivating example You can even fix all linter errors when you save a file by setting Proposal
Auto fixing individual errorsMotivating example interface IFoo {
bar(): void;
}
class Foo implements IFoo { }
new foo() You hit Proposal
|
Part of #62110 * Adds a new `CodeActionKind`: `source.autoFix`. * Implements a simple auto fix provider for typescript. This provider can auto fix `implement interface` and `spelling` errors (provided there is only a single valid fix listed) The provider is likely not something we actually want to check it (especially in its current state), we should ask TS for proper autoFix support
@mjbvz regarding Auto fixing individual errors: How would we deal with a case where there are two errors at the cursor position (from the same or different providers). How would the code action provider know which diagnostic to fix. IMO the VS Code API and the LSP is unclear when in comes to diagnostics in code action requests. The primary input IMO was always the range. In my language servers I never limited the code actions to the provided diagnostics. Instead of having |
@dbaeumer Good points
|
Part of #62110 - Adds a new field `canAutoApply` to code actions. This field indicates that a code action can be applied without additional user input. For quick fixes, this should be set if the fix properly addresses the error - Enable auto fixes for TS spelling errors - Added a `editor.action.autoFix` command to triggers auto fixes at the current cursor position
Quick status update:
Todo
|
Thanks for reaching out! Some of my concerns about implementing this on my own are already captured in I trust you folks to come up with the right API; I’m more worried about how this works in practice. In addition to the topics already under discussion, something I don’t see captured for fix-all is that applying one fix could obviate another -or- create new things to fix (especially cross-extension). Is the process iterative? What if two fixes conflict with each other or create a feedback loop forever? So far, this seems straightforward to prototype. If you want me to do so in a private branch for experimentation, let me know. That may be enlightening. :) |
Thanks for the feedback. You bring up some good points. For fix-all, we leave most of this up to extensions. Overlaps and changes that generate more auto fixable issues could be handled by your extension when it computes the changes. If two separate extensions return overlapping fix-all edits, we would currently try to apply both sets of edits together. We may switch to a sequential or iterative approach instead if this proves to be a common problem in real world usage |
I don't mind the concept of fix on save, and I think it's a good thing overall. However, I would not want it to be something that happens by default unless I turn it on. Reason, many times the default fixes can be problematic for what I'm actually doing. I most case I would just rather see the lint error and then deal with that myself because depending on the situation, I may prefer to change the settings to remedy the lint error. I don't always agree with every linter rule and don't like being forced to use them whether I want to or not. |
@rbiggs Fix all would not be enabled on save by default. Like organize imports it would have a command you could manually run and you could optionally enable it on save per language |
Part of #62110 Use the more generic name as suggested in Dart-Code/Dart-Code#1393. This makes the intent of the field more clear and also allows us to extend the concept of preferred code actions to refactorings and other classes of code actions Experimentally also allows a `preferred` value for `apply` when configuring code aciton keyboard shortcuts. This applies the preferred code action returned from the list of code actions returned
Part of #62110 * Adds a new `CodeActionKind`: `source.autoFix`. * Implements a simple auto fix provider for typescript. This provider can auto fix `implement interface` and `spelling` errors (provided there is only a single valid fix listed) The provider is likely not something we actually want to check it (especially in its current state), we should ask TS for proper autoFix support
Both api components are now merged into VS Code as proposed apis. We may want to rename
However "cleanup" may be too generic of a term since |
@mjbvz I read through the API again and I am still puzzled how we ensure that the provided fix is for a given diagnostic. Or is this based on trust? |
Similarly, the |
Part of #62110 `autoFix` is a confusing term since we have a `auto fix` command now. Using `fix all` as this term is used by many linters for this type of operation
Ok, I changed the name for the action from |
@mjbvz thanks for the clarification. One additinal question: how would you (if necessary) determine that two diagnostics are equal. Object identity? In LSP we create new diagnostic objects. |
@dbaeumer In the VS Code source, we have a |
Closing this exploration issue. We are tracking finalizing |
@mjbvz thanks. That makes live in LSP easier. |
(edit: see here for a more recent proposal. Original proposal below)
Problem
ES Lint, TS Lint, and other extensions have the concept of
auto fix on save
. On save, this goes through the errors file and fixes all simple ones, such as removing tailing whitespace or converting{ x: string }
toArray<{ x: string }>
. This functionality is quite useful but is currently implemented per extension. It would be nice if we could:Related
Many extensions implement a
fix all of this type
code action / quick fix. This can be viewed as a special case ofauto fix on save
where the candidate fixes are only of a single type.Sketches
(Note that the sketch below record some of the options being considered for this API. They are not yet concrete proposals)
Do nothing
Keep things as is. Let extensions handle this all individually
Benefits
Drawbacks
CodeAction.autoFixable
Add an
autoFixable
flag toCodeAction
Use this flag to determine which code actions should be applied on save.
Introduce a
editor.autoFixOnSave
setting to enable or disable auto fix on saveImplementing
auto fix on save
autoFixable
in the set, then apply that code actionImplementing
auto fix all errors of type X
auto fix all errors of type X
suggestion .X
in the file, request code actions.CodeAction.diagnostics
set to include the diagnostic of typeX
.Maybe
autoFixable
here should actually be an array of diagnostics? Would that help with filtering.Benefits
Drawbacks / Open questions
CodeActionKind.SourceAutoFix
Add a new code action kind called
source.autoFix
. Update extensions to return a source code action that fixes all auto fixable errors in the file.Use
"editor.sourceActionsOnSave": ["source.autoFix"]
to apply code actions on saveImplementing
auto fix on save
Basically no work on VS Code side. Get extensions to adopt this new API.
Implementing
auto fix all errors of type X
We could do something like have multiple source code actions for autofix, such as:
source.autoFix.123
source.autoFix.456
This may gets confusing though since requesting the code actions for
source.autoFix
would end up including the source code actions ofsource.autoFix.123
andsource.autoFix.456
unless the extension is smart. Maybe we'd have to have asource.autoFix.all
to prevent this for the autofix on save case? Or perhaps it doesn't matter? Ifeditor.sourceActionsOnSave
applies all actions ofsource.autoFix
, presumably applyingsource.autoFix.123
andsource.autoFix.456
should work?Benefits
"editor.sourceActionsOnSave": ["source.autoFix"]
Drawbacks / Open questions
fix all
actions? With current proposal, the source code menu would just show anfix all
action for each extension. Would be nice if we just have one entry (autofix on save may still work though, at least as long as the fixes don't overlap)auto fix all errors of type X
?autofix
in the UI with this proposal?The text was updated successfully, but these errors were encountered: