Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 99c205c

Browse files
committed
Auto merge of rust-lang#13392 - Veykril:spec-pref, r=Veykril
Prefer similar tokens when expanding macros speculatively Should improve completions in proc-macros in some cases
2 parents 43c7f3c + c069f1b commit 99c205c

File tree

1 file changed

+10
-2
lines changed
  • crates/hir-expand/src

1 file changed

+10
-2
lines changed

crates/hir-expand/src/db.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,16 @@ pub fn expand_speculative(
221221
fixup::reverse_fixups(&mut speculative_expansion.value, &spec_args_tmap, &fixups.undo_info);
222222
let (node, rev_tmap) = token_tree_to_syntax_node(&speculative_expansion.value, expand_to);
223223

224-
let range = rev_tmap.first_range_by_token(token_id, token_to_map.kind())?;
225-
let token = node.syntax_node().covering_element(range).into_token()?;
224+
let syntax_node = node.syntax_node();
225+
let token = rev_tmap
226+
.ranges_by_token(token_id, token_to_map.kind())
227+
.filter_map(|range| syntax_node.covering_element(range).into_token())
228+
.min_by_key(|t| {
229+
// prefer tokens of the same kind and text
230+
// Note the inversion of the score here, as we want to prefer the first token in case
231+
// of all tokens having the same score
232+
(t.kind() != token_to_map.kind()) as u8 + (t.text() != token_to_map.text()) as u8
233+
})?;
226234
Some((node.syntax_node(), token))
227235
}
228236

0 commit comments

Comments
 (0)