mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 09:27:27 +00:00
use either for uses
This commit is contained in:
parent
b863272899
commit
1cd184d653
6 changed files with 40 additions and 44 deletions
|
@ -4,7 +4,7 @@ use ra_db::{CrateId, SourceRootId, Edition};
|
||||||
use ra_syntax::{ast::self, TreeArc};
|
use ra_syntax::{ast::self, TreeArc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Name, ScopesWithSourceMap, Ty, HirFileId, ImportSource,
|
Name, ScopesWithSourceMap, Ty, HirFileId, Either,
|
||||||
HirDatabase, DefDatabase,
|
HirDatabase, DefDatabase,
|
||||||
type_ref::TypeRef,
|
type_ref::TypeRef,
|
||||||
nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
|
nameres::{ModuleScope, Namespace, ImportId, CrateModuleId},
|
||||||
|
@ -117,8 +117,14 @@ impl Module {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the syntax of the last path segment corresponding to this import
|
/// Returns the syntax of the last path segment corresponding to this import
|
||||||
pub fn import_source(&self, db: &impl HirDatabase, import: ImportId) -> ImportSource {
|
pub fn import_source(
|
||||||
self.import_source_impl(db, import)
|
&self,
|
||||||
|
db: &impl HirDatabase,
|
||||||
|
import: ImportId,
|
||||||
|
) -> Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>> {
|
||||||
|
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.
|
/// Returns the crate this module is part of.
|
||||||
|
|
|
@ -3,9 +3,9 @@ use ra_syntax::{ast, TreeArc};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
Module, ModuleSource, Name, AstId,
|
Module, ModuleSource, Name, AstId,
|
||||||
nameres::{CrateModuleId, ImportId},
|
nameres::CrateModuleId,
|
||||||
HirDatabase, DefDatabase,
|
HirDatabase, DefDatabase,
|
||||||
HirFileId, ImportSource,
|
HirFileId,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl ModuleSource {
|
impl ModuleSource {
|
||||||
|
@ -68,16 +68,6 @@ impl Module {
|
||||||
Some((decl.file_id(), ast))
|
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 {
|
pub(crate) fn crate_root_impl(&self, db: &impl DefDatabase) -> Module {
|
||||||
let def_map = db.crate_def_map(self.krate);
|
let def_map = db.crate_def_map(self.krate);
|
||||||
self.with_module_id(def_map.root())
|
self.with_module_id(def_map.root())
|
||||||
|
|
18
crates/ra_hir/src/either.rs
Normal file
18
crates/ra_hir/src/either.rs
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
#[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 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)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,8 @@ macro_rules! impl_froms {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod either;
|
||||||
|
|
||||||
pub mod db;
|
pub mod db;
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod mock;
|
pub mod mock;
|
||||||
|
@ -52,11 +54,12 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
|
either::Either,
|
||||||
path::{Path, PathKind},
|
path::{Path, PathKind},
|
||||||
name::Name,
|
name::Name,
|
||||||
source_id::{AstIdMap, ErasedFileAstId},
|
source_id::{AstIdMap, ErasedFileAstId},
|
||||||
ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc},
|
ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc},
|
||||||
nameres::{PerNs, Namespace, ImportId, ImportSource},
|
nameres::{PerNs, Namespace, ImportId},
|
||||||
ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},
|
ty::{Ty, ApplicationTy, TypeCtor, Substs, display::HirDisplay},
|
||||||
impl_block::{ImplBlock, ImplItem},
|
impl_block::{ImplBlock, ImplItem},
|
||||||
docs::{Docs, Documentation},
|
docs::{Docs, Documentation},
|
||||||
|
|
|
@ -75,7 +75,7 @@ pub(crate) use self::raw::{RawItems, ImportSourceMap};
|
||||||
|
|
||||||
pub use self::{
|
pub use self::{
|
||||||
per_ns::{PerNs, Namespace},
|
per_ns::{PerNs, Namespace},
|
||||||
raw::{ImportId, ImportSource},
|
raw::ImportId,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Contans all top-level defs from a macro-expanded crate
|
/// Contans all top-level defs from a macro-expanded crate
|
||||||
|
|
|
@ -12,7 +12,7 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
DefDatabase, Name, AsName, Path, HirFileId, ModuleSource,
|
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).
|
/// `RawItems` is a set of top-level items in a file (except for impls).
|
||||||
|
@ -34,26 +34,13 @@ pub struct ImportSourceMap {
|
||||||
map: ArenaMap<ImportId, ImportSourcePtr>,
|
map: ArenaMap<ImportId, ImportSourcePtr>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
|
||||||
enum ImportSourcePtr {
|
type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>;
|
||||||
UseTree(AstPtr<ast::UseTree>),
|
|
||||||
ExternCrate(AstPtr<ast::ExternCrateItem>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ImportSourcePtr {
|
impl ImportSourcePtr {
|
||||||
fn to_node(self, file: &SourceFile) -> ImportSource {
|
fn to_node(self, file: &SourceFile) -> ImportSource {
|
||||||
match self {
|
self.map(|ptr| ptr.to_node(file).to_owned(), |ptr| ptr.to_node(file).to_owned())
|
||||||
ImportSourcePtr::UseTree(ptr) => ImportSource::UseTree(ptr.to_node(file).to_owned()),
|
|
||||||
ImportSourcePtr::ExternCrate(ptr) => {
|
|
||||||
ImportSource::ExternCrate(ptr.to_node(file).to_owned())
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum ImportSource {
|
|
||||||
UseTree(TreeArc<ast::UseTree>),
|
|
||||||
ExternCrate(TreeArc<ast::ExternCrateItem>),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ImportSourceMap {
|
impl ImportSourceMap {
|
||||||
|
@ -281,11 +268,7 @@ impl RawItemsCollector {
|
||||||
Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| {
|
Path::expand_use_item(use_item, |path, use_tree, is_glob, alias| {
|
||||||
let import_data =
|
let import_data =
|
||||||
ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false };
|
ImportData { path, alias, is_glob, is_prelude, is_extern_crate: false };
|
||||||
self.push_import(
|
self.push_import(current_module, import_data, Either::A(AstPtr::new(use_tree)));
|
||||||
current_module,
|
|
||||||
import_data,
|
|
||||||
ImportSourcePtr::UseTree(AstPtr::new(use_tree)),
|
|
||||||
);
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -304,11 +287,7 @@ impl RawItemsCollector {
|
||||||
is_prelude: false,
|
is_prelude: false,
|
||||||
is_extern_crate: true,
|
is_extern_crate: true,
|
||||||
};
|
};
|
||||||
self.push_import(
|
self.push_import(current_module, import_data, Either::B(AstPtr::new(extern_crate)));
|
||||||
current_module,
|
|
||||||
import_data,
|
|
||||||
ImportSourcePtr::ExternCrate(AstPtr::new(extern_crate)),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue