Add support for using uv as an alternative formatter backend (#19665)

This adds a new `backend: internal | uv` option to the LSP
`FormatOptions` allowing users to perform document and range formatting
operations though uv. The idea here is to prototype a solution for users
to transition to a `uv format` command without encountering version
mismatches (and consequently, formatting differences) between the LSP's
version of `ruff` and uv's version of `ruff`.

The primarily alternative to this would be to use uv to discover the
`ruff` version used to start the LSP in the first place. However, this
would increase the scope of a minimal `uv format` command beyond "run a
formatter", and raise larger questions about how uv should be used to
coordinate toolchain discovery. I think those are good things to
explore, but I'm hesitant to let them block a `uv format`
implementation. Another downside of using uv to discover `ruff`, is that
it needs to be implemented _outside_ the LSP; e.g., we'd need to change
the instructions on how to run the LSP and implement it in each editor
integration, like the VS Code plugin.

---------

Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
This commit is contained in:
Zanie Blue
2025-09-09 10:09:53 -05:00
committed by GitHub
parent 79706a2e26
commit 9cdac2d6fb
12 changed files with 696 additions and 22 deletions

View File

@@ -971,6 +971,62 @@ Whether to enable Ruff's preview mode when formatting.
}
```
### `backend` {: #format_backend }
The backend to use for formatting files. Following options are available:
- `"internal"`: Use the built-in Ruff formatter
- `"uv"`: Use uv for formatting (requires uv >= 0.8.13)
For `internal`, the formatter version will match the selected Ruff version while for `uv`, the
formatter version may differ.
**Default value**: `"internal"`
**Type**: `"internal" | "uv"`
**Example usage**:
=== "VS Code"
```json
{
"ruff.format.backend": "uv"
}
```
=== "Neovim"
```lua
require('lspconfig').ruff.setup {
init_options = {
settings = {
format = {
backend = "uv"
}
}
}
}
```
=== "Zed"
```json
{
"lsp": {
"ruff": {
"initialization_options": {
"settings": {
"format": {
"backend": "uv"
}
}
}
}
}
}
```
## VS Code specific
Additionally, the Ruff extension provides the following settings specific to VS Code. These settings