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

@ -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)
}
}