From c5868a48795c899d43fde773091f0b133bee0a59 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 17:33:32 +0200 Subject: Clarify the names one more time --- crates/ide/src/doc_links.rs | 4 ++-- crates/ide/src/goto_definition.rs | 4 ++-- crates/ide/src/hover.rs | 4 ++-- crates/ide/src/references.rs | 4 ++-- crates/ide_db/src/defs.rs | 23 ++++++++++++++++------- crates/ide_db/src/imports_locator.rs | 2 +- crates/ide_db/src/search.rs | 2 +- 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/crates/ide/src/doc_links.rs b/crates/ide/src/doc_links.rs index 592a8e3ad..d9dc63b33 100644 --- a/crates/ide/src/doc_links.rs +++ b/crates/ide/src/doc_links.rs @@ -232,8 +232,8 @@ pub(crate) fn external_docs( let node = token.parent(); let definition = match_ast! { match node { - ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.definition(sema.db)), - ast::Name(name) => NameClass::classify(&sema, &name).map(|d| d.reference_or_definition(sema.db)), + ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), + ast::Name(name) => NameClass::classify(&sema, &name).map(|d| d.referenced_or_defined(sema.db)), _ => None, } }; diff --git a/crates/ide/src/goto_definition.rs b/crates/ide/src/goto_definition.rs index db6ad689e..a87e31019 100644 --- a/crates/ide/src/goto_definition.rs +++ b/crates/ide/src/goto_definition.rs @@ -40,7 +40,7 @@ pub(crate) fn goto_definition( reference_definition(&sema, &name_ref).to_vec() }, ast::Name(name) => { - let def = NameClass::classify(&sema, &name)?.reference_or_definition(sema.db); + let def = NameClass::classify(&sema, &name)?.referenced_or_defined(sema.db); let nav = def.try_to_nav(sema.db)?; vec![nav] }, @@ -83,7 +83,7 @@ pub(crate) fn reference_definition( ) -> ReferenceResult { let name_kind = NameRefClass::classify(sema, name_ref); if let Some(def) = name_kind { - let def = def.definition(sema.db); + let def = def.referenced(sema.db); return match def.try_to_nav(sema.db) { Some(nav) => ReferenceResult::Exact(nav), None => ReferenceResult::Approximate(Vec::new()), diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index e915a753e..845333e2a 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -107,8 +107,8 @@ pub(crate) fn hover( let node = token.parent(); let definition = match_ast! { match node { - ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.definition(sema.db)), - ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.definition(sema.db)), + ast::Name(name) => NameClass::classify(&sema, &name).and_then(|d| d.defined(sema.db)), + ast::NameRef(name_ref) => NameRefClass::classify(&sema, &name_ref).map(|d| d.referenced(sema.db)), _ => None, } }; diff --git a/crates/ide/src/references.rs b/crates/ide/src/references.rs index fc537b9c0..67ec257a8 100644 --- a/crates/ide/src/references.rs +++ b/crates/ide/src/references.rs @@ -132,13 +132,13 @@ fn find_name( opt_name: Option, ) -> Option> { if let Some(name) = opt_name { - let def = NameClass::classify(sema, &name)?.reference_or_definition(sema.db); + let def = NameClass::classify(sema, &name)?.referenced_or_defined(sema.db); let range = name.syntax().text_range(); return Some(RangeInfo::new(range, def)); } let name_ref = sema.find_node_at_offset_with_descend::(&syntax, position.offset)?; - let def = NameRefClass::classify(sema, &name_ref)?.definition(sema.db); + let def = NameRefClass::classify(sema, &name_ref)?.referenced(sema.db); let range = name_ref.syntax().text_range(); Some(RangeInfo::new(range, def)) } diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index 3cef0baf7..201a3d6fa 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -91,7 +91,8 @@ pub enum NameClass { } impl NameClass { - pub fn definition(self, db: &dyn HirDatabase) -> Option { + /// `Definition` defined by this name. + pub fn defined(self, db: &dyn HirDatabase) -> Option { let res = match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) => it, @@ -103,7 +104,8 @@ impl NameClass { Some(res) } - pub fn reference_or_definition(self, db: &dyn HirDatabase) -> Definition { + /// `Definition` referenced or defined by this name. + pub fn referenced_or_defined(self, db: &dyn HirDatabase) -> Definition { match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) | NameClass::ConstReference(it) => it, @@ -150,7 +152,7 @@ impl NameClass { }) .and_then(|name_ref| NameRefClass::classify(sema, &name_ref))?; - Some(NameClass::Definition(name_ref_class.definition(sema.db))) + Some(NameClass::Definition(name_ref_class.referenced(sema.db))) } else { let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?; let resolved = sema.resolve_extern_crate(&extern_crate)?; @@ -233,15 +235,20 @@ impl NameClass { pub enum NameRefClass { ExternCrate(Crate), Definition(Definition), - FieldShorthand { local: Local, field: Definition }, + FieldShorthand { local_ref: Local, field_ref: Definition }, } impl NameRefClass { - pub fn definition(self, db: &dyn HirDatabase) -> Definition { + /// `Definition`, which this name refers to. + pub fn referenced(self, db: &dyn HirDatabase) -> Definition { match self { NameRefClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameRefClass::Definition(def) => def, - NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local), + NameRefClass::FieldShorthand { local_ref, field_ref: _ } => { + // FIXME: this is inherently ambiguous -- this name refers to + // two different defs.... + Definition::Local(local_ref) + } } } @@ -272,7 +279,9 @@ impl NameRefClass { let field = Definition::Field(field); let res = match local { None => NameRefClass::Definition(field), - Some(local) => NameRefClass::FieldShorthand { field, local }, + Some(local) => { + NameRefClass::FieldShorthand { field_ref: field, local_ref: local } + } }; return Some(res); } diff --git a/crates/ide_db/src/imports_locator.rs b/crates/ide_db/src/imports_locator.rs index 8c925fbdc..df74be00b 100644 --- a/crates/ide_db/src/imports_locator.rs +++ b/crates/ide_db/src/imports_locator.rs @@ -60,5 +60,5 @@ fn get_name_definition<'a>( candidate_node }; let name = ast::Name::cast(candidate_name_node)?; - NameClass::classify(sema, &name)?.definition(sema.db) + NameClass::classify(sema, &name)?.defined(sema.db) } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 3af52ec5e..a24335240 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -292,7 +292,7 @@ impl<'a> FindUsages<'a> { }; sink(reference) } - Some(NameRefClass::FieldShorthand { local, field }) => { + Some(NameRefClass::FieldShorthand { local_ref: local, field_ref: field }) => { let reference = match self.def { Definition::Field(_) if &field == self.def => Reference { file_range: self.sema.original_range(name_ref.syntax()), -- cgit v1.2.3