Render matched macro arm on hover of macro calls

This commit is contained in:
Lukas Wirth 2023-12-08 18:24:24 +01:00
parent 062e1b9b81
commit 6bfdd38c69
11 changed files with 133 additions and 65 deletions

View file

@ -18,9 +18,9 @@ pub(crate) fn expand_rules(
new_meta_vars: bool,
call_site: Span,
def_site_edition: Edition,
) -> ExpandResult<tt::Subtree<Span>> {
let mut match_: Option<(matcher::Match, &crate::Rule)> = None;
for rule in rules {
) -> ExpandResult<(tt::Subtree<Span>, Option<u32>)> {
let mut match_: Option<(matcher::Match, &crate::Rule, usize)> = None;
for (idx, rule) in rules.iter().enumerate() {
let new_match = matcher::match_(&rule.lhs, input, def_site_edition);
if new_match.err.is_none() {
@ -35,31 +35,34 @@ pub(crate) fn expand_rules(
call_site,
);
if transcribe_err.is_none() {
return ExpandResult::ok(value);
return ExpandResult::ok((value, Some(idx as u32)));
}
}
// Use the rule if we matched more tokens, or bound variables count
if let Some((prev_match, _)) = &match_ {
if let Some((prev_match, _, _)) = &match_ {
if (new_match.unmatched_tts, -(new_match.bound_count as i32))
< (prev_match.unmatched_tts, -(prev_match.bound_count as i32))
{
match_ = Some((new_match, rule));
match_ = Some((new_match, rule, idx));
}
} else {
match_ = Some((new_match, rule));
match_ = Some((new_match, rule, idx));
}
}
if let Some((match_, rule)) = match_ {
if let Some((match_, rule, idx)) = match_ {
// if we got here, there was no match without errors
let ExpandResult { value, err: transcribe_err } =
transcriber::transcribe(&rule.rhs, &match_.bindings, marker, new_meta_vars, call_site);
ExpandResult { value, err: match_.err.or(transcribe_err) }
ExpandResult { value: (value, Some(idx as u32)), err: match_.err.or(transcribe_err) }
} else {
ExpandResult::new(
tt::Subtree {
delimiter: tt::Delimiter::invisible_spanned(call_site),
token_trees: Box::new([]),
},
(
tt::Subtree {
delimiter: tt::Delimiter::invisible_spanned(call_site),
token_trees: Box::default(),
},
None,
),
ExpandError::NoMatchingRule,
)
}