From 816971ebc9207c5fb5779d448613dd171c27f398 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 21 Feb 2019 12:04:14 +0200 Subject: Implement basic support for Associated Methods and Constants This is done in `infer_path_expr`. When `Resolver::resolve_path` returns `PartiallyResolved`, we use the returned `Resolution` together with the given `segment_index` to check if we can find something matching the segment at segment_index in the impls for that particular type. --- crates/ra_ide_api/src/completion/complete_path.rs | 2 +- crates/ra_ide_api/src/goto_definition.rs | 2 +- crates/ra_ide_api/src/hover.rs | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index d337fe970..a0c5572d5 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -10,7 +10,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { Some(path) => path.clone(), _ => return, }; - let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() { + let def = match ctx.resolver.resolve_path(ctx.db, &path).into_per_ns().take_types() { Some(Resolution::Def(def)) => def, _ => return, }; diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 96ed8c8e9..76aaebd52 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -79,7 +79,7 @@ pub(crate) fn reference_definition( if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast).and_then(hir::Path::from_ast) { - let resolved = resolver.resolve_path(db, &path); + let resolved = resolver.resolve_path(db, &path).into_per_ns(); match resolved.clone().take_types().or_else(|| resolved.take_values()) { Some(Resolution::Def(def)) => return Exact(NavigationTarget::from_def(db, def)), Some(Resolution::LocalBinding(pat)) => { diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 0888ab6de..38671b394 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -223,4 +223,24 @@ mod tests { assert_eq!("usize", &type_name); } + #[test] + fn test_hover_infer_associated_method_result() { + let (analysis, position) = single_file_with_position( + " + struct Thing { x: u32 }; + + impl Thing { + fn new() -> Thing { + Thing { x: 0 } + } + } + + fn main() { + let foo_<|>test = Thing::new(); + } + ", + ); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(hover.info, "Thing"); + } } -- cgit v1.2.3 From 247d1c17b385ff8a8c1dda2e899495146b643b98 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Fri, 22 Feb 2019 10:15:23 +0200 Subject: Change resolve_path to return the fully resolved path or PerNs::none This also adds new pub(crate) resolve_path_segments which returns the PathResult, which may or may not be fully resolved. PathResult is also now pub(crate) since it is an implementation detail. --- crates/ra_ide_api/src/completion/complete_path.rs | 2 +- crates/ra_ide_api/src/goto_definition.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide_api') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index a0c5572d5..d337fe970 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -10,7 +10,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { Some(path) => path.clone(), _ => return, }; - let def = match ctx.resolver.resolve_path(ctx.db, &path).into_per_ns().take_types() { + let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() { Some(Resolution::Def(def)) => def, _ => return, }; diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 76aaebd52..96ed8c8e9 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -79,7 +79,7 @@ pub(crate) fn reference_definition( if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast).and_then(hir::Path::from_ast) { - let resolved = resolver.resolve_path(db, &path).into_per_ns(); + let resolved = resolver.resolve_path(db, &path); match resolved.clone().take_types().or_else(|| resolved.take_values()) { Some(Resolution::Def(def)) => return Exact(NavigationTarget::from_def(db, def)), Some(Resolution::LocalBinding(pat)) => { -- cgit v1.2.3