-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: support on whitespace inside macro for descend_node_at_offset #22971
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ use stdx::{TupleExt, always}; | |
| use syntax::{ | ||
| AstNode, AstPtr, AstToken, Direction, SmolStr, SmolStrBuilder, SyntaxElement, SyntaxKind, | ||
| SyntaxNode, SyntaxNodePtr, SyntaxToken, T, TextRange, TextSize, | ||
| algo::skip_trivia_token, | ||
| algo::{skip_trivia_token, skip_whitespace_token}, | ||
| ast::{self, HasAttrs as _, HasGenericParams}, | ||
| }; | ||
|
|
||
|
|
@@ -1534,7 +1534,20 @@ impl<'db> SemanticsImpl<'db> { | |
| node: &SyntaxNode, | ||
| offset: TextSize, | ||
| ) -> impl Iterator<Item = impl Iterator<Item = SyntaxNode> + '_> + '_ { | ||
| node.token_at_offset(offset) | ||
| let (on_whitespace, tokens) = match node.token_at_offset(offset) { | ||
| syntax::TokenAtOffset::Single(token) if token.kind() == SyntaxKind::WHITESPACE => { | ||
| let prev = token | ||
| .prev_token() | ||
| .and_then(|token| skip_whitespace_token(token, Direction::Prev)); | ||
| let next = token | ||
| .next_token() | ||
| .and_then(|token| skip_whitespace_token(token, Direction::Next)); | ||
| (true, Either::Left(itertools::chain!([token], prev, next))) | ||
| } | ||
| tokens => (false, Either::Right(tokens)), | ||
| }; | ||
| let file_id = self.find_file(node).file_id; | ||
| tokens | ||
| .map(move |token| self.descend_into_macros_exact(token)) | ||
| .map(|descendants| { | ||
| descendants.into_iter().map(move |it| self.token_ancestors_with_macros(it)) | ||
|
|
@@ -1546,6 +1559,15 @@ impl<'db> SemanticsImpl<'db> { | |
| .map(|node| node.text_range().len()) | ||
| .lt(right.clone().map(|node| node.text_range().len())) | ||
| }) | ||
| .map(move |ancestors| { | ||
| ancestors.filter(move |node| { | ||
| if !on_whitespace { | ||
| return true; | ||
| } | ||
| let origin = self.original_range(node); | ||
| origin.file_id == file_id && origin.range.contains(offset) | ||
| }) | ||
| }) | ||
|
Comment on lines
+1562
to
+1570
Contributor
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 should come before the
Member
Author
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. If filtering is done before Checking the |
||
| } | ||
|
|
||
| /// Attempts to map the node out of macro expanded files returning the original file range. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Hmm, not a fan of this implementation, and it also doesn't account for comments.
How about:
View changes since the review
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.
I think triggering on comments is too surprising
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.
This is similar to the behavior of
trimmed_range