Handle right parens in join comma builder (#5711)

This commit is contained in:
Micha Reiser 2023-07-12 18:21:28 +02:00 committed by GitHub
parent f0aa6bd4d3
commit 653429bef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 159 additions and 136 deletions

View file

@ -76,12 +76,13 @@ impl Format<PyFormatContext<'_>> for FormatInheritanceClause<'_> {
bases,
keywords,
name,
body,
..
} = self.class_definition;
let source = f.context().source();
let mut joiner = f.join_comma_separated();
let mut joiner = f.join_comma_separated(body.first().unwrap().start());
if let Some((first, rest)) = bases.split_first() {
// Manually handle parentheses for the first expression because the logic in `FormatExpr`

View file

@ -4,7 +4,7 @@ use crate::expression::parentheses::Parenthesize;
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{block_indent, format_with, space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::StmtDelete;
use rustpython_parser::ast::{Ranged, StmtDelete};
#[derive(Default)]
pub struct FormatStmtDelete;
@ -35,7 +35,11 @@ impl FormatNodeRule<StmtDelete> for FormatStmtDelete {
write!(f, [single.format().with_options(Parenthesize::IfBreaks)])
}
targets => {
let item = format_with(|f| f.join_comma_separated().nodes(targets.iter()).finish());
let item = format_with(|f| {
f.join_comma_separated(item.end())
.nodes(targets.iter())
.finish()
});
parenthesize_if_expands(&item).fmt(f)
}
}

View file

@ -2,7 +2,7 @@ use crate::builders::{parenthesize_if_expands, PyFormatterExtensions};
use crate::{AsFormat, FormatNodeRule, PyFormatter};
use ruff_formatter::prelude::{dynamic_text, format_with, space, text};
use ruff_formatter::{write, Buffer, Format, FormatResult};
use rustpython_parser::ast::StmtImportFrom;
use rustpython_parser::ast::{Ranged, StmtImportFrom};
#[derive(Default)]
pub struct FormatStmtImportFrom;
@ -39,7 +39,7 @@ impl FormatNodeRule<StmtImportFrom> for FormatStmtImportFrom {
}
}
let names = format_with(|f| {
f.join_comma_separated()
f.join_comma_separated(item.end())
.entries(names.iter().map(|name| (name, name.format())))
.finish()
});

View file

@ -68,8 +68,11 @@ impl Format<PyFormatContext<'_>> for AnyStatementWith<'_> {
let comments = f.context().comments().clone();
let dangling_comments = comments.dangling_comments(self);
let joined_items =
format_with(|f| f.join_comma_separated().nodes(self.items().iter()).finish());
let joined_items = format_with(|f| {
f.join_comma_separated(self.body().first().unwrap().start())
.nodes(self.items().iter())
.finish()
});
if self.is_async() {
write!(f, [text("async"), space()])?;