Skip to content

Commit

Permalink
Fix build (#7437)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser authored Sep 16, 2023
1 parent 916dd5b commit c907317
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
21 changes: 13 additions & 8 deletions crates/ruff_python_formatter/src/comments/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,29 +305,31 @@ pub(crate) struct Comments<'a> {
/// }
/// ```
data: Rc<CommentsData<'a>>,
/// 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,
}),
}
}

/// Effectively a [`Default`] implementation that works around the lifetimes for tests
#[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.
Expand Down Expand Up @@ -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>);
Expand Down
6 changes: 5 additions & 1 deletion crates/ruff_python_formatter/src/expression/expr_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ impl FormatNodeRule<ExprCall> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ impl FormatNodeRule<ExprSubscript> 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() {
Expand Down

0 comments on commit c907317

Please sign in to comment.