Hi, may I know how to use Black formatter so that it will auto format on save?
The docs:
I have installed the LSP server, what’s the next step for auto format on save?
Thanks in advance
Hi, may I know how to use Black formatter so that it will auto format on save?
The docs:
A more modern alternative for subblack is python-black. It formats documents on save by default and directly uses black within ST’s plugin_host. It doesn’t need langauge servers or anything else.
To enable format on save for LSP, checkout its "lsp_format_on_save"
setting.
Is it possible to do it without this package?
I’d like to use Black’s suggestion with LSP, now the only thing I’m lack of is maybe something to configure Sublime Text to use Black:
This is quite easy on VSCode because there is a dropdown in the setting to choose the default formatter or can do something like:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}
Sublime Text relies on plugins for highler level document formatting features as it requires too many language specific knowledge/functionality to implement it in core. VS Code seems to rely on prettier by default - also an extension.
Sublime Text’s LSP package supports document formatting requests, but it requires language-servers to provide that functionality. It seems pyright doesn’t. Hence lsp_format_on_save
may not have any effect.
There are various formatting plugins availablen none of which I can say much about.
I rely on python-black as it doesn’t require node runtime or any other external dependencies.
Fans of prettier may try Prettiered Format, which seems to provide settings compared to those from VS Code and still seems maintained.
Hi @deathaxe, I really appreciate your patient.
It seems that the developer is already abandoned Python-Black:
However I’ve found a better alternative: https://docs.astral.sh/ruff/editors/setup/#sublime-text
Would you mind helping to see if this will work to format on save?