diff --git a/crates/ra_ide/src/imports_locator.rs b/crates/ra_ide/src/imports_locator.rs index 23bf43b07bf..0dca0c86c2b 100644 --- a/crates/ra_ide/src/imports_locator.rs +++ b/crates/ra_ide/src/imports_locator.rs @@ -4,16 +4,14 @@ use hir::{db::HirDatabase, ModuleDef, SourceBinder}; use ra_assists::ImportsLocator; use ra_ide_db::{ + defs::NameKind, symbol_index::{self, FileSymbol}, RootDatabase, }; use ra_prof::profile; use ra_syntax::{ast, AstNode, SyntaxKind::NAME}; -use crate::{ - references::{classify_name, NameKind}, - Query, -}; +use crate::{references::classify_name, Query}; pub(crate) struct ImportsLocatorIde<'a> { source_binder: SourceBinder<'a, RootDatabase>, diff --git a/crates/ra_ide/src/references.rs b/crates/ra_ide/src/references.rs index b47f8bcd9e4..c215040f471 100644 --- a/crates/ra_ide/src/references.rs +++ b/crates/ra_ide/src/references.rs @@ -10,7 +10,6 @@ //! resolved to the search element definition, we get a reference. mod classify; -mod name_definition; mod rename; mod search_scope; @@ -29,9 +28,9 @@ use crate::{display::ToNav, FilePosition, FileRange, NavigationTarget, RangeInfo pub(crate) use self::{ classify::{classify_name, classify_name_ref}, - name_definition::{NameDefinition, NameKind}, rename::rename, }; +pub(crate) use ra_ide_db::defs::{NameDefinition, NameKind}; pub use self::search_scope::SearchScope; @@ -137,7 +136,7 @@ pub(crate) fn find_all_refs( }; let search_scope = { - let base = def.search_scope(db); + let base = SearchScope::for_def(&def, db); match search_scope { None => base, Some(scope) => base.intersection(&scope), diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index 758ea4e8b78..0326fd379ee 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 71565e6d395..00000000000 --- 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 97c65c2cd49..279f57be06a 100644 --- a/crates/ra_ide/src/references/search_scope.rs +++ b/crates/ra_ide/src/references/search_scope.rs @@ -19,6 +19,79 @@ pub struct SearchScope { } impl SearchScope { + pub(crate) fn for_def(def: &NameDefinition, db: &RootDatabase) -> SearchScope { + let _p = profile("search_scope"); + + let module_src = def.container.definition_source(db); + let file_id = module_src.file_id.original_file(db); + + 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(), + DefWithBody::Static(s) => s.source(db).value.syntax().text_range(), + }; + let mut res = FxHashMap::default(); + res.insert(file_id, Some(range)); + return SearchScope::new(res); + } + + 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) = 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); + + match parent_src.value { + ModuleSource::Module(m) => { + let range = Some(m.syntax().text_range()); + res.insert(file_id, range); + } + ModuleSource::SourceFile(_) => { + res.insert(file_id, None); + res.extend(parent_module.children(db).map(|m| { + let src = m.definition_source(db); + (src.file_id.original_file(db), None) + })); + } + } + return SearchScope::new(res); + } + } + + if vis.as_str() != "" { + let source_root_id = db.file_source_root(file_id); + let source_root = db.source_root(source_root_id); + let mut res = source_root.walk().map(|id| (id, None)).collect::>(); + + // FIXME: add "pub(in path)" + + if vis.as_str() == "pub(crate)" { + return SearchScope::new(res); + } + if vis.as_str() == "pub" { + 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); + let source_root = db.source_root(source_root_id); + res.extend(source_root.walk().map(|id| (id, None))); + } + return SearchScope::new(res); + } + } + + let mut res = FxHashMap::default(); + let range = match module_src.value { + ModuleSource::Module(m) => Some(m.syntax().text_range()), + ModuleSource::SourceFile(_) => None, + }; + res.insert(file_id, range); + SearchScope::new(res) + } + fn new(entries: FxHashMap>) -> SearchScope { SearchScope { entries } } @@ -63,78 +136,3 @@ impl IntoIterator for SearchScope { self.entries.into_iter() } } - -impl NameDefinition { - pub(crate) fn search_scope(&self, db: &RootDatabase) -> SearchScope { - let _p = profile("search_scope"); - - let module_src = self.container.definition_source(db); - let file_id = module_src.file_id.original_file(db); - - if let NameKind::Local(var) = self.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(), - DefWithBody::Static(s) => s.source(db).value.syntax().text_range(), - }; - let mut res = FxHashMap::default(); - res.insert(file_id, Some(range)); - return SearchScope::new(res); - } - - let vis = self.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) { - let mut res = FxHashMap::default(); - let parent_src = parent_module.definition_source(db); - let file_id = parent_src.file_id.original_file(db); - - match parent_src.value { - ModuleSource::Module(m) => { - let range = Some(m.syntax().text_range()); - res.insert(file_id, range); - } - ModuleSource::SourceFile(_) => { - res.insert(file_id, None); - res.extend(parent_module.children(db).map(|m| { - let src = m.definition_source(db); - (src.file_id.original_file(db), None) - })); - } - } - return SearchScope::new(res); - } - } - - if vis.as_str() != "" { - let source_root_id = db.file_source_root(file_id); - let source_root = db.source_root(source_root_id); - let mut res = source_root.walk().map(|id| (id, None)).collect::>(); - - // FIXME: add "pub(in path)" - - if vis.as_str() == "pub(crate)" { - return SearchScope::new(res); - } - if vis.as_str() == "pub" { - let krate = self.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); - let source_root = db.source_root(source_root_id); - res.extend(source_root.walk().map(|id| (id, None))); - } - return SearchScope::new(res); - } - } - - let mut res = FxHashMap::default(); - let range = match module_src.value { - ModuleSource::Module(m) => Some(m.syntax().text_range()), - ModuleSource::SourceFile(_) => None, - }; - res.insert(file_id, range); - SearchScope::new(res) - } -} diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs new file mode 100644 index 00000000000..1c983fb604e --- /dev/null +++ b/crates/ra_ide_db/src/defs.rs @@ -0,0 +1,194 @@ +//! `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, InFile, Local, MacroDef, Module, ModuleDef, SourceBinder, + StructField, TypeParam, VariantDef, +}; +use ra_prof::profile; +use ra_syntax::{ + ast::{self, AstNode, VisibilityOwner}, + match_ast, +}; + +use crate::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 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 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 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 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 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_db/src/lib.rs b/crates/ra_ide_db/src/lib.rs index e922d1e5f9c..0715dfc66bf 100644 --- a/crates/ra_ide_db/src/lib.rs +++ b/crates/ra_ide_db/src/lib.rs @@ -7,6 +7,7 @@ pub mod line_index_utils; pub mod feature_flags; pub mod symbol_index; pub mod change; +pub mod defs; mod wasm_shims; use std::sync::Arc;