From 553008e7d922e775b60da3df99533496a23eec4e Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sat, 8 Jul 2023 15:36:28 +0200 Subject: [PATCH] Pass `FormatContext` to `NeedsParentheses` --- crates/ruff_python_formatter/src/builders.rs | 4 +- .../src/comments/format.rs | 14 ++-- crates/ruff_python_formatter/src/context.rs | 2 +- .../src/expression/expr_attribute.rs | 7 +- .../src/expression/expr_await.rs | 7 +- .../src/expression/expr_bin_op.rs | 9 ++- .../src/expression/expr_bool_op.rs | 7 +- .../src/expression/expr_call.rs | 8 +-- .../src/expression/expr_compare.rs | 7 +- .../src/expression/expr_constant.rs | 6 +- .../src/expression/expr_dict.rs | 7 +- .../src/expression/expr_dict_comp.rs | 7 +- .../src/expression/expr_formatted_value.rs | 7 +- .../src/expression/expr_generator_exp.rs | 7 +- .../src/expression/expr_if_exp.rs | 7 +- .../src/expression/expr_joined_str.rs | 7 +- .../src/expression/expr_lambda.rs | 7 +- .../src/expression/expr_list.rs | 7 +- .../src/expression/expr_list_comp.rs | 7 +- .../src/expression/expr_name.rs | 6 +- .../src/expression/expr_named_expr.rs | 7 +- .../src/expression/expr_set.rs | 6 +- .../src/expression/expr_set_comp.rs | 7 +- .../src/expression/expr_slice.rs | 11 ++-- .../src/expression/expr_starred.rs | 7 +- .../src/expression/expr_subscript.rs | 8 +-- .../src/expression/expr_tuple.rs | 9 ++- .../src/expression/expr_unary_op.rs | 10 +-- .../src/expression/expr_yield.rs | 7 +- .../src/expression/expr_yield_from.rs | 7 +- .../src/expression/mod.rs | 66 +++++++++---------- .../src/expression/parentheses.rs | 11 ++-- .../src/other/arguments.rs | 6 +- .../src/statement/stmt_class_def.rs | 2 +- .../src/statement/stmt_expr.rs | 2 +- .../src/statement/stmt_function_def.rs | 4 +- .../src/statement/suite.rs | 2 +- 37 files changed, 138 insertions(+), 174 deletions(-) diff --git a/crates/ruff_python_formatter/src/builders.rs b/crates/ruff_python_formatter/src/builders.rs index d39ff8a46dbde..8eeb07a819954 100644 --- a/crates/ruff_python_formatter/src/builders.rs +++ b/crates/ruff_python_formatter/src/builders.rs @@ -94,7 +94,7 @@ impl<'fmt, 'ast, 'buf> JoinNodesBuilder<'fmt, 'ast, 'buf> { self.result = self.result.and_then(|_| { if let Some(last_end) = self.last_end.replace(node.end()) { - let source = self.fmt.context().contents(); + let source = self.fmt.context().source(); let count_lines = |offset| { // It's necessary to skip any trailing line comment because RustPython doesn't include trailing comments // in the node's range @@ -262,7 +262,7 @@ impl<'fmt, 'ast, 'buf> JoinCommaSeparatedBuilder<'fmt, 'ast, 'buf> { if let Some(last_end) = self.end_of_last_entry.take() { let magic_trailing_comma = self.fmt.options().magic_trailing_comma().is_respect() && matches!( - first_non_trivia_token(last_end, self.fmt.context().contents()), + first_non_trivia_token(last_end, self.fmt.context().source()), Some(Token { kind: TokenKind::Comma, .. diff --git a/crates/ruff_python_formatter/src/comments/format.rs b/crates/ruff_python_formatter/src/comments/format.rs index c0a0a2a5bd3fc..84b0e3b654a6d 100644 --- a/crates/ruff_python_formatter/src/comments/format.rs +++ b/crates/ruff_python_formatter/src/comments/format.rs @@ -43,7 +43,7 @@ impl Format> for FormatLeadingComments<'_> { { let slice = comment.slice(); - let lines_after_comment = lines_after(slice.end(), f.context().contents()); + let lines_after_comment = lines_after(slice.end(), f.context().source()); write!( f, [format_comment(comment), empty_lines(lines_after_comment)] @@ -84,16 +84,16 @@ impl Format> for FormatLeadingAlternateBranchComments<'_> { if let Some(first_leading) = self.comments.first() { // Leading comments only preserves the lines after the comment but not before. // Insert the necessary lines. - if lines_before(first_leading.slice().start(), f.context().contents()) > 1 { + if lines_before(first_leading.slice().start(), f.context().source()) > 1 { write!(f, [empty_line()])?; } write!(f, [leading_comments(self.comments)])?; } else if let Some(last_preceding) = self.last_node { - let full_end = skip_trailing_trivia(last_preceding.end(), f.context().contents()); + let full_end = skip_trailing_trivia(last_preceding.end(), f.context().source()); // The leading comments formatting ensures that it preserves the right amount of lines after // We need to take care of this ourselves, if there's no leading `else` comment. - if lines_after(full_end, f.context().contents()) > 1 { + if lines_after(full_end, f.context().source()) > 1 { write!(f, [empty_line()])?; } } @@ -140,7 +140,7 @@ impl Format> for FormatTrailingComments<'_> { has_trailing_own_line_comment |= trailing.line_position().is_own_line(); if has_trailing_own_line_comment { - let lines_before_comment = lines_before(slice.start(), f.context().contents()); + let lines_before_comment = lines_before(slice.start(), f.context().source()); // A trailing comment at the end of a body or list // ```python @@ -217,7 +217,7 @@ impl Format> for FormatDanglingComments<'_> { f, [ format_comment(comment), - empty_lines(lines_after(comment.slice().end(), f.context().contents())) + empty_lines(lines_after(comment.slice().end(), f.context().source())) ] )?; @@ -245,7 +245,7 @@ struct FormatComment<'a> { impl Format> for FormatComment<'_> { fn fmt(&self, f: &mut Formatter>) -> FormatResult<()> { let slice = self.comment.slice(); - let comment_text = slice.text(SourceCode::new(f.context().contents())); + let comment_text = slice.text(SourceCode::new(f.context().source())); let trimmed = comment_text.trim_end(); let trailing_whitespace_len = comment_text.text_len() - trimmed.text_len(); diff --git a/crates/ruff_python_formatter/src/context.rs b/crates/ruff_python_formatter/src/context.rs index 2683641dd861a..d0828745c8c65 100644 --- a/crates/ruff_python_formatter/src/context.rs +++ b/crates/ruff_python_formatter/src/context.rs @@ -22,7 +22,7 @@ impl<'a> PyFormatContext<'a> { } } - pub(crate) fn contents(&self) -> &'a str { + pub(crate) fn source(&self) -> &'a str { self.contents } diff --git a/crates/ruff_python_formatter/src/expression/expr_attribute.rs b/crates/ruff_python_formatter/src/expression/expr_attribute.rs index 16ccaa330c318..2fd19d768f88e 100644 --- a/crates/ruff_python_formatter/src/expression/expr_attribute.rs +++ b/crates/ruff_python_formatter/src/expression/expr_attribute.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_await.rs b/crates/ruff_python_formatter/src/expression/expr_await.rs index 19c6bf5196a5e..6e275fd9a1215 100644 --- a/crates/ruff_python_formatter/src/expression/expr_await.rs +++ b/crates/ruff_python_formatter/src/expression/expr_await.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs index 9307ed204320d..f6f051066a80a 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bin_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bin_op.rs @@ -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 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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs index 5a350645095ce..9ba2f9a2b223e 100644 --- a/crates/ruff_python_formatter/src/expression/expr_bool_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_bool_op.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_call.rs b/crates/ruff_python_formatter/src/expression/expr_call.rs index 87ed281572c7e..070027fc21f9f 100644 --- a/crates/ruff_python_formatter/src/expression/expr_call.rs +++ b/crates/ruff_python_formatter/src/expression/expr_call.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_compare.rs b/crates/ruff_python_formatter/src/expression/expr_compare.rs index 10bffe948b056..988a64ddf4d84 100644 --- a/crates/ruff_python_formatter/src/expression/expr_compare.rs +++ b/crates/ruff_python_formatter/src/expression/expr_compare.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_constant.rs b/crates/ruff_python_formatter/src/expression/expr_constant.rs index 68d538ae8a0ba..b657b16c28607 100644 --- a/crates/ruff_python_formatter/src/expression/expr_constant.rs +++ b/crates/ruff_python_formatter/src/expression/expr_constant.rs @@ -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() { diff --git a/crates/ruff_python_formatter/src/expression/expr_dict.rs b/crates/ruff_python_formatter/src/expression/expr_dict.rs index 6299fc0845317..1f9fcca5681c9 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs index 4121acb76afc2..12af04579ac08 100644 --- a/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_dict_comp.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_formatted_value.rs b/crates/ruff_python_formatter/src/expression/expr_formatted_value.rs index ef2551de32153..7cc71b967dbee 100644 --- a/crates/ruff_python_formatter/src/expression/expr_formatted_value.rs +++ b/crates/ruff_python_formatter/src/expression/expr_formatted_value.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs index 8cf7e8d38cbb4..2f91b0c518205 100644 --- a/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_generator_exp.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs index ae142e7122277..85bcff304c2cf 100644 --- a/crates/ruff_python_formatter/src/expression/expr_if_exp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_if_exp.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_joined_str.rs b/crates/ruff_python_formatter/src/expression/expr_joined_str.rs index a13ff5f38971d..bd4bcf6fc91c0 100644 --- a/crates/ruff_python_formatter/src/expression/expr_joined_str.rs +++ b/crates/ruff_python_formatter/src/expression/expr_joined_str.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_lambda.rs b/crates/ruff_python_formatter/src/expression/expr_lambda.rs index bd63bfa0f63b9..fa2a4ce3e7b32 100644 --- a/crates/ruff_python_formatter/src/expression/expr_lambda.rs +++ b/crates/ruff_python_formatter/src/expression/expr_lambda.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_list.rs b/crates/ruff_python_formatter/src/expression/expr_list.rs index 90d5e244e1f05..c090182762025 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs index a6d5e236fd950..a911ecf2668dd 100644 --- a/crates/ruff_python_formatter/src/expression/expr_list_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_list_comp.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_name.rs b/crates/ruff_python_formatter/src/expression/expr_name.rs index 25673494818c8..ca07981ea16a6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_name.rs +++ b/crates/ruff_python_formatter/src/expression/expr_name.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs index 437a8981e3feb..e1ba5c1789e9f 100644 --- a/crates/ruff_python_formatter/src/expression/expr_named_expr.rs +++ b/crates/ruff_python_formatter/src/expression/expr_named_expr.rs @@ -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, diff --git a/crates/ruff_python_formatter/src/expression/expr_set.rs b/crates/ruff_python_formatter/src/expression/expr_set.rs index c1b1a9009187a..3af73764611c0 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs index d5174782e089e..e661b133868b1 100644 --- a/crates/ruff_python_formatter/src/expression/expr_set_comp.rs +++ b/crates/ruff_python_formatter/src/expression/expr_set_comp.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_slice.rs b/crates/ruff_python_formatter/src/expression/expr_slice.rs index bc4fbba2287fd..1dcf71099b9f4 100644 --- a/crates/ruff_python_formatter/src/expression/expr_slice.rs +++ b/crates/ruff_python_formatter/src/expression/expr_slice.rs @@ -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 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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_starred.rs b/crates/ruff_python_formatter/src/expression/expr_starred.rs index bb8492e0daa1f..e45e4456765b3 100644 --- a/crates/ruff_python_formatter/src/expression/expr_starred.rs +++ b/crates/ruff_python_formatter/src/expression/expr_starred.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index 7010af60d6ff1..7ac3073adc1e6 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -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, } diff --git a/crates/ruff_python_formatter/src/expression/expr_tuple.rs b/crates/ruff_python_formatter/src/expression/expr_tuple.rs index 82d716ae1d857..81107f8422da3 100644 --- a/crates/ruff_python_formatter/src/expression/expr_tuple.rs +++ b/crates/ruff_python_formatter/src/expression/expr_tuple.rs @@ -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>, ) -> 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 { diff --git a/crates/ruff_python_formatter/src/expression/expr_unary_op.rs b/crates/ruff_python_formatter/src/expression/expr_unary_op.rs index 41a7feced2e0e..8e2655d40b10f 100644 --- a/crates/ruff_python_formatter/src/expression/expr_unary_op.rs +++ b/crates/ruff_python_formatter/src/expression/expr_unary_op.rs @@ -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 diff --git a/crates/ruff_python_formatter/src/expression/expr_yield.rs b/crates/ruff_python_formatter/src/expression/expr_yield.rs index f2ece7ecac66e..a31c56bfbbc68 100644 --- a/crates/ruff_python_formatter/src/expression/expr_yield.rs +++ b/crates/ruff_python_formatter/src/expression/expr_yield.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/expr_yield_from.rs b/crates/ruff_python_formatter/src/expression/expr_yield_from.rs index bfaf10116221c..ae122ded3adfe 100644 --- a/crates/ruff_python_formatter/src/expression/expr_yield_from.rs +++ b/crates/ruff_python_formatter/src/expression/expr_yield_from.rs @@ -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) } } diff --git a/crates/ruff_python_formatter/src/expression/mod.rs b/crates/ruff_python_formatter/src/expression/mod.rs index cc395311de954..87ba364b565c6 100644 --- a/crates/ruff_python_formatter/src/expression/mod.rs +++ b/crates/ruff_python_formatter/src/expression/mod.rs @@ -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> for FormatExpr { impl FormatRule> 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> 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); diff --git a/crates/ruff_python_formatter/src/expression/parentheses.rs b/crates/ruff_python_formatter/src/expression/parentheses.rs index 444697213f698..132e1cf942832 100644 --- a/crates/ruff_python_formatter/src/expression/parentheses.rs +++ b/crates/ruff_python_formatter/src/expression/parentheses.rs @@ -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 diff --git a/crates/ruff_python_formatter/src/other/arguments.rs b/crates/ruff_python_formatter/src/other/arguments.rs index 0682a25632c48..3e84558ad2c6a 100644 --- a/crates/ruff_python_formatter/src/other/arguments.rs +++ b/crates/ruff_python_formatter/src/other/arguments.rs @@ -36,7 +36,7 @@ impl FormatNodeRule for FormatArguments { let comments = f.context().comments().clone(); let dangling = comments.dangling_comments(item); - let (slash, star) = find_argument_separators(f.context().contents(), item); + let (slash, star) = find_argument_separators(f.context().source(), item); let format_inner = format_with(|f: &mut PyFormatter| { let separator = format_with(|f| write!(f, [text(","), soft_line_break_or_space()])); @@ -142,7 +142,7 @@ impl FormatNodeRule for FormatArguments { let maybe_comma_token = if ends_with_pos_only_argument_separator { // `def a(b, c, /): ... ` let mut tokens = - SimpleTokenizer::starts_at(last_node.end(), f.context().contents()) + SimpleTokenizer::starts_at(last_node.end(), f.context().source()) .skip_trivia(); let comma = tokens.next(); @@ -153,7 +153,7 @@ impl FormatNodeRule for FormatArguments { tokens.next() } else { - first_non_trivia_token(last_node.end(), f.context().contents()) + first_non_trivia_token(last_node.end(), f.context().source()) }; if maybe_comma_token.map_or(false, |token| token.kind() == TokenKind::Comma) { diff --git a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs index 3fba6d4a55e9b..2388323ae1333 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_class_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_class_def.rs @@ -79,7 +79,7 @@ impl Format> for FormatInheritanceClause<'_> { .. } = self.class_definition; - let source = f.context().contents(); + let source = f.context().source(); let mut joiner = f.join_comma_separated(); diff --git a/crates/ruff_python_formatter/src/statement/stmt_expr.rs b/crates/ruff_python_formatter/src/statement/stmt_expr.rs index b0c451fa6a6df..4e415f77cf8e8 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_expr.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_expr.rs @@ -13,7 +13,7 @@ impl FormatNodeRule for FormatStmtExpr { if let Some(constant) = value.as_constant_expr() { if constant.value.is_str() - && !is_expression_parenthesized(value.as_ref().into(), f.context().contents()) + && !is_expression_parenthesized(value.as_ref().into(), f.context().source()) { return constant.format().with_options(StringLayout::Flat).fmt(f); } diff --git a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs index 6654a70958b05..d88263e894270 100644 --- a/crates/ruff_python_formatter/src/statement/stmt_function_def.rs +++ b/crates/ruff_python_formatter/src/statement/stmt_function_def.rs @@ -56,9 +56,9 @@ impl FormatRule, PyFormatContext<'_>> for FormatAnyFun // while maintaining the right amount of empty lines between the comment // and the last decorator. let decorator_end = - skip_trailing_trivia(last_decorator.end(), f.context().contents()); + skip_trailing_trivia(last_decorator.end(), f.context().source()); - let leading_line = if lines_after(decorator_end, f.context().contents()) <= 1 { + let leading_line = if lines_after(decorator_end, f.context().source()) <= 1 { hard_line_break() } else { empty_line() diff --git a/crates/ruff_python_formatter/src/statement/suite.rs b/crates/ruff_python_formatter/src/statement/suite.rs index be25b63399bc0..92fc32ed4e601 100644 --- a/crates/ruff_python_formatter/src/statement/suite.rs +++ b/crates/ruff_python_formatter/src/statement/suite.rs @@ -43,7 +43,7 @@ impl FormatRule> for FormatSuite { }; let comments = f.context().comments().clone(); - let source = f.context().contents(); + let source = f.context().source(); let saved_level = f.context().node_level(); f.context_mut().set_node_level(node_level);