Skip to content

Commit 6c70806

Browse files
committed
Invert token iteration order in macro mapping
1 parent 57683cf commit 6c70806

File tree

8 files changed

+23
-15
lines changed

8 files changed

+23
-15
lines changed

src/tools/rust-analyzer/crates/hir/src/semantics.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,10 @@ impl<'db> SemanticsImpl<'db> {
954954
};
955955

956956
while let Some((expansion, ref mut tokens)) = stack.pop() {
957+
// Reverse the tokens so we prefer first tokens (to accommodate for popping from the
958+
// back)
959+
// alternatively we could pop from the front but that would shift the content on every pop
960+
tokens.reverse();
957961
while let Some((token, ctx)) = tokens.pop() {
958962
let was_not_remapped = (|| {
959963
// First expand into attribute invocations

src/tools/rust-analyzer/crates/ide-db/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ pub struct Ranker<'a> {
301301
}
302302

303303
impl<'a> Ranker<'a> {
304+
pub const MAX_RANK: usize = 0b1110;
305+
304306
pub fn from_token(token: &'a syntax::SyntaxToken) -> Self {
305307
let kind = token.kind();
306308
Ranker { kind, text: token.text(), ident_kind: kind.is_any_identifier() }
@@ -317,9 +319,9 @@ impl<'a> Ranker<'a> {
317319
// anything that mapped into a token tree has likely no semantic information
318320
let no_tt_parent =
319321
tok.parent().map_or(false, |it| it.kind() != parser::SyntaxKind::TOKEN_TREE);
320-
!((both_idents as usize)
322+
(both_idents as usize)
321323
| ((exact_same_kind as usize) << 1)
322324
| ((same_text as usize) << 2)
323-
| ((no_tt_parent as usize) << 3))
325+
| ((no_tt_parent as usize) << 3)
324326
}
325327
}

src/tools/rust-analyzer/crates/ide/src/call_hierarchy.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ fn caller$0() {
510510
expect![[]],
511511
);
512512
}
513+
513514
#[test]
514515
fn test_call_hierarchy_in_macros_incoming_different_files() {
515516
check_hierarchy(
@@ -591,9 +592,9 @@ macro_rules! call {
591592
"#,
592593
expect!["callee Function FileId(0) 22..37 30..36"],
593594
expect![[r#"
594-
callee Function FileId(0) 38..52 44..50 : FileId(0):44..50
595595
caller Function FileId(0) 38..52 : FileId(0):44..50
596-
caller Function FileId(1) 130..136 130..136 : FileId(0):44..50"#]],
596+
caller Function FileId(1) 130..136 130..136 : FileId(0):44..50
597+
callee Function FileId(0) 38..52 44..50 : FileId(0):44..50"#]],
597598
expect![[]],
598599
);
599600
}

src/tools/rust-analyzer/crates/ide/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn hover_offset(
184184

185185
let ranker = Ranker::from_token(&original_token);
186186

187-
descended.sort_by_cached_key(|tok| ranker.rank_token(tok));
187+
descended.sort_by_cached_key(|tok| !ranker.rank_token(tok));
188188

189189
let mut res = vec![];
190190
for token in descended {

src/tools/rust-analyzer/crates/ide/src/hover/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ m!(ab$0c);
289289
*abc*
290290
291291
```rust
292-
test::module
292+
test
293293
```
294294
295295
```rust
@@ -298,11 +298,11 @@ m!(ab$0c);
298298
299299
---
300300
301-
Inner
301+
Outer
302302
---
303303
304304
```rust
305-
test
305+
test::module
306306
```
307307
308308
```rust
@@ -311,7 +311,7 @@ m!(ab$0c);
311311
312312
---
313313
314-
Outer
314+
Inner
315315
"#]],
316316
);
317317
}

src/tools/rust-analyzer/crates/ide/src/references.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,14 +1701,14 @@ fn f() {
17011701
}
17021702
"#,
17031703
expect![[r#"
1704-
func Function FileId(0) 137..146 140..144
1704+
func Function FileId(0) 137..146 140..144 module
17051705
1706-
FileId(0) 161..165
1706+
FileId(0) 181..185
17071707
17081708
1709-
func Function FileId(0) 137..146 140..144 module
1709+
func Function FileId(0) 137..146 140..144
17101710
1711-
FileId(0) 181..185
1711+
FileId(0) 161..165
17121712
"#]],
17131713
)
17141714
}

src/tools/rust-analyzer/crates/ide/src/syntax_highlighting.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ fn traverse(
397397
Some(AttrOrDerive::Derive(_)) => inside_attribute,
398398
None => false,
399399
};
400+
400401
let descended_element = if in_macro {
401402
// Attempt to descend tokens into macro-calls.
402403
let res = match element {
@@ -412,7 +413,7 @@ fn traverse(
412413
let tok = tok.value;
413414
let my_rank = ranker.rank_token(&tok);
414415

415-
if my_rank > 0b1110 {
416+
if my_rank >= Ranker::MAX_RANK {
416417
// a rank of 0b1110 means that we have found a maximally interesting
417418
// token so stop early.
418419
t = Some(tok);

src/tools/rust-analyzer/crates/ide/src/syntax_highlighting/test_data/highlight_issue_18089.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
<span class="brace">}</span>
5151

5252
<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>
53-
<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>
53+
<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>

0 commit comments

Comments
 (0)