Invert token iteration order in macro mapping

This commit is contained in:
Lukas Wirth 2024-10-25 13:29:07 +02:00
parent 57683cfe1f
commit 6c70806b31
8 changed files with 33 additions and 25 deletions

View File

@ -954,6 +954,10 @@ fn descend_into_macros_impl<T>(
}; };
while let Some((expansion, ref mut tokens)) = stack.pop() { while let Some((expansion, ref mut tokens)) = stack.pop() {
// Reverse the tokens so we prefer first tokens (to accommodate for popping from the
// back)
// alternatively we could pop from the front but that would shift the content on every pop
tokens.reverse();
while let Some((token, ctx)) = tokens.pop() { while let Some((token, ctx)) = tokens.pop() {
let was_not_remapped = (|| { let was_not_remapped = (|| {
// First expand into attribute invocations // First expand into attribute invocations

View File

@ -301,6 +301,8 @@ pub struct Ranker<'a> {
} }
impl<'a> Ranker<'a> { impl<'a> Ranker<'a> {
pub const MAX_RANK: usize = 0b1110;
pub fn from_token(token: &'a syntax::SyntaxToken) -> Self { pub fn from_token(token: &'a syntax::SyntaxToken) -> Self {
let kind = token.kind(); let kind = token.kind();
Ranker { kind, text: token.text(), ident_kind: kind.is_any_identifier() } Ranker { kind, text: token.text(), ident_kind: kind.is_any_identifier() }
@ -317,9 +319,9 @@ pub fn rank_token(&self, tok: &syntax::SyntaxToken) -> usize {
// anything that mapped into a token tree has likely no semantic information // anything that mapped into a token tree has likely no semantic information
let no_tt_parent = let no_tt_parent =
tok.parent().map_or(false, |it| it.kind() != parser::SyntaxKind::TOKEN_TREE); tok.parent().map_or(false, |it| it.kind() != parser::SyntaxKind::TOKEN_TREE);
!((both_idents as usize) (both_idents as usize)
| ((exact_same_kind as usize) << 1) | ((exact_same_kind as usize) << 1)
| ((same_text as usize) << 2) | ((same_text as usize) << 2)
| ((no_tt_parent as usize) << 3)) | ((no_tt_parent as usize) << 3)
} }
} }

View File

@ -510,6 +510,7 @@ fn caller$0() {
expect![[]], expect![[]],
); );
} }
#[test] #[test]
fn test_call_hierarchy_in_macros_incoming_different_files() { fn test_call_hierarchy_in_macros_incoming_different_files() {
check_hierarchy( check_hierarchy(
@ -591,9 +592,9 @@ fn $ident() {
"#, "#,
expect!["callee Function FileId(0) 22..37 30..36"], expect!["callee Function FileId(0) 22..37 30..36"],
expect![[r#" expect![[r#"
callee Function FileId(0) 38..52 44..50 : FileId(0):44..50
caller Function FileId(0) 38..52 : FileId(0):44..50 caller Function FileId(0) 38..52 : FileId(0):44..50
caller Function FileId(1) 130..136 130..136 : FileId(0):44..50"#]], caller Function FileId(1) 130..136 130..136 : FileId(0):44..50
callee Function FileId(0) 38..52 44..50 : FileId(0):44..50"#]],
expect![[]], expect![[]],
); );
} }

View File

@ -184,7 +184,7 @@ fn hover_offset(
let ranker = Ranker::from_token(&original_token); let ranker = Ranker::from_token(&original_token);
descended.sort_by_cached_key(|tok| ranker.rank_token(tok)); descended.sort_by_cached_key(|tok| !ranker.rank_token(tok));
let mut res = vec![]; let mut res = vec![];
for token in descended { for token in descended {

View File

@ -288,19 +288,6 @@ fn $name() {}
expect![[r#" expect![[r#"
*abc* *abc*
```rust
test::module
```
```rust
fn abc()
```
---
Inner
---
```rust ```rust
test test
``` ```
@ -312,6 +299,19 @@ fn abc()
--- ---
Outer Outer
---
```rust
test::module
```
```rust
fn abc()
```
---
Inner
"#]], "#]],
); );
} }

View File

@ -1701,14 +1701,14 @@ fn f() {
} }
"#, "#,
expect![[r#" expect![[r#"
func Function FileId(0) 137..146 140..144
FileId(0) 161..165
func Function FileId(0) 137..146 140..144 module func Function FileId(0) 137..146 140..144 module
FileId(0) 181..185 FileId(0) 181..185
func Function FileId(0) 137..146 140..144
FileId(0) 161..165
"#]], "#]],
) )
} }

View File

@ -397,6 +397,7 @@ fn item(&self) -> &ast::Item {
Some(AttrOrDerive::Derive(_)) => inside_attribute, Some(AttrOrDerive::Derive(_)) => inside_attribute,
None => false, None => false,
}; };
let descended_element = if in_macro { let descended_element = if in_macro {
// Attempt to descend tokens into macro-calls. // Attempt to descend tokens into macro-calls.
let res = match element { let res = match element {
@ -412,7 +413,7 @@ fn item(&self) -> &ast::Item {
let tok = tok.value; let tok = tok.value;
let my_rank = ranker.rank_token(&tok); let my_rank = ranker.rank_token(&tok);
if my_rank > 0b1110 { if my_rank >= Ranker::MAX_RANK {
// a rank of 0b1110 means that we have found a maximally interesting // a rank of 0b1110 means that we have found a maximally interesting
// token so stop early. // token so stop early.
t = Some(tok); t = Some(tok);

View File

@ -50,4 +50,4 @@ pre { color: #DCDCCC; background: #3F3F3F; font-size: 22px; padd
<span class="brace">}</span> <span class="brace">}</span>
<span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">issue_18089</span><span class="attribute_bracket attribute">]</span> <span class="attribute_bracket attribute">#</span><span class="attribute_bracket attribute">[</span><span class="module attribute crate_root library">proc_macros</span><span class="operator attribute">::</span><span class="attribute attribute library">issue_18089</span><span class="attribute_bracket attribute">]</span>
<span class="keyword">fn</span> <span class="function declaration">template</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span></code></pre> <span class="keyword">fn</span> <span class="macro declaration">template</span><span class="parenthesis">(</span><span class="parenthesis">)</span> <span class="brace">{</span><span class="brace">}</span></code></pre>