mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Parenthesize multi-context managers (#9222)
This commit is contained in:
parent
fa2c37b411
commit
a06723da2b
13 changed files with 928 additions and 649 deletions
|
@ -1,5 +1,4 @@
|
|||
use ruff_formatter::write;
|
||||
|
||||
use ruff_python_ast::WithItem;
|
||||
|
||||
use crate::comments::SourceComment;
|
||||
|
@ -8,6 +7,7 @@ use crate::expression::parentheses::{
|
|||
is_expression_parenthesized, parenthesized, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_wrap_multiple_context_managers_in_parens_enabled;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatWithItem;
|
||||
|
@ -23,26 +23,49 @@ impl FormatNodeRule<WithItem> for FormatWithItem {
|
|||
let comments = f.context().comments().clone();
|
||||
let trailing_as_comments = comments.dangling(item);
|
||||
|
||||
// Prefer keeping parentheses for already parenthesized expressions over
|
||||
// parenthesizing other nodes.
|
||||
let parenthesize = if is_expression_parenthesized(
|
||||
let is_parenthesized = is_expression_parenthesized(
|
||||
context_expr.into(),
|
||||
f.context().comments().ranges(),
|
||||
f.context().source(),
|
||||
) {
|
||||
Parenthesize::IfBreaks
|
||||
} else {
|
||||
Parenthesize::IfRequired
|
||||
};
|
||||
);
|
||||
|
||||
write!(
|
||||
f,
|
||||
[maybe_parenthesize_expression(
|
||||
context_expr,
|
||||
item,
|
||||
parenthesize
|
||||
)]
|
||||
)?;
|
||||
// Remove the parentheses of the `with_items` if the with statement adds parentheses
|
||||
if f.context().node_level().is_parenthesized()
|
||||
&& is_wrap_multiple_context_managers_in_parens_enabled(f.context())
|
||||
{
|
||||
if is_parenthesized {
|
||||
// ...except if the with item is parenthesized, then use this with item as a preferred breaking point
|
||||
// or when it has comments, then parenthesize it to prevent comments from moving.
|
||||
maybe_parenthesize_expression(
|
||||
context_expr,
|
||||
item,
|
||||
Parenthesize::IfBreaksOrIfRequired,
|
||||
)
|
||||
.fmt(f)?;
|
||||
} else {
|
||||
context_expr
|
||||
.format()
|
||||
.with_options(Parentheses::Never)
|
||||
.fmt(f)?;
|
||||
}
|
||||
} else {
|
||||
// Prefer keeping parentheses for already parenthesized expressions over
|
||||
// parenthesizing other nodes.
|
||||
let parenthesize = if is_parenthesized {
|
||||
Parenthesize::IfBreaks
|
||||
} else {
|
||||
Parenthesize::IfRequired
|
||||
};
|
||||
|
||||
write!(
|
||||
f,
|
||||
[maybe_parenthesize_expression(
|
||||
context_expr,
|
||||
item,
|
||||
parenthesize
|
||||
)]
|
||||
)?;
|
||||
}
|
||||
|
||||
if let Some(optional_vars) = optional_vars {
|
||||
write!(f, [space(), token("as"), space()])?;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue