Skip to content

Commit

Permalink
Use in_parentheses_only_soft_line_break_or_space
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jan 5, 2024
1 parent 2220563 commit 60d72a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::ExprNamedExpr;

use crate::comments::{dangling_comments, SourceComment};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::expression::parentheses::{
in_parentheses_only_soft_line_break_or_space, NeedsParentheses, OptionalParentheses,
};
use crate::prelude::*;

#[derive(Default)]
Expand All @@ -24,7 +26,10 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
write!(
f,
[
group(&format_args!(target.format(), soft_line_break_or_space())),
group(&format_args!(
target.format(),
in_parentheses_only_soft_line_break_or_space()
)),
token(":=")
]
)?;
Expand Down
19 changes: 2 additions & 17 deletions crates/ruff_python_formatter/src/other/match_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use ruff_python_ast::MatchCase;

use crate::builders::parenthesize_if_expands;
use crate::comments::SourceComment;
use crate::expression::maybe_parenthesize_expression;
use crate::expression::parentheses::{
NeedsParentheses, OptionalParentheses, Parentheses, Parenthesize,
};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses, Parentheses};
use crate::prelude::*;
use crate::statement::clause::{clause_body, clause_header, ClauseHeader};

Expand Down Expand Up @@ -61,19 +58,7 @@ impl FormatNodeRule<MatchCase> for FormatMatchCase {
}

if let Some(guard) = guard {
write!(
f,
[
space(),
token("if"),
space(),
maybe_parenthesize_expression(
guard,
item,
Parenthesize::Optional
)
]
)?;
write!(f, [space(), token("if"), space(), guard.format()])?;
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,7 @@ match newlines:
match long_lines:
case "this is a long line for if condition" if (
aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2
): # comment
case "this is a long line for if condition" if aaaaaaaaahhhhhhhh == 1 and bbbbbbaaaaaaaaaaa == 2: # comment
pass
case "this is a long line for if condition with parentheses" if (
Expand All @@ -703,9 +701,9 @@ match long_lines:
): # another comment
pass
case {"long_long_long_key": str(long_long_long_key)} if (
value := "long long long long long long long long long long long value"
):
case {
"long_long_long_key": str(long_long_long_key)
} if value := "long long long long long long long long long long long value":
pass
Expand Down

0 comments on commit 60d72a1

Please sign in to comment.