Format Function definitions (#4951)

This commit is contained in:
Micha Reiser 2023-06-08 18:07:33 +02:00 committed by GitHub
parent 07cc4bcb0f
commit 68969240c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
79 changed files with 2601 additions and 1223 deletions

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -39,7 +40,12 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
}
impl NeedsParentheses for ExprAttribute {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -17,7 +18,12 @@ impl FormatNodeRule<ExprAwait> for FormatExprAwait {
}
impl NeedsParentheses for ExprAwait {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::trailing_comments;
use crate::comments::{trailing_comments, Comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parenthesize,
};
@ -171,8 +171,13 @@ impl FormatRule<Operator, PyFormatContext<'_>> for FormatOperator {
}
impl NeedsParentheses for ExprBinOp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => {
if should_binary_break_right_side_first(self) {
Parentheses::Custom

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -20,7 +21,12 @@ impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
}
impl NeedsParentheses for ExprBoolOp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -18,8 +19,13 @@ impl FormatNodeRule<ExprCall> for FormatExprCall {
}
impl NeedsParentheses for ExprCall {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -3,6 +3,7 @@ use crate::expression::parentheses::{
};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use crate::comments::Comments;
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprCompare;
@ -21,7 +22,12 @@ impl FormatNodeRule<ExprCompare> for FormatExprCompare {
}
impl NeedsParentheses for ExprCompare {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -41,7 +42,15 @@ impl FormatNodeRule<ExprConstant> for FormatExprConstant {
}
impl NeedsParentheses for ExprConstant {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -20,8 +21,13 @@ impl FormatNodeRule<ExprDict> for FormatExprDict {
}
impl NeedsParentheses for ExprDict {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -20,8 +21,13 @@ impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
}
impl NeedsParentheses for ExprDictComp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprFormattedValue> for FormatExprFormattedValue {
}
impl NeedsParentheses for ExprFormattedValue {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,8 +16,13 @@ impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
}
impl NeedsParentheses for ExprGeneratorExp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -20,7 +21,12 @@ impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
}
impl NeedsParentheses for ExprIfExp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprJoinedStr> for FormatExprJoinedStr {
}
impl NeedsParentheses for ExprJoinedStr {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprLambda> for FormatExprLambda {
}
impl NeedsParentheses for ExprLambda {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::dangling_comments;
use crate::comments::{dangling_comments, Comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -57,8 +57,13 @@ impl FormatNodeRule<ExprList> for FormatExprList {
}
impl NeedsParentheses for ExprList {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,8 +16,13 @@ impl FormatNodeRule<ExprListComp> for FormatExprListComp {
}
impl NeedsParentheses for ExprListComp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -26,8 +27,13 @@ impl FormatNodeRule<ExprName> for FormatExprName {
}
impl NeedsParentheses for ExprName {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprNamedExpr> for FormatExprNamedExpr {
}
impl NeedsParentheses for ExprNamedExpr {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -34,8 +35,13 @@ impl FormatNodeRule<ExprSet> for FormatExprSet {
}
impl NeedsParentheses for ExprSet {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,8 +16,13 @@ impl FormatNodeRule<ExprSetComp> for FormatExprSetComp {
}
impl NeedsParentheses for ExprSetComp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -3,6 +3,7 @@ use crate::expression::parentheses::{
};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use crate::comments::Comments;
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprSlice;
@ -21,7 +22,12 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
}
impl NeedsParentheses for ExprSlice {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprStarred> for FormatExprStarred {
}
impl NeedsParentheses for ExprStarred {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -3,6 +3,7 @@ use crate::expression::parentheses::{
};
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
use crate::comments::Comments;
use ruff_formatter::{write, Buffer, FormatResult};
use rustpython_parser::ast::ExprSubscript;
@ -21,7 +22,12 @@ impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
}
impl NeedsParentheses for ExprSubscript {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,8 +16,13 @@ impl FormatNodeRule<ExprTuple> for FormatExprTuple {
}
impl NeedsParentheses for ExprTuple {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source) {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprUnaryOp> for FormatExprUnaryOp {
}
impl NeedsParentheses for ExprUnaryOp {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprYield> for FormatExprYield {
}
impl NeedsParentheses for ExprYield {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -15,7 +16,12 @@ impl FormatNodeRule<ExprYieldFrom> for FormatExprYieldFrom {
}
impl NeedsParentheses for ExprYieldFrom {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source)
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
}
}

View file

@ -1,3 +1,4 @@
use crate::comments::Comments;
use crate::context::NodeLevel;
use crate::expression::parentheses::{NeedsParentheses, Parentheses, Parenthesize};
use crate::prelude::*;
@ -51,7 +52,11 @@ impl FormatRuleWithOptions<Expr, PyFormatContext<'_>> for FormatExpr {
impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
fn fmt(&self, item: &Expr, f: &mut PyFormatter) -> FormatResult<()> {
let parentheses = item.needs_parentheses(self.parenthesize, f.context().contents());
let parentheses = item.needs_parentheses(
self.parenthesize,
f.context().contents(),
f.context().comments(),
);
let format_expr = format_with(|f| match item {
Expr::BoolOp(expr) => expr.format().fmt(f),
@ -118,35 +123,40 @@ impl FormatRule<Expr, PyFormatContext<'_>> for FormatExpr {
}
impl NeedsParentheses for Expr {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
match self {
Expr::BoolOp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::NamedExpr(expr) => expr.needs_parentheses(parenthesize, source),
Expr::BinOp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::UnaryOp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Lambda(expr) => expr.needs_parentheses(parenthesize, source),
Expr::IfExp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Dict(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Set(expr) => expr.needs_parentheses(parenthesize, source),
Expr::ListComp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::SetComp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::DictComp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::GeneratorExp(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Await(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Yield(expr) => expr.needs_parentheses(parenthesize, source),
Expr::YieldFrom(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Compare(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Call(expr) => expr.needs_parentheses(parenthesize, source),
Expr::FormattedValue(expr) => expr.needs_parentheses(parenthesize, source),
Expr::JoinedStr(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Constant(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Attribute(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Subscript(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Starred(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Name(expr) => expr.needs_parentheses(parenthesize, source),
Expr::List(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Tuple(expr) => expr.needs_parentheses(parenthesize, source),
Expr::Slice(expr) => expr.needs_parentheses(parenthesize, source),
Expr::BoolOp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::NamedExpr(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::BinOp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::UnaryOp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Lambda(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::IfExp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Dict(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Set(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::ListComp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::SetComp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::DictComp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::GeneratorExp(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Await(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Yield(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::YieldFrom(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Compare(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Call(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::FormattedValue(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::JoinedStr(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Constant(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Attribute(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Subscript(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Starred(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Name(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::List(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Tuple(expr) => expr.needs_parentheses(parenthesize, source, comments),
Expr::Slice(expr) => expr.needs_parentheses(parenthesize, source, comments),
}
}
}

View file

@ -1,15 +1,22 @@
use crate::comments::Comments;
use crate::trivia::{first_non_trivia_token, first_non_trivia_token_rev, Token, TokenKind};
use ruff_python_ast::node::AnyNodeRef;
use rustpython_parser::ast::Ranged;
pub(crate) trait NeedsParentheses {
fn needs_parentheses(&self, parenthesize: Parenthesize, source: &str) -> Parentheses;
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses;
}
pub(super) fn default_expression_needs_parentheses(
node: AnyNodeRef,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
) -> Parentheses {
debug_assert!(
node.is_expression(),
@ -20,9 +27,14 @@ pub(super) fn default_expression_needs_parentheses(
if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
Parentheses::Always
}
// `Optional` or `IfBreaks`: Add parentheses if the expression doesn't fit on a line
// `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() {
Parentheses::Optional
if comments.has_leading_comments(node) {
Parentheses::Always
} else {
Parentheses::Optional
}
} else {
//`Preserve` and expression has no parentheses in the source code
Parentheses::Never