Skip to content
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

Add editor.codeActionsOnSave #48086

Merged
merged 5 commits into from
Apr 20, 2018
Merged

Conversation

mjbvz
Copy link
Collaborator

@mjbvz mjbvz commented Apr 18, 2018

Fixes #42092

Adds a way to run code actions on save using the editor.codeActionsOnSave setting. This setting lists code action kinds to be executed automatically when the document is saved.

TODO / Questions:

  • TODO: Add timeout
  • Q: Is the order correct? Currently code actions are run before format: A: Yes, this order seems expected
  • Q: How to handle multiple code actions? A: Run sequentially
  • Q: How should we handle edit conflicts for the code actions? What do we do for format on save? **A: format on save currently picks a formatter to run. We'll try running all the code actions **
  • Q: What should the default setting for this be?: A: Default = off. Extensions can now contribute language specific overrides that would enable this
  • Q: Current behavior creates two undo stops (one for the action and one for the format). Is this expected?
  • Q: Should we restrict this to source. code actions? (This is what it is doing currently actually) A: yes i still think this makes the most sense. We pass the entire document range when requesting these code actions. We can relax this restriction if it causes problems for extension authors

@mjbvz mjbvz self-assigned this Apr 18, 2018
@mjbvz mjbvz requested a review from jrieken April 18, 2018 01:35
/**
* Code action kinds to be run on save.
*/
codeActionsOnSave?: string[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be an object literal because that enables merging/overwriting

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Yes an object is much nicer to work with


private async applyCodeActions(actionsToRun: CodeAction[], editor: ICodeEditor) {
for (const action of actionsToRun) {
await applyCodeAction(action, this._textModelService, this._fileService, this._commandService, editor);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like - running them one after the other is the sane thing to do IMO

) { }

async participate(editorModel: ITextFileEditorModel, env: { reason: SaveReason }): Promise<void> {
if (env.reason === SaveReason.AUTO) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add timeout and make it configurable, like editor.formatOnSaveTimeout

@jrieken
Copy link
Member

jrieken commented Apr 18, 2018

How should we handle edit conflicts for the code actions? What do we do for format on save?

In the format-case we only pick one provider (the last that got registered). That makes applying formatting edits simpler but there is a strong desire to change that, e.g. run them sequentially. If formatting or code actions undo/destroy the work of a pre-running operation that's tough luck. I'd make sure tho to maintain a somewhat stable order, e.g sort them by provider

What should the default setting for this be?

I'd say off by default

Should we restrict this to source. code actions? (This is what it is doing currently actually)

Unsure, extension authors might now start to use source for everything but also fair to start like this. We should document this well, e.g. should source be used for something like 'insert missing semi-colons'?

@mjbvz
Copy link
Collaborator Author

mjbvz commented Apr 20, 2018

Ok, I've switched the config to an object and added a timeout, and posted answers/thoughts to many of my original questions

Fixes microsoft#42092

Adds a way to run code actions on save using the `editor.codeActionsOnSave` setting. This setting lists code action kinds to be executed automatically when  the document is saved.
@mjbvz mjbvz merged commit c29f432 into microsoft:master Apr 20, 2018
@GammaGames
Copy link

GammaGames commented Jun 1, 2018

Is there a way to use this setting to convert indentation to spaces automatically on save? I tried adding

 "editor.codeActionsOnSave": {
    "editor.action.indentationToSpaces": true
}

to my settings and it didn't do anything, because

Q: Should we restrict this to source. code actions? (This is what it is doing currently actually) A: yes i still think this makes the most sense. We pass the entire document range when requesting these code actions. We can relax this restriction if it causes problems for extension authors

@mjbvz
Copy link
Collaborator Author

mjbvz commented Jun 1, 2018

@GammaGames Do you have an extension that contributes a editor.action.indentationToSpaces code action?

@GammaGames
Copy link

No, it's a built in command. I found it in the keyboard shortcuts:

image

I guess I'm looking for a editor.commandOnSave setting, sometimes when I'm working on older code it will have tabs and I'd like to automatically convert them to spaces if I modify the file.

@mjbvz
Copy link
Collaborator Author

mjbvz commented Jun 1, 2018

This feature is only about code actions, not commands. The command names make this a little confusing but they are not code actions

(but extensions can convert commands to code actions very easily)

@GammaGames
Copy link

Oooh, my bad! Thank you!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Run code actions on save
3 participants