Introduce Token element (#7048)

This commit is contained in:
Micha Reiser 2023-09-02 10:05:47 +02:00 committed by GitHub
parent 2f3a950f6f
commit c05e4628b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
78 changed files with 733 additions and 723 deletions

View file

@ -113,7 +113,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
f,
[
dangling_comments(before_dot),
text("."),
token("."),
dangling_comments(after_dot),
attr.format()
]

View file

@ -16,7 +16,7 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
write!(
f,
[
text("await"),
token("await"),
space(),
maybe_parenthesize_expression(value, item, Parenthesize::IfBreaks)
]

View file

@ -260,7 +260,7 @@ impl FormatRule<Operator, PyFormatContext<'_>> for FormatOperator {
Operator::FloorDiv => "//",
};
text(operator).fmt(f)
token(operator).fmt(f)
}
}

View file

@ -132,6 +132,6 @@ impl FormatRule<BoolOp, PyFormatContext<'_>> for FormatBoolOp {
BoolOp::Or => "or",
};
text(operator).fmt(f)
token(operator).fmt(f)
}
}

View file

@ -113,6 +113,6 @@ impl FormatRule<CmpOp, PyFormatContext<'_>> for FormatCmpOp {
CmpOp::NotIn => "not in",
};
text(operator).fmt(f)
token(operator).fmt(f)
}
}

View file

@ -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),

View file

@ -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()])
]
)
}

View file

@ -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(),

View file

@ -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(),
]
)?;

View file

@ -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()])?;

View file

@ -25,7 +25,7 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
f,
[
group(&format_args!(target.format(), soft_line_break_or_space())),
text(":=")
token(":=")
]
)?;

View file

@ -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)?;

View file

@ -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(

View file

@ -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("]")
])]
)
}

View file

@ -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()

View file

@ -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();

View file

@ -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(())
}

View file

@ -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),

View file

@ -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)
}
}

View file

@ -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)
])]
)
}

View file

@ -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()),
)