From e457d3d852f10c9d9763ba963091e505a78b8fd0 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sat, 8 Jul 2023 17:42:56 +0200 Subject: [PATCH] Track magic comma --- .../src/expression/mod.rs | 25 ++++++++++++------- .../src/expression/parentheses.rs | 16 +++--------- 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index 021d2c4a453931..b7b09422b442e9 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -1,14 +1,15 @@ +use std::cmp::Ordering; + use rustpython_parser::ast; use rustpython_parser::ast::{Expr, Operator}; -use std::cmp::Ordering; -use crate::builders::optional_parentheses; use ruff_formatter::{ format_args, FormatOwnedWithRule, FormatRefWithRule, FormatRule, FormatRuleWithOptions, }; -use ruff_python_ast::node::AnyNodeRef; +use ruff_python_ast::expression::ExpressionRef; use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor}; +use crate::builders::optional_parentheses; use crate::context::NodeLevel; use crate::expression::expr_tuple::TupleParentheses; use crate::expression::parentheses::{ @@ -217,11 +218,15 @@ impl<'ast> IntoFormat> for Expr { /// the expression `a * b * c` contains two multiply operations. We prefer parentheses in that case. /// `(a * b) * c` or `a * b + c` are okay, because the subexpression is parenthesized, or the expression uses operands with a lower priority fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool { - let mut visitor = CanOmitParenthesesVisitor::new(context.source()); + if context.comments().has_leading_comments(expr) { + false + } else { + let mut visitor = CanOmitParenthesesVisitor::new(context.source()); - visitor.visit_subexpression(expr); + visitor.visit_subexpression(expr); - visitor.can_omit_parentheses() + visitor.can_omit_parentheses() + } } #[derive(Clone, Debug)] @@ -338,8 +343,10 @@ impl<'source> CanOmitParenthesesVisitor<'source> { | Expr::DictComp(_) | Expr::Call(_) | Expr::Subscript(_) - ) || is_expression_parenthesized(AnyNodeRef::from(value.as_ref()), self.source) - { + ) || is_expression_parenthesized( + ExpressionRef::from(value.as_ref()), + self.source, + ) { self.update_max_priority(OperatorPriority::Attribute); } } @@ -369,7 +376,7 @@ impl<'source> CanOmitParenthesesVisitor<'source> { impl PreorderVisitor<'_> for CanOmitParenthesesVisitor<'_> { fn visit_expr(&mut self, expr: &'_ Expr) { // Rule only applies for non-parenthesized expressions. - if is_expression_parenthesized(AnyNodeRef::from(expr), self.source) { + if is_expression_parenthesized(ExpressionRef::from(expr), self.source) { return; } diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index 3bcb8d7706da10..ed2b9dc29a6bf0 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -32,10 +32,10 @@ pub(super) fn default_expression_needs_parentheses( // `Optional` or `IfBreaks`: Add parentheses if the expression doesn't fit on a line but enforce // parentheses if the expression has leading comments else if !parenthesize.is_preserve() { - if can_omit_optional_parentheses(node, context) { - Parentheses::Optional - } else { + if context.comments().has_leading_comments(node) { Parentheses::Always + } else { + Parentheses::Optional } } else { //`Preserve` and expression has no parentheses in the source code @@ -43,16 +43,6 @@ pub(super) fn default_expression_needs_parentheses( } } -fn can_omit_optional_parentheses(expr: ExpressionRef, context: &PyFormatContext) -> bool { - if context.comments().has_leading_comments(expr) { - false - } else { - true - } -} - -// fn has_magic_comma(expr: AnyNodeRef) - /// Configures if the expression should be parenthesized. #[derive(Copy, Clone, Debug, Default, Eq, PartialEq)] pub enum Parenthesize {