mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-25 05:53:51 +00:00
Respect magic trailing comma for set expression (#5782)
<!-- Thank you for contributing to Ruff! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? - Does this pull request include references to any relevant issues? --> ## Summary This PR uses the `join_comma_separated` builder for formatting set expressions to ensure the formatting preserves magic commas, if the setting is enabled. <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan See the fixed black tests <!-- How was it tested? -->
This commit is contained in:
parent
fa4855e6fe
commit
df2efe81c8
2 changed files with 8 additions and 319 deletions
|
@ -1,9 +1,10 @@
|
|||
use rustpython_parser::ast::{ExprSet, Ranged};
|
||||
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
|
||||
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
|
||||
use crate::prelude::*;
|
||||
use crate::FormatNodeRule;
|
||||
use ruff_formatter::format_args;
|
||||
use ruff_python_ast::node::AnyNodeRef;
|
||||
use rustpython_parser::ast::ExprSet;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprSet;
|
||||
|
@ -14,13 +15,13 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
|
|||
// That would be a dict expression
|
||||
assert!(!elts.is_empty());
|
||||
// Avoid second mutable borrow of f
|
||||
let joined = format_with(|f| {
|
||||
f.join_with(format_args!(text(","), soft_line_break_or_space()))
|
||||
.entries(elts.iter().formatted())
|
||||
let joined = format_with(|f: &mut PyFormatter| {
|
||||
f.join_comma_separated(item.end())
|
||||
.nodes(elts.iter())
|
||||
.finish()
|
||||
});
|
||||
|
||||
parenthesized("{", &format_args![joined, if_group_breaks(&text(","))], "}").fmt(f)
|
||||
parenthesized("{", &joined, "}").fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue