From 595b06a1b8fcd215c828d65ee1dd1a30c2697de9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 17:33:07 +0100 Subject: Create modules via SourceBinder --- crates/ra_hir/src/from_source.rs | 38 ----------------- crates/ra_hir/src/lib.rs | 1 - crates/ra_hir/src/source_binder.rs | 47 +++++++++++++--------- crates/ra_ide/src/completion/completion_context.rs | 10 ++--- crates/ra_ide/src/diagnostics.rs | 6 +-- crates/ra_ide/src/impls.rs | 18 +++------ crates/ra_ide/src/parent_module.rs | 29 +++++++------ crates/ra_ide/src/references/classify.rs | 22 +++------- crates/ra_ide/src/references/name_definition.rs | 2 + crates/ra_ide/src/runnables.rs | 4 +- 10 files changed, 65 insertions(+), 112 deletions(-) delete mode 100644 crates/ra_hir/src/from_source.rs (limited to 'crates') diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs deleted file mode 100644 index eb76aecb1..000000000 --- a/crates/ra_hir/src/from_source.rs +++ /dev/null @@ -1,38 +0,0 @@ -//! Finds a corresponding hir data structure for a syntax node in a specific -//! file. - -use hir_def::{nameres::ModuleSource, ModuleId}; -use ra_db::FileId; -use ra_prof::profile; - -use crate::{ - db::{DefDatabase, HirDatabase}, - InFile, Module, -}; - -impl Module { - pub fn from_definition(db: &impl HirDatabase, src: InFile) -> Option { - let _p = profile("Module::from_definition"); - let mut sb = crate::SourceBinder::new(db); - match src.value { - ModuleSource::Module(ref module) => { - assert!(!module.has_semi()); - return sb.to_def(InFile { file_id: src.file_id, value: module.clone() }); - } - ModuleSource::SourceFile(_) => (), - }; - - let original_file = src.file_id.original_file(db); - Module::from_file(db, original_file) - } - - fn from_file(db: &impl DefDatabase, file: FileId) -> Option { - let _p = profile("Module::from_file"); - let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| { - let crate_def_map = db.crate_def_map(crate_id); - let local_id = crate_def_map.modules_for_file(file).next()?; - Some((crate_id, local_id)) - })?; - Some(Module { id: ModuleId { krate, local_id } }) - } -} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 11829f42a..e1c7b7a20 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -36,7 +36,6 @@ mod from_id; mod code_model; mod has_source; -mod from_source; pub use crate::{ code_model::{ diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 26eedbb2c..fa225a4ed 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -19,7 +19,8 @@ use ra_syntax::{ }; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; +use crate::{db::HirDatabase, Local, Module, SourceAnalyzer, TypeParam}; +use ra_db::FileId; pub struct SourceBinder<'a, DB> { pub db: &'a DB, @@ -60,6 +61,16 @@ impl SourceBinder<'_, DB> { T::to_def(self, src) } + pub fn to_module_def(&mut self, file: FileId) -> Option { + let _p = profile("SourceBinder::to_module_def"); + let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { + let crate_def_map = self.db.crate_def_map(crate_id); + let local_id = crate_def_map.modules_for_file(file).next()?; + Some((crate_id, local_id)) + })?; + Some(Module { id: ModuleId { krate, local_id } }) + } + fn to_id(&mut self, src: InFile) -> Option { T::to_id(self, src) } @@ -107,8 +118,7 @@ impl SourceBinder<'_, DB> { return Some(res); } - let module_source = ModuleSource::from_child_node(self.db, src); - let c = crate::Module::from_definition(self.db, src.with_value(module_source))?; + let c = self.to_module_def(src.file_id.original_file(self.db))?; Some(c.id.into()) } @@ -248,14 +258,12 @@ impl ToId for ast::MacroCall { ) -> Option { let kind = MacroDefKind::Declarative; - let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax())); - let module = crate::Module::from_definition(sb.db, InFile::new(src.file_id, module_src))?; - let krate = Some(module.krate().id); + let krate = sb.to_module_def(src.file_id.original_file(sb.db))?.id.krate; let ast_id = Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value))); - Some(MacroDefId { krate, ast_id, kind }) + Some(MacroDefId { krate: Some(krate), ast_id, kind }) } } @@ -319,21 +327,22 @@ impl ToDef for ast::Module { ) -> Option { { let _p = profile("ast::Module::to_def"); - let parent_declaration = - src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); + let parent_declaration = src + .as_ref() + .map(|it| it.syntax()) + .cloned() + .ancestors_with_macros(sb.db) + .skip(1) + .find_map(|it| { + let m = ast::Module::cast(it.value.clone())?; + Some(it.with_value(m)) + }); let parent_module = match parent_declaration { - Some(parent_declaration) => { - let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; - sb.to_def(src_parent) - } + Some(parent_declaration) => sb.to_def(parent_declaration), None => { - let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); - let src_parent = InFile { - file_id: src.file_id, - value: ModuleSource::SourceFile(source_file), - }; - Module::from_definition(sb.db, src_parent) + let file_id = src.file_id.original_file(sb.db); + sb.to_module_def(file_id) } }?; diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 48d69f7e5..deaacda6c 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -52,15 +52,11 @@ impl<'a> CompletionContext<'a> { original_parse: &'a Parse, position: FilePosition, ) -> Option> { - let src = hir::ModuleSource::from_position(db, position); - let module = hir::Module::from_definition( - db, - hir::InFile { file_id: position.file_id.into(), value: src }, - ); + let mut sb = hir::SourceBinder::new(db); + let module = sb.to_module_def(position.file_id); let token = original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; - let analyzer = hir::SourceAnalyzer::new( - db, + let analyzer = sb.analyze( hir::InFile::new(position.file_id.into(), &token.parent()), Some(position.offset), ); diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 478368529..f403b3bcf 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -23,6 +23,7 @@ pub enum Severity { pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec { let _p = profile("diagnostics"); + let mut sb = hir::SourceBinder::new(db); let parse = db.parse(file_id); let mut res = Vec::new(); @@ -108,10 +109,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec fix: Some(fix), }) }); - let source_file = db.parse(file_id).tree(); - let src = - hir::InFile { file_id: file_id.into(), value: hir::ModuleSource::SourceFile(source_file) }; - if let Some(m) = hir::Module::from_definition(db, src) { + if let Some(m) = sb.to_module_def(file_id) { m.diagnostics(db, &mut sink); }; drop(sink); diff --git a/crates/ra_ide/src/impls.rs b/crates/ra_ide/src/impls.rs index fb9396195..9834025d3 100644 --- a/crates/ra_ide/src/impls.rs +++ b/crates/ra_ide/src/impls.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir::{ImplBlock, SourceBinder}; +use hir::{Crate, ImplBlock, SourceBinder}; use ra_db::SourceDatabase; use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; @@ -14,21 +14,17 @@ pub(crate) fn goto_implementation( let syntax = parse.tree().syntax().clone(); let mut sb = SourceBinder::new(db); - let src = hir::ModuleSource::from_position(db, position); - let module = hir::Module::from_definition( - db, - hir::InFile { file_id: position.file_id.into(), value: src }, - )?; + let krate = sb.to_module_def(position.file_id)?.krate(); if let Some(nominal_def) = find_node_at_offset::(&syntax, position.offset) { return Some(RangeInfo::new( nominal_def.syntax().text_range(), - impls_for_def(&mut sb, position, &nominal_def, module)?, + impls_for_def(&mut sb, position, &nominal_def, krate)?, )); } else if let Some(trait_def) = find_node_at_offset::(&syntax, position.offset) { return Some(RangeInfo::new( trait_def.syntax().text_range(), - impls_for_trait(&mut sb, position, &trait_def, module)?, + impls_for_trait(&mut sb, position, &trait_def, krate)?, )); } @@ -39,7 +35,7 @@ fn impls_for_def( sb: &mut SourceBinder, position: FilePosition, node: &ast::NominalDef, - module: hir::Module, + krate: Crate, ) -> Option> { let ty = match node { ast::NominalDef::StructDef(def) => { @@ -56,7 +52,6 @@ fn impls_for_def( } }; - let krate = module.krate(); let impls = ImplBlock::all_in_crate(sb.db, krate); Some( @@ -72,12 +67,11 @@ fn impls_for_trait( sb: &mut SourceBinder, position: FilePosition, node: &ast::TraitDef, - module: hir::Module, + krate: Crate, ) -> Option> { let src = hir::InFile { file_id: position.file_id.into(), value: node.clone() }; let tr = sb.to_def(src)?; - let krate = module.krate(); let impls = ImplBlock::for_trait(sb.db, krate, tr); Some(impls.into_iter().map(|imp| imp.to_nav(sb.db)).collect()) diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs index f5a788c07..2dbccfc3b 100644 --- a/crates/ra_ide/src/parent_module.rs +++ b/crates/ra_ide/src/parent_module.rs @@ -1,17 +1,23 @@ //! FIXME: write short doc here use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; +use ra_syntax::{ + algo::find_node_at_offset, + ast::{self, AstNode}, +}; use crate::{db::RootDatabase, NavigationTarget}; /// This returns `Vec` because a module may be included from several places. We /// don't handle this case yet though, so the Vec has length at most one. pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec { - let src = hir::ModuleSource::from_position(db, position); - let module = match hir::Module::from_definition( - db, - hir::InFile { file_id: position.file_id.into(), value: src }, - ) { + let mut sb = hir::SourceBinder::new(db); + let parse = db.parse(position.file_id); + let module = match find_node_at_offset::(parse.tree().syntax(), position.offset) { + Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)), + None => sb.to_module_def(position.file_id), + }; + let module = match module { None => return Vec::new(), Some(it) => it, }; @@ -21,14 +27,11 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec Vec { - let source_file = db.parse(file_id).tree(); - let src = hir::ModuleSource::SourceFile(source_file); - let module = - match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src }) - { - Some(it) => it, - None => return Vec::new(), - }; + let mut sb = hir::SourceBinder::new(db); + let module = match sb.to_module_def(file_id) { + Some(it) => it, + None => return Vec::new(), + }; let krate = module.krate(); vec![krate.into()] } diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs index cb7da1fff..46cba30a3 100644 --- a/crates/ra_ide/src/references/classify.rs +++ b/crates/ra_ide/src/references/classify.rs @@ -1,6 +1,6 @@ //! Functions that are used to classify an element from its definition or reference. -use hir::{InFile, Module, ModuleSource, PathResolution, SourceBinder}; +use hir::{InFile, PathResolution, SourceBinder}; use ra_prof::profile; use ra_syntax::{ast, match_ast, AstNode}; use test_utils::tested_by; @@ -35,16 +35,7 @@ pub(crate) fn classify_name( Some(from_struct_field(sb.db, field)) }, ast::Module(it) => { - let def = { - if !it.has_semi() { - let ast = hir::ModuleSource::Module(it); - let src = name.with_value(ast); - hir::Module::from_definition(sb.db, src) - } else { - let src = name.with_value(it); - sb.to_def(src) - } - }?; + let def = sb.to_def(name.with_value(it))?; Some(from_module_def(sb.db, def.into(), None)) }, ast::StructDef(it) => { @@ -103,8 +94,7 @@ pub(crate) fn classify_name( let src = name.with_value(it); let def = sb.to_def(src.clone())?; - let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax())); - let module = Module::from_definition(sb.db, src.with_value(module_src))?; + let module = sb.to_module_def(src.file_id.original_file(sb.db))?; Some(NameDefinition { visibility: None, @@ -157,10 +147,9 @@ pub(crate) fn classify_name_ref( } } - let ast = ModuleSource::from_child_node(sb.db, name_ref.with_value(&parent)); // FIXME: find correct container and visibility for each case - let container = Module::from_definition(sb.db, name_ref.with_value(ast))?; let visibility = None; + let container = sb.to_module_def(name_ref.file_id.original_file(sb.db))?; if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { tested_by!(goto_def_for_macros); @@ -178,12 +167,13 @@ pub(crate) fn classify_name_ref( PathResolution::Def(def) => Some(from_module_def(sb.db, def, Some(container))), PathResolution::AssocItem(item) => Some(from_assoc_item(sb.db, item)), PathResolution::Local(local) => { - let container = local.module(sb.db); let kind = NameKind::Local(local); + let container = local.module(sb.db); Some(NameDefinition { kind, container, visibility: None }) } PathResolution::TypeParam(par) => { let kind = NameKind::TypeParam(par); + let container = par.module(sb.db); Some(NameDefinition { kind, container, visibility }) } PathResolution::Macro(def) => { diff --git a/crates/ra_ide/src/references/name_definition.rs b/crates/ra_ide/src/references/name_definition.rs index 8c67c8863..1e4226ab9 100644 --- a/crates/ra_ide/src/references/name_definition.rs +++ b/crates/ra_ide/src/references/name_definition.rs @@ -25,6 +25,8 @@ pub enum NameKind { #[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, } diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs index e213e1a06..7533692f6 100644 --- a/crates/ra_ide/src/runnables.rs +++ b/crates/ra_ide/src/runnables.rs @@ -66,8 +66,8 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti return None; } let range = module.syntax().text_range(); - let src = hir::ModuleSource::from_child_node(db, InFile::new(file_id.into(), &module.syntax())); - let module = hir::Module::from_definition(db, InFile::new(file_id.into(), src))?; + let mut sb = hir::SourceBinder::new(db); + let module = sb.to_def(InFile::new(file_id.into(), module))?; let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::"); Some(Runnable { range, kind: RunnableKind::TestMod { path } }) -- cgit v1.2.3