Skip to content

Commit

Permalink
Merge pull request #18410 from Veykril/veykril/push-lvwxpnowqrxk
Browse files Browse the repository at this point in the history
internal: Invert token iteration order in macro mapping
  • Loading branch information
Veykril authored Oct 27, 2024
2 parents 79b86f2 + 20ac30f commit 715b67c
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 50 deletions.
4 changes: 4 additions & 0 deletions crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,10 @@ impl<'db> SemanticsImpl<'db> {
};

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() {
let was_not_remapped = (|| {
// First expand into attribute invocations
Expand Down
32 changes: 32 additions & 0 deletions crates/ide-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,35 @@ impl SnippetCap {
}
}
}

pub struct Ranker<'a> {
pub kind: parser::SyntaxKind,
pub text: &'a str,
pub ident_kind: bool,
}

impl<'a> Ranker<'a> {
pub const MAX_RANK: usize = 0b1110;

pub fn from_token(token: &'a syntax::SyntaxToken) -> Self {
let kind = token.kind();
Ranker { kind, text: token.text(), ident_kind: kind.is_any_identifier() }
}

/// A utility function that ranks a token again a given kind and text, returning a number that
/// represents how close the token is to the given kind and text.
pub fn rank_token(&self, tok: &syntax::SyntaxToken) -> usize {
let tok_kind = tok.kind();

let exact_same_kind = tok_kind == self.kind;
let both_idents = exact_same_kind || (tok_kind.is_any_identifier() && self.ident_kind);
let same_text = tok.text() == self.text;
// anything that mapped into a token tree has likely no semantic information
let no_tt_parent =
tok.parent().map_or(false, |it| it.kind() != parser::SyntaxKind::TOKEN_TREE);
(both_idents as usize)
| ((exact_same_kind as usize) << 1)
| ((same_text as usize) << 2)
| ((no_tt_parent as usize) << 3)
}
}
5 changes: 3 additions & 2 deletions crates/ide/src/call_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ fn caller$0() {
expect![[]],
);
}

#[test]
fn test_call_hierarchy_in_macros_incoming_different_files() {
check_hierarchy(
Expand Down Expand Up @@ -591,9 +592,9 @@ macro_rules! call {
"#,
expect!["callee Function FileId(0) 22..37 30..36"],
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(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![[]],
);
}
Expand Down
24 changes: 5 additions & 19 deletions crates/ide/src/hover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ide_db::{
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
famous_defs::FamousDefs,
helpers::pick_best_token,
FileRange, FxIndexSet, RootDatabase,
FileRange, FxIndexSet, Ranker, RootDatabase,
};
use itertools::{multizip, Itertools};
use span::Edition;
Expand Down Expand Up @@ -182,27 +182,13 @@ fn hover_offset(
// equivalency is more important
let mut descended = sema.descend_into_macros(original_token.clone());

let kind = original_token.kind();
let text = original_token.text();
let ident_kind = kind.is_any_identifier();

descended.sort_by_cached_key(|tok| {
let tok_kind = tok.kind();

let exact_same_kind = tok_kind == kind;
let both_idents = exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
let same_text = tok.text() == text;
// anything that mapped into a token tree has likely no semantic information
let no_tt_parent = tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
!((both_idents as usize)
| ((exact_same_kind as usize) << 1)
| ((same_text as usize) << 2)
| ((no_tt_parent as usize) << 3))
});
let ranker = Ranker::from_token(&original_token);

descended.sort_by_cached_key(|tok| !ranker.rank_token(tok));

let mut res = vec![];
for token in descended {
let is_same_kind = token.kind() == kind;
let is_same_kind = token.kind() == ranker.kind;
let lint_hover = (|| {
// FIXME: Definition should include known lints and the like instead of having this special case here
let attr = token.parent_ancestors().find_map(ast::Attr::cast)?;
Expand Down
8 changes: 4 additions & 4 deletions crates/ide/src/hover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ m!(ab$0c);
*abc*
```rust
test::module
test
```
```rust
Expand All @@ -298,11 +298,11 @@ m!(ab$0c);
---
Inner
Outer
---
```rust
test
test::module
```
```rust
Expand All @@ -311,7 +311,7 @@ m!(ab$0c);
---
Outer
Inner
"#]],
);
}
Expand Down
8 changes: 4 additions & 4 deletions crates/ide/src/references.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1701,14 +1701,14 @@ fn f() {
}
"#,
expect![[r#"
func Function FileId(0) 137..146 140..144
func Function FileId(0) 137..146 140..144 module
FileId(0) 161..165
FileId(0) 181..185
func Function FileId(0) 137..146 140..144 module
func Function FileId(0) 137..146 140..144
FileId(0) 181..185
FileId(0) 161..165
"#]],
)
}
Expand Down
25 changes: 6 additions & 19 deletions crates/ide/src/syntax_highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod tests;
use std::ops::ControlFlow;

use hir::{InRealFile, Name, Semantics};
use ide_db::{FxHashMap, RootDatabase, SymbolKind};
use ide_db::{FxHashMap, Ranker, RootDatabase, SymbolKind};
use span::EditionedFileId;
use syntax::{
ast::{self, IsString},
Expand Down Expand Up @@ -397,13 +397,12 @@ fn traverse(
Some(AttrOrDerive::Derive(_)) => inside_attribute,
None => false,
};

let descended_element = if in_macro {
// Attempt to descend tokens into macro-calls.
let res = match element {
NodeOrToken::Token(token) if token.kind() != COMMENT => {
let kind = token.kind();
let text = token.text();
let ident_kind = kind.is_any_identifier();
let ranker = Ranker::from_token(&token);

let mut t = None;
let mut r = 0;
Expand All @@ -412,21 +411,9 @@ fn traverse(
|tok, _ctx| {
// FIXME: Consider checking ctx transparency for being opaque?
let tok = tok.value;
let tok_kind = tok.kind();

let exact_same_kind = tok_kind == kind;
let both_idents =
exact_same_kind || (tok_kind.is_any_identifier() && ident_kind);
let same_text = tok.text() == text;
// anything that mapped into a token tree has likely no semantic information
let no_tt_parent =
tok.parent().map_or(false, |it| it.kind() != TOKEN_TREE);
let my_rank = (both_idents as usize)
| ((exact_same_kind as usize) << 1)
| ((same_text as usize) << 2)
| ((no_tt_parent as usize) << 3);

if my_rank > 0b1110 {
let my_rank = ranker.rank_token(&tok);

if my_rank >= Ranker::MAX_RANK {
// a rank of 0b1110 means that we have found a maximally interesting
// token so stop early.
t = Some(tok);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@
<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="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>
1 change: 0 additions & 1 deletion rust-bors.toml

This file was deleted.

0 comments on commit 715b67c

Please sign in to comment.