Respect fmt: skip for compound statements on single line (#20633)

Closes #11216

Essentially the approach is to implement `Format` for a new struct
`FormatClause` which is just a clause header _and_ its body. We then
have the information we need to see whether there is a skip suppression
comment on the last child in the body and it all fits on one line.
This commit is contained in:
Dylan
2025-11-18 12:02:09 -06:00
committed by GitHub
parent 8dad289062
commit 62343a101a
14 changed files with 989 additions and 322 deletions

View File

@@ -5,7 +5,7 @@ use ruff_text_size::Ranged;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::statement::clause::{ClauseHeader, ElseClause, clause_body, clause_header};
use crate::statement::clause::{ClauseHeader, ElseClause, clause};
use crate::statement::suite::SuiteKind;
#[derive(Default)]
@@ -33,22 +33,17 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
write!(
f,
[
clause_header(
ClauseHeader::While(item),
trailing_condition_comments,
&format_args![
token("while"),
space(),
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
]
),
clause_body(
body,
SuiteKind::other(orelse.is_empty()),
trailing_condition_comments
),
]
[clause(
ClauseHeader::While(item),
&format_args![
token("while"),
space(),
maybe_parenthesize_expression(test, item, Parenthesize::IfBreaks),
],
trailing_condition_comments,
body,
SuiteKind::other(orelse.is_empty()),
)]
)?;
if !orelse.is_empty() {
@@ -60,15 +55,14 @@ impl FormatNodeRule<StmtWhile> for FormatStmtWhile {
write!(
f,
[
clause_header(
ClauseHeader::OrElse(ElseClause::While(item)),
trailing,
&token("else")
)
.with_leading_comments(leading, body.last()),
clause_body(orelse, SuiteKind::other(true), trailing),
]
[clause(
ClauseHeader::OrElse(ElseClause::While(item)),
&token("else"),
trailing,
orelse,
SuiteKind::other(true),
)
.with_leading_comments(leading, body.last()),]
)?;
}