More precise NameKind::Self

This commit is contained in:
Aleksey Kladov 2019-11-26 21:18:26 +03:00
parent 141fca6006
commit 882fe0a47e
6 changed files with 61 additions and 69 deletions

View File

@ -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<Ty>,

View File

@ -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)",
);
}

View File

@ -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,9 +614,10 @@ 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!(trim_markup_opt(hover.info.first()), Some("Thing"));
assert_eq!(hover.info.is_exact(), true);
/* FIXME: revive these tests
let (analysis, position) = single_file_with_position(
"
struct Thing { x: u32 }
@ -635,8 +628,9 @@ 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!(trim_markup_opt(hover.info.first()), Some("Thing"));
assert_eq!(hover.info.is_exact(), true);
let (analysis, position) = single_file_with_position(
@ -665,6 +659,7 @@ fn func(foo: i32) { if true { <|>foo; }; }
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]

View File

@ -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,
};

View File

@ -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 })
}

View File

@ -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),
}