Skip to content

Commit 9a6c26e

Browse files
committed
Move module to SourceBinder
1 parent 16cfc8d commit 9a6c26e

File tree

5 files changed

+69
-55
lines changed

5 files changed

+69
-55
lines changed

crates/ra_hir/src/from_source.rs

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,22 @@
22
//! file.
33
44
use hir_def::{nameres::ModuleSource, ModuleId};
5-
use hir_expand::name::AsName;
65
use ra_db::FileId;
76
use ra_prof::profile;
8-
use ra_syntax::ast::{self, AstNode, NameOwner};
97

10-
use crate::{db::DefDatabase, InFile, Module};
8+
use crate::{
9+
db::{DefDatabase, HirDatabase},
10+
InFile, Module,
11+
};
1112

1213
impl Module {
13-
pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> {
14-
let _p = profile("Module::from_declaration");
15-
let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);
16-
17-
let parent_module = match parent_declaration {
18-
Some(parent_declaration) => {
19-
let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
20-
Module::from_declaration(db, src_parent)
21-
}
22-
None => {
23-
let source_file = db.parse(src.file_id.original_file(db)).tree();
24-
let src_parent =
25-
InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) };
26-
Module::from_definition(db, src_parent)
27-
}
28-
}?;
29-
30-
let child_name = src.value.name()?.as_name();
31-
let def_map = db.crate_def_map(parent_module.id.krate);
32-
let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?;
33-
Some(parent_module.with_module_id(*child_id))
34-
}
35-
36-
pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> {
14+
pub fn from_definition(db: &impl HirDatabase, src: InFile<ModuleSource>) -> Option<Self> {
3715
let _p = profile("Module::from_definition");
16+
let mut sb = crate::SourceBinder::new(db);
3817
match src.value {
3918
ModuleSource::Module(ref module) => {
4019
assert!(!module.has_semi());
41-
return Module::from_declaration(
42-
db,
43-
InFile { file_id: src.file_id, value: module.clone() },
44-
);
20+
return sb.to_def(InFile { file_id: src.file_id, value: module.clone() });
4521
}
4622
ModuleSource::SourceFile(_) => (),
4723
};

crates/ra_hir/src/source_binder.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ use hir_def::{
1111
ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId,
1212
StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId,
1313
};
14-
use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind};
14+
use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind};
1515
use ra_prof::profile;
16-
use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit};
16+
use ra_syntax::{
17+
ast::{self, NameOwner},
18+
match_ast, AstNode, SyntaxNode, TextUnit,
19+
};
1720
use rustc_hash::FxHashMap;
1821

19-
use crate::{db::HirDatabase, Local, ModuleSource, SourceAnalyzer, TypeParam};
22+
use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam};
2023

2124
pub struct SourceBinder<'a, DB> {
2225
pub db: &'a DB,
@@ -306,3 +309,38 @@ impl ToDef for ast::TypeParam {
306309
Some(TypeParam { id })
307310
}
308311
}
312+
313+
impl ToDef for ast::Module {
314+
type Def = Module;
315+
316+
fn to_def<DB: HirDatabase>(
317+
sb: &mut SourceBinder<'_, DB>,
318+
src: InFile<ast::Module>,
319+
) -> Option<Module> {
320+
{
321+
let _p = profile("ast::Module::to_def");
322+
let parent_declaration =
323+
src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast);
324+
325+
let parent_module = match parent_declaration {
326+
Some(parent_declaration) => {
327+
let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
328+
sb.to_def(src_parent)
329+
}
330+
None => {
331+
let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree();
332+
let src_parent = InFile {
333+
file_id: src.file_id,
334+
value: ModuleSource::SourceFile(source_file),
335+
};
336+
Module::from_definition(sb.db, src_parent)
337+
}
338+
}?;
339+
340+
let child_name = src.value.name()?.as_name();
341+
let def_map = sb.db.crate_def_map(parent_module.id.krate);
342+
let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?;
343+
Some(parent_module.with_module_id(*child_id))
344+
}
345+
}
346+
}

crates/ra_ide/src/goto_definition.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ pub(crate) fn goto_definition(
2424
let original_token = pick_best(file.token_at_offset(position.offset))?;
2525
let token = descend_into_macros(db, position.file_id, original_token.clone());
2626

27+
let mut sb = SourceBinder::new(db);
2728
let nav_targets = match_ast! {
2829
match (token.value.parent()) {
2930
ast::NameRef(name_ref) => {
30-
reference_definition(db, token.with_value(&name_ref)).to_vec()
31+
reference_definition(&mut sb, token.with_value(&name_ref)).to_vec()
3132
},
3233
ast::Name(name) => {
33-
name_definition(db, token.with_value(&name))?
34+
name_definition(&mut sb, token.with_value(&name))?
3435
},
3536
_ => return None,
3637
}
@@ -67,57 +68,56 @@ impl ReferenceResult {
6768
}
6869

6970
pub(crate) fn reference_definition(
70-
db: &RootDatabase,
71+
sb: &mut SourceBinder<RootDatabase>,
7172
name_ref: InFile<&ast::NameRef>,
7273
) -> ReferenceResult {
7374
use self::ReferenceResult::*;
7475

75-
let mut sb = SourceBinder::new(db);
76-
let name_kind = classify_name_ref(&mut sb, name_ref).map(|d| d.kind);
76+
let name_kind = classify_name_ref(sb, name_ref).map(|d| d.kind);
7777
match name_kind {
78-
Some(Macro(it)) => return Exact(it.to_nav(db)),
79-
Some(Field(it)) => return Exact(it.to_nav(db)),
80-
Some(TypeParam(it)) => return Exact(it.to_nav(db)),
81-
Some(AssocItem(it)) => return Exact(it.to_nav(db)),
82-
Some(Local(it)) => return Exact(it.to_nav(db)),
83-
Some(Def(def)) => match NavigationTarget::from_def(db, def) {
78+
Some(Macro(it)) => return Exact(it.to_nav(sb.db)),
79+
Some(Field(it)) => return Exact(it.to_nav(sb.db)),
80+
Some(TypeParam(it)) => return Exact(it.to_nav(sb.db)),
81+
Some(AssocItem(it)) => return Exact(it.to_nav(sb.db)),
82+
Some(Local(it)) => return Exact(it.to_nav(sb.db)),
83+
Some(Def(def)) => match NavigationTarget::from_def(sb.db, def) {
8484
Some(nav) => return Exact(nav),
8585
None => return Approximate(vec![]),
8686
},
8787
Some(SelfType(imp)) => {
8888
// FIXME: ideally, this should point to the type in the impl, and
8989
// not at the whole impl. And goto **type** definition should bring
9090
// us to the actual type
91-
return Exact(imp.to_nav(db));
91+
return Exact(imp.to_nav(sb.db));
9292
}
9393
None => {}
9494
};
9595

9696
// Fallback index based approach:
97-
let navs = crate::symbol_index::index_resolve(db, name_ref.value)
97+
let navs = crate::symbol_index::index_resolve(sb.db, name_ref.value)
9898
.into_iter()
99-
.map(|s| s.to_nav(db))
99+
.map(|s| s.to_nav(sb.db))
100100
.collect();
101101
Approximate(navs)
102102
}
103103

104-
pub(crate) fn name_definition(
105-
db: &RootDatabase,
104+
fn name_definition(
105+
sb: &mut SourceBinder<RootDatabase>,
106106
name: InFile<&ast::Name>,
107107
) -> Option<Vec<NavigationTarget>> {
108108
let parent = name.value.syntax().parent()?;
109109

110110
if let Some(module) = ast::Module::cast(parent.clone()) {
111111
if module.has_semi() {
112112
let src = name.with_value(module);
113-
if let Some(child_module) = hir::Module::from_declaration(db, src) {
114-
let nav = child_module.to_nav(db);
113+
if let Some(child_module) = sb.to_def(src) {
114+
let nav = child_module.to_nav(sb.db);
115115
return Some(vec![nav]);
116116
}
117117
}
118118
}
119119

120-
if let Some(nav) = named_target(db, name.with_value(&parent)) {
120+
if let Some(nav) = named_target(sb.db, name.with_value(&parent)) {
121121
return Some(vec![nav]);
122122
}
123123

crates/ra_ide/src/references/classify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn classify_name(
4242
hir::Module::from_definition(sb.db, src)
4343
} else {
4444
let src = name.with_value(it);
45-
hir::Module::from_declaration(sb.db, src)
45+
sb.to_def(src)
4646
}
4747
}?;
4848
Some(from_module_def(sb.db, def.into(), None))

crates/ra_ide/src/references/rename.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ fn rename_mod(
6363
let mut source_file_edits = Vec::new();
6464
let mut file_system_edits = Vec::new();
6565
let module_src = hir::InFile { file_id: position.file_id.into(), value: ast_module.clone() };
66-
if let Some(module) = hir::Module::from_declaration(db, module_src) {
66+
if let Some(module) = hir::SourceBinder::new(db).to_def(module_src) {
6767
let src = module.definition_source(db);
6868
let file_id = src.file_id.original_file(db);
6969
match src.value {

0 commit comments

Comments
 (0)