mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 07:04:53 +00:00

The Ruff autoformatter is going to be based on an intermediate representation (IR) formatted via [Wadler's algorithm](https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf). This is architecturally similar to [Rome](https://github.com/rome/tools), Prettier, [Skip](https://github.com/skiplang/skip/blob/master/src/tools/printer/printer.sk), and others. This PR adds a fork of the `rome_formatter` crate from [Rome](https://github.com/rome/tools), renamed here to `ruff_formatter`, which provides generic definitions for a formatter IR as well as a generic IR printer. (We've also pulled in `rome_rowan`, `rome_text_size`, and `rome_text_edit`, though some of these will be removed in future PRs.) Why fork? `rome_formatter` contains code that's specific to Rome's AST representation (e.g., it relies on a fork of rust-analyzer's `rowan`), and we'll likely want to support different abstractions and formatting capabilities (there are already a few changes coming in future PRs). Once we've dropped `ruff_rowan` and trimmed down `ruff_formatter` to the code we currently need, it's also not a huge surface area to maintain and update.
24 lines
448 B
Rust
24 lines
448 B
Rust
use ruff_text_size::TextSize;
|
|
|
|
#[derive(Copy, Clone)]
|
|
struct BadRope<'a>(&'a [&'a str]);
|
|
|
|
impl BadRope<'_> {
|
|
fn text_len(self) -> TextSize {
|
|
self.0.iter().copied().map(TextSize::of).sum()
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn main() {
|
|
let x: char = 'c';
|
|
let _ = TextSize::of(x);
|
|
|
|
let x: &str = "hello";
|
|
let _ = TextSize::of(x);
|
|
|
|
let x: &String = &"hello".into();
|
|
let _ = TextSize::of(x);
|
|
|
|
let _ = BadRope(&[""]).text_len();
|
|
}
|