mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 23:24:29 +00:00
Merge #254
254: Defids r=matklad a=matklad Fleshing out DefIds some more Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
84f2509730
12 changed files with 187 additions and 161 deletions
|
@ -163,7 +163,7 @@ fn complete_path(
|
|||
};
|
||||
let target_module = match def_id.resolve(db)? {
|
||||
Def::Module(it) => it,
|
||||
Def::Item => return Ok(()),
|
||||
_ => return Ok(()),
|
||||
};
|
||||
let module_scope = target_module.scope(db)?;
|
||||
let completions = module_scope.entries().map(|(name, _res)| CompletionItem {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::sync::Arc;
|
||||
use salsa::{self, Database};
|
||||
use ra_db::{LocationIntener, BaseDatabase};
|
||||
use hir::{self, DefId, DefLoc, FnId, SourceItemId};
|
||||
use hir::{self, DefId, DefLoc};
|
||||
|
||||
use crate::{
|
||||
symbol_index,
|
||||
|
@ -15,7 +15,6 @@ pub(crate) struct RootDatabase {
|
|||
|
||||
#[derive(Debug, Default)]
|
||||
struct IdMaps {
|
||||
fns: LocationIntener<SourceItemId, FnId>,
|
||||
defs: LocationIntener<DefLoc, DefId>,
|
||||
}
|
||||
|
||||
|
@ -58,12 +57,6 @@ impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase {
|
|||
}
|
||||
}
|
||||
|
||||
impl AsRef<LocationIntener<hir::SourceItemId, FnId>> for RootDatabase {
|
||||
fn as_ref(&self) -> &LocationIntener<hir::SourceItemId, FnId> {
|
||||
&self.id_maps.fns
|
||||
}
|
||||
}
|
||||
|
||||
salsa::database_storage! {
|
||||
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
||||
impl ra_db::FilesDatabase {
|
||||
|
|
|
@ -190,10 +190,7 @@ impl AnalysisImpl {
|
|||
Some(it) => it,
|
||||
};
|
||||
let root = descr.crate_root();
|
||||
let file_id = root
|
||||
.source()
|
||||
.as_file()
|
||||
.expect("root module always has a file as a source");
|
||||
let file_id = root.source().file_id();
|
||||
|
||||
let crate_graph = self.db.crate_graph();
|
||||
let crate_id = crate_graph.crate_id_for_crate_root(file_id);
|
||||
|
@ -213,7 +210,7 @@ impl AnalysisImpl {
|
|||
let syntax = file.syntax();
|
||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
|
||||
if let Some(fn_descr) =
|
||||
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)
|
||||
hir::Function::guess_for_name_ref(&*self.db, position.file_id, name_ref)?
|
||||
{
|
||||
let scope = fn_descr.scope(&*self.db);
|
||||
// First try to resolve the symbol locally
|
||||
|
@ -260,11 +257,11 @@ impl AnalysisImpl {
|
|||
Ok(vec![])
|
||||
}
|
||||
|
||||
pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
|
||||
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
|
||||
let file = self.db.source_file(position.file_id);
|
||||
// Find the binding associated with the offset
|
||||
let (binding, descr) = match find_binding(&self.db, &file, position) {
|
||||
None => return Vec::new(),
|
||||
let (binding, descr) = match find_binding(&self.db, &file, position)? {
|
||||
None => return Ok(Vec::new()),
|
||||
Some(it) => it,
|
||||
};
|
||||
|
||||
|
@ -277,25 +274,36 @@ impl AnalysisImpl {
|
|||
.map(|ref_desc| (position.file_id, ref_desc.range)),
|
||||
);
|
||||
|
||||
return ret;
|
||||
return Ok(ret);
|
||||
|
||||
fn find_binding<'a>(
|
||||
db: &db::RootDatabase,
|
||||
source_file: &'a SourceFileNode,
|
||||
position: FilePosition,
|
||||
) -> Option<(ast::BindPat<'a>, hir::Function)> {
|
||||
) -> Cancelable<Option<(ast::BindPat<'a>, hir::Function)>> {
|
||||
let syntax = source_file.syntax();
|
||||
if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) {
|
||||
let descr = hir::Function::guess_for_bind_pat(db, position.file_id, binding)?;
|
||||
return Some((binding, descr));
|
||||
let descr = ctry!(hir::Function::guess_for_bind_pat(
|
||||
db,
|
||||
position.file_id,
|
||||
binding
|
||||
)?);
|
||||
return Ok(Some((binding, descr)));
|
||||
};
|
||||
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
|
||||
let descr = hir::Function::guess_for_name_ref(db, position.file_id, name_ref)?;
|
||||
let name_ref = ctry!(find_node_at_offset::<ast::NameRef>(syntax, position.offset));
|
||||
let descr = ctry!(hir::Function::guess_for_name_ref(
|
||||
db,
|
||||
position.file_id,
|
||||
name_ref
|
||||
)?);
|
||||
let scope = descr.scope(db);
|
||||
let resolved = scope.resolve_local_name(name_ref)?;
|
||||
let resolved = ctry!(scope.resolve_local_name(name_ref));
|
||||
let resolved = resolved.ptr().resolve(source_file);
|
||||
let binding = find_node_at_offset::<ast::BindPat>(syntax, resolved.range().end())?;
|
||||
Some((binding, descr))
|
||||
let binding = ctry!(find_node_at_offset::<ast::BindPat>(
|
||||
syntax,
|
||||
resolved.range().end()
|
||||
));
|
||||
Ok(Some((binding, descr)))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -411,7 +419,9 @@ impl AnalysisImpl {
|
|||
if fs.kind == FN_DEF {
|
||||
let fn_file = self.db.source_file(fn_file_id);
|
||||
if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
|
||||
let descr = hir::Function::guess_from_source(&*self.db, fn_file_id, fn_def);
|
||||
let descr = ctry!(hir::Function::guess_from_source(
|
||||
&*self.db, fn_file_id, fn_def
|
||||
)?);
|
||||
if let Some(descriptor) = descr.signature_info(&*self.db) {
|
||||
// If we have a calling expression let's find which argument we are on
|
||||
let mut current_parameter = None;
|
||||
|
|
|
@ -248,7 +248,7 @@ impl Analysis {
|
|||
self.imp.approximately_resolve_symbol(position)
|
||||
}
|
||||
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
|
||||
Ok(self.imp.find_all_refs(position))
|
||||
self.imp.find_all_refs(position)
|
||||
}
|
||||
pub fn doc_comment_for(
|
||||
&self,
|
||||
|
|
|
@ -7,10 +7,11 @@ use ra_syntax::{
|
|||
use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase, FileId, Cancelable};
|
||||
|
||||
use crate::{
|
||||
DefLoc, DefId, FnId,
|
||||
DefLoc, DefId,
|
||||
SourceFileItems, SourceItemId,
|
||||
query_definitions,
|
||||
FnScopes,
|
||||
function::FnId,
|
||||
module::{ModuleId, ModuleTree, ModuleSource,
|
||||
nameres::{ItemMap, InputModuleItems}},
|
||||
};
|
||||
|
@ -19,7 +20,6 @@ salsa::query_group! {
|
|||
|
||||
pub trait HirDatabase: SyntaxDatabase
|
||||
+ AsRef<LocationIntener<DefLoc, DefId>>
|
||||
+ AsRef<LocationIntener<SourceItemId, FnId>>
|
||||
{
|
||||
fn fn_scopes(fn_id: FnId) -> Arc<FnScopes> {
|
||||
type FnScopesQuery;
|
||||
|
|
|
@ -12,39 +12,49 @@ use ra_syntax::{
|
|||
use ra_db::FileId;
|
||||
|
||||
use crate::{
|
||||
FnId, HirDatabase, SourceItemId,
|
||||
Cancelable,
|
||||
DefLoc, DefKind, DefId, HirDatabase, SourceItemId,
|
||||
Module,
|
||||
};
|
||||
|
||||
pub use self::scope::FnScopes;
|
||||
|
||||
impl FnId {
|
||||
pub fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
|
||||
let file_items = db.file_items(file_id);
|
||||
let item_id = file_items.id_of(fn_def.syntax());
|
||||
let item_id = SourceItemId { file_id, item_id };
|
||||
FnId::from_loc(db, &item_id)
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct FnId(pub(crate) DefId);
|
||||
|
||||
pub struct Function {
|
||||
fn_id: FnId,
|
||||
}
|
||||
|
||||
impl Function {
|
||||
pub(crate) fn new(def_id: DefId) -> Function {
|
||||
let fn_id = FnId(def_id);
|
||||
Function { fn_id }
|
||||
}
|
||||
|
||||
pub fn guess_from_source(
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
fn_def: ast::FnDef,
|
||||
) -> Function {
|
||||
let fn_id = FnId::get(db, file_id, fn_def);
|
||||
Function { fn_id }
|
||||
) -> Cancelable<Option<Function>> {
|
||||
let module = ctry!(Module::guess_from_child_node(db, file_id, fn_def.syntax())?);
|
||||
let file_items = db.file_items(file_id);
|
||||
let item_id = file_items.id_of(fn_def.syntax());
|
||||
let source_item_id = SourceItemId { file_id, item_id };
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Function,
|
||||
source_root_id: module.source_root_id,
|
||||
module_id: module.module_id,
|
||||
source_item_id,
|
||||
};
|
||||
Ok(Some(Function::new(def_loc.id(db))))
|
||||
}
|
||||
|
||||
pub fn guess_for_name_ref(
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
name_ref: ast::NameRef,
|
||||
) -> Option<Function> {
|
||||
) -> Cancelable<Option<Function>> {
|
||||
Function::guess_for_node(db, file_id, name_ref.syntax())
|
||||
}
|
||||
|
||||
|
@ -52,7 +62,7 @@ impl Function {
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
bind_pat: ast::BindPat,
|
||||
) -> Option<Function> {
|
||||
) -> Cancelable<Option<Function>> {
|
||||
Function::guess_for_node(db, file_id, bind_pat.syntax())
|
||||
}
|
||||
|
||||
|
@ -60,10 +70,9 @@ impl Function {
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
node: SyntaxNodeRef,
|
||||
) -> Option<Function> {
|
||||
let fn_def = node.ancestors().find_map(ast::FnDef::cast)?;
|
||||
let res = Function::guess_from_source(db, file_id, fn_def);
|
||||
Some(res)
|
||||
) -> Cancelable<Option<Function>> {
|
||||
let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast));
|
||||
Function::guess_from_source(db, file_id, fn_def)
|
||||
}
|
||||
|
||||
pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> {
|
||||
|
|
|
@ -41,63 +41,58 @@ pub use self::{
|
|||
|
||||
pub use self::function::FnSignatureInfo;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct FnId(u32);
|
||||
ra_db::impl_numeric_id!(FnId);
|
||||
|
||||
impl FnId {
|
||||
pub fn from_loc(
|
||||
db: &impl AsRef<LocationIntener<SourceItemId, FnId>>,
|
||||
loc: &SourceItemId,
|
||||
) -> FnId {
|
||||
db.as_ref().loc2id(loc)
|
||||
}
|
||||
pub fn loc(self, db: &impl AsRef<LocationIntener<SourceItemId, FnId>>) -> SourceItemId {
|
||||
db.as_ref().id2loc(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
|
||||
/// in a specific module.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub struct DefId(u32);
|
||||
ra_db::impl_numeric_id!(DefId);
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub(crate) enum DefKind {
|
||||
Module,
|
||||
Function,
|
||||
Item,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum DefLoc {
|
||||
Module {
|
||||
id: ModuleId,
|
||||
source_root: SourceRootId,
|
||||
},
|
||||
Item {
|
||||
source_item_id: SourceItemId,
|
||||
},
|
||||
pub struct DefLoc {
|
||||
pub(crate) kind: DefKind,
|
||||
source_root_id: SourceRootId,
|
||||
module_id: ModuleId,
|
||||
source_item_id: SourceItemId,
|
||||
}
|
||||
|
||||
impl DefId {
|
||||
pub fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
|
||||
pub(crate) fn loc(self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefLoc {
|
||||
db.as_ref().id2loc(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl DefLoc {
|
||||
pub fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
|
||||
pub(crate) fn id(&self, db: &impl AsRef<LocationIntener<DefLoc, DefId>>) -> DefId {
|
||||
db.as_ref().loc2id(&self)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Def {
|
||||
Module(Module),
|
||||
Function(Function),
|
||||
Item,
|
||||
}
|
||||
|
||||
impl DefId {
|
||||
pub fn resolve(self, db: &impl HirDatabase) -> Cancelable<Def> {
|
||||
let loc = self.loc(db);
|
||||
let res = match loc {
|
||||
DefLoc::Module { id, source_root } => {
|
||||
let descr = Module::new(db, source_root, id)?;
|
||||
Def::Module(descr)
|
||||
let res = match loc.kind {
|
||||
DefKind::Module => {
|
||||
let module = Module::new(db, loc.source_root_id, loc.module_id)?;
|
||||
Def::Module(module)
|
||||
}
|
||||
DefLoc::Item { .. } => Def::Item,
|
||||
DefKind::Function => {
|
||||
let function = Function::new(self);
|
||||
Def::Function(function)
|
||||
}
|
||||
DefKind::Item => Def::Item,
|
||||
};
|
||||
Ok(res)
|
||||
}
|
||||
|
@ -131,6 +126,10 @@ impl SourceFileItems {
|
|||
.unwrap();
|
||||
id
|
||||
}
|
||||
pub fn id_of_source_file(&self) -> SourceFileItemId {
|
||||
let (id, _syntax) = self.arena.iter().next().unwrap();
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
impl Index<SourceFileItemId> for SourceFileItems {
|
||||
|
|
|
@ -6,7 +6,7 @@ use ra_db::{LocationIntener, BaseDatabase, FilePosition, mock::FileMap, FileId,
|
|||
use relative_path::RelativePathBuf;
|
||||
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
|
||||
|
||||
use crate::{db, DefId, DefLoc, FnId, SourceItemId};
|
||||
use crate::{db, DefId, DefLoc};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct MockDatabase {
|
||||
|
@ -65,7 +65,6 @@ impl MockDatabase {
|
|||
|
||||
#[derive(Debug, Default)]
|
||||
struct IdMaps {
|
||||
fns: LocationIntener<SourceItemId, FnId>,
|
||||
defs: LocationIntener<DefLoc, DefId>,
|
||||
}
|
||||
|
||||
|
@ -117,12 +116,6 @@ impl AsRef<LocationIntener<DefLoc, DefId>> for MockDatabase {
|
|||
}
|
||||
}
|
||||
|
||||
impl AsRef<LocationIntener<SourceItemId, FnId>> for MockDatabase {
|
||||
fn as_ref(&self) -> &LocationIntener<SourceItemId, FnId> {
|
||||
&self.id_maps.fns
|
||||
}
|
||||
}
|
||||
|
||||
impl MockDatabase {
|
||||
pub(crate) fn log(&self, f: impl FnOnce()) -> Vec<salsa::Event<MockDatabase>> {
|
||||
*self.events.lock() = Some(Vec::new());
|
||||
|
|
|
@ -66,7 +66,7 @@ fn create_module_tree<'a>(
|
|||
|
||||
let source_root = db.source_root(source_root);
|
||||
for &file_id in source_root.files.iter() {
|
||||
let source = ModuleSource::SourceFile(file_id);
|
||||
let source = ModuleSource::new_file(db, file_id);
|
||||
if visited.contains(&source) {
|
||||
continue; // TODO: use explicit crate_roots here
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ fn build_subtree(
|
|||
visited,
|
||||
roots,
|
||||
Some(link),
|
||||
ModuleSource::SourceFile(file_id),
|
||||
ModuleSource::new_file(db, file_id),
|
||||
),
|
||||
})
|
||||
.collect::<Cancelable<Vec<_>>>()?;
|
||||
|
@ -157,13 +157,8 @@ fn resolve_submodule(
|
|||
name: &SmolStr,
|
||||
file_resolver: &FileResolverImp,
|
||||
) -> (Vec<FileId>, Option<Problem>) {
|
||||
let file_id = match source {
|
||||
ModuleSource::SourceFile(it) => it,
|
||||
ModuleSource::Module(..) => {
|
||||
// TODO
|
||||
return (Vec::new(), None);
|
||||
}
|
||||
};
|
||||
// TODO: handle submodules of inline modules properly
|
||||
let file_id = source.file_id();
|
||||
let mod_name = file_resolver.file_stem(file_id);
|
||||
let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main";
|
||||
|
||||
|
|
|
@ -8,13 +8,13 @@ use ra_editor::find_node_at_offset;
|
|||
use ra_syntax::{
|
||||
algo::generate,
|
||||
ast::{self, AstNode, NameOwner},
|
||||
SmolStr, SyntaxNode,
|
||||
SmolStr, SyntaxNode, SyntaxNodeRef,
|
||||
};
|
||||
use ra_db::{SourceRootId, FileId, FilePosition, Cancelable};
|
||||
use relative_path::RelativePathBuf;
|
||||
|
||||
use crate::{
|
||||
DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId,
|
||||
DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId,
|
||||
arena::{Arena, Id},
|
||||
};
|
||||
|
||||
|
@ -25,8 +25,8 @@ pub use self::nameres::ModuleScope;
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Module {
|
||||
tree: Arc<ModuleTree>,
|
||||
source_root_id: SourceRootId,
|
||||
module_id: ModuleId,
|
||||
pub(crate) source_root_id: SourceRootId,
|
||||
pub(crate) module_id: ModuleId,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
|
@ -37,7 +37,8 @@ impl Module {
|
|||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id))
|
||||
let module_source = ModuleSource::new_file(db, file_id);
|
||||
Module::guess_from_source(db, module_source)
|
||||
}
|
||||
|
||||
/// Lookup `Module` by position in the source code. Note that this
|
||||
|
@ -51,17 +52,33 @@ impl Module {
|
|||
let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset)
|
||||
{
|
||||
Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m),
|
||||
_ => ModuleSource::SourceFile(position.file_id),
|
||||
_ => ModuleSource::new_file(db, position.file_id),
|
||||
};
|
||||
Module::guess_from_source(db, position.file_id, module_source)
|
||||
Module::guess_from_source(db, module_source)
|
||||
}
|
||||
|
||||
pub fn guess_from_child_node(
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
node: SyntaxNodeRef,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
let module_source = if let Some(m) = node
|
||||
.ancestors()
|
||||
.filter_map(ast::Module::cast)
|
||||
.find(|it| !it.has_semi())
|
||||
{
|
||||
ModuleSource::new_inline(db, file_id, m)
|
||||
} else {
|
||||
ModuleSource::new_file(db, file_id)
|
||||
};
|
||||
Module::guess_from_source(db, module_source)
|
||||
}
|
||||
|
||||
fn guess_from_source(
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
module_source: ModuleSource,
|
||||
) -> Cancelable<Option<Module>> {
|
||||
let source_root_id = db.file_source_root(file_id);
|
||||
let source_root_id = db.file_source_root(module_source.file_id());
|
||||
let module_tree = db.module_tree(source_root_id)?;
|
||||
|
||||
let res = match module_tree.any_module_for_source(module_source) {
|
||||
|
@ -127,9 +144,11 @@ impl Module {
|
|||
}
|
||||
|
||||
pub fn def_id(&self, db: &impl HirDatabase) -> DefId {
|
||||
let def_loc = DefLoc::Module {
|
||||
id: self.module_id,
|
||||
source_root: self.source_root_id,
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Module,
|
||||
source_root_id: self.source_root_id,
|
||||
module_id: self.module_id,
|
||||
source_item_id: self.module_id.source(&self.tree).0,
|
||||
};
|
||||
def_loc.id(db)
|
||||
}
|
||||
|
@ -161,7 +180,12 @@ impl Module {
|
|||
let segments = path.segments;
|
||||
for name in segments.iter() {
|
||||
let module = match curr.loc(db) {
|
||||
DefLoc::Module { id, source_root } => Module::new(db, source_root, id)?,
|
||||
DefLoc {
|
||||
kind: DefKind::Module,
|
||||
source_root_id,
|
||||
module_id,
|
||||
..
|
||||
} => Module::new(db, source_root_id, module_id)?,
|
||||
_ => return Ok(None),
|
||||
};
|
||||
let scope = module.scope(db)?;
|
||||
|
@ -209,10 +233,7 @@ impl ModuleTree {
|
|||
/// `ModuleSource` is the syntax tree element that produced this module:
|
||||
/// either a file, or an inlinde module.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum ModuleSource {
|
||||
SourceFile(FileId),
|
||||
Module(SourceItemId),
|
||||
}
|
||||
pub struct ModuleSource(SourceItemId);
|
||||
|
||||
/// An owned syntax node for a module. Unlike `ModuleSource`,
|
||||
/// this holds onto the AST for the whole file.
|
||||
|
@ -310,45 +331,41 @@ pub struct ModuleData {
|
|||
}
|
||||
|
||||
impl ModuleSource {
|
||||
// precondition: item_id **must** point to module
|
||||
fn new(file_id: FileId, item_id: SourceFileItemId) -> ModuleSource {
|
||||
let source_item_id = SourceItemId { file_id, item_id };
|
||||
ModuleSource(source_item_id)
|
||||
}
|
||||
|
||||
pub(crate) fn new_file(db: &impl HirDatabase, file_id: FileId) -> ModuleSource {
|
||||
let file_items = db.file_items(file_id);
|
||||
let item_id = file_items.id_of_source_file();
|
||||
ModuleSource::new(file_id, item_id)
|
||||
}
|
||||
|
||||
pub(crate) fn new_inline(
|
||||
db: &impl HirDatabase,
|
||||
file_id: FileId,
|
||||
module: ast::Module,
|
||||
m: ast::Module,
|
||||
) -> ModuleSource {
|
||||
assert!(!module.has_semi());
|
||||
let items = db.file_items(file_id);
|
||||
let item_id = items.id_of(module.syntax());
|
||||
let id = SourceItemId { file_id, item_id };
|
||||
ModuleSource::Module(id)
|
||||
}
|
||||
|
||||
pub fn as_file(self) -> Option<FileId> {
|
||||
match self {
|
||||
ModuleSource::SourceFile(f) => Some(f),
|
||||
ModuleSource::Module(..) => None,
|
||||
}
|
||||
assert!(!m.has_semi());
|
||||
let file_items = db.file_items(file_id);
|
||||
let item_id = file_items.id_of(m.syntax());
|
||||
ModuleSource::new(file_id, item_id)
|
||||
}
|
||||
|
||||
pub fn file_id(self) -> FileId {
|
||||
match self {
|
||||
ModuleSource::SourceFile(f) => f,
|
||||
ModuleSource::Module(source_item_id) => source_item_id.file_id,
|
||||
}
|
||||
self.0.file_id
|
||||
}
|
||||
|
||||
pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode {
|
||||
match self {
|
||||
ModuleSource::SourceFile(file_id) => {
|
||||
let syntax = db.source_file(file_id);
|
||||
ModuleSourceNode::SourceFile(syntax.ast().owned())
|
||||
}
|
||||
ModuleSource::Module(item_id) => {
|
||||
let syntax = db.file_item(item_id);
|
||||
let syntax = syntax.borrowed();
|
||||
let module = ast::Module::cast(syntax).unwrap();
|
||||
ModuleSourceNode::Module(module.owned())
|
||||
}
|
||||
let syntax_node = db.file_item(self.0);
|
||||
let syntax_node = syntax_node.borrowed();
|
||||
if let Some(file) = ast::SourceFile::cast(syntax_node) {
|
||||
return ModuleSourceNode::SourceFile(file.owned());
|
||||
}
|
||||
let module = ast::Module::cast(syntax_node).unwrap();
|
||||
ModuleSourceNode::Module(module.owned())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ use ra_db::SourceRootId;
|
|||
|
||||
use crate::{
|
||||
Cancelable, FileId,
|
||||
DefId, DefLoc,
|
||||
DefId, DefLoc, DefKind,
|
||||
SourceItemId, SourceFileItemId, SourceFileItems,
|
||||
Path, PathKind,
|
||||
HirDatabase,
|
||||
|
@ -247,7 +247,10 @@ where
|
|||
// handle submodules separatelly
|
||||
continue;
|
||||
}
|
||||
let def_loc = DefLoc::Item {
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Item,
|
||||
source_root_id: self.source_root,
|
||||
module_id,
|
||||
source_item_id: SourceItemId {
|
||||
file_id,
|
||||
item_id: item.id,
|
||||
|
@ -261,10 +264,12 @@ where
|
|||
module_items.items.insert(item.name.clone(), resolution);
|
||||
}
|
||||
|
||||
for (name, mod_id) in module_id.children(&self.module_tree) {
|
||||
let def_loc = DefLoc::Module {
|
||||
id: mod_id,
|
||||
source_root: self.source_root,
|
||||
for (name, module_id) in module_id.children(&self.module_tree) {
|
||||
let def_loc = DefLoc {
|
||||
kind: DefKind::Module,
|
||||
source_root_id: self.source_root,
|
||||
module_id,
|
||||
source_item_id: module_id.source(&self.module_tree).0,
|
||||
};
|
||||
let def_id = def_loc.id(self.db);
|
||||
let resolution = Resolution {
|
||||
|
@ -316,7 +321,11 @@ where
|
|||
|
||||
if !is_last {
|
||||
curr = match def_id.loc(self.db) {
|
||||
DefLoc::Module { id, .. } => id,
|
||||
DefLoc {
|
||||
kind: DefKind::Module,
|
||||
module_id,
|
||||
..
|
||||
} => module_id,
|
||||
_ => return,
|
||||
}
|
||||
} else {
|
||||
|
|
|
@ -11,21 +11,21 @@ use ra_syntax::{
|
|||
use ra_db::{SourceRootId, FileId, Cancelable,};
|
||||
|
||||
use crate::{
|
||||
FnId,
|
||||
SourceFileItems, SourceItemId,
|
||||
db::HirDatabase,
|
||||
function::FnScopes,
|
||||
module::{
|
||||
ModuleSource, ModuleSourceNode, ModuleId,
|
||||
imp::Submodule,
|
||||
nameres::{InputModuleItems, ItemMap, Resolver},
|
||||
},
|
||||
SourceFileItems, SourceItemId, DefKind,
|
||||
db::HirDatabase,
|
||||
function::{FnScopes, FnId},
|
||||
module::{
|
||||
ModuleSource, ModuleSourceNode, ModuleId,
|
||||
imp::Submodule,
|
||||
nameres::{InputModuleItems, ItemMap, Resolver},
|
||||
},
|
||||
};
|
||||
|
||||
/// Resolve `FnId` to the corresponding `SyntaxNode`
|
||||
pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode {
|
||||
let item_id = fn_id.loc(db);
|
||||
let syntax = db.file_item(item_id);
|
||||
let def_loc = fn_id.0.loc(db);
|
||||
assert!(def_loc.kind == DefKind::Function);
|
||||
let syntax = db.file_item(def_loc.source_item_id);
|
||||
FnDef::cast(syntax.borrowed()).unwrap().owned()
|
||||
}
|
||||
|
||||
|
@ -36,9 +36,10 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> {
|
|||
}
|
||||
|
||||
pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> {
|
||||
let source_file = db.source_file(file_id);
|
||||
let source_file = source_file.borrowed();
|
||||
let mut res = SourceFileItems::default();
|
||||
let source_file = db.source_file(file_id);
|
||||
res.alloc(source_file.syntax().owned());
|
||||
let source_file = source_file.borrowed();
|
||||
source_file
|
||||
.syntax()
|
||||
.descendants()
|
||||
|
|
Loading…
Reference in a new issue