Pass FormatContext to NeedsParentheses

<!--
Thank you for contributing to Ruff! To help us out with reviewing, please consider the following:

- Does this pull request include a summary of the change? (See below.)
- Does this pull request include a descriptive title?
- Does this pull request include references to any relevant issues?
-->

## Summary

I started working on this because I assumed that I would need access to options inside of `NeedsParantheses` but it then turned out that I won't. 
Anyway, it kind of felt nice to pass fewer arguments. So I'm gonna put this out here to get your feedback if you prefer this over passing individual fiels. 

Oh, I sneeked in another change. I renamed `context.contents` to `source`. `contents` is too generic and doesn't tell you anything. 

<!-- What's the purpose of the change? What does it do, and why? -->

## Test Plan

It compiles
This commit is contained in:
Micha Reiser 2023-07-11 14:28:50 +02:00 committed by GitHub
parent 9a8ba58b4c
commit 8665a1a19d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
37 changed files with 138 additions and 174 deletions

View file

@ -97,14 +97,13 @@ impl NeedsParentheses for ExprAttribute {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
if has_breaking_comments_attribute_chain(self, comments) {
if has_breaking_comments_attribute_chain(self, context.comments()) {
return Parentheses::Always;
}
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -21,9 +21,8 @@ impl NeedsParentheses for ExprAwait {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::{trailing_comments, trailing_node_comments, Comments};
use crate::comments::{trailing_comments, trailing_node_comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, in_parentheses_only_group, is_expression_parenthesized,
NeedsParentheses, Parenthesize,
@ -33,7 +33,7 @@ impl FormatNodeRule<ExprBinOp> for FormatExprBinOp {
let comments = f.context().comments().clone();
let format_inner = format_with(|f: &mut PyFormatter| {
let source = f.context().contents();
let source = f.context().source();
let binary_chain: SmallVec<[&ExprBinOp; 4]> = iter::successors(Some(item), |parent| {
parent.left.as_bin_op_expr().and_then(|bin_expression| {
if is_expression_parenthesized(bin_expression.as_any_node_ref(), source) {
@ -176,9 +176,8 @@ impl NeedsParentheses for ExprBinOp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::{leading_comments, Comments};
use crate::comments::leading_comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
Parenthesize,
@ -71,10 +71,9 @@ impl NeedsParentheses for ExprBoolOp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,5 +1,6 @@
use crate::builders::PyFormatterExtensions;
use crate::comments::{dangling_comments, Comments};
use crate::comments::dangling_comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -88,10 +89,9 @@ impl NeedsParentheses for ExprCall {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::{leading_comments, Comments};
use crate::comments::leading_comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
Parenthesize,
@ -70,10 +70,9 @@ impl NeedsParentheses for ExprCompare {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,3 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -63,10 +62,9 @@ impl NeedsParentheses for ExprConstant {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional if self.value.is_str() && parenthesize.is_if_breaks() => {
// Custom handling that only adds parentheses for implicit concatenated strings.
if parenthesize.is_if_breaks() {

View file

@ -1,4 +1,4 @@
use crate::comments::{dangling_node_comments, leading_comments, Comments};
use crate::comments::{dangling_node_comments, leading_comments};
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -100,10 +100,9 @@ impl NeedsParentheses for ExprDict {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -24,10 +24,9 @@ impl NeedsParentheses for ExprDictComp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -19,9 +19,8 @@ impl NeedsParentheses for ExprFormattedValue {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -24,10 +24,9 @@ impl NeedsParentheses for ExprGeneratorExp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::{leading_comments, Comments};
use crate::comments::leading_comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, in_parentheses_only_group, NeedsParentheses, Parentheses,
Parenthesize,
@ -47,9 +47,8 @@ impl NeedsParentheses for ExprIfExp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -19,9 +19,8 @@ impl NeedsParentheses for ExprJoinedStr {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -19,9 +19,8 @@ impl NeedsParentheses for ExprLambda {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::{dangling_comments, CommentLinePosition, Comments};
use crate::comments::{dangling_comments, CommentLinePosition};
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -68,10 +68,9 @@ impl NeedsParentheses for ExprList {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -45,10 +45,9 @@ impl NeedsParentheses for ExprListComp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,3 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -30,10 +29,9 @@ impl NeedsParentheses for ExprName {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -34,10 +34,9 @@ impl NeedsParentheses for ExprNamedExpr {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
// Unlike tuples, named expression parentheses are not part of the range even when
// mandatory. See [PEP 572](https://peps.python.org/pep-0572/) for details.
Parentheses::Optional => Parentheses::Always,

View file

@ -1,4 +1,3 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -31,10 +30,9 @@ impl NeedsParentheses for ExprSet {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -24,10 +24,9 @@ impl NeedsParentheses for ExprSetComp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,4 +1,5 @@
use crate::comments::{dangling_comments, Comments, SourceComment};
use crate::comments::{dangling_comments, SourceComment};
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -27,8 +28,7 @@ impl FormatNodeRule<ExprSlice> for FormatExprSlice {
step,
} = item;
let (first_colon, second_colon) =
find_colons(f.context().contents(), *range, lower, upper)?;
let (first_colon, second_colon) = find_colons(f.context().source(), *range, lower, upper)?;
// Handle comment placement
// In placements.rs, we marked comment for None nodes a dangling and associated all others
@ -263,9 +263,8 @@ impl NeedsParentheses for ExprSlice {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -2,7 +2,7 @@ use rustpython_parser::ast::ExprStarred;
use ruff_formatter::write;
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -34,9 +34,8 @@ impl NeedsParentheses for ExprStarred {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -3,8 +3,9 @@ use rustpython_parser::ast::ExprSubscript;
use ruff_formatter::{format_args, write};
use ruff_python_ast::node::AstNode;
use crate::comments::{trailing_comments, Comments};
use crate::comments::trailing_comments;
use crate::context::NodeLevel;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -66,10 +67,9 @@ impl NeedsParentheses for ExprSubscript {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}

View file

@ -1,5 +1,5 @@
use crate::builders::optional_parentheses;
use crate::comments::{dangling_comments, CommentLinePosition, Comments};
use crate::comments::{dangling_comments, CommentLinePosition};
use crate::expression::parentheses::{
default_expression_needs_parentheses, parenthesized, NeedsParentheses, Parentheses,
Parenthesize,
@ -131,10 +131,9 @@ impl NeedsParentheses for ExprTuple {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => Parentheses::Never,
parentheses => parentheses,
}
@ -148,7 +147,7 @@ fn is_parenthesized(
f: &mut Formatter<PyFormatContext<'_>>,
) -> bool {
let parentheses = '(';
let first_char = &f.context().contents()[usize::from(tuple_range.start())..]
let first_char = &f.context().source()[usize::from(tuple_range.start())..]
.chars()
.next();
let Some(first_char) = first_char else {

View file

@ -1,4 +1,5 @@
use crate::comments::{trailing_comments, Comments};
use crate::comments::trailing_comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -70,13 +71,12 @@ impl NeedsParentheses for ExprUnaryOp {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match default_expression_needs_parentheses(self.into(), parenthesize, source, comments) {
match default_expression_needs_parentheses(self.into(), parenthesize, context) {
Parentheses::Optional => {
// We preserve the parentheses of the operand. It should not be necessary to break this expression.
if is_operand_parenthesized(self, source) {
if is_operand_parenthesized(self, context.source()) {
Parentheses::Never
} else {
Parentheses::Optional

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -19,9 +19,8 @@ impl NeedsParentheses for ExprYield {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
@ -19,9 +19,8 @@ impl NeedsParentheses for ExprYieldFrom {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
default_expression_needs_parentheses(self.into(), parenthesize, source, comments)
default_expression_needs_parentheses(self.into(), parenthesize, context)
}
}

View file

@ -9,7 +9,6 @@ use ruff_formatter::{
use ruff_python_ast::node::AnyNodeRef;
use ruff_python_ast::visitor::preorder::{walk_expr, PreorderVisitor};
use crate::comments::Comments;
use crate::context::NodeLevel;
use crate::expression::expr_tuple::TupleParentheses;
use crate::expression::parentheses::{
@ -64,11 +63,7 @@ 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(),
f.context().comments(),
);
let parentheses = item.needs_parentheses(self.parenthesize, f.context());
let format_expr = format_with(|f| match item {
Expr::BoolOp(expr) => expr.format().with_options(Some(parentheses)).fmt(f),
@ -172,37 +167,36 @@ impl NeedsParentheses for Expr {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
match self {
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),
Expr::BoolOp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::NamedExpr(expr) => expr.needs_parentheses(parenthesize, context),
Expr::BinOp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::UnaryOp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Lambda(expr) => expr.needs_parentheses(parenthesize, context),
Expr::IfExp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Dict(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Set(expr) => expr.needs_parentheses(parenthesize, context),
Expr::ListComp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::SetComp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::DictComp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::GeneratorExp(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Await(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Yield(expr) => expr.needs_parentheses(parenthesize, context),
Expr::YieldFrom(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Compare(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Call(expr) => expr.needs_parentheses(parenthesize, context),
Expr::FormattedValue(expr) => expr.needs_parentheses(parenthesize, context),
Expr::JoinedStr(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Constant(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Attribute(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Subscript(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Starred(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Name(expr) => expr.needs_parentheses(parenthesize, context),
Expr::List(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Tuple(expr) => expr.needs_parentheses(parenthesize, context),
Expr::Slice(expr) => expr.needs_parentheses(parenthesize, context),
}
}
}
@ -233,7 +227,7 @@ impl<'ast> IntoFormat<PyFormatContext<'ast>> for Expr {
///
/// This mimics Black's [`_maybe_split_omitting_optional_parens`](https://github.com/psf/black/blob/d1248ca9beaf0ba526d265f4108836d89cf551b7/src/black/linegen.py#L746-L820)
fn can_omit_optional_parentheses(expr: &Expr, context: &PyFormatContext) -> bool {
let mut visitor = MaxOperatorPriorityVisitor::new(context.contents());
let mut visitor = MaxOperatorPriorityVisitor::new(context.source());
visitor.visit_subexpression(expr);

View file

@ -1,4 +1,3 @@
use crate::comments::Comments;
use crate::context::NodeLevel;
use crate::prelude::*;
use crate::trivia::{first_non_trivia_token, first_non_trivia_token_rev, Token, TokenKind};
@ -11,16 +10,14 @@ pub(crate) trait NeedsParentheses {
fn needs_parentheses(
&self,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses;
}
pub(super) fn default_expression_needs_parentheses(
node: AnyNodeRef,
parenthesize: Parenthesize,
source: &str,
comments: &Comments,
context: &PyFormatContext,
) -> Parentheses {
debug_assert!(
node.is_expression(),
@ -34,13 +31,13 @@ pub(super) fn default_expression_needs_parentheses(
Parentheses::Never
}
// `Optional` or `Preserve` and expression has parentheses in source code.
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, context.source()) {
Parentheses::Always
}
// `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 comments.has_leading_comments(node) {
if context.comments().has_leading_comments(node) {
Parentheses::Always
} else {
Parentheses::Optional