mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Introduce Token element (#7048)
This commit is contained in:
parent
2f3a950f6f
commit
c05e4628b1
78 changed files with 733 additions and 723 deletions
|
@ -16,7 +16,7 @@
|
|||
///
|
||||
/// # fn main() -> FormatResult<()> {
|
||||
/// let formatted = format!(SimpleFormatContext::default(), [
|
||||
/// format_args!(text("Hello World"))
|
||||
/// format_args!(token("Hello World"))
|
||||
/// ])?;
|
||||
///
|
||||
/// assert_eq!("Hello World", formatted.print()?.as_code());
|
||||
|
@ -52,15 +52,15 @@ macro_rules! format_args {
|
|||
/// # fn main() -> FormatResult<()> {
|
||||
/// let mut state = FormatState::new(SimpleFormatContext::default());
|
||||
/// let mut buffer = VecBuffer::new(&mut state);
|
||||
/// write!(&mut buffer, [text("Hello"), space()])?;
|
||||
/// write!(&mut buffer, [text("World")])?;
|
||||
/// write!(&mut buffer, [token("Hello"), space()])?;
|
||||
/// write!(&mut buffer, [token("World")])?;
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// buffer.into_vec(),
|
||||
/// vec![
|
||||
/// FormatElement::StaticText { text: "Hello" },
|
||||
/// FormatElement::Token { text: "Hello" },
|
||||
/// FormatElement::Space,
|
||||
/// FormatElement::StaticText { text: "World" },
|
||||
/// FormatElement::Token { text: "World" },
|
||||
/// ]
|
||||
/// );
|
||||
/// # Ok(())
|
||||
|
@ -86,10 +86,10 @@ macro_rules! write {
|
|||
/// let mut state = FormatState::new(SimpleFormatContext::default());
|
||||
/// let mut buffer = VecBuffer::new(&mut state);
|
||||
///
|
||||
/// dbg_write!(buffer, [text("Hello")])?;
|
||||
/// dbg_write!(buffer, [token("Hello")])?;
|
||||
/// // ^-- prints: [src/main.rs:7][0] = StaticToken("Hello")
|
||||
///
|
||||
/// assert_eq!(buffer.into_vec(), vec![FormatElement::StaticText { text: "Hello" }]);
|
||||
/// assert_eq!(buffer.into_vec(), vec![FormatElement::Token { text: "Hello" }]);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
|
@ -126,14 +126,14 @@ macro_rules! dbg_write {
|
|||
/// use ruff_formatter::prelude::*;
|
||||
/// use ruff_formatter::format;
|
||||
///
|
||||
/// let formatted = format!(SimpleFormatContext::default(), [text("("), text("a"), text(")")]).unwrap();
|
||||
/// let formatted = format!(SimpleFormatContext::default(), [token("("), token("a"), token(")")]).unwrap();
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// formatted.into_document(),
|
||||
/// Document::from(vec![
|
||||
/// FormatElement::StaticText { text: "(" },
|
||||
/// FormatElement::StaticText { text: "a" },
|
||||
/// FormatElement::StaticText { text: ")" },
|
||||
/// FormatElement::Token { text: "(" },
|
||||
/// FormatElement::Token { text: "a" },
|
||||
/// FormatElement::Token { text: ")" },
|
||||
/// ])
|
||||
/// );
|
||||
/// ```
|
||||
|
@ -160,49 +160,49 @@ macro_rules! format {
|
|||
/// let formatted = format!(
|
||||
/// SimpleFormatContext::default(),
|
||||
/// [
|
||||
/// text("aVeryLongIdentifier"),
|
||||
/// token("aVeryLongIdentifier"),
|
||||
/// best_fitting!(
|
||||
/// // Everything fits on a single line
|
||||
/// format_args!(
|
||||
/// text("("),
|
||||
/// token("("),
|
||||
/// group(&format_args![
|
||||
/// text("["),
|
||||
/// token("["),
|
||||
/// soft_block_indent(&format_args![
|
||||
/// text("1,"),
|
||||
/// token("1,"),
|
||||
/// soft_line_break_or_space(),
|
||||
/// text("2,"),
|
||||
/// token("2,"),
|
||||
/// soft_line_break_or_space(),
|
||||
/// text("3"),
|
||||
/// token("3"),
|
||||
/// ]),
|
||||
/// text("]")
|
||||
/// token("]")
|
||||
/// ]),
|
||||
/// text(")")
|
||||
/// token(")")
|
||||
/// ),
|
||||
///
|
||||
/// // Breaks after `[`, but prints all elements on a single line
|
||||
/// format_args!(
|
||||
/// text("("),
|
||||
/// text("["),
|
||||
/// block_indent(&text("1, 2, 3")),
|
||||
/// text("]"),
|
||||
/// text(")"),
|
||||
/// token("("),
|
||||
/// token("["),
|
||||
/// block_indent(&token("1, 2, 3")),
|
||||
/// token("]"),
|
||||
/// token(")"),
|
||||
/// ),
|
||||
///
|
||||
/// // Breaks after `[` and prints each element on a single line
|
||||
/// format_args!(
|
||||
/// text("("),
|
||||
/// token("("),
|
||||
/// block_indent(&format_args![
|
||||
/// text("["),
|
||||
/// token("["),
|
||||
/// block_indent(&format_args![
|
||||
/// text("1,"),
|
||||
/// token("1,"),
|
||||
/// hard_line_break(),
|
||||
/// text("2,"),
|
||||
/// token("2,"),
|
||||
/// hard_line_break(),
|
||||
/// text("3"),
|
||||
/// token("3"),
|
||||
/// ]),
|
||||
/// text("]"),
|
||||
/// token("]"),
|
||||
/// ]),
|
||||
/// text(")")
|
||||
/// token(")")
|
||||
/// )
|
||||
/// )
|
||||
/// ]
|
||||
|
@ -251,38 +251,38 @@ macro_rules! format {
|
|||
/// best_fitting!(
|
||||
/// // Prints the method call on the line but breaks the array.
|
||||
/// format_args!(
|
||||
/// text("expect(a).toMatch("),
|
||||
/// token("expect(a).toMatch("),
|
||||
/// group(&format_args![
|
||||
/// text("["),
|
||||
/// token("["),
|
||||
/// soft_block_indent(&format_args![
|
||||
/// text("1,"),
|
||||
/// token("1,"),
|
||||
/// soft_line_break_or_space(),
|
||||
/// text("2,"),
|
||||
/// token("2,"),
|
||||
/// soft_line_break_or_space(),
|
||||
/// text("3"),
|
||||
/// token("3"),
|
||||
/// ]),
|
||||
/// text("]")
|
||||
/// token("]")
|
||||
/// ]).should_expand(true),
|
||||
/// text(")")
|
||||
/// token(")")
|
||||
/// ),
|
||||
///
|
||||
/// // Breaks after `(`
|
||||
/// format_args!(
|
||||
/// text("expect(a).toMatch("),
|
||||
/// token("expect(a).toMatch("),
|
||||
/// group(&soft_block_indent(
|
||||
/// &group(&format_args![
|
||||
/// text("["),
|
||||
/// token("["),
|
||||
/// soft_block_indent(&format_args![
|
||||
/// text("1,"),
|
||||
/// token("1,"),
|
||||
/// soft_line_break_or_space(),
|
||||
/// text("2,"),
|
||||
/// token("2,"),
|
||||
/// soft_line_break_or_space(),
|
||||
/// text("3"),
|
||||
/// token("3"),
|
||||
/// ]),
|
||||
/// text("]")
|
||||
/// token("]")
|
||||
/// ]).should_expand(true),
|
||||
/// )).should_expand(true),
|
||||
/// text(")")
|
||||
/// token(")")
|
||||
/// ),
|
||||
/// )
|
||||
/// ]
|
||||
|
@ -345,7 +345,7 @@ mod tests {
|
|||
|
||||
impl Format<()> for TestFormat {
|
||||
fn fmt(&self, f: &mut Formatter<()>) -> FormatResult<()> {
|
||||
write!(f, [text("test")])
|
||||
write!(f, [token("test")])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -358,7 +358,7 @@ mod tests {
|
|||
|
||||
assert_eq!(
|
||||
buffer.into_vec(),
|
||||
vec![FormatElement::StaticText { text: "test" }]
|
||||
vec![FormatElement::Token { text: "test" }]
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -369,18 +369,18 @@ mod tests {
|
|||
|
||||
write![
|
||||
&mut buffer,
|
||||
[text("a"), space(), text("simple"), space(), TestFormat]
|
||||
[token("a"), space(), token("simple"), space(), TestFormat]
|
||||
]
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
buffer.into_vec(),
|
||||
vec![
|
||||
FormatElement::StaticText { text: "a" },
|
||||
FormatElement::Token { text: "a" },
|
||||
FormatElement::Space,
|
||||
FormatElement::StaticText { text: "simple" },
|
||||
FormatElement::Token { text: "simple" },
|
||||
FormatElement::Space,
|
||||
FormatElement::StaticText { text: "test" }
|
||||
FormatElement::Token { text: "test" }
|
||||
]
|
||||
);
|
||||
}
|
||||
|
@ -394,41 +394,41 @@ mod tests {
|
|||
let formatted_best_fitting = format!(
|
||||
SimpleFormatContext::default(),
|
||||
[
|
||||
text("aVeryLongIdentifier"),
|
||||
token("aVeryLongIdentifier"),
|
||||
soft_line_break_or_space(),
|
||||
best_fitting![
|
||||
format_args![text(
|
||||
format_args![token(
|
||||
"Something that will not fit on a line with 30 character print width."
|
||||
)],
|
||||
format_args![group(&format_args![
|
||||
text("Start"),
|
||||
token("Start"),
|
||||
soft_line_break(),
|
||||
group(&soft_block_indent(&format_args![
|
||||
text("1,"),
|
||||
token("1,"),
|
||||
soft_line_break_or_space(),
|
||||
text("2,"),
|
||||
token("2,"),
|
||||
soft_line_break_or_space(),
|
||||
text("3"),
|
||||
token("3"),
|
||||
])),
|
||||
soft_line_break_or_space(),
|
||||
soft_block_indent(&format_args![
|
||||
text("1,"),
|
||||
token("1,"),
|
||||
soft_line_break_or_space(),
|
||||
text("2,"),
|
||||
token("2,"),
|
||||
soft_line_break_or_space(),
|
||||
group(&format_args!(
|
||||
text("A,"),
|
||||
token("A,"),
|
||||
soft_line_break_or_space(),
|
||||
text("B")
|
||||
token("B")
|
||||
)),
|
||||
soft_line_break_or_space(),
|
||||
text("3")
|
||||
token("3")
|
||||
]),
|
||||
soft_line_break_or_space(),
|
||||
text("End")
|
||||
token("End")
|
||||
])
|
||||
.should_expand(true)],
|
||||
format_args!(text("Most"), hard_line_break(), text("Expanded"))
|
||||
format_args!(token("Most"), hard_line_break(), token("Expanded"))
|
||||
]
|
||||
]
|
||||
)
|
||||
|
@ -439,34 +439,34 @@ mod tests {
|
|||
let formatted_normal_list = format!(
|
||||
SimpleFormatContext::default(),
|
||||
[
|
||||
text("aVeryLongIdentifier"),
|
||||
token("aVeryLongIdentifier"),
|
||||
soft_line_break_or_space(),
|
||||
format_args![
|
||||
text("Start"),
|
||||
token("Start"),
|
||||
soft_line_break(),
|
||||
&group(&soft_block_indent(&format_args![
|
||||
text("1,"),
|
||||
token("1,"),
|
||||
soft_line_break_or_space(),
|
||||
text("2,"),
|
||||
token("2,"),
|
||||
soft_line_break_or_space(),
|
||||
text("3"),
|
||||
token("3"),
|
||||
])),
|
||||
soft_line_break_or_space(),
|
||||
&soft_block_indent(&format_args![
|
||||
text("1,"),
|
||||
token("1,"),
|
||||
soft_line_break_or_space(),
|
||||
text("2,"),
|
||||
token("2,"),
|
||||
soft_line_break_or_space(),
|
||||
group(&format_args!(
|
||||
text("A,"),
|
||||
token("A,"),
|
||||
soft_line_break_or_space(),
|
||||
text("B")
|
||||
token("B")
|
||||
)),
|
||||
soft_line_break_or_space(),
|
||||
text("3")
|
||||
token("3")
|
||||
]),
|
||||
soft_line_break_or_space(),
|
||||
text("End")
|
||||
token("End")
|
||||
],
|
||||
]
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue