mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-02 12:58:20 +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.
83 lines
1.8 KiB
Rust
83 lines
1.8 KiB
Rust
use {
|
|
ruff_text_size::{TextRange, TextSize},
|
|
serde_test::{assert_de_tokens_error, assert_tokens, Token},
|
|
std::ops,
|
|
};
|
|
|
|
fn size(x: u32) -> TextSize {
|
|
TextSize::from(x)
|
|
}
|
|
|
|
fn range(x: ops::Range<u32>) -> TextRange {
|
|
TextRange::new(x.start.into(), x.end.into())
|
|
}
|
|
|
|
#[test]
|
|
fn size_serialization() {
|
|
assert_tokens(&size(00), &[Token::U32(00)]);
|
|
assert_tokens(&size(10), &[Token::U32(10)]);
|
|
assert_tokens(&size(20), &[Token::U32(20)]);
|
|
assert_tokens(&size(30), &[Token::U32(30)]);
|
|
}
|
|
|
|
#[test]
|
|
fn range_serialization() {
|
|
assert_tokens(
|
|
&range(00..10),
|
|
&[
|
|
Token::Tuple { len: 2 },
|
|
Token::U32(00),
|
|
Token::U32(10),
|
|
Token::TupleEnd,
|
|
],
|
|
);
|
|
assert_tokens(
|
|
&range(10..20),
|
|
&[
|
|
Token::Tuple { len: 2 },
|
|
Token::U32(10),
|
|
Token::U32(20),
|
|
Token::TupleEnd,
|
|
],
|
|
);
|
|
assert_tokens(
|
|
&range(20..30),
|
|
&[
|
|
Token::Tuple { len: 2 },
|
|
Token::U32(20),
|
|
Token::U32(30),
|
|
Token::TupleEnd,
|
|
],
|
|
);
|
|
assert_tokens(
|
|
&range(30..40),
|
|
&[
|
|
Token::Tuple { len: 2 },
|
|
Token::U32(30),
|
|
Token::U32(40),
|
|
Token::TupleEnd,
|
|
],
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_range_deserialization() {
|
|
assert_tokens::<TextRange>(
|
|
&range(62..92),
|
|
&[
|
|
Token::Tuple { len: 2 },
|
|
Token::U32(62),
|
|
Token::U32(92),
|
|
Token::TupleEnd,
|
|
],
|
|
);
|
|
assert_de_tokens_error::<TextRange>(
|
|
&[
|
|
Token::Tuple { len: 2 },
|
|
Token::U32(92),
|
|
Token::U32(62),
|
|
Token::TupleEnd,
|
|
],
|
|
"invalid range: 92..62",
|
|
);
|
|
}
|