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

@ -79,7 +79,7 @@ impl Format<PyFormatContext<'_>> for FormatInheritanceClause<'_> {
..
} = self.class_definition;
let source = f.context().contents();
let source = f.context().source();
let mut joiner = f.join_comma_separated();

View file

@ -13,7 +13,7 @@ impl FormatNodeRule<StmtExpr> 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);
}

View file

@ -56,9 +56,9 @@ impl FormatRule<AnyFunctionDefinition<'_>, 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()

View file

@ -43,7 +43,7 @@ impl FormatRule<Suite, PyFormatContext<'_>> 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);