mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-26 06:23:50 +00:00
format StmtWith (#5350)
This commit is contained in:
parent
49cabca3e7
commit
d00559e42a
13 changed files with 380 additions and 80 deletions
|
@ -1,5 +1,9 @@
|
|||
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
|
||||
use crate::builders::optional_parentheses;
|
||||
use crate::comments::trailing_comments;
|
||||
use crate::prelude::*;
|
||||
use crate::{FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use ruff_python_ast::prelude::*;
|
||||
use rustpython_parser::ast::StmtWith;
|
||||
|
||||
#[derive(Default)]
|
||||
|
@ -7,6 +11,33 @@ pub struct FormatStmtWith;
|
|||
|
||||
impl FormatNodeRule<StmtWith> for FormatStmtWith {
|
||||
fn fmt_fields(&self, item: &StmtWith, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented(item)])
|
||||
let StmtWith {
|
||||
range: _,
|
||||
items,
|
||||
body,
|
||||
type_comment: _,
|
||||
} = item;
|
||||
|
||||
let comments = f.context().comments().clone();
|
||||
let dangling_comments = comments.dangling_comments(item.as_any_node_ref());
|
||||
|
||||
let joined_items = format_with(|f| f.join_comma_separated().nodes(items.iter()).finish());
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
text("with"),
|
||||
space(),
|
||||
group(&optional_parentheses(&joined_items)),
|
||||
text(":"),
|
||||
trailing_comments(dangling_comments),
|
||||
block_indent(&body.format())
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
fn fmt_dangling_comments(&self, _node: &StmtWith, _f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// Handled in `fmt_fields`
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue