aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/from_source.rs38
-rw-r--r--crates/ra_hir/src/lib.rs1
-rw-r--r--crates/ra_hir/src/source_binder.rs47
-rw-r--r--crates/ra_ide/src/completion/completion_context.rs10
-rw-r--r--crates/ra_ide/src/diagnostics.rs6
-rw-r--r--crates/ra_ide/src/impls.rs18
-rw-r--r--crates/ra_ide/src/parent_module.rs29
-rw-r--r--crates/ra_ide/src/references/classify.rs22
-rw-r--r--crates/ra_ide/src/references/name_definition.rs2
-rw-r--r--crates/ra_ide/src/runnables.rs4
10 files changed, 65 insertions, 112 deletions
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs
deleted file mode 100644
index eb76aecb1..000000000
--- a/crates/ra_hir/src/from_source.rs
+++ /dev/null
@@ -1,38 +0,0 @@
1//! Finds a corresponding hir data structure for a syntax node in a specific
2//! file.
3
4use hir_def::{nameres::ModuleSource, ModuleId};
5use ra_db::FileId;
6use ra_prof::profile;
7
8use crate::{
9 db::{DefDatabase, HirDatabase},
10 InFile, Module,
11};
12
13impl Module {
14 pub fn from_definition(db: &impl HirDatabase, src: InFile<ModuleSource>) -> Option<Self> {
15 let _p = profile("Module::from_definition");
16 let mut sb = crate::SourceBinder::new(db);
17 match src.value {
18 ModuleSource::Module(ref module) => {
19 assert!(!module.has_semi());
20 return sb.to_def(InFile { file_id: src.file_id, value: module.clone() });
21 }
22 ModuleSource::SourceFile(_) => (),
23 };
24
25 let original_file = src.file_id.original_file(db);
26 Module::from_file(db, original_file)
27 }
28
29 fn from_file(db: &impl DefDatabase, file: FileId) -> Option<Self> {
30 let _p = profile("Module::from_file");
31 let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| {
32 let crate_def_map = db.crate_def_map(crate_id);
33 let local_id = crate_def_map.modules_for_file(file).next()?;
34 Some((crate_id, local_id))
35 })?;
36 Some(Module { id: ModuleId { krate, local_id } })
37 }
38}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 11829f42a..e1c7b7a20 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -36,7 +36,6 @@ mod from_id;
36mod code_model; 36mod code_model;
37 37
38mod has_source; 38mod has_source;
39mod from_source;
40 39
41pub use crate::{ 40pub use crate::{
42 code_model::{ 41 code_model::{
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs
index 26eedbb2c..fa225a4ed 100644
--- a/crates/ra_hir/src/source_binder.rs
+++ b/crates/ra_hir/src/source_binder.rs
@@ -19,7 +19,8 @@ use ra_syntax::{
19}; 19};
20use rustc_hash::FxHashMap; 20use rustc_hash::FxHashMap;
21 21
22use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; 22use crate::{db::HirDatabase, Local, Module, SourceAnalyzer, TypeParam};
23use ra_db::FileId;
23 24
24pub struct SourceBinder<'a, DB> { 25pub struct SourceBinder<'a, DB> {
25 pub db: &'a DB, 26 pub db: &'a DB,
@@ -60,6 +61,16 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
60 T::to_def(self, src) 61 T::to_def(self, src)
61 } 62 }
62 63
64 pub fn to_module_def(&mut self, file: FileId) -> Option<Module> {
65 let _p = profile("SourceBinder::to_module_def");
66 let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| {
67 let crate_def_map = self.db.crate_def_map(crate_id);
68 let local_id = crate_def_map.modules_for_file(file).next()?;
69 Some((crate_id, local_id))
70 })?;
71 Some(Module { id: ModuleId { krate, local_id } })
72 }
73
63 fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> { 74 fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> {
64 T::to_id(self, src) 75 T::to_id(self, src)
65 } 76 }
@@ -107,8 +118,7 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> {
107 return Some(res); 118 return Some(res);
108 } 119 }
109 120
110 let module_source = ModuleSource::from_child_node(self.db, src); 121 let c = self.to_module_def(src.file_id.original_file(self.db))?;
111 let c = crate::Module::from_definition(self.db, src.with_value(module_source))?;
112 Some(c.id.into()) 122 Some(c.id.into())
113 } 123 }
114 124
@@ -248,14 +258,12 @@ impl ToId for ast::MacroCall {
248 ) -> Option<Self::ID> { 258 ) -> Option<Self::ID> {
249 let kind = MacroDefKind::Declarative; 259 let kind = MacroDefKind::Declarative;
250 260
251 let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax())); 261 let krate = sb.to_module_def(src.file_id.original_file(sb.db))?.id.krate;
252 let module = crate::Module::from_definition(sb.db, InFile::new(src.file_id, module_src))?;
253 let krate = Some(module.krate().id);
254 262
255 let ast_id = 263 let ast_id =
256 Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value))); 264 Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value)));
257 265
258 Some(MacroDefId { krate, ast_id, kind }) 266 Some(MacroDefId { krate: Some(krate), ast_id, kind })
259 } 267 }
260} 268}
261 269
@@ -319,21 +327,22 @@ impl ToDef for ast::Module {
319 ) -> Option<Module> { 327 ) -> Option<Module> {
320 { 328 {
321 let _p = profile("ast::Module::to_def"); 329 let _p = profile("ast::Module::to_def");
322 let parent_declaration = 330 let parent_declaration = src
323 src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); 331 .as_ref()
332 .map(|it| it.syntax())
333 .cloned()
334 .ancestors_with_macros(sb.db)
335 .skip(1)
336 .find_map(|it| {
337 let m = ast::Module::cast(it.value.clone())?;
338 Some(it.with_value(m))
339 });
324 340
325 let parent_module = match parent_declaration { 341 let parent_module = match parent_declaration {
326 Some(parent_declaration) => { 342 Some(parent_declaration) => sb.to_def(parent_declaration),
327 let src_parent = InFile { file_id: src.file_id, value: parent_declaration };
328 sb.to_def(src_parent)
329 }
330 None => { 343 None => {
331 let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); 344 let file_id = src.file_id.original_file(sb.db);
332 let src_parent = InFile { 345 sb.to_module_def(file_id)
333 file_id: src.file_id,
334 value: ModuleSource::SourceFile(source_file),
335 };
336 Module::from_definition(sb.db, src_parent)
337 } 346 }
338 }?; 347 }?;
339 348
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs
index 48d69f7e5..deaacda6c 100644
--- a/crates/ra_ide/src/completion/completion_context.rs
+++ b/crates/ra_ide/src/completion/completion_context.rs
@@ -52,15 +52,11 @@ impl<'a> CompletionContext<'a> {
52 original_parse: &'a Parse<ast::SourceFile>, 52 original_parse: &'a Parse<ast::SourceFile>,
53 position: FilePosition, 53 position: FilePosition,
54 ) -> Option<CompletionContext<'a>> { 54 ) -> Option<CompletionContext<'a>> {
55 let src = hir::ModuleSource::from_position(db, position); 55 let mut sb = hir::SourceBinder::new(db);
56 let module = hir::Module::from_definition( 56 let module = sb.to_module_def(position.file_id);
57 db,
58 hir::InFile { file_id: position.file_id.into(), value: src },
59 );
60 let token = 57 let token =
61 original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?; 58 original_parse.tree().syntax().token_at_offset(position.offset).left_biased()?;
62 let analyzer = hir::SourceAnalyzer::new( 59 let analyzer = sb.analyze(
63 db,
64 hir::InFile::new(position.file_id.into(), &token.parent()), 60 hir::InFile::new(position.file_id.into(), &token.parent()),
65 Some(position.offset), 61 Some(position.offset),
66 ); 62 );
diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs
index 478368529..f403b3bcf 100644
--- a/crates/ra_ide/src/diagnostics.rs
+++ b/crates/ra_ide/src/diagnostics.rs
@@ -23,6 +23,7 @@ pub enum Severity {
23 23
24pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> { 24pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic> {
25 let _p = profile("diagnostics"); 25 let _p = profile("diagnostics");
26 let mut sb = hir::SourceBinder::new(db);
26 let parse = db.parse(file_id); 27 let parse = db.parse(file_id);
27 let mut res = Vec::new(); 28 let mut res = Vec::new();
28 29
@@ -108,10 +109,7 @@ pub(crate) fn diagnostics(db: &RootDatabase, file_id: FileId) -> Vec<Diagnostic>
108 fix: Some(fix), 109 fix: Some(fix),
109 }) 110 })
110 }); 111 });
111 let source_file = db.parse(file_id).tree(); 112 if let Some(m) = sb.to_module_def(file_id) {
112 let src =
113 hir::InFile { file_id: file_id.into(), value: hir::ModuleSource::SourceFile(source_file) };
114 if let Some(m) = hir::Module::from_definition(db, src) {
115 m.diagnostics(db, &mut sink); 113 m.diagnostics(db, &mut sink);
116 }; 114 };
117 drop(sink); 115 drop(sink);
diff --git a/crates/ra_ide/src/impls.rs b/crates/ra_ide/src/impls.rs
index fb9396195..9834025d3 100644
--- a/crates/ra_ide/src/impls.rs
+++ b/crates/ra_ide/src/impls.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir::{ImplBlock, SourceBinder}; 3use hir::{Crate, ImplBlock, SourceBinder};
4use ra_db::SourceDatabase; 4use ra_db::SourceDatabase;
5use ra_syntax::{algo::find_node_at_offset, ast, AstNode}; 5use ra_syntax::{algo::find_node_at_offset, ast, AstNode};
6 6
@@ -14,21 +14,17 @@ pub(crate) fn goto_implementation(
14 let syntax = parse.tree().syntax().clone(); 14 let syntax = parse.tree().syntax().clone();
15 let mut sb = SourceBinder::new(db); 15 let mut sb = SourceBinder::new(db);
16 16
17 let src = hir::ModuleSource::from_position(db, position); 17 let krate = sb.to_module_def(position.file_id)?.krate();
18 let module = hir::Module::from_definition(
19 db,
20 hir::InFile { file_id: position.file_id.into(), value: src },
21 )?;
22 18
23 if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) { 19 if let Some(nominal_def) = find_node_at_offset::<ast::NominalDef>(&syntax, position.offset) {
24 return Some(RangeInfo::new( 20 return Some(RangeInfo::new(
25 nominal_def.syntax().text_range(), 21 nominal_def.syntax().text_range(),
26 impls_for_def(&mut sb, position, &nominal_def, module)?, 22 impls_for_def(&mut sb, position, &nominal_def, krate)?,
27 )); 23 ));
28 } else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) { 24 } else if let Some(trait_def) = find_node_at_offset::<ast::TraitDef>(&syntax, position.offset) {
29 return Some(RangeInfo::new( 25 return Some(RangeInfo::new(
30 trait_def.syntax().text_range(), 26 trait_def.syntax().text_range(),
31 impls_for_trait(&mut sb, position, &trait_def, module)?, 27 impls_for_trait(&mut sb, position, &trait_def, krate)?,
32 )); 28 ));
33 } 29 }
34 30
@@ -39,7 +35,7 @@ fn impls_for_def(
39 sb: &mut SourceBinder<RootDatabase>, 35 sb: &mut SourceBinder<RootDatabase>,
40 position: FilePosition, 36 position: FilePosition,
41 node: &ast::NominalDef, 37 node: &ast::NominalDef,
42 module: hir::Module, 38 krate: Crate,
43) -> Option<Vec<NavigationTarget>> { 39) -> Option<Vec<NavigationTarget>> {
44 let ty = match node { 40 let ty = match node {
45 ast::NominalDef::StructDef(def) => { 41 ast::NominalDef::StructDef(def) => {
@@ -56,7 +52,6 @@ fn impls_for_def(
56 } 52 }
57 }; 53 };
58 54
59 let krate = module.krate();
60 let impls = ImplBlock::all_in_crate(sb.db, krate); 55 let impls = ImplBlock::all_in_crate(sb.db, krate);
61 56
62 Some( 57 Some(
@@ -72,12 +67,11 @@ fn impls_for_trait(
72 sb: &mut SourceBinder<RootDatabase>, 67 sb: &mut SourceBinder<RootDatabase>,
73 position: FilePosition, 68 position: FilePosition,
74 node: &ast::TraitDef, 69 node: &ast::TraitDef,
75 module: hir::Module, 70 krate: Crate,
76) -> Option<Vec<NavigationTarget>> { 71) -> Option<Vec<NavigationTarget>> {
77 let src = hir::InFile { file_id: position.file_id.into(), value: node.clone() }; 72 let src = hir::InFile { file_id: position.file_id.into(), value: node.clone() };
78 let tr = sb.to_def(src)?; 73 let tr = sb.to_def(src)?;
79 74
80 let krate = module.krate();
81 let impls = ImplBlock::for_trait(sb.db, krate, tr); 75 let impls = ImplBlock::for_trait(sb.db, krate, tr);
82 76
83 Some(impls.into_iter().map(|imp| imp.to_nav(sb.db)).collect()) 77 Some(impls.into_iter().map(|imp| imp.to_nav(sb.db)).collect())
diff --git a/crates/ra_ide/src/parent_module.rs b/crates/ra_ide/src/parent_module.rs
index f5a788c07..2dbccfc3b 100644
--- a/crates/ra_ide/src/parent_module.rs
+++ b/crates/ra_ide/src/parent_module.rs
@@ -1,17 +1,23 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_db::{CrateId, FileId, FilePosition, SourceDatabase}; 3use ra_db::{CrateId, FileId, FilePosition, SourceDatabase};
4use ra_syntax::{
5 algo::find_node_at_offset,
6 ast::{self, AstNode},
7};
4 8
5use crate::{db::RootDatabase, NavigationTarget}; 9use crate::{db::RootDatabase, NavigationTarget};
6 10
7/// This returns `Vec` because a module may be included from several places. We 11/// This returns `Vec` because a module may be included from several places. We
8/// don't handle this case yet though, so the Vec has length at most one. 12/// don't handle this case yet though, so the Vec has length at most one.
9pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> { 13pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
10 let src = hir::ModuleSource::from_position(db, position); 14 let mut sb = hir::SourceBinder::new(db);
11 let module = match hir::Module::from_definition( 15 let parse = db.parse(position.file_id);
12 db, 16 let module = match find_node_at_offset::<ast::Module>(parse.tree().syntax(), position.offset) {
13 hir::InFile { file_id: position.file_id.into(), value: src }, 17 Some(module) => sb.to_def(hir::InFile::new(position.file_id.into(), module)),
14 ) { 18 None => sb.to_module_def(position.file_id),
19 };
20 let module = match module {
15 None => return Vec::new(), 21 None => return Vec::new(),
16 Some(it) => it, 22 Some(it) => it,
17 }; 23 };
@@ -21,14 +27,11 @@ pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<Na
21 27
22/// Returns `Vec` for the same reason as `parent_module` 28/// Returns `Vec` for the same reason as `parent_module`
23pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> { 29pub(crate) fn crate_for(db: &RootDatabase, file_id: FileId) -> Vec<CrateId> {
24 let source_file = db.parse(file_id).tree(); 30 let mut sb = hir::SourceBinder::new(db);
25 let src = hir::ModuleSource::SourceFile(source_file); 31 let module = match sb.to_module_def(file_id) {
26 let module = 32 Some(it) => it,
27 match hir::Module::from_definition(db, hir::InFile { file_id: file_id.into(), value: src }) 33 None => return Vec::new(),
28 { 34 };
29 Some(it) => it,
30 None => return Vec::new(),
31 };
32 let krate = module.krate(); 35 let krate = module.krate();
33 vec![krate.into()] 36 vec![krate.into()]
34} 37}
diff --git a/crates/ra_ide/src/references/classify.rs b/crates/ra_ide/src/references/classify.rs
index cb7da1fff..46cba30a3 100644
--- a/crates/ra_ide/src/references/classify.rs
+++ b/crates/ra_ide/src/references/classify.rs
@@ -1,6 +1,6 @@
1//! Functions that are used to classify an element from its definition or reference. 1//! Functions that are used to classify an element from its definition or reference.
2 2
3use hir::{InFile, Module, ModuleSource, PathResolution, SourceBinder}; 3use hir::{InFile, PathResolution, SourceBinder};
4use ra_prof::profile; 4use ra_prof::profile;
5use ra_syntax::{ast, match_ast, AstNode}; 5use ra_syntax::{ast, match_ast, AstNode};
6use test_utils::tested_by; 6use test_utils::tested_by;
@@ -35,16 +35,7 @@ pub(crate) fn classify_name(
35 Some(from_struct_field(sb.db, field)) 35 Some(from_struct_field(sb.db, field))
36 }, 36 },
37 ast::Module(it) => { 37 ast::Module(it) => {
38 let def = { 38 let def = sb.to_def(name.with_value(it))?;
39 if !it.has_semi() {
40 let ast = hir::ModuleSource::Module(it);
41 let src = name.with_value(ast);
42 hir::Module::from_definition(sb.db, src)
43 } else {
44 let src = name.with_value(it);
45 sb.to_def(src)
46 }
47 }?;
48 Some(from_module_def(sb.db, def.into(), None)) 39 Some(from_module_def(sb.db, def.into(), None))
49 }, 40 },
50 ast::StructDef(it) => { 41 ast::StructDef(it) => {
@@ -103,8 +94,7 @@ pub(crate) fn classify_name(
103 let src = name.with_value(it); 94 let src = name.with_value(it);
104 let def = sb.to_def(src.clone())?; 95 let def = sb.to_def(src.clone())?;
105 96
106 let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax())); 97 let module = sb.to_module_def(src.file_id.original_file(sb.db))?;
107 let module = Module::from_definition(sb.db, src.with_value(module_src))?;
108 98
109 Some(NameDefinition { 99 Some(NameDefinition {
110 visibility: None, 100 visibility: None,
@@ -157,10 +147,9 @@ pub(crate) fn classify_name_ref(
157 } 147 }
158 } 148 }
159 149
160 let ast = ModuleSource::from_child_node(sb.db, name_ref.with_value(&parent));
161 // FIXME: find correct container and visibility for each case 150 // FIXME: find correct container and visibility for each case
162 let container = Module::from_definition(sb.db, name_ref.with_value(ast))?;
163 let visibility = None; 151 let visibility = None;
152 let container = sb.to_module_def(name_ref.file_id.original_file(sb.db))?;
164 153
165 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { 154 if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
166 tested_by!(goto_def_for_macros); 155 tested_by!(goto_def_for_macros);
@@ -178,12 +167,13 @@ pub(crate) fn classify_name_ref(
178 PathResolution::Def(def) => Some(from_module_def(sb.db, def, Some(container))), 167 PathResolution::Def(def) => Some(from_module_def(sb.db, def, Some(container))),
179 PathResolution::AssocItem(item) => Some(from_assoc_item(sb.db, item)), 168 PathResolution::AssocItem(item) => Some(from_assoc_item(sb.db, item)),
180 PathResolution::Local(local) => { 169 PathResolution::Local(local) => {
181 let container = local.module(sb.db);
182 let kind = NameKind::Local(local); 170 let kind = NameKind::Local(local);
171 let container = local.module(sb.db);
183 Some(NameDefinition { kind, container, visibility: None }) 172 Some(NameDefinition { kind, container, visibility: None })
184 } 173 }
185 PathResolution::TypeParam(par) => { 174 PathResolution::TypeParam(par) => {
186 let kind = NameKind::TypeParam(par); 175 let kind = NameKind::TypeParam(par);
176 let container = par.module(sb.db);
187 Some(NameDefinition { kind, container, visibility }) 177 Some(NameDefinition { kind, container, visibility })
188 } 178 }
189 PathResolution::Macro(def) => { 179 PathResolution::Macro(def) => {
diff --git a/crates/ra_ide/src/references/name_definition.rs b/crates/ra_ide/src/references/name_definition.rs
index 8c67c8863..1e4226ab9 100644
--- a/crates/ra_ide/src/references/name_definition.rs
+++ b/crates/ra_ide/src/references/name_definition.rs
@@ -25,6 +25,8 @@ pub enum NameKind {
25#[derive(PartialEq, Eq)] 25#[derive(PartialEq, Eq)]
26pub(crate) struct NameDefinition { 26pub(crate) struct NameDefinition {
27 pub visibility: Option<ast::Visibility>, 27 pub visibility: Option<ast::Visibility>,
28 /// FIXME: this doesn't really make sense. For example, builtin types don't
29 /// really have a module.
28 pub container: Module, 30 pub container: Module,
29 pub kind: NameKind, 31 pub kind: NameKind,
30} 32}
diff --git a/crates/ra_ide/src/runnables.rs b/crates/ra_ide/src/runnables.rs
index e213e1a06..7533692f6 100644
--- a/crates/ra_ide/src/runnables.rs
+++ b/crates/ra_ide/src/runnables.rs
@@ -66,8 +66,8 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti
66 return None; 66 return None;
67 } 67 }
68 let range = module.syntax().text_range(); 68 let range = module.syntax().text_range();
69 let src = hir::ModuleSource::from_child_node(db, InFile::new(file_id.into(), &module.syntax())); 69 let mut sb = hir::SourceBinder::new(db);
70 let module = hir::Module::from_definition(db, InFile::new(file_id.into(), src))?; 70 let module = sb.to_def(InFile::new(file_id.into(), module))?;
71 71
72 let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::"); 72 let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::");
73 Some(Runnable { range, kind: RunnableKind::TestMod { path } }) 73 Some(Runnable { range, kind: RunnableKind::TestMod { path } })