Skip to content

Commit

Permalink
Respect magic trailing comma for set expression (astral-sh#5782)
Browse files Browse the repository at this point in the history
<!--
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? -->
  • Loading branch information
MichaReiser authored and evanrittenhouse committed Jul 19, 2023
1 parent 4f6799d commit 8bff960
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 319 deletions.
15 changes: 8 additions & 7 deletions crates/ruff_python_formatter/src/expression/expr_set.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
}
}

Expand Down

This file was deleted.

0 comments on commit 8bff960

Please sign in to comment.