Reputation: 1160
Someone in my team uses vim, but the rest of us use VS Code's default formatter (i.e. vscode.typescript-language-features
). I'd like to add a git precommit hook or something that invokes the formatter without opening up the app.
See also:
Upvotes: 5
Views: 2670
Reputation: 50035
VS Code doesn't have "a formatter". It has multiple builtin formatting providers and extensions can implement their own formatting providers as well.
There's no easy way to do what is being asked for as far as I am aware at the time of this writing.
I'd suggest to figure out if there's an underlying formatting tool that whichever formatter you're using in VS Code is built on top of / wrapping, and then find out if you can invoke that tool from the commandline. This will probably require reading some docs and/or source code. I give an example of the process for doing this in my answer to What JSON formatter does Visual Studio Code use to format JSON?. It's a bit involved and the exact process very likely varies significantly depending on the formatter / VS Code extension providing the formatting capability, so this is kind of the best answer I can imagine giving without giving a thorough answer for every formatter under the sun for all time here, which would be too broad.
But to try to generalize,
{}
icon to get a popup with that info. Or check if you've touched the editor.defaultFormatter
setting for any language override settings.vscode/extensions/
directory under your user home directory)depndencies
property of the extension manifest (package.json file) gives any info, or otherwise grep for things related to DocumentFormattingEditProvider
and see if there are any calls to a non-JS program.This could theoretically be simpler if VS Code made it so, but it does not- at least- not at the time of this writing. See Possible to invoke a VSCode extension command from command line? and Can a command be send to VSCode, directly from the integrated terminal (integrated terminal -> VSCode API)?, for example.
Upvotes: 4
Reputation: 1
Format Document (ctrl+shift+I) - Format the entire active file.
Format Selection (ctrl+K - ctrl+F) - Format the selected text.
Upvotes: -3
Reputation: 1160
As of July 2021, it seems vscode does not provide any way to run its formatter outside of the app itself
Upvotes: 2
Reputation: 711
There is a great git hooks tool called Husky and its documentation can be found here.
Here is an example of it inside of a package.json
file that uses pretty-quick to execute prettier whenever a git pre-commit
is executed.
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
....
I find this method far superior to running prettier on every file save.
Upvotes: 1