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

Allow autoSave and formatOnSave #45997

Closed
andrecasal opened this issue Mar 16, 2018 · 23 comments
Closed

Allow autoSave and formatOnSave #45997

andrecasal opened this issue Mar 16, 2018 · 23 comments

Comments

@andrecasal
Copy link

@andrecasal andrecasal commented Mar 16, 2018

VS Code documentation regarding configuration says that formatOnSave only works when autoSave is turned off.

The formatOnSave config:

// Format a file on save. A formatter must be available, the file must not be auto-saved,
// and editor must not be shutting down.
  "editor.formatOnSave": true,

and the autoSave config:

// Controls auto save of dirty files. Accepted values:  'off', 'afterDelay',
// 'onFocusChange' (editor loses focus), 'onWindowChange' (window loses focus).
// If set to 'afterDelay', you can configure the delay in 'files.autoSaveDelay'.
  "files.autoSave": "off",

// Controls the delay in ms after which a dirty file is saved automatically.
// Only applies when 'files.autoSave' is set to 'afterDelay'
  "files.autoSaveDelay": 1000,

Both live inside the user settings.

It would be a great developer experience to have files automatically saved AND format the documents automatically.

There's a closed issue with a similar title #37820, but the author was looking to solve an extension bug and it got closed when it was solved. I think this issue remains an important feature request.

@campelo
Copy link

@campelo campelo commented Oct 9, 2018

I think this issue remains an important feature request.

I agree with you.

@eladcandroid
Copy link

@eladcandroid eladcandroid commented Dec 19, 2018

Any news about this issue? It can be very helpful...

@varthc
Copy link

@varthc varthc commented Feb 16, 2019

+1 This feature would be awesome.

@ildarmgt
Copy link

@ildarmgt ildarmgt commented Mar 3, 2019

Might be related: it doesn't say it for "files.trimTrailingWhitespace": true, but it doesn't seem to trim trailing white spaces when it auto saves "files.autoSave": "afterDelay", but works when you save the file (like with ctrl-s). Similar behavior seen elsewhere. Autosave treated differently than regular Save on my end.

@andraz
Copy link

@andraz andraz commented Mar 26, 2019

I would paraphrase the request a bit:

How can we get the functionality a setting named "editor.formatOnAutoSave": true would provide us?

At the moment I am struggling to make Prettier format code as soon as I change focus which triggers auto-save. Both work on their own, but do not work together.

expected result:
File gets formatted first, then gets saved.

actual result:
File gets saved without formatting.

edit: We could manage to hack together a solution using this extension and prettier's command line interface, but why should all other less hack-wise users stay affected by this bug?

https://github.com/emeraldwalk/vscode-runonsave

@JerryGreen
Copy link

@JerryGreen JerryGreen commented May 31, 2019

expected result:
File gets formatted first, then gets saved.

actual result:
File gets saved without formatting.

This is not true. Having autoSave and formatOnSave both set to true, it works properly: it gets formatted, then saved.

However, I agree that "editor.formatOnAutoSave": true | false setting is a good idea. I don't want my file to be formatted on autoSave, I want it to get formatted on a manual save only.

If you try to make a keybinding cmd+s to assign editor.action.format, then yes - it works improperly, it gets saved, then gets formatted.

@lopsch
Copy link

@lopsch lopsch commented Oct 9, 2019

expected result:
File gets formatted first, then gets saved.

actual result:
File gets saved without formatting.

This is not true. Having autoSave and formatOnSave both set to true, it works properly: it gets formatted, then saved.

autoSave can't be set to true. If it is set to afterDelay the formatOnSave will not work even if this is set to true:

Format a file on save. A formatter must be available, the file must not be saved after delay, and the editor must not be shutting down.

At least this is my understanding.

@JerryGreen
Copy link

@JerryGreen JerryGreen commented Oct 9, 2019

@lopsch oh sorry by autoSave set to true I actually meant it's not disabled, and in my case it's set to onFocusChange. What I have, basically, in my settings.json:

"editor.codeActionsOnSave": {
  "source.fixAll.tslint": true
},
"editor.formatOnSave": true,
"files.autoSave": "onFocusChange",

And it works just fine. At least from VSCode side. Sometimes it happened that tslint was broken in newer versions, so I had to change version to an older one (currently 1.2.1 TSLint for VSCode works just fine).

So I don't see any problem here, actually. And...

It would be a great developer experience to have files automatically saved AND format the documents automatically.

It's already working in such a way. Both Prettier (which is controlled by editor.formatOnSave) and TSLint (which is controlled by editor.codeActionsOnSave and source.fixAll.tslint). At least they are working for me. And automatically on save, which is happened when my tab lose its focus (or if I save manually too).

So I think this issue should be closed.

@andrecasal
Copy link
Author

@andrecasal andrecasal commented Oct 9, 2019

This feature isn't yet implemented @JerryGreen

To reproduce:

  1. Copy this to an HTML file:
<!DOCTYPE html>
<html>
<head>
	<title>Hello World</title>
</head>
<body>
	<div>Hello world!</div>
</body>
</html>
  1. Set the following settings.json settings:
{
	"html.format.indentInnerHtml": true,
	"files.autoSave": "afterDelay",
	"editor.formatOnSave": true,
}
  1. Make a change to the file and wait for the afterDelay to trigger a save.
  2. Notice that the <head> and the <body> tags are not indented as per the "html.format.indentInnerHtml": true setting.

However, if you save the file manually by pressing Cmd+s, it will trigger a file format.

@JerryGreen
Copy link

@JerryGreen JerryGreen commented Oct 9, 2019

@andrecasal oh, that's true, - just checked this setting. It does not work together. It has something to do with "files.autoSave": "afterDelay" setting. I don't really like this setting though. In more complex cases (than HTML formatter case) this will work in a pretty weird way, I suppose. But you're right, - it's not working.

@jrieken jrieken self-assigned this Oct 22, 2019
@davidbarton
Copy link

@davidbarton davidbarton commented Dec 9, 2019

For me it is very important part of developer experience. I know it is already possible to hack some linters to get the code formatted for you. For example Prettier + TSLint or rust-lang can do that.

As described above this should be possible for all formatters using simple setting. Is there a chance to get official response from team on this issue please? I see @jrieken is now assigned to the issue.

@jrieken jrieken added this to the Backlog milestone Jan 13, 2020
@carlogonzalo
Copy link

@carlogonzalo carlogonzalo commented Jun 26, 2020

+1 for this feature and suprising that it is not possible at the moment considering awsome vscode.

@rtbo
Copy link

@rtbo rtbo commented Aug 5, 2020

+1
I confirm that auto formatting is done with onFocusChange and onWindowChange, but not with afterDelay.

Note that some formatters take a substantial time to execute and editing experience could quickly become annoying, so the current behavior is not unsound.
An additional setting like editor.formatOnAutoSave is needed.

@symonxdd
Copy link

@symonxdd symonxdd commented Aug 17, 2020

Yup, would be nice to have this feature, 2 years later...

@vscode-triage-bot
Copy link
Collaborator

@vscode-triage-bot vscode-triage-bot commented Oct 6, 2020

We closed this issue because we don't plan to address it in the foreseeable future. You can find more detailed information about our decision-making process here. If you disagree and feel that this issue is crucial: We are happy to listen and to reconsider.

If you wonder what we are up to, please see our roadmap and issue reporting guidelines.

Thanks for your understanding and happy coding!

@julix-unity
Copy link

@julix-unity julix-unity commented Mar 1, 2021

"We are happy to listen and to reconsider." - Please reconsider. Fair amount of up votes. What's the main concern with implementing this? Performance?

@steinybot
Copy link

@steinybot steinybot commented Mar 25, 2021

What's worse is that if you manually save after the auto save delay then it doesn't run the formatter either (#119857).

@maljaroudi
Copy link

@maljaroudi maljaroudi commented Apr 5, 2021

I don't see why we still don't have this option in vscode. If the concern on implementing this is about performance, then that's why it's an option, like any other performance impacting options.

@skngetich
Copy link

@skngetich skngetich commented Apr 6, 2021

How can we get it to be added as a feature?
I have been waiting on this feature for 3 years now

@RafaelGlober
Copy link

@RafaelGlober RafaelGlober commented Apr 7, 2021

I literally discover the "AutoSave" setting today, and instantly I started searching "how to format on autosave" and now I'm commenting on this thread... so if you can see... "format on auto save" is a real need.

@ktvdev
Copy link

@ktvdev ktvdev commented Apr 10, 2021

In the same boat here, somehow missed that autosave was a thing, discovered it is, tried to make it work with format on save and no go.

I know very little about the inner-workings of VSCode since I've had little interest in that aspect of it so I'm not sure if a better workaround exists, but in the meantime the quickest solution I've found is using an extension that forces the format command.

The extension in question: Run on Save
Specifically the one I linked, the first one I tried had more downloads but didn't work for me at all.


   Then in your settings.json add something along the lines of:

"runOnSave.commands": [
    {
        "match": ".ps1",
        "command": "editor.action.formatDocument",
        "runIn": "vscode"
    }
]


Enable files.autoSave as normal, match your filetype or use your own regex in settings and you should be good to go.

@rudyhadoux
Copy link

@rudyhadoux rudyhadoux commented Apr 23, 2021

Hi, "format on auto save after delay" is a real need indeed.

@skngetich
Copy link

@skngetich skngetich commented May 4, 2021

In the same boat here, somehow missed that autosave was a thing, discovered it is, tried to make it work with format on save and no go.

I know very little about the inner-workings of VSCode since I've had little interest in that aspect of it so I'm not sure if a better workaround exists, but in the meantime the quickest solution I've found is using an extension that forces the format command.

The extension in question: Run on Save
Specifically the one I linked, the first one I tried had more downloads but didn't work for me at all.

   Then in your settings.json add something along the lines of:

"runOnSave.commands": [
    {
        "match": ".ps1",
        "command": "editor.action.formatDocument",
        "runIn": "vscode"
    }
]

Enable files.autoSave as normal, match your filetype or use your own regex in settings and you should be good to go.

As we wait for this feature.This hack by @ktvdev works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet