mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-02 18:02:23 +00:00
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:
parent
9a8ba58b4c
commit
8665a1a19d
37 changed files with 138 additions and 174 deletions
|
@ -1,4 +1,3 @@
|
|||
use crate::comments::Comments;
|
||||
use crate::context::NodeLevel;
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::{first_non_trivia_token, first_non_trivia_token_rev, Token, TokenKind};
|
||||
|
@ -11,16 +10,14 @@ pub(crate) trait NeedsParentheses {
|
|||
fn needs_parentheses(
|
||||
&self,
|
||||
parenthesize: Parenthesize,
|
||||
source: &str,
|
||||
comments: &Comments,
|
||||
context: &PyFormatContext,
|
||||
) -> Parentheses;
|
||||
}
|
||||
|
||||
pub(super) fn default_expression_needs_parentheses(
|
||||
node: AnyNodeRef,
|
||||
parenthesize: Parenthesize,
|
||||
source: &str,
|
||||
comments: &Comments,
|
||||
context: &PyFormatContext,
|
||||
) -> Parentheses {
|
||||
debug_assert!(
|
||||
node.is_expression(),
|
||||
|
@ -34,13 +31,13 @@ pub(super) fn default_expression_needs_parentheses(
|
|||
Parentheses::Never
|
||||
}
|
||||
// `Optional` or `Preserve` and expression has parentheses in source code.
|
||||
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, source) {
|
||||
else if !parenthesize.is_if_breaks() && is_expression_parenthesized(node, context.source()) {
|
||||
Parentheses::Always
|
||||
}
|
||||
// `Optional` or `IfBreaks`: Add parentheses if the expression doesn't fit on a line but enforce
|
||||
// parentheses if the expression has leading comments
|
||||
else if !parenthesize.is_preserve() {
|
||||
if comments.has_leading_comments(node) {
|
||||
if context.comments().has_leading_comments(node) {
|
||||
Parentheses::Always
|
||||
} else {
|
||||
Parentheses::Optional
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue