Skip to content

Commit

Permalink
Pass FormatContext to NeedsParentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 10, 2023
1 parent f06aa8e commit 2c04bdc
Show file tree
Hide file tree
Showing 37 changed files with 138 additions and 174 deletions.
4 changes: 2 additions & 2 deletions crates/ruff_python_formatter/src/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
..
Expand Down
14 changes: 7 additions & 7 deletions crates/ruff_python_formatter/src/comments/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl Format<PyFormatContext<'_>> 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)]
Expand Down Expand Up @@ -84,16 +84,16 @@ impl Format<PyFormatContext<'_>> 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()])?;
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Format<PyFormatContext<'_>> 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
Expand Down Expand Up @@ -217,7 +217,7 @@ impl Format<PyFormatContext<'_>> 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()))
]
)?;

Expand Down Expand Up @@ -245,7 +245,7 @@ struct FormatComment<'a> {
impl Format<PyFormatContext<'_>> for FormatComment<'_> {
fn fmt(&self, f: &mut Formatter<PyFormatContext<'_>>) -> 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();
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_python_formatter/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a> PyFormatContext<'a> {
}
}

pub(crate) fn contents(&self) -> &'a str {
pub(crate) fn source(&self) -> &'a str {
self.contents
}

Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_await.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -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)
}
}
9 changes: 4 additions & 5 deletions crates/ruff_python_formatter/src/expression/expr_bin_op.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_bool_op.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_call.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_compare.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
}

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_constant.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::comments::Comments;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand Down Expand Up @@ -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() {
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_dict.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_dict_comp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -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,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -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,
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_if_exp.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -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)
}
}
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_lambda.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -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)
}
}
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_list.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down
7 changes: 3 additions & 4 deletions crates/ruff_python_formatter/src/expression/expr_list_comp.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::comments::Comments;
use crate::context::PyFormatContext;
use crate::expression::parentheses::{
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
};
Expand All @@ -24,10 +24,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,
}
Expand Down
Loading

0 comments on commit 2c04bdc

Please sign in to comment.