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 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<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.
|
||||
|
|
|
@ -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())
|
||||
|
|
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;
|
||||
#[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},
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,26 +34,13 @@ pub struct ImportSourceMap {
|
|||
map: ArenaMap<ImportId, ImportSourcePtr>,
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||
enum ImportSourcePtr {
|
||||
UseTree(AstPtr<ast::UseTree>),
|
||||
ExternCrate(AstPtr<ast::ExternCrateItem>),
|
||||
}
|
||||
type ImportSourcePtr = Either<AstPtr<ast::UseTree>, AstPtr<ast::ExternCrateItem>>;
|
||||
type ImportSource = Either<TreeArc<ast::UseTree>, TreeArc<ast::ExternCrateItem>>;
|
||||
|
||||
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<ast::UseTree>),
|
||||
ExternCrate(TreeArc<ast::ExternCrateItem>),
|
||||
}
|
||||
|
||||
impl ImportSourceMap {
|
||||
|
@ -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)));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue