From b05d6e53fb0e9a008dc2e1220b1201818e63ed2d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 30 Oct 2019 18:50:10 +0300 Subject: push either to hir_expand --- crates/ra_hir/src/lib.rs | 17 ++++++------ crates/ra_hir_def/src/either.rs | 54 ------------------------------------ crates/ra_hir_def/src/hygiene.rs | 6 ++-- crates/ra_hir_def/src/lib.rs | 1 - crates/ra_hir_def/src/nameres/raw.rs | 11 +++----- crates/ra_hir_def/src/path.rs | 2 +- crates/ra_hir_expand/src/either.rs | 54 ++++++++++++++++++++++++++++++++++++ crates/ra_hir_expand/src/lib.rs | 1 + 8 files changed, 70 insertions(+), 76 deletions(-) delete mode 100644 crates/ra_hir_def/src/either.rs create mode 100644 crates/ra_hir_expand/src/either.rs (limited to 'crates') diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index f765490b0..989818c0e 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -60,6 +60,13 @@ use crate::{ids::MacroFileKind, resolve::Resolver}; pub use crate::{ adt::VariantDef, + code_model::{ + docs::{DocDef, Docs, Documentation}, + src::{HasBodySource, HasSource, Source}, + Adt, AssocItem, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, + DefWithBody, Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, + ModuleDef, ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, + }, expr::ExprScopes, from_source::FromSource, generics::{GenericDef, GenericParam, GenericParams, HasGenericParams}, @@ -73,17 +80,9 @@ pub use crate::{ }, }; -pub use self::code_model::{ - docs::{DocDef, Docs, Documentation}, - src::{HasBodySource, HasSource, Source}, - Adt, AssocItem, BuiltinType, Const, ConstData, Container, Crate, CrateDependency, DefWithBody, - Enum, EnumVariant, FieldSource, FnData, Function, HasBody, MacroDef, Module, ModuleDef, - ModuleSource, Static, Struct, StructField, Trait, TypeAlias, Union, -}; - pub use hir_def::{ - either::Either, name::Name, path::{Path, PathKind}, type_ref::Mutability, }; +pub use hir_expand::either::Either; diff --git a/crates/ra_hir_def/src/either.rs b/crates/ra_hir_def/src/either.rs deleted file mode 100644 index 83583ef8b..000000000 --- a/crates/ra_hir_def/src/either.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! FIXME: write short doc here - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Either { - A(A), - B(B), -} - -impl Either { - pub fn either(self, f1: F1, f2: F2) -> R - where - F1: FnOnce(A) -> R, - F2: FnOnce(B) -> R, - { - match self { - Either::A(a) => f1(a), - Either::B(b) => f2(b), - } - } - 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)), - } - } - pub fn map_a(self, f: F) -> Either - where - F: FnOnce(A) -> U, - { - self.map(f, |it| it) - } - pub fn a(self) -> Option { - match self { - Either::A(it) => Some(it), - Either::B(_) => None, - } - } - pub fn b(self) -> Option { - match self { - Either::A(_) => None, - Either::B(it) => Some(it), - } - } - pub fn as_ref(&self) -> Either<&A, &B> { - match self { - Either::A(it) => Either::A(it), - Either::B(it) => Either::B(it), - } - } -} diff --git a/crates/ra_hir_def/src/hygiene.rs b/crates/ra_hir_def/src/hygiene.rs index e1ae58a3b..f51c46fcb 100644 --- a/crates/ra_hir_def/src/hygiene.rs +++ b/crates/ra_hir_def/src/hygiene.rs @@ -4,14 +4,12 @@ //! this moment, this is horribly incomplete and handles only `$crate`. // Should this be moved to `hir_expand`? Seems like it. +use hir_expand::either::Either; use hir_expand::{db::AstDatabase, HirFileId}; use ra_db::CrateId; use ra_syntax::ast; -use crate::{ - either::Either, - name::{AsName, Name}, -}; +use crate::name::{AsName, Name}; #[derive(Debug)] pub struct Hygiene { diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 61ccdb30d..0de728dc1 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs @@ -8,7 +8,6 @@ //! actually true. pub mod db; -pub mod either; pub mod attr; pub mod name; pub mod path; diff --git a/crates/ra_hir_def/src/nameres/raw.rs b/crates/ra_hir_def/src/nameres/raw.rs index 636364628..f1896c0cc 100644 --- a/crates/ra_hir_def/src/nameres/raw.rs +++ b/crates/ra_hir_def/src/nameres/raw.rs @@ -2,7 +2,7 @@ use std::{ops::Index, sync::Arc}; -use hir_expand::{ast_id_map::AstIdMap, db::AstDatabase}; +use hir_expand::{ast_id_map::AstIdMap, db::AstDatabase, either::Either}; use ra_arena::{impl_arena_id, map::ArenaMap, Arena, RawId}; use ra_syntax::{ ast::{self, AttrsOwner, NameOwner}, @@ -12,7 +12,6 @@ use ra_syntax::{ use crate::{ attr::Attr, db::DefDatabase2, - either::Either, hygiene::Hygiene, name::{AsName, Name}, path::Path, @@ -41,10 +40,8 @@ pub struct ImportSourceMap { type ImportSourcePtr = Either, AstPtr>; type ImportSource = Either; -impl ImportSourcePtr { - fn to_node(self, file: &SourceFile) -> ImportSource { - self.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) - } +fn to_node(ptr: ImportSourcePtr, file: &SourceFile) -> ImportSource { + ptr.map(|ptr| ptr.to_node(file.syntax()), |ptr| ptr.to_node(file.syntax())) } impl ImportSourceMap { @@ -58,7 +55,7 @@ impl ImportSourceMap { ModuleSource::Module(m) => m.syntax().ancestors().find_map(SourceFile::cast).unwrap(), }; - self.map[import].to_node(&file) + to_node(self.map[import], &file) } } diff --git a/crates/ra_hir_def/src/path.rs b/crates/ra_hir_def/src/path.rs index 39f394c3f..8d57e7761 100644 --- a/crates/ra_hir_def/src/path.rs +++ b/crates/ra_hir_def/src/path.rs @@ -2,6 +2,7 @@ use std::{iter, sync::Arc}; +use hir_expand::either::Either; use ra_db::CrateId; use ra_syntax::{ ast::{self, NameOwner, TypeAscriptionOwner}, @@ -9,7 +10,6 @@ use ra_syntax::{ }; use crate::{ - either::Either, hygiene::Hygiene, name::{self, AsName, Name}, type_ref::TypeRef, diff --git a/crates/ra_hir_expand/src/either.rs b/crates/ra_hir_expand/src/either.rs new file mode 100644 index 000000000..83583ef8b --- /dev/null +++ b/crates/ra_hir_expand/src/either.rs @@ -0,0 +1,54 @@ +//! FIXME: write short doc here + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum Either { + A(A), + B(B), +} + +impl Either { + pub fn either(self, f1: F1, f2: F2) -> R + where + F1: FnOnce(A) -> R, + F2: FnOnce(B) -> R, + { + match self { + Either::A(a) => f1(a), + Either::B(b) => f2(b), + } + } + 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)), + } + } + pub fn map_a(self, f: F) -> Either + where + F: FnOnce(A) -> U, + { + self.map(f, |it| it) + } + pub fn a(self) -> Option { + match self { + Either::A(it) => Some(it), + Either::B(_) => None, + } + } + pub fn b(self) -> Option { + match self { + Either::A(_) => None, + Either::B(it) => Some(it), + } + } + pub fn as_ref(&self) -> Either<&A, &B> { + match self { + Either::A(it) => Either::A(it), + Either::B(it) => Either::B(it), + } + } +} diff --git a/crates/ra_hir_expand/src/lib.rs b/crates/ra_hir_expand/src/lib.rs index 3c0ef8f1c..6359b2b4d 100644 --- a/crates/ra_hir_expand/src/lib.rs +++ b/crates/ra_hir_expand/src/lib.rs @@ -6,6 +6,7 @@ pub mod db; pub mod ast_id_map; +pub mod either; use std::hash::{Hash, Hasher}; -- cgit v1.2.3