From 882fe0a47ee6f60928395326d1f194eec521ce2e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 26 Nov 2019 21:18:26 +0300 Subject: More precise NameKind::Self --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_ide_api/src/goto_definition.rs | 23 +++--- crates/ra_ide_api/src/hover.rs | 95 ++++++++++------------ crates/ra_ide_api/src/references.rs | 5 +- crates/ra_ide_api/src/references/classify.rs | 3 +- .../ra_ide_api/src/references/name_definition.rs | 6 +- 6 files changed, 63 insertions(+), 71 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index c5cf39ee1..0edcdb177 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -982,7 +982,7 @@ impl ImplBlock { } } -#[derive(Clone, PartialEq, Eq)] +#[derive(Clone, PartialEq, Eq, Debug)] pub struct Type { pub(crate) krate: CrateId, pub(crate) ty: InEnvironment, diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index b6c72efdf..c10a6c844 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -71,10 +71,11 @@ pub(crate) fn reference_definition( Some(nav) => return Exact(nav), None => return Approximate(vec![]), }, - Some(SelfType(ty)) => { - if let Some((adt, _)) = ty.as_adt() { - return Exact(adt.to_nav(db)); - } + Some(SelfType(imp)) => { + // FIXME: ideally, this should point to the type in the impl, and + // not at the whole impl. And goto **type** definition should bring + // us to the actual type + return Exact(imp.to_nav(db)); } Some(Local(local)) => return Exact(local.to_nav(db)), Some(GenericParam(_)) => { @@ -503,7 +504,7 @@ mod tests { } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [12; 73)", ); check_goto( @@ -516,7 +517,7 @@ mod tests { } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [12; 73)", ); check_goto( @@ -529,7 +530,7 @@ mod tests { } } ", - "Foo ENUM_DEF FileId(1) [0; 14) [5; 8)", + "impl IMPL_BLOCK FileId(1) [15; 75)", ); check_goto( @@ -541,7 +542,7 @@ mod tests { } } ", - "Foo ENUM_DEF FileId(1) [0; 14) [5; 8)", + "impl IMPL_BLOCK FileId(1) [15; 62)", ); } @@ -560,7 +561,7 @@ mod tests { } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [49; 115)", ); check_goto( @@ -572,11 +573,11 @@ mod tests { } impl Make for Foo { fn new() -> Self<|> { - Self{} + Self {} } } ", - "Foo STRUCT_DEF FileId(1) [0; 11) [7; 10)", + "impl IMPL_BLOCK FileId(1) [49; 115)", ); } diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index 9839be985..260a7b869 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -133,20 +133,12 @@ fn hover_text_from_name_kind( hir::ModuleDef::TypeAlias(it) => from_def_source(db, it), hir::ModuleDef::BuiltinType(it) => Some(it.to_string()), }, - SelfType(ty) => match ty.as_adt() { - Some((adt_def, _)) => match adt_def { - hir::Adt::Struct(it) => from_def_source(db, it), - hir::Adt::Union(it) => from_def_source(db, it), - hir::Adt::Enum(it) => from_def_source(db, it), - }, - _ => None, - }, Local(_) => { // Hover for these shows type names *no_fallback = true; None } - GenericParam(_) => { + GenericParam(_) | SelfType(_) => { // FIXME: Hover for generic param None } @@ -622,49 +614,52 @@ fn func(foo: i32) { if true { <|>foo; }; } ", ); let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("struct Thing")); - assert_eq!(hover.info.is_exact(), true); - - let (analysis, position) = single_file_with_position( - " - struct Thing { x: u32 } - impl Thing { - fn new() -> Self<|> { - Self { x: 0 } - } - } - ", - ); - let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("struct Thing")); - assert_eq!(hover.info.is_exact(), true); - - let (analysis, position) = single_file_with_position( - " - enum Thing { A } - impl Thing { - pub fn new() -> Self<|> { - Thing::A - } - } - ", - ); - let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); + assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing")); assert_eq!(hover.info.is_exact(), true); - let (analysis, position) = single_file_with_position( - " - enum Thing { A } - impl Thing { - pub fn thing(a: Self<|>) { - } - } - ", - ); - let hover = analysis.hover(position).unwrap().unwrap(); - assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); - assert_eq!(hover.info.is_exact(), true); + /* FIXME: revive these tests + let (analysis, position) = single_file_with_position( + " + struct Thing { x: u32 } + impl Thing { + fn new() -> Self<|> { + Self { x: 0 } + } + } + ", + ); + + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("Thing")); + assert_eq!(hover.info.is_exact(), true); + + let (analysis, position) = single_file_with_position( + " + enum Thing { A } + impl Thing { + pub fn new() -> Self<|> { + Thing::A + } + } + ", + ); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); + assert_eq!(hover.info.is_exact(), true); + + let (analysis, position) = single_file_with_position( + " + enum Thing { A } + impl Thing { + pub fn thing(a: Self<|>) { + } + } + ", + ); + let hover = analysis.hover(position).unwrap().unwrap(); + assert_eq!(trim_markup_opt(hover.info.first()), Some("enum Thing")); + assert_eq!(hover.info.is_exact(), true); + */ } #[test] diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index cb343e59a..21a1ea69e 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -83,10 +83,7 @@ pub(crate) fn find_all_refs( NameKind::Field(field) => field.to_nav(db), NameKind::AssocItem(assoc) => assoc.to_nav(db), NameKind::Def(def) => NavigationTarget::from_def(db, def)?, - NameKind::SelfType(ref ty) => match ty.as_adt() { - Some((adt, _)) => adt.to_nav(db), - None => return None, - }, + NameKind::SelfType(imp) => imp.to_nav(db), NameKind::Local(local) => local.to_nav(db), NameKind::GenericParam(_) => return None, }; diff --git a/crates/ra_ide_api/src/references/classify.rs b/crates/ra_ide_api/src/references/classify.rs index 227737ad2..5cea805ec 100644 --- a/crates/ra_ide_api/src/references/classify.rs +++ b/crates/ra_ide_api/src/references/classify.rs @@ -178,8 +178,7 @@ pub(crate) fn classify_name_ref( Some(NameDefinition { kind, container, visibility }) } PathResolution::SelfType(impl_block) => { - let ty = impl_block.target_ty(db); - let kind = NameKind::SelfType(ty); + let kind = NameKind::SelfType(impl_block); let container = impl_block.module(db); Some(NameDefinition { kind, container, visibility }) } diff --git a/crates/ra_ide_api/src/references/name_definition.rs b/crates/ra_ide_api/src/references/name_definition.rs index cf12db066..10d3a2364 100644 --- a/crates/ra_ide_api/src/references/name_definition.rs +++ b/crates/ra_ide_api/src/references/name_definition.rs @@ -4,8 +4,8 @@ //! Note that the reference search is possible for not all of the classified items. use hir::{ - Adt, AssocItem, GenericParam, HasSource, Local, MacroDef, Module, ModuleDef, StructField, Ty, - VariantDef, + Adt, AssocItem, GenericParam, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, + StructField, VariantDef, }; use ra_syntax::{ast, ast::VisibilityOwner}; @@ -17,7 +17,7 @@ pub enum NameKind { Field(StructField), AssocItem(AssocItem), Def(ModuleDef), - SelfType(Ty), + SelfType(ImplBlock), Local(Local), GenericParam(GenericParam), } -- cgit v1.2.3