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