Format SetComp (#5774)

<!--
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

Format `SetComp` like `ListComp`.

## Test Plan

Derived from `ListComp`'s fixture.
This commit is contained in:
Luc Khai Hai 2023-07-15 23:50:47 +09:00 committed by GitHub
parent daa4b72d5f
commit e1c119fde3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 193 additions and 35 deletions

View file

@ -1,7 +1,9 @@
use crate::context::PyFormatContext;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::expression::parentheses::{parenthesized, NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
use crate::AsFormat;
use crate::{FormatNodeRule, PyFormatter};
use ruff_formatter::{format_args, write, Buffer, FormatResult};
use ruff_python_ast::node::AnyNodeRef;
use rustpython_parser::ast::ExprSetComp;
@ -9,11 +11,29 @@ use rustpython_parser::ast::ExprSetComp;
pub struct FormatExprSetComp;
impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
fn fmt_fields(&self, _item: &ExprSetComp, f: &mut PyFormatter) -> FormatResult<()> {
fn fmt_fields(&self, item: &ExprSetComp, f: &mut PyFormatter) -> FormatResult<()> {
let ExprSetComp {
range: _,
elt,
generators,
} = item;
let joined = format_with(|f| {
f.join_with(soft_line_break_or_space())
.entries(generators.iter().formatted())
.finish()
});
write!(
f,
[not_yet_implemented_custom_text(
"{NOT_IMPLEMENTED_set_value for value in NOT_IMPLEMENTED_set}"
[parenthesized(
"{",
&format_args!(
group(&elt.format()),
soft_line_break_or_space(),
group(&joined)
),
"}"
)]
)
}