mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Merge #942
942: Hover for associated items in patterns r=matklad a=kjeremy Co-authored-by: kjeremy <kjeremy@gmail.com>
This commit is contained in:
commit
b94e1eee83
2 changed files with 46 additions and 4 deletions
|
@ -100,6 +100,7 @@ pub(crate) fn reference_definition(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Try name resolution
|
||||
let resolver = hir::source_binder::resolver_for_node(db, file_id, name_ref.syntax());
|
||||
if let Some(path) =
|
||||
|
@ -126,14 +127,14 @@ pub(crate) fn reference_definition(
|
|||
None => {
|
||||
// If we failed to resolve then check associated items
|
||||
if let Some(function) = function {
|
||||
// Should we do this above and then grab path from the PathExpr?
|
||||
// Resolve associated item for path expressions
|
||||
if let Some(path_expr) =
|
||||
name_ref.syntax().ancestors().find_map(ast::PathExpr::cast)
|
||||
{
|
||||
let infer_result = function.infer(db);
|
||||
let source_map = function.body_source_map(db);
|
||||
let expr = ast::Expr::cast(path_expr.syntax()).unwrap();
|
||||
|
||||
if let Some(expr) = ast::Expr::cast(path_expr.syntax()) {
|
||||
if let Some(res) = source_map
|
||||
.node_expr(expr)
|
||||
.and_then(|it| infer_result.assoc_resolutions_for_expr(it.into()))
|
||||
|
@ -142,6 +143,24 @@ pub(crate) fn reference_definition(
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Resolve associated item for path patterns
|
||||
if let Some(path_pat) =
|
||||
name_ref.syntax().ancestors().find_map(ast::PathPat::cast)
|
||||
{
|
||||
let infer_result = function.infer(db);
|
||||
let source_map = function.body_source_map(db);
|
||||
|
||||
let pat: &ast::Pat = path_pat.into();
|
||||
|
||||
if let Some(res) = source_map
|
||||
.node_pat(pat)
|
||||
.and_then(|it| infer_result.assoc_resolutions_for_pat(it.into()))
|
||||
{
|
||||
return Exact(NavigationTarget::from_impl_item(db, res));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -534,4 +534,27 @@ mod tests {
|
|||
assert_eq!(trim_markup_opt(hover.info.first()), Some("fn new() -> Thing"));
|
||||
assert_eq!(hover.info.is_exact(), true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hover_infer_associated_const_in_pattern() {
|
||||
let (analysis, position) = single_file_with_position(
|
||||
"
|
||||
struct X;
|
||||
impl X {
|
||||
const C: u32 = 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match 1 {
|
||||
X::C<|> => {},
|
||||
2 => {},
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
",
|
||||
);
|
||||
let hover = analysis.hover(position).unwrap().unwrap();
|
||||
assert_eq!(trim_markup_opt(hover.info.first()), Some("const C: u32"));
|
||||
assert_eq!(hover.info.is_exact(), true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue