Skip to content

Commit

Permalink
Prefer splitting right hand side of assignments
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Dec 8, 2023
1 parent d0d88d9 commit e465bfc
Show file tree
Hide file tree
Showing 16 changed files with 924 additions and 296 deletions.
@@ -0,0 +1,5 @@
[
{
"preview": "enabled"
}
]
@@ -0,0 +1,71 @@
# Don't parenthesize the value because the target's trailing comma forces it to split.
a[
aaaaaaa,
b,
] = cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc # comment

# Parenthesize the value, but don't duplicate the comment.
a[
aaaaaaa,
b
] = cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc # comment

# Format both as flat, but don't loos the comment.
a[
aaaaaaa,
b
] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb # comment

#######################################################
# Test the case where a parenthesized value now fits:
a[
aaaaaaa,
b
] = (
cccccccc # comment
)

# Doesn't use `BestFit` because the target always breaks because of the trailing comma
a[
aaaaaaa,
b,
] = (
cccccccc # comment
)

# Doesn't use `BestFit` because the target always breaks because of the trailing comma
# The group breaks because of its comments
a[
aaaaaaa,
b
] = (
# leading comment
b
) = (
cccccccc # comment
)


a[bbbbbbbbbbbbbbbbbb] = ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc

# Does not double-parenthesize tuples
(
first_item,
second_item,
) = some_looooooooong_module.some_loooooog_function_name(
first_argument, second_argument, third_argument
)


# Preserve parentheses around the first target
(
req["ticket"]["steps"]["step"][0]["tasks"]["task"]["fields"]["field"][
"access_request"
]["destinations"]["destination"][0]["ip_address"]
) = dst

(
req["ticket"]["steps"]["step"][0]["tasks"]["task"]["fields"]["field"][
"access_request"
]["destinations"]["destination"][0]["ip_address"]
) += dst
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/expression/mod.rs
Expand Up @@ -952,7 +952,7 @@ impl OwnParentheses {
/// Differs from [`has_own_parentheses`] in that it returns [`OwnParentheses::NonEmpty`] for
/// parenthesized expressions, like `(1)` or `([1])`, regardless of whether those expression have
/// their _own_ parentheses.
fn has_parentheses(expr: &Expr, context: &PyFormatContext) -> Option<OwnParentheses> {
pub(crate) fn has_parentheses(expr: &Expr, context: &PyFormatContext) -> Option<OwnParentheses> {
let own_parentheses = has_own_parentheses(expr, context);

// If the node has its own non-empty parentheses, we don't need to check for surrounding
Expand Down
7 changes: 7 additions & 0 deletions crates/ruff_python_formatter/src/preview.rs
Expand Up @@ -17,3 +17,10 @@ pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
) -> bool {
context.is_preview()
}

/// Returns `true` if the [`prefer_splitting_right_hand_side_of_assignments`](https://github.com/astral-sh/ruff/issues/6975) preview style is enabled.
pub(crate) const fn is_prefer_splitting_right_hand_side_of_assignments_enabled(
context: &PyFormatContext,
) -> bool {
context.is_preview()
}
44 changes: 30 additions & 14 deletions crates/ruff_python_formatter/src/statement/stmt_ann_assign.rs
Expand Up @@ -2,8 +2,12 @@ use ruff_formatter::write;
use ruff_python_ast::StmtAnnAssign;

use crate::comments::{SourceComment, SuppressionKind};
use crate::expression::has_parentheses;
use crate::prelude::*;
use crate::statement::stmt_assign::FormatStatementsLastExpression;
use crate::preview::is_prefer_splitting_right_hand_side_of_assignments_enabled;
use crate::statement::stmt_assign::{
AnyAssignmentOperator, FormatAssignmentValue, FormatStatementsLastExpression,
};
use crate::statement::trailing_semicolon;

#[derive(Default)]
Expand All @@ -19,21 +23,33 @@ impl FormatNodeRule<StmtAnnAssign> for FormatStmtAnnAssign {
simple: _,
} = item;

write!(
f,
[target.format(), token(":"), space(), annotation.format(),]
)?;
write!(f, [target.format(), token(":"), space()])?;

if let Some(value) = value {
write!(
f,
[
space(),
token("="),
space(),
FormatStatementsLastExpression::new(value, item)
]
)?;
if is_prefer_splitting_right_hand_side_of_assignments_enabled(f.context())
&& has_parentheses(annotation, f.context()).is_some()
{
FormatAssignmentValue {
before_operator: annotation,
operator: AnyAssignmentOperator::Assign,
value,
statement: item.into(),
}
.fmt(f)?;
} else {
write!(
f,
[
annotation.format(),
space(),
token("="),
space(),
FormatStatementsLastExpression::new(value, item)
]
)?;
}
} else {
annotation.format().fmt(f)?;
}

if f.options().source_type().is_ipynb()
Expand Down

0 comments on commit e465bfc

Please sign in to comment.