From c907317199016870b5a0e45ba3fe3593e1b9bbf6 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Sat, 16 Sep 2023 16:50:36 +0200 Subject: [PATCH] Fix build (#7437) --- .../ruff_python_formatter/src/comments/mod.rs | 21 ++++++++++++------- .../src/expression/expr_call.rs | 6 +++++- .../src/expression/expr_subscript.rs | 6 +++++- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/ruff_python_formatter/src/comments/mod.rs b/crates/ruff_python_formatter/src/comments/mod.rs index 3e99fc0942f0f..35beb9244706f 100644 --- a/crates/ruff_python_formatter/src/comments/mod.rs +++ b/crates/ruff_python_formatter/src/comments/mod.rs @@ -305,15 +305,15 @@ pub(crate) struct Comments<'a> { /// } /// ``` data: Rc>, - /// We need those for backwards lexing - comment_ranges: &'a CommentRanges, } impl<'a> Comments<'a> { fn new(comments: CommentsMap<'a>, comment_ranges: &'a CommentRanges) -> Self { Self { - data: Rc::new(CommentsData { comments }), - comment_ranges, + data: Rc::new(CommentsData { + comments, + comment_ranges, + }), } } @@ -321,13 +321,15 @@ impl<'a> Comments<'a> { #[cfg(test)] pub(crate) fn from_ranges(comment_ranges: &'a CommentRanges) -> Self { Self { - data: Rc::new(CommentsData::default()), - comment_ranges, + data: Rc::new(CommentsData { + comments: CommentsMap::default(), + comment_ranges, + }), } } pub(crate) fn ranges(&self) -> &'a CommentRanges { - self.comment_ranges + self.data.comment_ranges } /// Extracts the comments from the AST. @@ -521,9 +523,12 @@ impl LeadingDanglingTrailingComments<'_> { } } -#[derive(Debug, Default)] +#[derive(Debug)] struct CommentsData<'a> { comments: CommentsMap<'a>, + + /// We need those for backwards lexing + comment_ranges: &'a CommentRanges, } struct MarkVerbatimCommentsAsFormattedVisitor<'a>(&'a Comments<'a>); diff --git a/crates/ruff_python_formatter/src/expression/expr_call.rs b/crates/ruff_python_formatter/src/expression/expr_call.rs index c0054dc672c0c..842d59062c2d4 100644 --- a/crates/ruff_python_formatter/src/expression/expr_call.rs +++ b/crates/ruff_python_formatter/src/expression/expr_call.rs @@ -38,7 +38,11 @@ impl FormatNodeRule for FormatExprCall { let fmt_func = format_with(|f: &mut PyFormatter| { // Format the function expression. - if is_expression_parenthesized(func.into(), f.context().source()) { + if is_expression_parenthesized( + func.into(), + f.context().comments().ranges(), + f.context().source(), + ) { func.format().with_options(Parentheses::Always).fmt(f) } else { match func.as_ref() { diff --git a/crates/ruff_python_formatter/src/expression/expr_subscript.rs b/crates/ruff_python_formatter/src/expression/expr_subscript.rs index d290e2f18b43f..1397973d1be57 100644 --- a/crates/ruff_python_formatter/src/expression/expr_subscript.rs +++ b/crates/ruff_python_formatter/src/expression/expr_subscript.rs @@ -43,7 +43,11 @@ impl FormatNodeRule for FormatExprSubscript { ); let format_inner = format_with(|f: &mut PyFormatter| { - if is_expression_parenthesized(value.into(), f.context().source()) { + if is_expression_parenthesized( + value.into(), + f.context().comments().ranges(), + f.context().source(), + ) { value.format().with_options(Parentheses::Always).fmt(f) } else { match value.as_ref() {