mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
Render matched macro arm on hover of macro calls
This commit is contained in:
parent
062e1b9b81
commit
6bfdd38c69
11 changed files with 133 additions and 65 deletions
|
@ -48,7 +48,7 @@ fn benchmark_expand_macro_rules() {
|
|||
.map(|(id, tt)| {
|
||||
let res = rules[&id].expand(&tt, |_| (), true, DUMMY, Edition::CURRENT);
|
||||
assert!(res.err.is_none());
|
||||
res.value.token_trees.len()
|
||||
res.value.0.token_trees.len()
|
||||
})
|
||||
.sum()
|
||||
};
|
||||
|
|
|
@ -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,
|
||||
)
|
||||
}
|
||||
|
|
|
@ -251,7 +251,7 @@ impl DeclarativeMacro {
|
|||
new_meta_vars: bool,
|
||||
call_site: Span,
|
||||
def_site_edition: Edition,
|
||||
) -> ExpandResult<tt::Subtree<Span>> {
|
||||
) -> ExpandResult<(tt::Subtree<Span>, Option<u32>)> {
|
||||
expander::expand_rules(&self.rules, tt, marker, new_meta_vars, call_site, def_site_edition)
|
||||
}
|
||||
}
|
||||
|
@ -330,6 +330,10 @@ impl<T, E> ValueResult<T, E> {
|
|||
Self { value: Default::default(), err: Some(err) }
|
||||
}
|
||||
|
||||
pub fn zip_val<U>(self, other: U) -> ValueResult<(T, U), E> {
|
||||
ValueResult { value: (self.value, other), err: self.err }
|
||||
}
|
||||
|
||||
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ValueResult<U, E> {
|
||||
ValueResult { value: f(self.value), err: self.err }
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue