From 271017e6bf5b5f46464d09db7fc869c92998fc80 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 16:23:28 +0100 Subject: Move NameKind up --- crates/ra_ide/src/references/classify.rs | 112 +----------------------- crates/ra_ide/src/references/name_definition.rs | 85 ------------------ crates/ra_ide/src/references/search_scope.rs | 102 +++++++++++---------- 3 files changed, 53 insertions(+), 246 deletions(-) delete mode 100644 crates/ra_ide/src/references/name_definition.rs (limited to 'crates/ra_ide/src/references') diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 758ea4e8b..0326fd379 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -2,119 +2,13 @@ use hir::{InFile, PathResolution, SourceBinder}; use ra_prof::profile; -use ra_syntax::{ast, match_ast, AstNode}; +use ra_syntax::{ast, AstNode}; use test_utils::tested_by; -use super::{ - name_definition::{from_assoc_item, from_module_def, from_struct_field}, - NameDefinition, NameKind, -}; +use super::{NameDefinition, NameKind}; use ra_ide_db::RootDatabase; -pub(crate) fn classify_name( - sb: &mut SourceBinder, - name: InFile<&ast::Name>, -) -> Option { - let _p = profile("classify_name"); - let parent = name.value.syntax().parent()?; - - match_ast! { - match parent { - ast::BindPat(it) => { - let src = name.with_value(it); - let local = sb.to_def(src)?; - Some(NameDefinition { - visibility: None, - container: local.module(sb.db), - kind: NameKind::Local(local), - }) - }, - ast::RecordFieldDef(it) => { - let src = name.with_value(it); - let field: hir::StructField = sb.to_def(src)?; - Some(from_struct_field(sb.db, field)) - }, - ast::Module(it) => { - let def = sb.to_def(name.with_value(it))?; - Some(from_module_def(sb.db, def.into(), None)) - }, - ast::StructDef(it) => { - let src = name.with_value(it); - let def: hir::Struct = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into(), None)) - }, - ast::EnumDef(it) => { - let src = name.with_value(it); - let def: hir::Enum = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into(), None)) - }, - ast::TraitDef(it) => { - let src = name.with_value(it); - let def: hir::Trait = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into(), None)) - }, - ast::StaticDef(it) => { - let src = name.with_value(it); - let def: hir::Static = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into(), None)) - }, - ast::EnumVariant(it) => { - let src = name.with_value(it); - let def: hir::EnumVariant = sb.to_def(src)?; - Some(from_module_def(sb.db, def.into(), None)) - }, - ast::FnDef(it) => { - let src = name.with_value(it); - let def: hir::Function = sb.to_def(src)?; - if parent.parent().and_then(ast::ItemList::cast).is_some() { - Some(from_assoc_item(sb.db, def.into())) - } else { - Some(from_module_def(sb.db, def.into(), None)) - } - }, - ast::ConstDef(it) => { - let src = name.with_value(it); - let def: hir::Const = sb.to_def(src)?; - if parent.parent().and_then(ast::ItemList::cast).is_some() { - Some(from_assoc_item(sb.db, def.into())) - } else { - Some(from_module_def(sb.db, def.into(), None)) - } - }, - ast::TypeAliasDef(it) => { - let src = name.with_value(it); - let def: hir::TypeAlias = sb.to_def(src)?; - if parent.parent().and_then(ast::ItemList::cast).is_some() { - Some(from_assoc_item(sb.db, def.into())) - } else { - Some(from_module_def(sb.db, def.into(), None)) - } - }, - ast::MacroCall(it) => { - let src = name.with_value(it); - let def = sb.to_def(src.clone())?; - - let module = sb.to_module_def(src.file_id.original_file(sb.db))?; - - Some(NameDefinition { - visibility: None, - container: module, - kind: NameKind::Macro(def), - }) - }, - ast::TypeParam(it) => { - let src = name.with_value(it); - let def = sb.to_def(src)?; - Some(NameDefinition { - visibility: None, - container: def.module(sb.db), - kind: NameKind::TypeParam(def), - }) - }, - _ => None, - } - } -} +pub use ra_ide_db::defs::{classify_name, from_assoc_item, from_module_def, from_struct_field}; pub(crate) fn classify_name_ref( sb: &mut SourceBinder, diff --git a/crates/ra_ide/src/references/name_definition.rs b/crates/ra_ide/src/references/name_definition.rs deleted file mode 100644 index 71565e6d3..000000000 --- a/crates/ra_ide/src/references/name_definition.rs +++ /dev/null @@ -1,85 +0,0 @@ -//! `NameDefinition` keeps information about the element we want to search references for. -//! The element is represented by `NameKind`. It's located inside some `container` and -//! has a `visibility`, which defines a search scope. -//! Note that the reference search is possible for not all of the classified items. - -use hir::{ - Adt, AssocItem, HasSource, ImplBlock, Local, MacroDef, Module, ModuleDef, StructField, - TypeParam, VariantDef, -}; -use ra_syntax::{ast, ast::VisibilityOwner}; - -use ra_ide_db::RootDatabase; - -#[derive(Debug, PartialEq, Eq)] -pub enum NameKind { - Macro(MacroDef), - Field(StructField), - AssocItem(AssocItem), - Def(ModuleDef), - SelfType(ImplBlock), - Local(Local), - TypeParam(TypeParam), -} - -#[derive(PartialEq, Eq)] -pub(crate) struct NameDefinition { - pub visibility: Option, - /// FIXME: this doesn't really make sense. For example, builtin types don't - /// really have a module. - pub container: Module, - pub kind: NameKind, -} - -pub(super) fn from_assoc_item(db: &RootDatabase, item: AssocItem) -> NameDefinition { - let container = item.module(db); - let visibility = match item { - AssocItem::Function(f) => f.source(db).value.visibility(), - AssocItem::Const(c) => c.source(db).value.visibility(), - AssocItem::TypeAlias(a) => a.source(db).value.visibility(), - }; - let kind = NameKind::AssocItem(item); - NameDefinition { kind, container, visibility } -} - -pub(super) fn from_struct_field(db: &RootDatabase, field: StructField) -> NameDefinition { - let kind = NameKind::Field(field); - let parent = field.parent_def(db); - let container = parent.module(db); - let visibility = match parent { - VariantDef::Struct(s) => s.source(db).value.visibility(), - VariantDef::Union(e) => e.source(db).value.visibility(), - VariantDef::EnumVariant(e) => e.source(db).value.parent_enum().visibility(), - }; - NameDefinition { kind, container, visibility } -} - -pub(super) fn from_module_def( - db: &RootDatabase, - def: ModuleDef, - module: Option, -) -> NameDefinition { - let kind = NameKind::Def(def); - let (container, visibility) = match def { - ModuleDef::Module(it) => { - let container = it.parent(db).or_else(|| Some(it)).unwrap(); - let visibility = it.declaration_source(db).and_then(|s| s.value.visibility()); - (container, visibility) - } - ModuleDef::EnumVariant(it) => { - let container = it.module(db); - let visibility = it.source(db).value.parent_enum().visibility(); - (container, visibility) - } - ModuleDef::Function(it) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::Const(it) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::Static(it) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::Trait(it) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).value.visibility()), - ModuleDef::BuiltinType(..) => (module.unwrap(), None), - }; - NameDefinition { kind, container, visibility } -} diff --git a/crates/ra_ide/src/references/search_scope.rs b/crates/ra_ide/src/references/search_scope.rs index 97c65c2cd..279f57be0 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -19,59 +19,13 @@ pub struct SearchScope { } impl SearchScope { - fn new(entries: FxHashMap>) -> SearchScope { - SearchScope { entries } - } - pub fn single_file(file: FileId) -> SearchScope { - SearchScope::new(std::iter::once((file, None)).collect()) - } - pub(crate) fn intersection(&self, other: &SearchScope) -> SearchScope { - let (mut small, mut large) = (&self.entries, &other.entries); - if small.len() > large.len() { - mem::swap(&mut small, &mut large) - } - - let res = small - .iter() - .filter_map(|(file_id, r1)| { - let r2 = large.get(file_id)?; - let r = intersect_ranges(*r1, *r2)?; - Some((*file_id, r)) - }) - .collect(); - return SearchScope::new(res); - - fn intersect_ranges( - r1: Option, - r2: Option, - ) -> Option> { - match (r1, r2) { - (None, r) | (r, None) => Some(r), - (Some(r1), Some(r2)) => { - let r = r1.intersection(&r2)?; - Some(Some(r)) - } - } - } - } -} - -impl IntoIterator for SearchScope { - type Item = (FileId, Option); - type IntoIter = std::collections::hash_map::IntoIter>; - fn into_iter(self) -> Self::IntoIter { - self.entries.into_iter() - } -} - -impl NameDefinition { - pub(crate) fn search_scope(&self, db: &RootDatabase) -> SearchScope { + pub(crate) fn for_def(def: &NameDefinition, db: &RootDatabase) -> SearchScope { let _p = profile("search_scope"); - let module_src = self.container.definition_source(db); + let module_src = def.container.definition_source(db); let file_id = module_src.file_id.original_file(db); - if let NameKind::Local(var) = self.kind { + if let NameKind::Local(var) = def.kind { let range = match var.parent(db) { DefWithBody::Function(f) => f.source(db).value.syntax().text_range(), DefWithBody::Const(c) => c.source(db).value.syntax().text_range(), @@ -82,10 +36,10 @@ impl NameDefinition { return SearchScope::new(res); } - let vis = self.visibility.as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); + let vis = def.visibility.as_ref().map(|v| v.syntax().to_string()).unwrap_or_default(); if vis.as_str() == "pub(super)" { - if let Some(parent_module) = self.container.parent(db) { + if let Some(parent_module) = def.container.parent(db) { let mut res = FxHashMap::default(); let parent_src = parent_module.definition_source(db); let file_id = parent_src.file_id.original_file(db); @@ -118,7 +72,7 @@ impl NameDefinition { return SearchScope::new(res); } if vis.as_str() == "pub" { - let krate = self.container.krate(); + let krate = def.container.krate(); for rev_dep in krate.reverse_dependencies(db) { let root_file = rev_dep.root_file(db); let source_root_id = db.file_source_root(root_file); @@ -137,4 +91,48 @@ impl NameDefinition { res.insert(file_id, range); SearchScope::new(res) } + + fn new(entries: FxHashMap>) -> SearchScope { + SearchScope { entries } + } + pub fn single_file(file: FileId) -> SearchScope { + SearchScope::new(std::iter::once((file, None)).collect()) + } + pub(crate) fn intersection(&self, other: &SearchScope) -> SearchScope { + let (mut small, mut large) = (&self.entries, &other.entries); + if small.len() > large.len() { + mem::swap(&mut small, &mut large) + } + + let res = small + .iter() + .filter_map(|(file_id, r1)| { + let r2 = large.get(file_id)?; + let r = intersect_ranges(*r1, *r2)?; + Some((*file_id, r)) + }) + .collect(); + return SearchScope::new(res); + + fn intersect_ranges( + r1: Option, + r2: Option, + ) -> Option> { + match (r1, r2) { + (None, r) | (r, None) => Some(r), + (Some(r1), Some(r2)) => { + let r = r1.intersection(&r2)?; + Some(Some(r)) + } + } + } + } +} + +impl IntoIterator for SearchScope { + type Item = (FileId, Option); + type IntoIter = std::collections::hash_map::IntoIter>; + fn into_iter(self) -> Self::IntoIter { + self.entries.into_iter() + } } -- cgit v1.2.3