Skip to content

Commit c069f1b

Browse files
committed
Prefer similar tokens when expanding macros speculatively
1 parent d235373 commit c069f1b

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

+10-2
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)