mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 13:05:06 +00:00
Add formatting for StmtMatch
(#6286)
## Summary This PR adds support for `StmtMatch` with subs for `MatchCase`. ## Test Plan Add a few additional test cases around `match` statement, comments, line breaks. resolves: #6298
This commit is contained in:
parent
87984e9ac7
commit
001aa486df
12 changed files with 882 additions and 444 deletions
|
@ -1,12 +1,49 @@
|
|||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::StmtMatch;
|
||||
|
||||
use crate::comments::trailing_comments;
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::Parenthesize;
|
||||
use crate::prelude::*;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatStmtMatch;
|
||||
|
||||
impl FormatNodeRule<StmtMatch> for FormatStmtMatch {
|
||||
fn fmt_fields(&self, item: &StmtMatch, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented(item)])
|
||||
let StmtMatch {
|
||||
range: _,
|
||||
subject,
|
||||
cases,
|
||||
} = item;
|
||||
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling_item_comments = comments.dangling_comments(item);
|
||||
|
||||
// There can be at most one dangling comment after the colon in a match statement.
|
||||
debug_assert!(dangling_item_comments.len() <= 1);
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
text("match"),
|
||||
space(),
|
||||
maybe_parenthesize_expression(subject, item, Parenthesize::IfBreaks),
|
||||
text(":"),
|
||||
trailing_comments(dangling_item_comments)
|
||||
]
|
||||
)?;
|
||||
|
||||
for case in cases {
|
||||
write!(f, [block_indent(&case.format())])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtMatch, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Handled as part of `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue