Skip to content

Add editor.codeActionsOnSave - #48086

Merged
Matt Bierner (mjbvz) merged 5 commits into
microsoft:masterfrom
mjbvz:code-actions-on-save
Apr 20, 2018
Merged

Matt Bierner (mjbvz) merged 5 commits into
microsoft:masterfrom
mjbvz:code-actions-on-save

Conversation

@mjbvz

@mjbvz Matt Bierner (mjbvz) commented Apr 18, 2018 •

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor 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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Johannes Rieken (jrieken) commented Apr 18, 2018 •

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

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

Matt Bierner added 4 commits April 20, 2018 13:30
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
Matt Bierner (mjbvz) merged commit c29f432 into microsoft:master Apr 20, 2018
@GammaGames

Jesse (GammaGames) commented Jun 1, 2018 •

Copy link
Copy Markdown

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
Copy Markdown
Contributor Author

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

@GammaGames

Copy link
Copy Markdown

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

Matt Bierner (mjbvz) commented Jun 1, 2018 •

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown

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