Reputation: 7891
Is it possible to make Visual Studio Code run cargo fmt
on file save?
Upvotes: 55
Views: 41931
Reputation: 481
Posting on behalf of @Stacks. Here's a more complete set of steps that will identify any problems with steps 1-2. Just enabling "format on save" may not always be enough:
Editor: Format On Save
(as in the other answers)The last steps 3-4 are important, because if your formatting preferences are incorrect or if there are duplicate formatters installed, it will fail and prompt you to select the correct one to use.
Upvotes: 1
Reputation: 5008
If you have formatOnSaveMode is set to modifications or modificationsIfAvailable, you may need to change it to file. This is probably a small issue with the analyzer.
And if you want to leave the default settings unchanged, do it for only rust;
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "file"
},
Upvotes: 18
Reputation: 15
Install rust-analyzer and add this to your settings.json
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
}
Upvotes: -1
Reputation: 329
This is what worked for me. In the file settings.json somewhere inside the curly braces insert the following :
"editor.formatOnSave": true,
"editor.formatOnType": true,
"rust-analyzer.rustfmt.enableRangeFormatting": true,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
},
Upvotes: 22
Reputation: 1232
Install the extension rust-analyzer (the officially recommended vscode extension), and add the following to settings.json:
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
}
Upvotes: 106
Reputation: 5117
Current version of the Rust extension > 0.7.8 requires nothing else to be installed. Enable formatOnSave
in VS Code settings.json
file:
"[rust]": {
"editor.formatOnSave": true
}
Upvotes: 9
Reputation: 43733
editor.formatOnSave
).Upvotes: 30