Format PatternMatchOr (#6905)

This commit is contained in:
Victor Hugo Gomes 2023-08-28 05:09:17 -03:00 committed by GitHub
parent 30ebf7fc86
commit 99f4c6886e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 913 deletions

View file

@ -2,8 +2,10 @@ use ruff_formatter::write;
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::PatternMatchOr;
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::not_yet_implemented_custom_text;
use crate::comments::leading_comments;
use crate::expression::parentheses::{
in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses,
};
use crate::prelude::*;
#[derive(Default)]
@ -11,13 +13,35 @@ pub struct FormatPatternMatchOr;
impl FormatNodeRule<PatternMatchOr> for FormatPatternMatchOr {
fn fmt_fields(&self, item: &PatternMatchOr, f: &mut PyFormatter) -> FormatResult<()> {
write!(
f,
[not_yet_implemented_custom_text(
"NOT_YET_IMPLEMENTED_PatternMatchOf | (y)",
item
)]
)
let PatternMatchOr { range: _, patterns } = item;
let inner = format_with(|f: &mut PyFormatter| {
let mut patterns = patterns.iter();
let comments = f.context().comments().clone();
let Some(first) = patterns.next() else {
return Ok(());
};
first.format().fmt(f)?;
for pattern in patterns {
let leading_value_comments = comments.leading(pattern);
// Format the expressions leading comments **before** the operator
if leading_value_comments.is_empty() {
write!(f, [in_parentheses_only_soft_line_break_or_space()])?;
} else {
write!(
f,
[hard_line_break(), leading_comments(leading_value_comments)]
)?;
}
write!(f, [text("|"), space(), pattern.format()])?;
}
Ok(())
});
inner.fmt(f)
}
}