-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Cache parenthesized expression boundaries in the formatter #26344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0a1c825
017969c
ac5d548
dac066c
c8c7637
a47a229
d81b427
cc9584b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| use std::{iter::FusedIterator, ops::Deref}; | ||
|
|
||
| use super::{Token, TokenKind}; | ||
| use ruff_python_trivia::CommentRanges; | ||
| use ruff_python_trivia::{CommentRanges, ParenthesizedExpressions, TriviaRanges}; | ||
| use ruff_text_size::{Ranged as _, TextRange, TextSize}; | ||
| use rustc_hash::FxHashSet; | ||
|
|
||
| /// Tokens represents a vector of lexed [`Token`]. | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
|
|
@@ -279,15 +280,62 @@ impl FusedIterator for TokenAt {} | |
| impl From<&Tokens> for CommentRanges { | ||
| fn from(tokens: &Tokens) -> Self { | ||
| let mut ranges = vec![]; | ||
|
|
||
| for token in tokens { | ||
| if token.kind() == TokenKind::Comment { | ||
| ranges.push(token.range()); | ||
| } | ||
| } | ||
|
|
||
| CommentRanges::new(ranges) | ||
| } | ||
| } | ||
|
|
||
| impl From<&Tokens> for TriviaRanges { | ||
| fn from(tokens: &Tokens) -> Self { | ||
| let mut comments = vec![]; | ||
| let mut parenthesized = FxHashSet::default(); | ||
| let mut stack = Vec::<Option<TextSize>>::new(); | ||
| let mut previous_end = None; | ||
|
|
||
| for token in tokens { | ||
| if token.kind() == TokenKind::Comment { | ||
| comments.push(token.range()); | ||
| } | ||
|
|
||
| if token.kind().is_trivia() { | ||
| continue; | ||
| } | ||
|
|
||
| match token.kind() { | ||
| TokenKind::Lpar => { | ||
| if let Some(start) = stack.last_mut() { | ||
| start.get_or_insert(token.start()); | ||
| } | ||
| stack.push(None); | ||
| } | ||
| TokenKind::Rpar => { | ||
| if let (Some(Some(start)), Some(end)) = (stack.pop(), previous_end) { | ||
| parenthesized.insert(TextRange::new(start, end)); | ||
| } | ||
| } | ||
| _ => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This opens an interesting question. Is it intentional that we set Edit: We actually don't do this... I should not review code this late :) |
||
| if let Some(start) = stack.last_mut() { | ||
| start.get_or_insert(token.start()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| previous_end = Some(token.end()); | ||
| } | ||
|
|
||
| TriviaRanges::new( | ||
| CommentRanges::new(comments), | ||
| ParenthesizedExpressions::new(parenthesized), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// An iterator over the [`Token`]s with context. | ||
| /// | ||
| /// This struct is created by the [`iter_with_context`] method on [`Tokens`]. Refer to its | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for expanding scope, it's completely fine not to do this in this PR. Should we also replace the function that we use in the linter to use the new trivia ranges?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, I think that should be a separate change.