mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00
Parenthesize match..case
if
guards (#13513)
This commit is contained in:
parent
8012707348
commit
9442cd8fae
7 changed files with 155 additions and 88 deletions
|
@ -3,7 +3,10 @@ use ruff_python_ast::AstNode;
|
|||
use ruff_python_ast::MatchCase;
|
||||
|
||||
use crate::builders::parenthesize_if_expands;
|
||||
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
|
||||
use crate::expression::maybe_parenthesize_expression;
|
||||
use crate::expression::parentheses::{
|
||||
NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::pattern::maybe_parenthesize_pattern;
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_match_case_parentheses_enabled;
|
||||
|
@ -62,6 +65,19 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
}
|
||||
});
|
||||
|
||||
let format_guard = guard.as_deref().map(|guard| {
|
||||
format_with(|f| {
|
||||
write!(f, [space(), token("if"), space()])?;
|
||||
|
||||
if is_match_case_parentheses_enabled(f.context()) {
|
||||
maybe_parenthesize_expression(guard, item, Parenthesize::IfBreaksParenthesized)
|
||||
.fmt(f)
|
||||
} else {
|
||||
guard.format().fmt(f)
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
|
@ -69,13 +85,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
|
|||
ClauseHeader::MatchCase(item),
|
||||
dangling_item_comments,
|
||||
&format_with(|f| {
|
||||
write!(f, [token("case"), space(), format_pattern])?;
|
||||
|
||||
if let Some(guard) = guard {
|
||||
write!(f, [space(), token("if"), space(), guard.format()])?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
write!(f, [token("case"), space(), format_pattern, format_guard])
|
||||
}),
|
||||
),
|
||||
clause_body(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue