format StmtWith (#5350)

This commit is contained in:
David Szotten 2023-06-26 15:09:06 +01:00 committed by GitHub
parent 49cabca3e7
commit d00559e42a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 380 additions and 80 deletions

View file

@ -2,7 +2,7 @@ use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprStarred;
@ -10,8 +10,13 @@ use rustpython_parser::ast::ExprStarred;
pub struct FormatExprStarred;
impl FormatNodeRule<ExprStarred> for FormatExprStarred {
fn fmt_fields(&self, item: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
fn fmt_fields(&self, _item: &ExprStarred, f: &mut PyFormatter) -> FormatResult<()> {
write!(
f,
[not_yet_implemented_custom_text(
"*NOT_YET_IMPLEMENTED_ExprStarred"
)]
)
}
}

View file

@ -1,4 +1,6 @@
use crate::{not_yet_implemented, FormatNodeRule, PyFormatter};
use crate::expression::parentheses::Parenthesize;
use crate::prelude::*;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::WithItem;
@ -7,6 +9,22 @@ pub struct FormatWithItem;
impl FormatNodeRule<WithItem> for FormatWithItem {
fn fmt_fields(&self, item: &WithItem, f: &mut PyFormatter) -> FormatResult<()> {
write!(f, [not_yet_implemented(item)])
let WithItem {
range: _,
context_expr,
optional_vars,
} = item;
let inner = format_with(|f| {
write!(
f,
[context_expr.format().with_options(Parenthesize::IfBreaks)]
)?;
if let Some(optional_vars) = optional_vars {
write!(f, [space(), text("as"), space(), optional_vars.format()])?;
}
Ok(())
});
write!(f, [group(&inner)])
}
}

View file

@ -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(())
}
}