Replace ra_hir_expand::either with crate

This commit is contained in:
ice1000 2019-12-03 11:07:56 -05:00
parent 15f143f0c3
commit 009437f5d9
23 changed files with 69 additions and 112 deletions

4
Cargo.lock generated
View File

@ -944,6 +944,7 @@ dependencies = [
name = "ra_hir"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_db 0.1.0",
"ra_hir_def 0.1.0",
@ -957,6 +958,7 @@ dependencies = [
name = "ra_hir_def"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"insta 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"once_cell 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -976,6 +978,7 @@ dependencies = [
name = "ra_hir_expand"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
"ra_arena 0.1.0",
"ra_db 0.1.0",
@ -1012,6 +1015,7 @@ dependencies = [
name = "ra_ide"
version = "0.1.0"
dependencies = [
"either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"format-buf 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"fst 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"insta 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -10,6 +10,7 @@ doctest = false
[dependencies]
log = "0.4.5"
rustc-hash = "1.0"
either = "1.5"
ra_syntax = { path = "../ra_syntax" }
ra_db = { path = "../ra_db" }

View File

@ -4,6 +4,7 @@
use std::sync::Arc;
use either::Either;
use hir_def::{
adt::VariantData,
body::{Body, BodySourceMap},
@ -30,7 +31,7 @@
db::{DefDatabase, HirDatabase},
ty::display::HirFormatter,
ty::{self, InEnvironment, InferenceResult, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk},
CallableDef, Either, HirDisplay, InFile, Name,
CallableDef, HirDisplay, InFile, Name,
};
/// hir::Crate describes a single crate. It's the main interface with which
@ -905,7 +906,9 @@ pub fn source(self, db: &impl HirDatabase) -> InFile<Either<ast::BindPat, ast::S
let (_body, source_map) = db.body_with_source_map(self.parent.into());
let src = source_map.pat_syntax(self.pat_id).unwrap(); // Hmm...
let root = src.file_syntax(db);
src.map(|ast| ast.map(|it| it.cast().unwrap().to_node(&root), |it| it.to_node(&root)))
src.map(|ast| {
ast.map_left(|it| it.cast().unwrap().to_node(&root)).map_right(|it| it.to_node(&root))
})
}
}

View File

@ -1,10 +1,10 @@
//! FIXME: write short doc here
use either::Either;
use hir_def::{
src::{HasChildSource, HasSource as _},
AstItemDef, Lookup, VariantId,
};
use hir_expand::either::Either;
use ra_syntax::ast;
use crate::{
@ -27,8 +27,8 @@ pub fn definition_source(self, db: &impl DefDatabase) -> InFile<ModuleSource> {
let def_map = db.crate_def_map(self.id.krate);
let src = def_map[self.id.local_id].definition_source(db);
src.map(|it| match it {
Either::A(it) => ModuleSource::SourceFile(it),
Either::B(it) => ModuleSource::Module(it),
Either::Left(it) => ModuleSource::SourceFile(it),
Either::Right(it) => ModuleSource::Module(it),
})
}
@ -46,8 +46,8 @@ fn source(self, db: &impl DefDatabase) -> InFile<FieldSource> {
let var = VariantId::from(self.parent);
let src = var.child_source(db);
src.map(|it| match it[self.id].clone() {
Either::A(it) => FieldSource::Pos(it),
Either::B(it) => FieldSource::Named(it),
Either::Left(it) => FieldSource::Pos(it),
Either::Right(it) => FieldSource::Named(it),
})
}
}
@ -126,6 +126,6 @@ fn source(self, db: &impl DefDatabase) -> InFile<Self::Ast> {
let (_, source_map) = db.raw_items_with_source_map(src.file_id);
let root = db.parse_or_expand(src.file_id).unwrap();
let ptr = source_map.get(self.id);
src.with_value(ptr.map(|it| it.to_node(&root), |it| it.to_node(&root)))
src.with_value(ptr.map_left(|it| it.to_node(&root)).map_right(|it| it.to_node(&root)))
}
}

View File

@ -63,5 +63,5 @@ fn from(it: $sv) -> $e {
type_ref::Mutability,
};
pub use hir_expand::{
either::Either, name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile,
name::Name, HirFileId, InFile, MacroCallId, MacroCallLoc, MacroDefId, MacroFile,
};

View File

@ -7,6 +7,7 @@
//! purely for "IDE needs".
use std::sync::Arc;
use either::Either;
use hir_def::{
body::{
scope::{ExprScopes, ScopeId},
@ -33,8 +34,8 @@
method_resolution::{self, implements_trait},
InEnvironment, TraitEnvironment, Ty,
},
Adt, AssocItem, Const, DefWithBody, Either, Enum, EnumVariant, FromSource, Function,
GenericParam, Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias,
Adt, AssocItem, Const, DefWithBody, Enum, EnumVariant, FromSource, Function, GenericParam,
Local, MacroDef, Name, Path, ScopeDef, Static, Struct, Trait, Type, TypeAlias,
};
fn try_get_resolver_for_node(db: &impl HirDatabase, node: InFile<&SyntaxNode>) -> Option<Resolver> {
@ -349,7 +350,7 @@ pub fn process_all_names(&self, db: &impl HirDatabase, f: &mut dyn FnMut(Name, S
// should switch to general reference search infra there.
pub fn find_all_refs(&self, pat: &ast::BindPat) -> Vec<ReferenceDescriptor> {
let fn_def = pat.syntax().ancestors().find_map(ast::FnDef::cast).unwrap();
let ptr = Either::A(AstPtr::new(&ast::Pat::from(pat.clone())));
let ptr = Either::Left(AstPtr::new(&ast::Pat::from(pat.clone())));
fn_def
.syntax()
.descendants()

View File

@ -11,6 +11,7 @@ doctest = false
log = "0.4.5"
once_cell = "1.0.1"
rustc-hash = "1.0"
either = "1.5"
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }

View File

@ -2,8 +2,8 @@
use std::sync::Arc;
use either::Either;
use hir_expand::{
either::Either,
name::{AsName, Name},
InFile,
};
@ -184,7 +184,7 @@ fn lower_struct(
ast::StructKind::Tuple(fl) => {
for (i, fd) in fl.fields().enumerate() {
trace.alloc(
|| Either::A(fd.clone()),
|| Either::Left(fd.clone()),
|| StructFieldData {
name: Name::new_tuple_field(i),
type_ref: TypeRef::from_ast_opt(fd.type_ref()),
@ -196,7 +196,7 @@ fn lower_struct(
ast::StructKind::Record(fl) => {
for fd in fl.fields() {
trace.alloc(
|| Either::B(fd.clone()),
|| Either::Right(fd.clone()),
|| StructFieldData {
name: fd.name().map(|n| n.as_name()).unwrap_or_else(Name::missing),
type_ref: TypeRef::from_ast_opt(fd.ascribed_type()),

View File

@ -2,7 +2,8 @@
use std::{ops, sync::Arc};
use hir_expand::{either::Either, hygiene::Hygiene, AstId, InFile};
use either::Either;
use hir_expand::{hygiene::Hygiene, AstId, InFile};
use mbe::ast_to_token_tree;
use ra_syntax::{
ast::{self, AstNode, AttrsOwner},
@ -45,8 +46,8 @@ pub(crate) fn attrs_query(db: &impl DefDatabase, def: AttrDefId) -> Attrs {
AttrDefId::StructFieldId(it) => {
let src = it.parent.child_source(db);
match &src.value[it.local_id] {
Either::A(_tuple) => Attrs::default(),
Either::B(record) => Attrs::from_attrs_owner(db, src.with_value(record)),
Either::Left(_tuple) => Attrs::default(),
Either::Right(record) => Attrs::from_attrs_owner(db, src.with_value(record)),
}
}
AttrDefId::EnumVariantId(var_id) => {

View File

@ -5,9 +5,8 @@
use std::{ops::Index, sync::Arc};
use hir_expand::{
either::Either, hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId, MacroFileKind,
};
use either::Either;
use hir_expand::{hygiene::Hygiene, AstId, HirFileId, InFile, MacroDefId, MacroFileKind};
use ra_arena::{map::ArenaMap, Arena};
use ra_syntax::{ast, AstNode, AstPtr};
use rustc_hash::FxHashMap;
@ -210,7 +209,7 @@ pub fn expr_syntax(&self, expr: ExprId) -> Option<ExprSource> {
}
pub fn node_expr(&self, node: InFile<&ast::Expr>) -> Option<ExprId> {
let src = node.map(|it| Either::A(AstPtr::new(it)));
let src = node.map(|it| Either::Left(AstPtr::new(it)));
self.expr_map.get(&src).cloned()
}
@ -219,7 +218,7 @@ pub fn pat_syntax(&self, pat: PatId) -> Option<PatSource> {
}
pub fn node_pat(&self, node: InFile<&ast::Pat>) -> Option<PatId> {
let src = node.map(|it| Either::A(AstPtr::new(it)));
let src = node.map(|it| Either::Left(AstPtr::new(it)));
self.pat_map.get(&src).cloned()
}

View File

@ -1,10 +1,8 @@
//! Transforms `ast::Expr` into an equivalent `hir_def::expr::Expr`
//! representation.
use hir_expand::{
either::Either,
name::{self, AsName, Name},
};
use either::Either;
use hir_expand::name::{self, AsName, Name};
use ra_arena::Arena;
use ra_syntax::{
ast::{
@ -74,7 +72,7 @@ fn collect(
mode: BindingAnnotation::Unannotated,
subpat: None,
},
Either::B(ptr),
Either::Right(ptr),
);
self.body.params.push(param_pat);
}
@ -94,7 +92,7 @@ fn collect(
}
fn alloc_expr(&mut self, expr: Expr, ptr: AstPtr<ast::Expr>) -> ExprId {
let ptr = Either::A(ptr);
let ptr = Either::Left(ptr);
let id = self.body.exprs.alloc(expr);
let src = self.expander.to_source(ptr);
self.source_map.expr_map.insert(src, id);
@ -107,7 +105,7 @@ fn alloc_expr_desugared(&mut self, expr: Expr) -> ExprId {
self.body.exprs.alloc(expr)
}
fn alloc_expr_field_shorthand(&mut self, expr: Expr, ptr: AstPtr<ast::RecordField>) -> ExprId {
let ptr = Either::B(ptr);
let ptr = Either::Right(ptr);
let id = self.body.exprs.alloc(expr);
let src = self.expander.to_source(ptr);
self.source_map.expr_map.insert(src, id);
@ -277,7 +275,7 @@ fn collect_expr(&mut self, expr: ast::Expr) -> ExprId {
ast::Expr::ParenExpr(e) => {
let inner = self.collect_expr_opt(e.expr());
// make the paren expr point to the inner expression as well
let src = self.expander.to_source(Either::A(syntax_ptr));
let src = self.expander.to_source(Either::Left(syntax_ptr));
self.source_map.expr_map.insert(src, inner);
inner
}
@ -550,7 +548,7 @@ fn collect_pat(&mut self, pat: ast::Pat) -> PatId {
ast::Pat::SlicePat(_) | ast::Pat::RangePat(_) => Pat::Missing,
};
let ptr = AstPtr::new(&pat);
self.alloc_pat(pattern, Either::A(ptr))
self.alloc_pat(pattern, Either::Left(ptr))
}
fn collect_pat_opt(&mut self, pat: Option<ast::Pat>) -> PatId {

View File

@ -5,7 +5,7 @@
use std::sync::Arc;
use hir_expand::either::Either;
use either::Either;
use ra_syntax::ast;
use crate::{
@ -46,8 +46,8 @@ pub(crate) fn documentation_query(
AttrDefId::StructFieldId(it) => {
let src = it.parent.child_source(db);
match &src.value[it.local_id] {
Either::A(_tuple) => None,
Either::B(record) => docs_from_ast(record),
Either::Left(_tuple) => None,
Either::Right(record) => docs_from_ast(record),
}
}
AttrDefId::AdtId(it) => match it {

View File

@ -57,9 +57,9 @@
use std::sync::Arc;
use either::Either;
use hir_expand::{
ast_id_map::FileAstId, diagnostics::DiagnosticSink, either::Either, name::Name, InFile,
MacroDefId,
ast_id_map::FileAstId, diagnostics::DiagnosticSink, name::Name, InFile, MacroDefId,
};
use once_cell::sync::Lazy;
use ra_arena::Arena;
@ -287,10 +287,10 @@ pub fn definition_source(
) -> InFile<Either<ast::SourceFile, ast::Module>> {
if let Some(file_id) = self.definition {
let sf = db.parse(file_id).tree();
return InFile::new(file_id.into(), Either::A(sf));
return InFile::new(file_id.into(), Either::Left(sf));
}
let decl = self.declaration.unwrap();
InFile::new(decl.file_id, Either::B(decl.to_node(db)))
InFile::new(decl.file_id, Either::Right(decl.to_node(db)))
}
/// Returns a node which declares this module, either a `mod foo;` or a `mod foo {}`.

View File

@ -7,10 +7,10 @@
use std::{ops::Index, sync::Arc};
use either::Either;
use hir_expand::{
ast_id_map::AstIdMap,
db::AstDatabase,
either::Either,
hygiene::Hygiene,
name::{AsName, Name},
};
@ -324,7 +324,7 @@ fn add_use_item(&mut self, current_module: Option<Module>, use_item: ast::UseIte
is_extern_crate: false,
is_macro_use: false,
};
buf.push((import_data, Either::A(AstPtr::new(use_tree))));
buf.push((import_data, Either::Left(AstPtr::new(use_tree))));
},
);
for (import_data, ptr) in buf {
@ -355,7 +355,7 @@ fn add_extern_crate_item(
current_module,
attrs,
import_data,
Either::B(AstPtr::new(&extern_crate)),
Either::Right(AstPtr::new(&extern_crate)),
);
}
}

View File

@ -2,8 +2,8 @@
use std::{iter, sync::Arc};
use either::Either;
use hir_expand::{
either::Either,
hygiene::Hygiene,
name::{self, AsName, Name},
};
@ -111,7 +111,7 @@ pub fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
ast::PathSegmentKind::Name(name_ref) => {
// FIXME: this should just return name
match hygiene.name_ref_to_name(name_ref) {
Either::A(name) => {
Either::Left(name) => {
let args = segment
.type_arg_list()
.and_then(GenericArgs::from_ast)
@ -125,7 +125,7 @@ pub fn from_src(mut path: ast::Path, hygiene: &Hygiene) -> Option<Path> {
let segment = PathSegment { name, args_and_bindings: args };
segments.push(segment);
}
Either::B(crate_id) => {
Either::Right(crate_id) => {
kind = PathKind::DollarCrate(crate_id);
break;
}
@ -347,7 +347,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
let res = match segment.kind()? {
ast::PathSegmentKind::Name(name_ref) => {
match hygiene.name_ref_to_name(name_ref) {
Either::A(name) => {
Either::Left(name) => {
// no type args in use
let mut res = prefix.unwrap_or_else(|| Path {
kind: PathKind::Plain,
@ -359,7 +359,7 @@ fn convert_path(prefix: Option<Path>, path: ast::Path, hygiene: &Hygiene) -> Opt
});
res
}
Either::B(crate_id) => {
Either::Right(crate_id) => {
return Some(Path::from_simple_segments(
PathKind::DollarCrate(crate_id),
iter::empty(),

View File

@ -9,6 +9,7 @@ doctest = false
[dependencies]
log = "0.4.5"
either = "1.5"
ra_arena = { path = "../ra_arena" }
ra_db = { path = "../ra_db" }

View File

@ -1,54 +0,0 @@
//! FIXME: write short doc here
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Either<A, B> {
A(A),
B(B),
}
impl<A, B> Either<A, B> {
pub fn either<R, F1, F2>(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<U, V, F1, F2>(self, f1: F1, f2: F2) -> Either<U, V>
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<U, F>(self, f: F) -> Either<U, B>
where
F: FnOnce(A) -> U,
{
self.map(f, |it| it)
}
pub fn a(self) -> Option<A> {
match self {
Either::A(it) => Some(it),
Either::B(_) => None,
}
}
pub fn b(self) -> Option<B> {
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),
}
}
}

View File

@ -2,12 +2,12 @@
//!
//! Specifically, `ast` + `Hygiene` allows you to create a `Name`. Note that, at
//! this moment, this is horribly incomplete and handles only `$crate`.
use either::Either;
use ra_db::CrateId;
use ra_syntax::ast;
use crate::{
db::AstDatabase,
either::Either,
name::{AsName, Name},
HirFileId, HirFileIdRepr, MacroDefKind,
};
@ -41,9 +41,9 @@ pub fn new_unhygienic() -> Hygiene {
pub fn name_ref_to_name(&self, name_ref: ast::NameRef) -> Either<Name, CrateId> {
if let Some(def_crate) = self.def_crate {
if name_ref.text() == "$crate" {
return Either::B(def_crate);
return Either::Right(def_crate);
}
}
Either::A(name_ref.as_name())
Either::Left(name_ref.as_name())
}
}

View File

@ -6,7 +6,6 @@
pub mod db;
pub mod ast_id_map;
pub mod either;
pub mod name;
pub mod hygiene;
pub mod diagnostics;

View File

@ -97,7 +97,7 @@ fn validate_record_literal(
let (_, source_map) = db.body_with_source_map(self.func.into());
if let Some(source_ptr) = source_map.expr_syntax(id) {
if let Some(expr) = source_ptr.value.a() {
if let Some(expr) = source_ptr.value.left() {
let root = source_ptr.file_syntax(db);
if let ast::Expr::RecordLit(record_lit) = expr.to_node(&root) {
if let Some(field_list) = record_lit.record_field_list() {
@ -142,7 +142,7 @@ fn validate_results_in_tail_expr(
let (_, source_map) = db.body_with_source_map(self.func.into());
if let Some(source_ptr) = source_map.expr_syntax(id) {
if let Some(expr) = source_ptr.value.a() {
if let Some(expr) = source_ptr.value.left() {
self.sink.push(MissingOkInTailExpr { file: source_ptr.file_id, expr });
}
}

View File

@ -11,6 +11,7 @@ doctest = false
wasm = []
[dependencies]
either = "1.5"
format-buf = "1.0.0"
itertools = "0.8.0"
join_to_string = "0.1.3"

View File

@ -1,6 +1,7 @@
//! FIXME: write short doc here
use hir::{Adt, Either, HasSource, PathResolution};
use either::Either;
use hir::{Adt, HasSource, PathResolution};
use ra_syntax::AstNode;
use test_utils::tested_by;
@ -27,7 +28,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
}
if Some(module) == ctx.module {
if let Some(import) = import {
if let Either::A(use_tree) = import.source(ctx.db).value {
if let Either::Left(use_tree) = import.source(ctx.db).value {
if use_tree.syntax().text_range().contains_inclusive(ctx.offset) {
// for `use self::foo<|>`, don't suggest `foo` as a completion
tested_by!(dont_complete_current_use);

View File

@ -1,6 +1,7 @@
//! FIXME: write short doc here
use hir::{AssocItem, Either, FieldSource, HasSource, InFile, ModuleSource};
use either::Either;
use hir::{AssocItem, FieldSource, HasSource, InFile, ModuleSource};
use ra_db::{FileId, SourceDatabase};
use ra_syntax::{
ast::{self, DocCommentsOwner, NameOwner},
@ -342,10 +343,10 @@ impl ToNav for hir::Local {
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
let src = self.source(db);
let (full_range, focus_range) = match src.value {
Either::A(it) => {
Either::Left(it) => {
(it.syntax().text_range(), it.name().map(|it| it.syntax().text_range()))
}
Either::B(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())),
Either::Right(it) => (it.syntax().text_range(), Some(it.self_kw_token().text_range())),
};
let name = match self.name(db) {
Some(it) => it.to_string().into(),