Skip to content

Commit 9cdac2d

Browse files
zaniebdhruvmanila
andauthored
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>
1 parent 79706a2 commit 9cdac2d

File tree

12 files changed

+696
-22
lines changed

12 files changed

+696
-22
lines changed

.github/workflows/ci.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ jobs:
259259
uses: taiki-e/install-action@6064345e6658255e90e9500fdf9a06ab77e6909c # v2.57.6
260260
with:
261261
tool: cargo-insta
262+
- name: "Install uv"
263+
uses: astral-sh/setup-uv@e92bafb6253dcd438e0484186d7669ea7a8ca1cc # v6.4.3
264+
with:
265+
enable-cache: "true"
262266
- name: ty mdtests (GitHub annotations)
263267
if: ${{ needs.determine_changes.outputs.ty == 'true' }}
264268
env:
@@ -317,6 +321,10 @@ jobs:
317321
uses: taiki-e/install-action@6064345e6658255e90e9500fdf9a06ab77e6909c # v2.57.6
318322
with:
319323
tool: cargo-insta
324+
- name: "Install uv"
325+
uses: astral-sh/setup-uv@e92bafb6253dcd438e0484186d7669ea7a8ca1cc # v6.4.3
326+
with:
327+
enable-cache: "true"
320328
- name: "Run tests"
321329
shell: bash
322330
env:
@@ -340,6 +348,10 @@ jobs:
340348
uses: taiki-e/install-action@6064345e6658255e90e9500fdf9a06ab77e6909c # v2.57.6
341349
with:
342350
tool: cargo-nextest
351+
- name: "Install uv"
352+
uses: astral-sh/setup-uv@e92bafb6253dcd438e0484186d7669ea7a8ca1cc # v6.4.3
353+
with:
354+
enable-cache: "true"
343355
- name: "Run tests"
344356
shell: bash
345357
env:

crates/ruff_formatter/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,19 @@ impl IndentStyle {
8181
pub const fn is_space(&self) -> bool {
8282
matches!(self, IndentStyle::Space)
8383
}
84+
85+
/// Returns the string representation of the indent style.
86+
pub const fn as_str(&self) -> &'static str {
87+
match self {
88+
IndentStyle::Tab => "tab",
89+
IndentStyle::Space => "space",
90+
}
91+
}
8492
}
8593

8694
impl std::fmt::Display for IndentStyle {
8795
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88-
match self {
89-
IndentStyle::Tab => std::write!(f, "tab"),
90-
IndentStyle::Space => std::write!(f, "space"),
91-
}
96+
f.write_str(self.as_str())
9297
}
9398
}
9499

crates/ruff_formatter/src/printer/printer_options/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,16 @@ impl LineEnding {
139139
LineEnding::CarriageReturn => "\r",
140140
}
141141
}
142+
143+
/// Returns the string used to configure this line ending.
144+
///
145+
/// See [`LineEnding::as_str`] for the actual string representation of the line ending.
146+
#[inline]
147+
pub const fn as_setting_str(&self) -> &'static str {
148+
match self {
149+
LineEnding::LineFeed => "lf",
150+
LineEnding::CarriageReturnLineFeed => "crlf",
151+
LineEnding::CarriageReturn => "cr",
152+
}
153+
}
142154
}

crates/ruff_python_formatter/src/options.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,20 @@ impl QuoteStyle {
252252
pub const fn is_preserve(self) -> bool {
253253
matches!(self, QuoteStyle::Preserve)
254254
}
255+
256+
/// Returns the string representation of the quote style.
257+
pub const fn as_str(&self) -> &'static str {
258+
match self {
259+
QuoteStyle::Single => "single",
260+
QuoteStyle::Double => "double",
261+
QuoteStyle::Preserve => "preserve",
262+
}
263+
}
255264
}
256265

257266
impl fmt::Display for QuoteStyle {
258267
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
259-
match self {
260-
Self::Single => write!(f, "single"),
261-
Self::Double => write!(f, "double"),
262-
Self::Preserve => write!(f, "preserve"),
263-
}
268+
f.write_str(self.as_str())
264269
}
265270
}
266271

@@ -302,10 +307,10 @@ impl MagicTrailingComma {
302307

303308
impl fmt::Display for MagicTrailingComma {
304309
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
305-
match self {
306-
Self::Respect => write!(f, "respect"),
307-
Self::Ignore => write!(f, "ignore"),
308-
}
310+
f.write_str(match self {
311+
MagicTrailingComma::Respect => "respect",
312+
MagicTrailingComma::Ignore => "ignore",
313+
})
309314
}
310315
}
311316

crates/ruff_server/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ insta = { workspace = true }
5050
[target.'cfg(target_vendor = "apple")'.dependencies]
5151
libc = { workspace = true }
5252

53+
[features]
54+
test-uv = []
55+
5356
[lints]
5457
workspace = true

0 commit comments

Comments
 (0)