mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Introduce Token element (#7048)
This commit is contained in:
parent
2f3a950f6f
commit
c05e4628b1
78 changed files with 733 additions and 723 deletions
|
@ -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()])?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue