mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Introduce Token element (#7048)
This commit is contained in:
parent
2f3a950f6f
commit
c05e4628b1
78 changed files with 733 additions and 723 deletions
|
@ -27,9 +27,9 @@ impl<'ast> Format<PyFormatContext<'ast>> for ParenthesizeIfExpands<'_, 'ast> {
|
|||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
if_group_breaks(&text("(")),
|
||||
if_group_breaks(&token("(")),
|
||||
soft_block_indent(&Arguments::from(&self.inner)),
|
||||
if_group_breaks(&text(")")),
|
||||
if_group_breaks(&token(")")),
|
||||
])]
|
||||
)
|
||||
}
|
||||
|
@ -152,7 +152,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
|||
{
|
||||
self.result = self.result.and_then(|_| {
|
||||
if self.entries.is_one_or_more() {
|
||||
write!(self.fmt, [text(","), separator])?;
|
||||
write!(self.fmt, [token(","), separator])?;
|
||||
}
|
||||
|
||||
self.entries = self.entries.next(node.end());
|
||||
|
@ -204,7 +204,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> {
|
|||
|| self.trailing_comma == TrailingComma::OneOrMore
|
||||
|| self.entries.is_more_than_one()
|
||||
{
|
||||
if_group_breaks(&text(",")).fmt(self.fmt)?;
|
||||
if_group_breaks(&token(",")).fmt(self.fmt)?;
|
||||
}
|
||||
|
||||
if magic_trailing_comma {
|
||||
|
|
|
@ -434,7 +434,7 @@ impl Format<PyFormatContext<'_>> for FormatNormalizedComment<'_> {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
dynamic_text(owned, Some(self.range.start())),
|
||||
text(owned, Some(self.range.start())),
|
||||
source_position(self.range.end())
|
||||
]
|
||||
)
|
||||
|
|
|
@ -113,7 +113,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
|
|||
f,
|
||||
[
|
||||
dangling_comments(before_dot),
|
||||
text("."),
|
||||
token("."),
|
||||
dangling_comments(after_dot),
|
||||
attr.format()
|
||||
]
|
||||
|
|
|
@ -16,7 +16,7 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("await"),
|
||||
token("await"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
|
|
|
@ -260,7 +260,7 @@ impl FormatRule<Operator, PyFormatContext<'_>> for FormatOperator {
|
|||
Operator::FloorDiv => "//",
|
||||
};
|
||||
|
||||
text(operator).fmt(f)
|
||||
token(operator).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -132,6 +132,6 @@ impl FormatRule<BoolOp, PyFormatContext<'_>> for FormatBoolOp {
|
|||
BoolOp::Or => "or",
|
||||
};
|
||||
|
||||
text(operator).fmt(f)
|
||||
token(operator).fmt(f)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -113,6 +113,6 @@ impl FormatRule<CmpOp, PyFormatContext<'_>> for FormatCmpOp {
|
|||
CmpOp::NotIn => "not in",
|
||||
};
|
||||
|
||||
text(operator).fmt(f)
|
||||
token(operator).fmt(f)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -42,11 +42,11 @@ impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
|||
} = item;
|
||||
|
||||
match value {
|
||||
Constant::Ellipsis => text("...").fmt(f),
|
||||
Constant::None => text("None").fmt(f),
|
||||
Constant::Ellipsis => token("...").fmt(f),
|
||||
Constant::None => token("None").fmt(f),
|
||||
Constant::Bool(value) => match value {
|
||||
true => text("True").fmt(f),
|
||||
false => text("False").fmt(f),
|
||||
true => token("True").fmt(f),
|
||||
false => token("False").fmt(f),
|
||||
},
|
||||
Constant::Int(_) => FormatInt::new(item).fmt(f),
|
||||
Constant::Float(_) => FormatFloat::new(item).fmt(f),
|
||||
|
|
|
@ -34,7 +34,7 @@ impl Format<PyFormatContext<'_>> for KeyValuePair<'_> {
|
|||
f,
|
||||
[group(&format_args![
|
||||
key.format(),
|
||||
text(":"),
|
||||
token(":"),
|
||||
space(),
|
||||
self.value.format()
|
||||
])]
|
||||
|
@ -49,7 +49,7 @@ impl Format<PyFormatContext<'_>> for KeyValuePair<'_> {
|
|||
[
|
||||
// make sure the leading comments are hoisted past the `**`
|
||||
leading_comments(leading_value_comments),
|
||||
group(&format_args![text("**"), self.value.format()])
|
||||
group(&format_args![token("**"), self.value.format()])
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use ruff_formatter::prelude::{
|
||||
format_args, format_with, group, soft_line_break_or_space, space, text,
|
||||
format_args, format_with, group, soft_line_break_or_space, space, token,
|
||||
};
|
||||
use ruff_formatter::write;
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
|
@ -35,7 +35,7 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
|
|||
"{",
|
||||
&group(&format_args!(
|
||||
group(&key.format()),
|
||||
text(":"),
|
||||
token(":"),
|
||||
space(),
|
||||
value.format(),
|
||||
soft_line_break_or_space(),
|
||||
|
|
|
@ -62,12 +62,12 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
|
|||
body.format(),
|
||||
in_parentheses_only_soft_line_break_or_space(),
|
||||
leading_comments(comments.leading(test.as_ref())),
|
||||
text("if"),
|
||||
token("if"),
|
||||
space(),
|
||||
test.format(),
|
||||
in_parentheses_only_soft_line_break_or_space(),
|
||||
leading_comments(comments.leading(orelse.as_ref())),
|
||||
text("else"),
|
||||
token("else"),
|
||||
space(),
|
||||
]
|
||||
)?;
|
||||
|
|
|
@ -21,7 +21,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
|||
let comments = f.context().comments().clone();
|
||||
let dangling = comments.dangling(item);
|
||||
|
||||
write!(f, [text("lambda")])?;
|
||||
write!(f, [token("lambda")])?;
|
||||
|
||||
if let Some(parameters) = parameters {
|
||||
write!(
|
||||
|
@ -35,7 +35,7 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
|||
)?;
|
||||
}
|
||||
|
||||
write!(f, [text(":")])?;
|
||||
write!(f, [token(":")])?;
|
||||
|
||||
if dangling.is_empty() {
|
||||
write!(f, [space()])?;
|
||||
|
|
|
@ -25,7 +25,7 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
|
|||
f,
|
||||
[
|
||||
group(&format_args!(target.format(), soft_line_break_or_space())),
|
||||
text(":=")
|
||||
token(":=")
|
||||
]
|
||||
)?;
|
||||
|
||||
|
|
|
@ -91,7 +91,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
|
|||
if !all_simple && lower.is_some() {
|
||||
space().fmt(f)?;
|
||||
}
|
||||
text(":").fmt(f)?;
|
||||
token(":").fmt(f)?;
|
||||
// No upper node, no need for a space, e.g. `x[a() :]`
|
||||
if !all_simple && upper.is_some() {
|
||||
space().fmt(f)?;
|
||||
|
@ -125,7 +125,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
|
|||
if !all_simple && (upper.is_some() || step.is_none()) {
|
||||
space().fmt(f)?;
|
||||
}
|
||||
text(":").fmt(f)?;
|
||||
token(":").fmt(f)?;
|
||||
// No step node, no need for a space
|
||||
if !all_simple && step.is_some() {
|
||||
space().fmt(f)?;
|
||||
|
|
|
@ -21,7 +21,7 @@ impl FormatNodeRule<ExprStarred> for FormatExprStarred {
|
|||
let comments = f.context().comments().clone();
|
||||
let dangling = comments.dangling(item);
|
||||
|
||||
write!(f, [text("*"), dangling_comments(dangling), value.format()])
|
||||
write!(f, [token("*"), dangling_comments(dangling), value.format()])
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(
|
||||
|
|
|
@ -70,10 +70,10 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
|
|||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
text("["),
|
||||
token("["),
|
||||
trailing_comments(dangling_comments),
|
||||
soft_block_indent(&format_slice),
|
||||
text("]")
|
||||
token("]")
|
||||
])]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -140,12 +140,12 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
|||
TupleParentheses::Preserve
|
||||
if !is_tuple_parenthesized(item, f.context().source()) =>
|
||||
{
|
||||
write!(f, [single.format(), text(",")])
|
||||
write!(f, [single.format(), token(",")])
|
||||
}
|
||||
_ =>
|
||||
// A single element tuple always needs parentheses and a trailing comma, except when inside of a subscript
|
||||
{
|
||||
parenthesized("(", &format_args![single.format(), text(",")], ")")
|
||||
parenthesized("(", &format_args![single.format(), token(",")], ")")
|
||||
.with_dangling_comments(dangling)
|
||||
.fmt(f)
|
||||
}
|
||||
|
@ -166,7 +166,7 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
|||
_ => match self.parentheses {
|
||||
TupleParentheses::Never => {
|
||||
let separator =
|
||||
format_with(|f| group(&format_args![text(","), space()]).fmt(f));
|
||||
format_with(|f| group(&format_args![token(","), space()]).fmt(f));
|
||||
f.join_with(separator)
|
||||
.entries(elts.iter().formatted())
|
||||
.finish()
|
||||
|
|
|
@ -26,7 +26,7 @@ impl FormatNodeRule<ExprUnaryOp> for FormatExprUnaryOp {
|
|||
UnaryOp::USub => "-",
|
||||
};
|
||||
|
||||
text(operator).fmt(f)?;
|
||||
token(operator).fmt(f)?;
|
||||
|
||||
let comments = f.context().comments().clone();
|
||||
|
||||
|
|
|
@ -87,14 +87,14 @@ impl Format<PyFormatContext<'_>> for AnyExpressionYield<'_> {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text(keyword),
|
||||
token(keyword),
|
||||
space(),
|
||||
maybe_parenthesize_expression(val, self, Parenthesize::Optional)
|
||||
]
|
||||
)?;
|
||||
} else {
|
||||
// ExprYieldFrom always has Some(value) so we should never get a bare `yield from`
|
||||
write!(f, [&text(keyword)])?;
|
||||
write!(f, [&token(keyword)])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -248,9 +248,9 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
|||
// The group here is necessary because `format_expression` may contain IR elements
|
||||
// that refer to the group id
|
||||
group(&format_args![
|
||||
text("("),
|
||||
token("("),
|
||||
soft_block_indent(&format_expression),
|
||||
text(")")
|
||||
token(")")
|
||||
])
|
||||
.with_group_id(Some(group_id))
|
||||
.fmt(f)
|
||||
|
@ -267,9 +267,9 @@ impl Format<PyFormatContext<'_>> for MaybeParenthesizeExpression<'_> {
|
|||
// Variant 2:
|
||||
// Try to fit the expression by adding parentheses and indenting the expression.
|
||||
group(&format_args![
|
||||
text("("),
|
||||
token("("),
|
||||
soft_block_indent(&format_expression),
|
||||
text(")")
|
||||
token(")")
|
||||
])
|
||||
.with_group_id(Some(group_id))
|
||||
.should_expand(true),
|
||||
|
|
|
@ -25,7 +25,7 @@ impl Format<PyFormatContext<'_>> for FormatInt<'_> {
|
|||
|
||||
match normalized {
|
||||
Cow::Borrowed(_) => source_text_slice(range, ContainsNewlines::No).fmt(f),
|
||||
Cow::Owned(normalized) => dynamic_text(&normalized, Some(range.start())).fmt(f),
|
||||
Cow::Owned(normalized) => text(&normalized, Some(range.start())).fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ impl Format<PyFormatContext<'_>> for FormatFloat<'_> {
|
|||
|
||||
match normalized {
|
||||
Cow::Borrowed(_) => source_text_slice(range, ContainsNewlines::No).fmt(f),
|
||||
Cow::Owned(normalized) => dynamic_text(&normalized, Some(range.start())).fmt(f),
|
||||
Cow::Owned(normalized) => text(&normalized, Some(range.start())).fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,11 +78,11 @@ impl Format<PyFormatContext<'_>> for FormatComplex<'_> {
|
|||
source_text_slice(range.sub_end(TextSize::from(1)), ContainsNewlines::No).fmt(f)?;
|
||||
}
|
||||
Cow::Owned(normalized) => {
|
||||
dynamic_text(&normalized, Some(range.start())).fmt(f)?;
|
||||
text(&normalized, Some(range.start())).fmt(f)?;
|
||||
}
|
||||
}
|
||||
|
||||
text("j").fmt(f)
|
||||
token("j").fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -172,10 +172,10 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatParenthesized<'_, 'ast> {
|
|||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
||||
let inner = format_with(|f| {
|
||||
group(&format_args![
|
||||
text(self.left),
|
||||
token(self.left),
|
||||
dangling_open_parenthesis_comments(self.comments),
|
||||
soft_block_indent(&Arguments::from(&self.content)),
|
||||
text(self.right)
|
||||
token(self.right)
|
||||
])
|
||||
.fmt(f)
|
||||
});
|
||||
|
@ -232,13 +232,13 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatOptionalParentheses<'_, 'ast>
|
|||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
if_group_breaks(&text("(")),
|
||||
if_group_breaks(&token("(")),
|
||||
indent_if_group_breaks(
|
||||
&format_args![soft_line_break(), Arguments::from(&self.content)],
|
||||
parens_id
|
||||
),
|
||||
soft_line_break(),
|
||||
if_group_breaks(&text(")"))
|
||||
if_group_breaks(&token(")"))
|
||||
])
|
||||
.with_group_id(Some(parens_id))]
|
||||
)
|
||||
|
@ -375,7 +375,7 @@ impl Format<PyFormatContext<'_>> for FormatEmptyParenthesized<'_> {
|
|||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
text(self.left),
|
||||
token(self.left),
|
||||
// end-of-line comments
|
||||
trailing_comments(&self.comments[..end_of_line_split]),
|
||||
// Avoid unstable formatting with
|
||||
|
@ -390,7 +390,7 @@ impl Format<PyFormatContext<'_>> for FormatEmptyParenthesized<'_> {
|
|||
(!self.comments[..end_of_line_split].is_empty()).then_some(hard_line_break()),
|
||||
// own line comments, which need to be indented
|
||||
soft_block_indent(&dangling_comments(&self.comments[end_of_line_split..])),
|
||||
text(self.right)
|
||||
token(self.right)
|
||||
])]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -326,7 +326,7 @@ impl Format<PyFormatContext<'_>> for FormatStringPart {
|
|||
source_text_slice(self.range(), contains_newlines).fmt(f)?;
|
||||
}
|
||||
Cow::Owned(normalized) => {
|
||||
dynamic_text(&normalized, Some(self.start())).fmt(f)?;
|
||||
text(&normalized, Some(self.start())).fmt(f)?;
|
||||
}
|
||||
}
|
||||
self.preferred_quotes.fmt(f)
|
||||
|
@ -386,17 +386,17 @@ impl Format<PyFormatContext<'_>> for StringPrefix {
|
|||
// Retain the casing for the raw prefix:
|
||||
// https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#r-strings-and-r-strings
|
||||
if self.contains(StringPrefix::RAW) {
|
||||
text("r").fmt(f)?;
|
||||
token("r").fmt(f)?;
|
||||
} else if self.contains(StringPrefix::RAW_UPPER) {
|
||||
text("R").fmt(f)?;
|
||||
token("R").fmt(f)?;
|
||||
}
|
||||
|
||||
if self.contains(StringPrefix::BYTE) {
|
||||
text("b").fmt(f)?;
|
||||
token("b").fmt(f)?;
|
||||
}
|
||||
|
||||
if self.contains(StringPrefix::F_STRING) {
|
||||
text("f").fmt(f)?;
|
||||
token("f").fmt(f)?;
|
||||
}
|
||||
|
||||
// Remove the unicode prefix `u` if any because it is meaningless in Python 3+.
|
||||
|
@ -596,7 +596,7 @@ impl Format<PyFormatContext<'_>> for StringQuotes {
|
|||
(QuoteStyle::Double, true) => "\"\"\"",
|
||||
};
|
||||
|
||||
text(quotes).fmt(f)
|
||||
token(quotes).fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -839,7 +839,7 @@ fn format_docstring(string_part: &FormatStringPart, f: &mut PyFormatter) -> Form
|
|||
if already_normalized {
|
||||
source_text_slice(trimmed_line_range, ContainsNewlines::No).fmt(f)?;
|
||||
} else {
|
||||
dynamic_text(trim_both, Some(trimmed_line_range.start())).fmt(f)?;
|
||||
text(trim_both, Some(trimmed_line_range.start())).fmt(f)?;
|
||||
}
|
||||
}
|
||||
offset += first.text_len();
|
||||
|
@ -947,7 +947,7 @@ fn format_docstring_line(
|
|||
let indent_len =
|
||||
count_indentation_like_black(trim_end, f.options().tab_width()) - stripped_indentation;
|
||||
let in_docstring_indent = " ".repeat(indent_len.to_usize()) + trim_end.trim_start();
|
||||
dynamic_text(&in_docstring_indent, Some(offset)).fmt(f)?;
|
||||
text(&in_docstring_indent, Some(offset)).fmt(f)?;
|
||||
} else {
|
||||
// Take the string with the trailing whitespace removed, then also skip the leading
|
||||
// whitespace
|
||||
|
@ -957,7 +957,7 @@ fn format_docstring_line(
|
|||
source_text_slice(trimmed_line_range, ContainsNewlines::No).fmt(f)?;
|
||||
} else {
|
||||
// All indents are ascii spaces, so the slicing is correct
|
||||
dynamic_text(
|
||||
text(
|
||||
&trim_end[stripped_indentation.to_usize()..],
|
||||
Some(trimmed_line_range.start()),
|
||||
)
|
||||
|
|
|
@ -276,16 +276,16 @@ for converter in connection.ops.get_db_converters(
|
|||
f: &mut ruff_formatter::formatter::Formatter<SimpleFormatContext>,
|
||||
) -> FormatResult<()> {
|
||||
let format_str = format_with(|f| {
|
||||
write!(f, [text("\"")])?;
|
||||
write!(f, [token("\"")])?;
|
||||
|
||||
let mut words = self.0.split_whitespace().peekable();
|
||||
let mut fill = f.fill();
|
||||
|
||||
let separator = format_with(|f| {
|
||||
group(&format_args![
|
||||
if_group_breaks(&text("\"")),
|
||||
if_group_breaks(&token("\"")),
|
||||
soft_line_break_or_space(),
|
||||
if_group_breaks(&text("\" "))
|
||||
if_group_breaks(&token("\" "))
|
||||
])
|
||||
.fmt(f)
|
||||
});
|
||||
|
@ -293,10 +293,10 @@ for converter in connection.ops.get_db_converters(
|
|||
while let Some(word) = words.next() {
|
||||
let is_last = words.peek().is_none();
|
||||
let format_word = format_with(|f| {
|
||||
write!(f, [dynamic_text(word, None)])?;
|
||||
write!(f, [text(word, None)])?;
|
||||
|
||||
if is_last {
|
||||
write!(f, [text("\"")])?;
|
||||
write!(f, [token("\"")])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -311,9 +311,9 @@ for converter in connection.ops.get_db_converters(
|
|||
write!(
|
||||
f,
|
||||
[group(&format_args![
|
||||
if_group_breaks(&text("(")),
|
||||
if_group_breaks(&token("(")),
|
||||
soft_block_indent(&format_str),
|
||||
if_group_breaks(&text(")"))
|
||||
if_group_breaks(&token(")"))
|
||||
])]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ impl FormatNodeRule<Alias> for FormatAlias {
|
|||
} = item;
|
||||
name.format().fmt(f)?;
|
||||
if let Some(asname) = asname {
|
||||
write!(f, [space(), text("as"), space(), asname.format()])?;
|
||||
write!(f, [space(), token("as"), space(), asname.format()])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
|
|||
} = item;
|
||||
|
||||
if *is_async {
|
||||
write!(f, [text("async"), space()])?;
|
||||
write!(f, [token("async"), space()])?;
|
||||
}
|
||||
|
||||
let comments = f.context().comments().clone();
|
||||
|
@ -54,14 +54,14 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("for"),
|
||||
token("for"),
|
||||
trailing_comments(before_target_comments),
|
||||
group(&format_args!(
|
||||
Spacer(target),
|
||||
ExprTupleWithoutParentheses(target),
|
||||
in_spacer,
|
||||
leading_comments(before_in_comments),
|
||||
text("in"),
|
||||
token("in"),
|
||||
trailing_comments(trailing_in_comments),
|
||||
Spacer(iter),
|
||||
iter.format(),
|
||||
|
@ -81,7 +81,7 @@ impl FormatNodeRule<Comprehension> for FormatComprehension {
|
|||
);
|
||||
joiner.entry(&group(&format_args!(
|
||||
leading_comments(own_line_if_comments),
|
||||
text("if"),
|
||||
token("if"),
|
||||
trailing_comments(end_of_line_if_comments),
|
||||
Spacer(if_case),
|
||||
if_case.format(),
|
||||
|
|
|
@ -19,7 +19,7 @@ impl FormatNodeRule<Decorator> for FormatDecorator {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("@"),
|
||||
token("@"),
|
||||
maybe_parenthesize_expression(expression, item, Parenthesize::Optional)
|
||||
]
|
||||
)
|
||||
|
|
|
@ -57,10 +57,10 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("except"),
|
||||
token("except"),
|
||||
match self.except_handler_kind {
|
||||
ExceptHandlerKind::Regular => None,
|
||||
ExceptHandlerKind::Starred => Some(text("*")),
|
||||
ExceptHandlerKind::Starred => Some(token("*")),
|
||||
}
|
||||
]
|
||||
)?;
|
||||
|
@ -78,7 +78,7 @@ impl FormatNodeRule<ExceptHandlerExceptHandler> for FormatExceptHandlerExceptHan
|
|||
]
|
||||
)?;
|
||||
if let Some(name) = name {
|
||||
write!(f, [space(), text("as"), space(), name.format()])?;
|
||||
write!(f, [space(), token("as"), space(), name.format()])?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,9 +15,9 @@ impl FormatNodeRule<Keyword> for FormatKeyword {
|
|||
} = item;
|
||||
// Comments after the `=` or `**` are reassigned as leading comments on the value.
|
||||
if let Some(arg) = arg {
|
||||
write!(f, [arg.format(), text("="), value.format()])
|
||||
write!(f, [arg.format(), token("="), value.format()])
|
||||
} else {
|
||||
write!(f, [text("**"), value.format()])
|
||||
write!(f, [token("**"), value.format()])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
ClauseHeader::MatchCase(item),
|
||||
dangling_item_comments,
|
||||
&format_with(|f| {
|
||||
write!(f, [text("case"), space()])?;
|
||||
write!(f, [token("case"), space()])?;
|
||||
|
||||
let has_comments = comments.has_leading(pattern)
|
||||
|| comments.has_trailing_own_line(pattern);
|
||||
|
@ -58,7 +58,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
}
|
||||
|
||||
if let Some(guard) = guard {
|
||||
write!(f, [space(), text("if"), space(), guard.format()])?;
|
||||
write!(f, [space(), token("if"), space(), guard.format()])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -17,7 +17,7 @@ impl FormatNodeRule<Parameter> for FormatParameter {
|
|||
name.format().fmt(f)?;
|
||||
|
||||
if let Some(annotation) = annotation {
|
||||
write!(f, [text(":"), space(), annotation.format()])?;
|
||||
write!(f, [token(":"), space(), annotation.format()])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -18,7 +18,7 @@ impl FormatNodeRule<ParameterWithDefault> for FormatParameterWithDefault {
|
|||
|
||||
if let Some(default) = default {
|
||||
let space = parameter.annotation.is_some().then_some(space());
|
||||
write!(f, [space, text("="), space, group(&default.format())])?;
|
||||
write!(f, [space, token("="), space, group(&default.format())])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -102,7 +102,7 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
dangling.split_at(parenthesis_comments_end);
|
||||
|
||||
let format_inner = format_with(|f: &mut PyFormatter| {
|
||||
let separator = format_with(|f| write!(f, [text(","), soft_line_break_or_space()]));
|
||||
let separator = format_with(|f| write!(f, [token(","), soft_line_break_or_space()]));
|
||||
let mut joiner = f.join_with(separator);
|
||||
let mut last_node: Option<AnyNodeRef> = None;
|
||||
|
||||
|
@ -156,7 +156,7 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
if let Some(vararg) = vararg {
|
||||
joiner.entry(&format_args![
|
||||
leading_node_comments(vararg.as_ref()),
|
||||
text("*"),
|
||||
token("*"),
|
||||
vararg.format()
|
||||
]);
|
||||
last_node = Some(vararg.as_any_node_ref());
|
||||
|
@ -192,7 +192,7 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
if let Some(kwarg) = kwarg {
|
||||
joiner.entry(&format_args![
|
||||
leading_node_comments(kwarg.as_ref()),
|
||||
text("**"),
|
||||
token("**"),
|
||||
kwarg.format()
|
||||
]);
|
||||
last_node = Some(kwarg.as_any_node_ref());
|
||||
|
@ -216,10 +216,10 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
// For lambdas (no parentheses), preserve the trailing comma. It doesn't
|
||||
// behave like a magic trailing comma, it's just preserved
|
||||
if has_trailing_comma(item, last_node, f.context().source()) {
|
||||
write!(f, [text(",")])?;
|
||||
write!(f, [token(",")])?;
|
||||
}
|
||||
} else {
|
||||
write!(f, [if_group_breaks(&text(","))])?;
|
||||
write!(f, [if_group_breaks(&token(","))])?;
|
||||
|
||||
if f.options().magic_trailing_comma().is_respect()
|
||||
&& has_trailing_comma(item, last_node, f.context().source())
|
||||
|
@ -252,10 +252,10 @@ impl FormatNodeRule<Parameters> for FormatParameters {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("("),
|
||||
token("("),
|
||||
dangling_open_parenthesis_comments(parenthesis_dangling),
|
||||
soft_block_indent(&group(&format_inner)),
|
||||
text(")")
|
||||
token(")")
|
||||
]
|
||||
)
|
||||
}
|
||||
|
@ -279,7 +279,7 @@ struct CommentsAroundText<'a> {
|
|||
impl Format<PyFormatContext<'_>> for CommentsAroundText<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
if self.comments.is_empty() {
|
||||
text(self.text).fmt(f)
|
||||
token(self.text).fmt(f)
|
||||
} else {
|
||||
// There might be own line comments in trailing, but those are weird and we can kinda
|
||||
// ignore them
|
||||
|
@ -301,7 +301,7 @@ impl Format<PyFormatContext<'_>> for CommentsAroundText<'_> {
|
|||
f,
|
||||
[
|
||||
leading_comments(leading),
|
||||
text(self.text),
|
||||
token(self.text),
|
||||
trailing_comments(trailing)
|
||||
]
|
||||
)
|
||||
|
|
|
@ -30,7 +30,7 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
|
|||
)?;
|
||||
|
||||
if let Some(optional_vars) = optional_vars {
|
||||
write!(f, [space(), text("as"), space()])?;
|
||||
write!(f, [space(), token("as"), space()])?;
|
||||
|
||||
if trailing_as_comments.is_empty() {
|
||||
write!(f, [optional_vars.format()])?;
|
||||
|
|
|
@ -12,6 +12,6 @@ impl FormatNodeRule<PatternKeyword> for FormatPatternKeyword {
|
|||
attr,
|
||||
pattern,
|
||||
} = item;
|
||||
write!(f, [attr.format(), text("="), pattern.format()])
|
||||
write!(f, [attr.format(), token("="), pattern.format()])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs {
|
|||
write!(f, [space()])?;
|
||||
}
|
||||
|
||||
write!(f, [text("as")])?;
|
||||
write!(f, [token("as")])?;
|
||||
|
||||
let trailing_as_comments = comments.dangling(item);
|
||||
if trailing_as_comments.is_empty() {
|
||||
|
@ -45,7 +45,7 @@ impl FormatNodeRule<PatternMatchAs> for FormatPatternMatchAs {
|
|||
name.format().fmt(f)
|
||||
} else {
|
||||
debug_assert!(pattern.is_none());
|
||||
text("_").fmt(f)
|
||||
token("_").fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -130,7 +130,7 @@ impl Format<PyFormatContext<'_>> for RestPattern<'_> {
|
|||
f,
|
||||
[
|
||||
leading_comments(self.comments),
|
||||
text("**"),
|
||||
token("**"),
|
||||
self.identifier.format()
|
||||
]
|
||||
)
|
||||
|
@ -156,7 +156,7 @@ impl Format<PyFormatContext<'_>> for KeyPatternPair<'_> {
|
|||
f,
|
||||
[group(&format_args![
|
||||
self.key.format(),
|
||||
text(":"),
|
||||
token(":"),
|
||||
space(),
|
||||
self.pattern.format()
|
||||
])]
|
||||
|
|
|
@ -35,7 +35,7 @@ impl FormatNodeRule<PatternMatchOr> for FormatPatternMatchOr {
|
|||
[hard_line_break(), leading_comments(leading_value_comments)]
|
||||
)?;
|
||||
}
|
||||
write!(f, [text("|"), space(), pattern.format()])?;
|
||||
write!(f, [token("|"), space(), pattern.format()])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -10,9 +10,9 @@ pub struct FormatPatternMatchSingleton;
|
|||
impl FormatNodeRule<PatternMatchSingleton> for FormatPatternMatchSingleton {
|
||||
fn fmt_fields(&self, item: &PatternMatchSingleton, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
match item.value {
|
||||
Constant::None => text("None").fmt(f),
|
||||
Constant::Bool(true) => text("True").fmt(f),
|
||||
Constant::Bool(false) => text("False").fmt(f),
|
||||
Constant::None => token("None").fmt(f),
|
||||
Constant::Bool(true) => token("True").fmt(f),
|
||||
Constant::Bool(false) => token("False").fmt(f),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ impl FormatNodeRule<PatternMatchStar> for FormatPatternMatchStar {
|
|||
let comments = f.context().comments().clone();
|
||||
let dangling = comments.dangling(item);
|
||||
|
||||
write!(f, [text("*"), dangling_comments(dangling)])?;
|
||||
write!(f, [token("*"), dangling_comments(dangling)])?;
|
||||
|
||||
match name {
|
||||
Some(name) => write!(f, [name.format()]),
|
||||
None => write!(f, [text("_")]),
|
||||
None => write!(f, [token("_")]),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -349,7 +349,7 @@ impl<'ast> Format<PyFormatContext<'ast>> for FormatClauseHeader<'_, 'ast> {
|
|||
write_suppressed_clause_header(self.header, f)?;
|
||||
} else {
|
||||
f.write_fmt(Arguments::from(&self.formatter))?;
|
||||
text(":").fmt(f)?;
|
||||
token(":").fmt(f)?;
|
||||
}
|
||||
|
||||
trailing_comments(self.trailing_colon_comment).fmt(f)
|
||||
|
|
|
@ -23,7 +23,7 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
|
|||
f,
|
||||
[
|
||||
target.format(),
|
||||
text(":"),
|
||||
token(":"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(annotation, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
|
@ -34,7 +34,7 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
|
|||
f,
|
||||
[
|
||||
space(),
|
||||
text("="),
|
||||
token("="),
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use ruff_formatter::prelude::{space, text};
|
||||
use ruff_formatter::prelude::{space, token};
|
||||
use ruff_formatter::write;
|
||||
use ruff_python_ast::StmtAssert;
|
||||
|
||||
|
@ -22,7 +22,7 @@ impl FormatNodeRule<StmtAssert> for FormatStmtAssert {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("assert"),
|
||||
token("assert"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
|
@ -32,7 +32,7 @@ impl FormatNodeRule<StmtAssert> for FormatStmtAssert {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text(","),
|
||||
token(","),
|
||||
space(),
|
||||
maybe_parenthesize_expression(msg, item, Parenthesize::IfBreaks),
|
||||
]
|
||||
|
|
|
@ -27,7 +27,7 @@ impl FormatNodeRule<StmtAssign> for FormatStmtAssign {
|
|||
[
|
||||
first.format(),
|
||||
space(),
|
||||
text("="),
|
||||
token("="),
|
||||
space(),
|
||||
FormatTargets { targets: rest }
|
||||
]
|
||||
|
@ -89,9 +89,9 @@ impl Format<PyFormatContext<'_>> for FormatTargets<'_> {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
if_group_breaks(&text("(")),
|
||||
if_group_breaks(&token("(")),
|
||||
soft_block_indent(&first.format().with_options(Parentheses::Never)),
|
||||
if_group_breaks(&text(")"))
|
||||
if_group_breaks(&token(")"))
|
||||
]
|
||||
)
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ impl Format<PyFormatContext<'_>> for FormatTargets<'_> {
|
|||
[group(&format_args![
|
||||
format_first,
|
||||
space(),
|
||||
text("="),
|
||||
token("="),
|
||||
space(),
|
||||
FormatTargets { targets: rest }
|
||||
])
|
||||
|
|
|
@ -24,7 +24,7 @@ impl FormatNodeRule<StmtAugAssign> for FormatStmtAugAssign {
|
|||
target.format(),
|
||||
space(),
|
||||
op.format(),
|
||||
text("="),
|
||||
token("="),
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
|
|
|
@ -8,7 +8,7 @@ pub struct FormatStmtBreak;
|
|||
|
||||
impl FormatNodeRule<StmtBreak> for FormatStmtBreak {
|
||||
fn fmt_fields(&self, _item: &StmtBreak, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
text("break").fmt(f)
|
||||
token("break").fmt(f)
|
||||
}
|
||||
|
||||
fn is_suppressed(
|
||||
|
|
|
@ -44,7 +44,7 @@ impl FormatNodeRule<StmtClassDef> for FormatStmtClassDef {
|
|||
ClauseHeader::Class(item),
|
||||
trailing_definition_comments,
|
||||
&format_with(|f| {
|
||||
write!(f, [text("class"), space(), name.format()])?;
|
||||
write!(f, [token("class"), space(), name.format()])?;
|
||||
|
||||
if let Some(type_params) = type_params.as_deref() {
|
||||
write!(f, [type_params.format()])?;
|
||||
|
|
|
@ -8,7 +8,7 @@ pub struct FormatStmtContinue;
|
|||
|
||||
impl FormatNodeRule<StmtContinue> for FormatStmtContinue {
|
||||
fn fmt_fields(&self, _item: &StmtContinue, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
text("continue").fmt(f)
|
||||
token("continue").fmt(f)
|
||||
}
|
||||
|
||||
fn is_suppressed(
|
||||
|
|
|
@ -15,7 +15,7 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
|
|||
fn fmt_fields(&self, item: &StmtDelete, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtDelete { range: _, targets } = item;
|
||||
|
||||
write!(f, [text("del"), space()])?;
|
||||
write!(f, [token("del"), space()])?;
|
||||
|
||||
match targets.as_slice() {
|
||||
[] => {
|
||||
|
@ -27,9 +27,9 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
|
|||
// del (
|
||||
// # Dangling comment
|
||||
// )
|
||||
text("("),
|
||||
token("("),
|
||||
block_indent(&dangling_node_comments(item)),
|
||||
text(")"),
|
||||
token(")"),
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -54,12 +54,12 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
|||
ClauseHeader::For(item),
|
||||
trailing_condition_comments,
|
||||
&format_args![
|
||||
is_async.then_some(format_args![text("async"), space()]),
|
||||
text("for"),
|
||||
is_async.then_some(format_args![token("async"), space()]),
|
||||
token("for"),
|
||||
space(),
|
||||
ExprTupleWithoutParentheses(target),
|
||||
space(),
|
||||
text("in"),
|
||||
token("in"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(iter, item, Parenthesize::IfBreaks),
|
||||
],
|
||||
|
@ -83,7 +83,7 @@ impl FormatNodeRule<StmtFor> for FormatStmtFor {
|
|||
clause_header(
|
||||
ClauseHeader::OrElse(ElseClause::For(item)),
|
||||
trailing,
|
||||
&text("else"),
|
||||
&token("else"),
|
||||
)
|
||||
.with_leading_comments(leading, body.last()),
|
||||
clause_body(orelse, trailing),
|
||||
|
|
|
@ -50,10 +50,10 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
|||
trailing_definition_comments,
|
||||
&format_with(|f| {
|
||||
if *is_async {
|
||||
write!(f, [text("async"), space()])?;
|
||||
write!(f, [token("async"), space()])?;
|
||||
}
|
||||
|
||||
write!(f, [text("def"), space(), name.format()])?;
|
||||
write!(f, [token("def"), space(), name.format()])?;
|
||||
|
||||
if let Some(type_params) = type_params.as_ref() {
|
||||
write!(f, [type_params.format()])?;
|
||||
|
@ -63,7 +63,7 @@ impl FormatNodeRule<StmtFunctionDef> for FormatStmtFunctionDef {
|
|||
write!(f, [parameters.format()])?;
|
||||
|
||||
if let Some(return_annotation) = returns.as_ref() {
|
||||
write!(f, [space(), text("->"), space()])?;
|
||||
write!(f, [space(), token("->"), space()])?;
|
||||
|
||||
if return_annotation.is_tuple_expr() {
|
||||
let parentheses =
|
||||
|
|
|
@ -15,18 +15,18 @@ impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
|
|||
// move the comment "off" of the `global` statement.
|
||||
if f.context().comments().has_trailing(item.as_any_node_ref()) {
|
||||
let joined = format_with(|f| {
|
||||
f.join_with(format_args![text(","), space()])
|
||||
f.join_with(format_args![token(","), space()])
|
||||
.entries(item.names.iter().formatted())
|
||||
.finish()
|
||||
});
|
||||
|
||||
write!(f, [text("global"), space(), &joined])
|
||||
write!(f, [token("global"), space(), &joined])
|
||||
} else {
|
||||
let joined = format_with(|f| {
|
||||
f.join_with(&format_args![
|
||||
text(","),
|
||||
token(","),
|
||||
space(),
|
||||
if_group_breaks(&text("\\")),
|
||||
if_group_breaks(&token("\\")),
|
||||
soft_line_break(),
|
||||
])
|
||||
.entries(item.names.iter().formatted())
|
||||
|
@ -36,10 +36,10 @@ impl FormatNodeRule<StmtGlobal> for FormatStmtGlobal {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("global"),
|
||||
token("global"),
|
||||
space(),
|
||||
group(&format_args!(
|
||||
if_group_breaks(&text("\\")),
|
||||
if_group_breaks(&token("\\")),
|
||||
soft_line_break(),
|
||||
soft_block_indent(&joined)
|
||||
))
|
||||
|
|
|
@ -30,7 +30,7 @@ impl FormatNodeRule<StmtIf> for FormatStmtIf {
|
|||
ClauseHeader::If(item),
|
||||
trailing_colon_comment,
|
||||
&format_args![
|
||||
text("if"),
|
||||
token("if"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
|
||||
],
|
||||
|
@ -86,13 +86,13 @@ pub(crate) fn format_elif_else_clause(
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("elif"),
|
||||
token("elif"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
|
||||
]
|
||||
)
|
||||
} else {
|
||||
text("else").fmt(f)
|
||||
token("else").fmt(f)
|
||||
}
|
||||
}),
|
||||
)
|
||||
|
|
|
@ -11,11 +11,11 @@ impl FormatNodeRule<StmtImport> for FormatStmtImport {
|
|||
fn fmt_fields(&self, item: &StmtImport, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtImport { names, range: _ } = item;
|
||||
let names = format_with(|f| {
|
||||
f.join_with(&format_args![text(","), space()])
|
||||
f.join_with(&format_args![token(","), space()])
|
||||
.entries(names.iter().formatted())
|
||||
.finish()
|
||||
});
|
||||
write!(f, [text("import"), space(), names])
|
||||
write!(f, [token("import"), space(), names])
|
||||
}
|
||||
|
||||
fn is_suppressed(
|
||||
|
|
|
@ -27,12 +27,12 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("from"),
|
||||
token("from"),
|
||||
space(),
|
||||
dynamic_text(&level_str, None),
|
||||
text(&level_str, None),
|
||||
module.as_ref().map(AsFormat::format),
|
||||
space(),
|
||||
text("import"),
|
||||
token("import"),
|
||||
space(),
|
||||
]
|
||||
)?;
|
||||
|
@ -40,7 +40,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
|
|||
if let [name] = names.as_slice() {
|
||||
// star can't be surrounded by parentheses
|
||||
if name.name.as_str() == "*" {
|
||||
return text("*").fmt(f);
|
||||
return token("*").fmt(f);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -29,7 +29,7 @@ impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
|
|||
ClauseHeader::Match(item),
|
||||
dangling_item_comments,
|
||||
&format_args![
|
||||
text("match"),
|
||||
token("match"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(subject, item, Parenthesize::IfBreaks),
|
||||
],
|
||||
|
|
|
@ -15,18 +15,18 @@ impl FormatNodeRule<StmtNonlocal> for FormatStmtNonlocal {
|
|||
// move the comment "off" of the `nonlocal` statement.
|
||||
if f.context().comments().has_trailing(item.as_any_node_ref()) {
|
||||
let joined = format_with(|f| {
|
||||
f.join_with(format_args![text(","), space()])
|
||||
f.join_with(format_args![token(","), space()])
|
||||
.entries(item.names.iter().formatted())
|
||||
.finish()
|
||||
});
|
||||
|
||||
write!(f, [text("nonlocal"), space(), &joined])
|
||||
write!(f, [token("nonlocal"), space(), &joined])
|
||||
} else {
|
||||
let joined = format_with(|f| {
|
||||
f.join_with(&format_args![
|
||||
text(","),
|
||||
token(","),
|
||||
space(),
|
||||
if_group_breaks(&text("\\")),
|
||||
if_group_breaks(&token("\\")),
|
||||
soft_line_break(),
|
||||
])
|
||||
.entries(item.names.iter().formatted())
|
||||
|
@ -36,10 +36,10 @@ impl FormatNodeRule<StmtNonlocal> for FormatStmtNonlocal {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
text("nonlocal"),
|
||||
token("nonlocal"),
|
||||
space(),
|
||||
group(&format_args!(
|
||||
if_group_breaks(&text("\\")),
|
||||
if_group_breaks(&token("\\")),
|
||||
soft_line_break(),
|
||||
soft_block_indent(&joined)
|
||||
))
|
||||
|
|
|
@ -8,7 +8,7 @@ pub struct FormatStmtPass;
|
|||
|
||||
impl FormatNodeRule<StmtPass> for FormatStmtPass {
|
||||
fn fmt_fields(&self, _item: &StmtPass, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
text("pass").fmt(f)
|
||||
token("pass").fmt(f)
|
||||
}
|
||||
|
||||
fn is_suppressed(
|
||||
|
|
|
@ -17,7 +17,7 @@ impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
|
|||
cause,
|
||||
} = item;
|
||||
|
||||
text("raise").fmt(f)?;
|
||||
token("raise").fmt(f)?;
|
||||
|
||||
if let Some(value) = exc {
|
||||
write!(
|
||||
|
@ -34,7 +34,7 @@ impl FormatNodeRule<StmtRaise> for FormatStmtRaise {
|
|||
f,
|
||||
[
|
||||
space(),
|
||||
text("from"),
|
||||
token("from"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::Optional)
|
||||
]
|
||||
|
|
|
@ -14,7 +14,7 @@ impl FormatNodeRule<StmtReturn> for FormatStmtReturn {
|
|||
fn fmt_fields(&self, item: &StmtReturn, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let StmtReturn { range: _, value } = item;
|
||||
|
||||
text("return").fmt(f)?;
|
||||
token("return").fmt(f)?;
|
||||
|
||||
match value.as_deref() {
|
||||
Some(Expr::Tuple(tuple)) if !f.context().comments().has_leading(tuple) => {
|
||||
|
|
|
@ -136,7 +136,7 @@ fn format_case<'a>(
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
clause_header(header, trailing_case_comments, &text(kind.keyword()))
|
||||
clause_header(header, trailing_case_comments, &token(kind.keyword()))
|
||||
.with_leading_comments(leading_case_comments, previous_node),
|
||||
clause_body(body, trailing_case_comments),
|
||||
]
|
||||
|
|
|
@ -18,7 +18,7 @@ impl FormatNodeRule<StmtTypeAlias> for FormatStmtTypeAlias {
|
|||
range: _,
|
||||
} = item;
|
||||
|
||||
write!(f, [text("type"), space(), name.as_ref().format()])?;
|
||||
write!(f, [token("type"), space(), name.as_ref().format()])?;
|
||||
|
||||
if let Some(type_params) = type_params {
|
||||
write!(f, [type_params.format()])?;
|
||||
|
@ -28,7 +28,7 @@ impl FormatNodeRule<StmtTypeAlias> for FormatStmtTypeAlias {
|
|||
f,
|
||||
[
|
||||
space(),
|
||||
text("="),
|
||||
token("="),
|
||||
space(),
|
||||
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
|
||||
]
|
||||
|
|
|
@ -38,7 +38,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
|
|||
ClauseHeader::While(item),
|
||||
trailing_condition_comments,
|
||||
&format_args![
|
||||
text("while"),
|
||||
token("while"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
|
||||
]
|
||||
|
@ -60,7 +60,7 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
|
|||
clause_header(
|
||||
ClauseHeader::OrElse(ElseClause::While(item)),
|
||||
trailing,
|
||||
&text("else")
|
||||
&token("else")
|
||||
)
|
||||
.with_leading_comments(leading, body.last()),
|
||||
clause_body(orelse, trailing),
|
||||
|
|
|
@ -52,8 +52,8 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
|||
f,
|
||||
[
|
||||
item.is_async
|
||||
.then_some(format_args![text("async"), space()]),
|
||||
text("with"),
|
||||
.then_some(format_args![token("async"), space()]),
|
||||
token("with"),
|
||||
space()
|
||||
]
|
||||
)?;
|
||||
|
@ -92,7 +92,7 @@ impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
|||
item.format().fmt(f)?;
|
||||
}
|
||||
} else {
|
||||
f.join_with(format_args![text(","), space()])
|
||||
f.join_with(format_args![token(","), space()])
|
||||
.entries(item.items.iter().formatted())
|
||||
.finish()?;
|
||||
}
|
||||
|
|
|
@ -9,6 +9,6 @@ pub struct FormatTypeParamParamSpec;
|
|||
impl FormatNodeRule<TypeParamParamSpec> for FormatTypeParamParamSpec {
|
||||
fn fmt_fields(&self, item: &TypeParamParamSpec, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let TypeParamParamSpec { range: _, name } = item;
|
||||
write!(f, [text("**"), name.format()])
|
||||
write!(f, [token("**"), name.format()])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ impl FormatNodeRule<TypeParamTypeVar> for FormatTypeParamTypeVar {
|
|||
} = item;
|
||||
name.format().fmt(f)?;
|
||||
if let Some(bound) = bound {
|
||||
write!(f, [text(":"), space(), bound.format()])?;
|
||||
write!(f, [token(":"), space(), bound.format()])?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -9,6 +9,6 @@ pub struct FormatTypeParamTypeVarTuple;
|
|||
impl FormatNodeRule<TypeParamTypeVarTuple> for FormatTypeParamTypeVarTuple {
|
||||
fn fmt_fields(&self, item: &TypeParamTypeVarTuple, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let TypeParamTypeVarTuple { range: _, name } = item;
|
||||
write!(f, [text("*"), name.format()])
|
||||
write!(f, [token("*"), name.format()])
|
||||
}
|
||||
}
|
||||
|
|
|
@ -871,7 +871,7 @@ impl Format<PyFormatContext<'_>> for VerbatimText {
|
|||
write!(
|
||||
f,
|
||||
[
|
||||
dynamic_text(&cleaned, Some(self.verbatim_range.start())),
|
||||
text(&cleaned, Some(self.verbatim_range.start())),
|
||||
source_position(self.verbatim_range.end())
|
||||
]
|
||||
)?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue