mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-27 12:29:28 +00:00
Use CommentRanges in backwards lexing (#7360)
## Summary The tokenizer was split into a forward and a backwards tokenizer. The backwards tokenizer uses the same names as the forwards ones (e.g. `next_token`). The backwards tokenizer gets the comment ranges that we already built to skip comments. --------- Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
parent
1f6e1485f9
commit
2cbe1733c8
41 changed files with 744 additions and 628 deletions
|
@ -45,7 +45,7 @@ impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
|
|||
value: Constant::Int(_) | Constant::Float(_),
|
||||
..
|
||||
})
|
||||
) || is_expression_parenthesized(value.into(), f.context().source());
|
||||
) || is_expression_parenthesized(value.into(), f.context().comments().ranges(), f.context().source());
|
||||
|
||||
if call_chain_layout == CallChainLayout::Fluent {
|
||||
if parenthesize_value {
|
||||
|
@ -142,15 +142,22 @@ impl NeedsParentheses for ExprAttribute {
|
|||
context: &PyFormatContext,
|
||||
) -> OptionalParentheses {
|
||||
// Checks if there are any own line comments in an attribute chain (a.b.c).
|
||||
if CallChainLayout::from_expression(self.into(), context.source())
|
||||
== CallChainLayout::Fluent
|
||||
if CallChainLayout::from_expression(
|
||||
self.into(),
|
||||
context.comments().ranges(),
|
||||
context.source(),
|
||||
) == CallChainLayout::Fluent
|
||||
{
|
||||
OptionalParentheses::Multiline
|
||||
} else if context.comments().has_dangling(self) {
|
||||
OptionalParentheses::Always
|
||||
} else if self.value.is_name_expr() {
|
||||
OptionalParentheses::BestFit
|
||||
} else if is_expression_parenthesized(self.value.as_ref().into(), context.source()) {
|
||||
} else if is_expression_parenthesized(
|
||||
self.value.as_ref().into(),
|
||||
context.comments().ranges(),
|
||||
context.source(),
|
||||
) {
|
||||
OptionalParentheses::Never
|
||||
} else {
|
||||
self.value.needs_parentheses(self.into(), context)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue