3031: Move imports locator to ide_db r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-02-06 15:27:23 +00:00 committed by GitHub
commit ff0f0fc31e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 287 additions and 285 deletions

View file

@ -3,9 +3,9 @@
use either::Either;
use ra_assists::{AssistAction, AssistLabel};
use ra_db::{FilePosition, FileRange};
use ra_ide_db::RootDatabase;
use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase};
use crate::{imports_locator::ImportsLocatorIde, FileId, SourceChange, SourceFileEdit};
use crate::{FileId, SourceChange, SourceFileEdit};
pub use ra_assists::AssistId;

View file

@ -26,7 +26,6 @@ mod syntax_highlighting;
mod parent_module;
mod references;
mod impls;
mod imports_locator;
mod assists;
mod diagnostics;
mod syntax_tree;

View file

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

View file

@ -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<RootDatabase>,
name: InFile<&ast::Name>,
) -> Option<NameDefinition> {
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<RootDatabase>,

View file

@ -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<ast::Visibility>,
/// 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<Module>,
) -> 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 }
}

View file

@ -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::<FxHashMap<_, _>>();
// 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<FileId, Option<TextRange>>) -> 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::<FxHashMap<_, _>>();
// 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)
}
}

View file

@ -0,0 +1,196 @@
//! `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.
// FIXME: this badly needs rename/rewrite (matklad, 2020-02-06).
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<ast::Visibility>,
/// 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<RootDatabase>,
name: InFile<&ast::Name>,
) -> Option<NameDefinition> {
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<Module>,
) -> 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 }
}

View file

@ -3,24 +3,22 @@
use hir::{db::HirDatabase, ModuleDef, SourceBinder};
use ra_assists::ImportsLocator;
use ra_ide_db::{
symbol_index::{self, FileSymbol},
RootDatabase,
};
use ra_prof::profile;
use ra_syntax::{ast, AstNode, SyntaxKind::NAME};
use crate::{
references::{classify_name, NameDefinition, NameKind},
Query,
defs::classify_name,
defs::NameKind,
symbol_index::{self, FileSymbol, Query},
RootDatabase,
};
pub(crate) struct ImportsLocatorIde<'a> {
pub struct ImportsLocatorIde<'a> {
source_binder: SourceBinder<'a, RootDatabase>,
}
impl<'a> ImportsLocatorIde<'a> {
pub(crate) fn new(db: &'a RootDatabase) -> Self {
pub fn new(db: &'a RootDatabase) -> Self {
Self { source_binder: SourceBinder::new(db) }
}
@ -28,7 +26,7 @@ impl<'a> ImportsLocatorIde<'a> {
&mut self,
db: &impl HirDatabase,
import_candidate: &FileSymbol,
) -> Option<NameDefinition> {
) -> Option<NameKind> {
let _p = profile("get_name_definition");
let file_id = import_candidate.file_id.into();
let candidate_node = import_candidate.ptr.to_node(&db.parse_or_expand(file_id)?);
@ -41,6 +39,7 @@ impl<'a> ImportsLocatorIde<'a> {
&mut self.source_binder,
hir::InFile { file_id, value: &ast::Name::cast(candidate_name_node)? },
)
.map(|it| it.kind)
}
}
@ -67,7 +66,7 @@ impl ImportsLocator for ImportsLocatorIde<'_> {
.into_iter()
.chain(lib_results.into_iter())
.filter_map(|import_candidate| self.get_name_definition(db, &import_candidate))
.filter_map(|name_definition_to_import| match name_definition_to_import.kind {
.filter_map(|name_definition_to_import| match name_definition_to_import {
NameKind::Def(module_def) => Some(module_def),
_ => None,
})

View file

@ -7,6 +7,8 @@ pub mod line_index_utils;
pub mod feature_flags;
pub mod symbol_index;
pub mod change;
pub mod defs;
pub mod imports_locator;
mod wasm_shims;
use std::sync::Arc;