From 1cd184d6539478c7e54c92835902921976dce5d1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 10 Apr 2019 10:12:54 +0300 Subject: use either for uses --- crates/ra_hir/src/code_model_api.rs | 12 ++++++++--- crates/ra_hir/src/code_model_impl/module.rs | 14 ++---------- crates/ra_hir/src/either.rs | 18 ++++++++++++++++ crates/ra_hir/src/lib.rs | 5 ++++- crates/ra_hir/src/nameres.rs | 2 +- crates/ra_hir/src/nameres/raw.rs | 33 ++++++----------------------- 6 files changed, 40 insertions(+), 44 deletions(-) create mode 100644 crates/ra_hir/src/either.rs (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs index 9e6170440..f13a6b37a 100644 --- a/crates/ra_hir/src/code_model_api.rs +++ b/crates/ra_hir/src/code_model_api.rs @@ -4,7 +4,7 @@ use ra_db::{CrateId, SourceRootId, Edition}; use ra_syntax::{ast::self, TreeArc}; use crate::{ - Name, ScopesWithSourceMap, Ty, HirFileId, ImportSource, + Name, ScopesWithSourceMap, Ty, HirFileId, Either, HirDatabase, DefDatabase, type_ref::TypeRef, nameres::{ModuleScope, Namespace, ImportId, CrateModuleId}, @@ -117,8 +117,14 @@ impl Module { } /// Returns the syntax of the last path segment corresponding to this import - pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> ImportSource { - self.import_source_impl(db, import) + pub fn import_source( + &self, + db: &impl HirDatabase, + import: ImportId, + ) -> Either, TreeArc> { + let (file_id, source) = self.definition_source(db); + let (_, source_map) = db.raw_items_with_source_map(file_id); + source_map.get(&source, import) } /// Returns the crate this module is part of. diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 88dee3a69..5c2ea73ce 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -3,9 +3,9 @@ use ra_syntax::{ast, TreeArc}; use crate::{ Module, ModuleSource, Name, AstId, - nameres::{CrateModuleId, ImportId}, + nameres::CrateModuleId, HirDatabase, DefDatabase, - HirFileId, ImportSource, + HirFileId, }; impl ModuleSource { @@ -68,16 +68,6 @@ impl Module { Some((decl.file_id(), ast)) } - pub(crate) fn import_source_impl( - &self, - db: &impl HirDatabase, - import: ImportId, - ) -> ImportSource { - let (file_id, source) = self.definition_source(db); - let (_, source_map) = db.raw_items_with_source_map(file_id); - source_map.get(&source, import) - } - pub(crate) fn crate_root_impl(&self, db: &impl DefDatabase) -> Module { let def_map = db.crate_def_map(self.krate); self.with_module_id(def_map.root()) diff --git a/crates/ra_hir/src/either.rs b/crates/ra_hir/src/either.rs new file mode 100644 index 000000000..6714529d9 --- /dev/null +++ b/crates/ra_hir/src/either.rs @@ -0,0 +1,18 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Either { + A(A), + B(B), +} + +impl Either { + pub fn map(self, f1: F1, f2: F2) -> Either + where + F1: FnOnce(A) -> U, + F2: FnOnce(B) -> V, + { + match self { + Either::A(a) => Either::A(f1(a)), + Either::B(b) => Either::B(f2(b)), + } + } +} diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 4d337d2e3..0881939a2 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -17,6 +17,8 @@ macro_rules! impl_froms { } } +mod either; + pub mod db; #[macro_use] pub mod mock; @@ -52,11 +54,12 @@ use crate::{ }; pub use self::{ + either::Either, path::{Path, PathKind}, name::Name, source_id::{AstIdMap, ErasedFileAstId}, ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc}, - nameres::{PerNs, Namespace, ImportId, ImportSource}, + nameres::{PerNs, Namespace, ImportId}, ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay}, impl_block::{ImplBlock, ImplItem}, docs::{Docs, Documentation}, diff --git a/crates/ra_hir/src/nameres.rs b/crates/ra_hir/src/nameres.rs index 4ae04514a..0eddfab12 100644 --- a/crates/ra_hir/src/nameres.rs +++ b/crates/ra_hir/src/nameres.rs @@ -75,7 +75,7 @@ pub(crate) use self::raw::{RawItems, ImportSourceMap}; pub use self::{ per_ns::{PerNs, Namespace}, - raw::{ImportId, ImportSource}, + raw::ImportId, }; /// Contans all top-level defs from a macro-expanded crate diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index b7416ede6..43c97a0bf 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -12,7 +12,7 @@ use ra_syntax::{ use crate::{ DefDatabase, Name, AsName, Path, HirFileId, ModuleSource, - AstIdMap, FileAstId, + AstIdMap, FileAstId, Either, }; /// `RawItems` is a set of top-level items in a file (except for impls). @@ -34,28 +34,15 @@ pub struct ImportSourceMap { map: ArenaMap, } -#[derive(Debug, PartialEq, Eq, Clone, Copy)] -enum ImportSourcePtr { - UseTree(AstPtr), - ExternCrate(AstPtr), -} +type ImportSourcePtr = Either, AstPtr>; +type ImportSource = Either, TreeArc>; impl ImportSourcePtr { fn to_node(self, file: &SourceFile) -> ImportSource { - match self { - ImportSourcePtr::UseTree(ptr) => ImportSource::UseTree(ptr.to_node(file).to_owned()), - ImportSourcePtr::ExternCrate(ptr) => { - ImportSource::ExternCrate(ptr.to_node(file).to_owned()) - } - } + self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned()) } } -pub enum ImportSource { - UseTree(TreeArc), - ExternCrate(TreeArc), -} - impl ImportSourceMap { fn insert(&mut self, import: ImportId, ptr: ImportSourcePtr) { self.map.insert(import, ptr) @@ -281,11 +268,7 @@ impl RawItemsCollector { Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| { let import_data = ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false }; - self.push_import( - current_module, - import_data, - ImportSourcePtr::UseTree(AstPtr::new(use_tree)), - ); + self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree))); }) } @@ -304,11 +287,7 @@ impl RawItemsCollector { is_prelude: false, is_extern_crate: true, }; - self.push_import( - current_module, - import_data, - ImportSourcePtr::ExternCrate(AstPtr::new(extern_crate)), - ); + self.push_import(current_module, import_data, Either::B(AstPtr::new(extern_crate))); } } -- cgit v1.2.3