From fa3c449d8f5a67865cab8d4717b3e32dca5b672a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 16:15:01 +0200 Subject: Clarify NameClass names a bit --- crates/ide_db/src/defs.rs | 4 ++-- crates/ide_db/src/imports_locator.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index f8c7aa491..a243dcd15 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -90,7 +90,7 @@ pub enum NameClass { } impl NameClass { - pub fn into_definition(self, db: &dyn HirDatabase) -> Option { + pub fn definition(self, db: &dyn HirDatabase) -> Option { Some(match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) => it, @@ -99,7 +99,7 @@ impl NameClass { }) } - pub fn definition(self, db: &dyn HirDatabase) -> Definition { + pub fn definition_or_reference(self, db: &dyn HirDatabase) -> Definition { match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) | NameClass::ConstReference(it) => it, diff --git a/crates/ide_db/src/imports_locator.rs b/crates/ide_db/src/imports_locator.rs index ed67e3553..e0d3e7293 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)?; - classify_name(sema, &name)?.into_definition(sema.db) + classify_name(sema, &name)?.definition(sema.db) } -- cgit v1.2.3 From bc287b8f9b7711a38d97be3b619758bb05d54c45 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 16:23:55 +0200 Subject: Unconfuse expression and pattern field init shorthands --- crates/ide_db/src/defs.rs | 16 ++++++++++------ crates/ide_db/src/search.rs | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index a243dcd15..a2682d73c 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -83,7 +83,8 @@ pub enum NameClass { Definition(Definition), /// `None` in `if let None = Some(82) {}` ConstReference(Definition), - FieldShorthand { + /// `field` in `if let Foo { field } = todo!() {}` + PatFieldShorthand { local: Local, field: Definition, }, @@ -91,19 +92,22 @@ pub enum NameClass { impl NameClass { pub fn definition(self, db: &dyn HirDatabase) -> Option { - Some(match self { + let res = match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) => it, NameClass::ConstReference(_) => return None, - NameClass::FieldShorthand { local, field: _ } => Definition::Local(local), - }) + /// Both `local` and `field` are definitions here, but only `local` + /// is the definition which is introduced by this name. + NameClass::PatFieldShorthand { local, field: _ } => Definition::Local(local), + }; + Some(res) } pub fn definition_or_reference(self, db: &dyn HirDatabase) -> Definition { match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) | NameClass::ConstReference(it) => it, - NameClass::FieldShorthand { local: _, field } => field, + NameClass::PatFieldShorthand { local: _, field } => field, } } } @@ -161,7 +165,7 @@ pub fn classify_name(sema: &Semantics, name: &ast::Name) -> Option if record_pat_field.name_ref().is_none() { if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { let field = Definition::Field(field); - return Some(NameClass::FieldShorthand { local, field }); + return Some(NameClass::PatFieldShorthand { local, field }); } } } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 8e3dcd99c..43b8560ca 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -314,7 +314,7 @@ impl<'a> FindUsages<'a> { fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { match classify_name(self.sema, name) { - Some(NameClass::FieldShorthand { local: _, field }) => { + Some(NameClass::PatFieldShorthand { local: _, field }) => { let reference = match self.def { Definition::Field(_) if &field == self.def => Reference { file_range: self.sema.original_range(name.syntax()), -- cgit v1.2.3 From f9c1336873d65805ad34129939d800dbf59daf61 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 17:18:07 +0200 Subject: More clarifications --- crates/ide_db/src/defs.rs | 20 ++++++++++---------- crates/ide_db/src/search.rs | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index a2682d73c..717581593 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -81,12 +81,12 @@ impl Definition { pub enum NameClass { ExternCrate(Crate), Definition(Definition), - /// `None` in `if let None = Some(82) {}` + /// `None` in `if let None = Some(82) {}`. ConstReference(Definition), - /// `field` in `if let Foo { field } = todo!() {}` + /// `field` in `if let Foo { field } = foo`. PatFieldShorthand { - local: Local, - field: Definition, + local_def: Local, + field_ref: Definition, }, } @@ -96,18 +96,18 @@ impl NameClass { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) => it, NameClass::ConstReference(_) => return None, - /// Both `local` and `field` are definitions here, but only `local` - /// is the definition which is introduced by this name. - NameClass::PatFieldShorthand { local, field: _ } => Definition::Local(local), + NameClass::PatFieldShorthand { local_def, field_ref: _ } => { + Definition::Local(local_def) + } }; Some(res) } - pub fn definition_or_reference(self, db: &dyn HirDatabase) -> Definition { + pub fn reference_or_definition(self, db: &dyn HirDatabase) -> Definition { match self { NameClass::ExternCrate(krate) => Definition::ModuleDef(krate.root_module(db).into()), NameClass::Definition(it) | NameClass::ConstReference(it) => it, - NameClass::PatFieldShorthand { local: _, field } => field, + NameClass::PatFieldShorthand { local_def: _, field_ref } => field_ref, } } } @@ -165,7 +165,7 @@ pub fn classify_name(sema: &Semantics, name: &ast::Name) -> Option if record_pat_field.name_ref().is_none() { if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { let field = Definition::Field(field); - return Some(NameClass::PatFieldShorthand { local, field }); + return Some(NameClass::PatFieldShorthand { local_def: local, field_ref: field }); } } } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 43b8560ca..8048aa621 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -314,9 +314,9 @@ impl<'a> FindUsages<'a> { fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { match classify_name(self.sema, name) { - Some(NameClass::PatFieldShorthand { local: _, field }) => { + Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { let reference = match self.def { - Definition::Field(_) if &field == self.def => Reference { + Definition::Field(_) if &field_ref == self.def => Reference { file_range: self.sema.original_range(name.syntax()), kind: ReferenceKind::FieldShorthandForField, // FIXME: mutable patterns should have `Write` access -- cgit v1.2.3 From 56e67e3a392d7c7c7f30ca39d8e83446ea8bbec3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 15 Oct 2020 17:27:50 +0200 Subject: More idiomatic classification API --- crates/ide_db/src/defs.rs | 338 +++++++++++++++++------------------ crates/ide_db/src/imports_locator.rs | 4 +- crates/ide_db/src/search.rs | 6 +- 3 files changed, 174 insertions(+), 174 deletions(-) (limited to 'crates/ide_db') diff --git a/crates/ide_db/src/defs.rs b/crates/ide_db/src/defs.rs index 717581593..3cef0baf7 100644 --- a/crates/ide_db/src/defs.rs +++ b/crates/ide_db/src/defs.rs @@ -110,121 +110,121 @@ impl NameClass { NameClass::PatFieldShorthand { local_def: _, field_ref } => field_ref, } } -} -pub fn classify_name(sema: &Semantics, name: &ast::Name) -> Option { - let _p = profile::span("classify_name"); + pub fn classify(sema: &Semantics, name: &ast::Name) -> Option { + let _p = profile::span("classify_name"); - let parent = name.syntax().parent()?; + let parent = name.syntax().parent()?; - if let Some(bind_pat) = ast::IdentPat::cast(parent.clone()) { - if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) { - return Some(NameClass::ConstReference(Definition::ModuleDef(def))); + if let Some(bind_pat) = ast::IdentPat::cast(parent.clone()) { + if let Some(def) = sema.resolve_bind_pat_to_const(&bind_pat) { + return Some(NameClass::ConstReference(Definition::ModuleDef(def))); + } } - } - match_ast! { - match parent { - ast::Rename(it) => { - if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) { - let path = use_tree.path()?; - let path_segment = path.segment()?; - let name_ref_class = path_segment - .name_ref() - // The rename might be from a `self` token, so fallback to the name higher - // in the use tree. - .or_else(||{ - if path_segment.self_token().is_none() { - return None; - } + match_ast! { + match parent { + ast::Rename(it) => { + if let Some(use_tree) = it.syntax().parent().and_then(ast::UseTree::cast) { + let path = use_tree.path()?; + let path_segment = path.segment()?; + let name_ref_class = path_segment + .name_ref() + // The rename might be from a `self` token, so fallback to the name higher + // in the use tree. + .or_else(||{ + if path_segment.self_token().is_none() { + return None; + } - let use_tree = use_tree - .syntax() - .parent() - .as_ref() - // Skip over UseTreeList - .and_then(SyntaxNode::parent) - .and_then(ast::UseTree::cast)?; - let path = use_tree.path()?; - let path_segment = path.segment()?; - path_segment.name_ref() - }) - .and_then(|name_ref| classify_name_ref(sema, &name_ref))?; + let use_tree = use_tree + .syntax() + .parent() + .as_ref() + // Skip over UseTreeList + .and_then(SyntaxNode::parent) + .and_then(ast::UseTree::cast)?; + let path = use_tree.path()?; + let path_segment = path.segment()?; + path_segment.name_ref() + }) + .and_then(|name_ref| NameRefClass::classify(sema, &name_ref))?; - Some(NameClass::Definition(name_ref_class.definition(sema.db))) - } else { - let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?; - let resolved = sema.resolve_extern_crate(&extern_crate)?; - Some(NameClass::ExternCrate(resolved)) - } - }, - ast::IdentPat(it) => { - let local = sema.to_def(&it)?; + Some(NameClass::Definition(name_ref_class.definition(sema.db))) + } else { + let extern_crate = it.syntax().parent().and_then(ast::ExternCrate::cast)?; + let resolved = sema.resolve_extern_crate(&extern_crate)?; + Some(NameClass::ExternCrate(resolved)) + } + }, + ast::IdentPat(it) => { + let local = sema.to_def(&it)?; - if let Some(record_pat_field) = it.syntax().parent().and_then(ast::RecordPatField::cast) { - if record_pat_field.name_ref().is_none() { - if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { - let field = Definition::Field(field); - return Some(NameClass::PatFieldShorthand { local_def: local, field_ref: field }); + if let Some(record_pat_field) = it.syntax().parent().and_then(ast::RecordPatField::cast) { + if record_pat_field.name_ref().is_none() { + if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { + let field = Definition::Field(field); + return Some(NameClass::PatFieldShorthand { local_def: local, field_ref: field }); + } } } - } - Some(NameClass::Definition(Definition::Local(local))) - }, - ast::RecordField(it) => { - let field: hir::Field = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::Field(field))) - }, - ast::Module(it) => { - let def = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Struct(it) => { - let def: hir::Struct = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Union(it) => { - let def: hir::Union = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Enum(it) => { - let def: hir::Enum = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Trait(it) => { - let def: hir::Trait = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Static(it) => { - let def: hir::Static = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Variant(it) => { - let def: hir::EnumVariant = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Fn(it) => { - let def: hir::Function = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::Const(it) => { - let def: hir::Const = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::TypeAlias(it) => { - let def: hir::TypeAlias = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::ModuleDef(def.into()))) - }, - ast::MacroCall(it) => { - let def = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::Macro(def))) - }, - ast::TypeParam(it) => { - let def = sema.to_def(&it)?; - Some(NameClass::Definition(Definition::TypeParam(def))) - }, - _ => None, + Some(NameClass::Definition(Definition::Local(local))) + }, + ast::RecordField(it) => { + let field: hir::Field = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::Field(field))) + }, + ast::Module(it) => { + let def = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Struct(it) => { + let def: hir::Struct = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Union(it) => { + let def: hir::Union = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Enum(it) => { + let def: hir::Enum = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Trait(it) => { + let def: hir::Trait = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Static(it) => { + let def: hir::Static = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Variant(it) => { + let def: hir::EnumVariant = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Fn(it) => { + let def: hir::Function = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::Const(it) => { + let def: hir::Const = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::TypeAlias(it) => { + let def: hir::TypeAlias = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::ModuleDef(def.into()))) + }, + ast::MacroCall(it) => { + let def = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::Macro(def))) + }, + ast::TypeParam(it) => { + let def = sema.to_def(&it)?; + Some(NameClass::Definition(Definition::TypeParam(def))) + }, + _ => None, + } } } } @@ -244,91 +244,91 @@ impl NameRefClass { NameRefClass::FieldShorthand { local, field: _ } => Definition::Local(local), } } -} -// Note: we don't have unit-tests for this rather important function. -// It is primarily exercised via goto definition tests in `ide`. -pub fn classify_name_ref( - sema: &Semantics, - name_ref: &ast::NameRef, -) -> Option { - let _p = profile::span("classify_name_ref"); + // Note: we don't have unit-tests for this rather important function. + // It is primarily exercised via goto definition tests in `ide`. + pub fn classify( + sema: &Semantics, + name_ref: &ast::NameRef, + ) -> Option { + let _p = profile::span("classify_name_ref"); - let parent = name_ref.syntax().parent()?; + let parent = name_ref.syntax().parent()?; - if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { - if let Some(func) = sema.resolve_method_call(&method_call) { - return Some(NameRefClass::Definition(Definition::ModuleDef(func.into()))); + if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { + if let Some(func) = sema.resolve_method_call(&method_call) { + return Some(NameRefClass::Definition(Definition::ModuleDef(func.into()))); + } } - } - if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { - if let Some(field) = sema.resolve_field(&field_expr) { - return Some(NameRefClass::Definition(Definition::Field(field))); + if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { + if let Some(field) = sema.resolve_field(&field_expr) { + return Some(NameRefClass::Definition(Definition::Field(field))); + } } - } - if let Some(record_field) = ast::RecordExprField::for_field_name(name_ref) { - if let Some((field, local)) = sema.resolve_record_field(&record_field) { - let field = Definition::Field(field); - let res = match local { - None => NameRefClass::Definition(field), - Some(local) => NameRefClass::FieldShorthand { field, local }, - }; - return Some(res); + if let Some(record_field) = ast::RecordExprField::for_field_name(name_ref) { + if let Some((field, local)) = sema.resolve_record_field(&record_field) { + let field = Definition::Field(field); + let res = match local { + None => NameRefClass::Definition(field), + Some(local) => NameRefClass::FieldShorthand { field, local }, + }; + return Some(res); + } } - } - if let Some(record_pat_field) = ast::RecordPatField::cast(parent.clone()) { - if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { - let field = Definition::Field(field); - return Some(NameRefClass::Definition(field)); + if let Some(record_pat_field) = ast::RecordPatField::cast(parent.clone()) { + if let Some(field) = sema.resolve_record_pat_field(&record_pat_field) { + let field = Definition::Field(field); + return Some(NameRefClass::Definition(field)); + } } - } - if ast::AssocTypeArg::cast(parent.clone()).is_some() { - // `Trait` - // ^^^^^ - let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; - let resolved = sema.resolve_path(&path)?; - if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved { - if let Some(ty) = tr - .items(sema.db) - .iter() - .filter_map(|assoc| match assoc { - hir::AssocItem::TypeAlias(it) => Some(*it), - _ => None, - }) - .find(|alias| alias.name(sema.db).to_string() == **name_ref.text()) - { - return Some(NameRefClass::Definition(Definition::ModuleDef( - ModuleDef::TypeAlias(ty), - ))); + if ast::AssocTypeArg::cast(parent.clone()).is_some() { + // `Trait` + // ^^^^^ + let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?; + let resolved = sema.resolve_path(&path)?; + if let PathResolution::Def(ModuleDef::Trait(tr)) = resolved { + if let Some(ty) = tr + .items(sema.db) + .iter() + .filter_map(|assoc| match assoc { + hir::AssocItem::TypeAlias(it) => Some(*it), + _ => None, + }) + .find(|alias| alias.name(sema.db).to_string() == **name_ref.text()) + { + return Some(NameRefClass::Definition(Definition::ModuleDef( + ModuleDef::TypeAlias(ty), + ))); + } } } - } - if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { - if let Some(path) = macro_call.path() { - if path.qualifier().is_none() { - // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment - // paths are handled below (allowing `log<|>::info!` to resolve to the log crate). - if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { - return Some(NameRefClass::Definition(Definition::Macro(macro_def))); + if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { + if let Some(path) = macro_call.path() { + if path.qualifier().is_none() { + // Only use this to resolve single-segment macro calls like `foo!()`. Multi-segment + // paths are handled below (allowing `log<|>::info!` to resolve to the log crate). + if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { + return Some(NameRefClass::Definition(Definition::Macro(macro_def))); + } } } } - } - if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast) { - if let Some(resolved) = sema.resolve_path(&path) { - return Some(NameRefClass::Definition(resolved.into())); + if let Some(path) = name_ref.syntax().ancestors().find_map(ast::Path::cast) { + if let Some(resolved) = sema.resolve_path(&path) { + return Some(NameRefClass::Definition(resolved.into())); + } } - } - let extern_crate = ast::ExternCrate::cast(parent)?; - let resolved = sema.resolve_extern_crate(&extern_crate)?; - Some(NameRefClass::ExternCrate(resolved)) + let extern_crate = ast::ExternCrate::cast(parent)?; + let resolved = sema.resolve_extern_crate(&extern_crate)?; + Some(NameRefClass::ExternCrate(resolved)) + } } impl From for Definition { diff --git a/crates/ide_db/src/imports_locator.rs b/crates/ide_db/src/imports_locator.rs index e0d3e7293..8c925fbdc 100644 --- a/crates/ide_db/src/imports_locator.rs +++ b/crates/ide_db/src/imports_locator.rs @@ -5,7 +5,7 @@ use hir::{Crate, MacroDef, ModuleDef, Semantics}; use syntax::{ast, AstNode, SyntaxKind::NAME}; use crate::{ - defs::{classify_name, Definition}, + defs::{Definition, NameClass}, symbol_index::{self, FileSymbol, Query}, RootDatabase, }; @@ -60,5 +60,5 @@ fn get_name_definition<'a>( candidate_node }; let name = ast::Name::cast(candidate_name_node)?; - classify_name(sema, &name)?.definition(sema.db) + NameClass::classify(sema, &name)?.definition(sema.db) } diff --git a/crates/ide_db/src/search.rs b/crates/ide_db/src/search.rs index 8048aa621..3af52ec5e 100644 --- a/crates/ide_db/src/search.rs +++ b/crates/ide_db/src/search.rs @@ -14,7 +14,7 @@ use syntax::{ast, match_ast, AstNode, TextRange, TextSize}; use crate::defs::NameClass; use crate::{ - defs::{classify_name, classify_name_ref, Definition, NameRefClass}, + defs::{Definition, NameRefClass}, RootDatabase, }; @@ -276,7 +276,7 @@ impl<'a> FindUsages<'a> { name_ref: &ast::NameRef, sink: &mut dyn FnMut(Reference) -> bool, ) -> bool { - match classify_name_ref(self.sema, &name_ref) { + match NameRefClass::classify(self.sema, &name_ref) { Some(NameRefClass::Definition(def)) if &def == self.def => { let kind = if is_record_lit_name_ref(&name_ref) || is_call_expr_name_ref(&name_ref) { @@ -313,7 +313,7 @@ impl<'a> FindUsages<'a> { } fn found_name(&self, name: &ast::Name, sink: &mut dyn FnMut(Reference) -> bool) -> bool { - match classify_name(self.sema, name) { + match NameClass::classify(self.sema, name) { Some(NameClass::PatFieldShorthand { local_def: _, field_ref }) => { let reference = match self.def { Definition::Field(_) if &field_ref == self.def => Reference { -- cgit v1.2.3 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_db/src/defs.rs | 23 ++++++++++++++++------- crates/ide_db/src/imports_locator.rs | 2 +- crates/ide_db/src/search.rs | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'crates/ide_db') 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