diff options
author | Aleksey Kladov <[email protected]> | 2018-11-27 23:09:09 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-27 23:09:09 +0000 |
commit | 3922503205e2798e21273a22112f584951f25623 (patch) | |
tree | e9c6f25113b9082978a8799b305a27c537ff681d /crates/ra_analysis/src/hir | |
parent | 9027a21f9a1c7fcee0a59a1e28928fed29781dd8 (diff) |
ItemId based module source
Diffstat (limited to 'crates/ra_analysis/src/hir')
-rw-r--r-- | crates/ra_analysis/src/hir/module/mod.rs | 32 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/query_definitions.rs | 7 |
2 files changed, 23 insertions, 16 deletions
diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 4213bc39f..a6b7a5466 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs | |||
@@ -13,8 +13,8 @@ use ra_syntax::{ | |||
13 | use relative_path::RelativePathBuf; | 13 | use relative_path::RelativePathBuf; |
14 | 14 | ||
15 | use crate::{ | 15 | use crate::{ |
16 | db::SyntaxDatabase, syntax_ptr::SyntaxPtr, FileId, FilePosition, Cancelable, | 16 | FileId, FilePosition, Cancelable, |
17 | hir::{Path, PathKind, HirDatabase}, | 17 | hir::{Path, PathKind, HirDatabase, SourceItemId}, |
18 | input::SourceRootId, | 18 | input::SourceRootId, |
19 | arena::{Arena, Id}, | 19 | arena::{Arena, Id}, |
20 | loc2id::{DefLoc, DefId}, | 20 | loc2id::{DefLoc, DefId}, |
@@ -52,7 +52,7 @@ impl Module { | |||
52 | let file = db.file_syntax(position.file_id); | 52 | let file = db.file_syntax(position.file_id); |
53 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) | 53 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) |
54 | { | 54 | { |
55 | Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m), | 55 | Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), |
56 | _ => ModuleSource::SourceFile(position.file_id), | 56 | _ => ModuleSource::SourceFile(position.file_id), |
57 | }; | 57 | }; |
58 | Module::guess_from_source(db, position.file_id, module_source) | 58 | Module::guess_from_source(db, position.file_id, module_source) |
@@ -218,7 +218,7 @@ impl ModuleTree { | |||
218 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 218 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
219 | pub(crate) enum ModuleSource { | 219 | pub(crate) enum ModuleSource { |
220 | SourceFile(FileId), | 220 | SourceFile(FileId), |
221 | Module(SyntaxPtr), | 221 | Module(SourceItemId), |
222 | } | 222 | } |
223 | 223 | ||
224 | /// An owned syntax node for a module. Unlike `ModuleSource`, | 224 | /// An owned syntax node for a module. Unlike `ModuleSource`, |
@@ -273,7 +273,7 @@ impl ModuleId { | |||
273 | Some((link.name.clone(), module)) | 273 | Some((link.name.clone(), module)) |
274 | }) | 274 | }) |
275 | } | 275 | } |
276 | fn problems(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> Vec<(SyntaxNode, Problem)> { | 276 | fn problems(self, tree: &ModuleTree, db: &impl HirDatabase) -> Vec<(SyntaxNode, Problem)> { |
277 | tree.mods[self] | 277 | tree.mods[self] |
278 | .children | 278 | .children |
279 | .iter() | 279 | .iter() |
@@ -294,7 +294,7 @@ impl LinkId { | |||
294 | fn name(self, tree: &ModuleTree) -> SmolStr { | 294 | fn name(self, tree: &ModuleTree) -> SmolStr { |
295 | tree.links[self].name.clone() | 295 | tree.links[self].name.clone() |
296 | } | 296 | } |
297 | fn bind_source<'a>(self, tree: &ModuleTree, db: &impl SyntaxDatabase) -> ast::ModuleNode { | 297 | fn bind_source<'a>(self, tree: &ModuleTree, db: &impl HirDatabase) -> ast::ModuleNode { |
298 | let owner = self.owner(tree); | 298 | let owner = self.owner(tree); |
299 | match owner.source(tree).resolve(db) { | 299 | match owner.source(tree).resolve(db) { |
300 | ModuleSourceNode::SourceFile(root) => { | 300 | ModuleSourceNode::SourceFile(root) => { |
@@ -317,10 +317,16 @@ pub(crate) struct ModuleData { | |||
317 | } | 317 | } |
318 | 318 | ||
319 | impl ModuleSource { | 319 | impl ModuleSource { |
320 | pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { | 320 | pub(crate) fn new_inline( |
321 | db: &impl HirDatabase, | ||
322 | file_id: FileId, | ||
323 | module: ast::Module, | ||
324 | ) -> ModuleSource { | ||
321 | assert!(!module.has_semi()); | 325 | assert!(!module.has_semi()); |
322 | let ptr = SyntaxPtr::new(file_id, module.syntax()); | 326 | let items = db.file_items(file_id); |
323 | ModuleSource::Module(ptr) | 327 | let item_id = items.id_of(module.syntax()); |
328 | let id = SourceItemId { file_id, item_id }; | ||
329 | ModuleSource::Module(id) | ||
324 | } | 330 | } |
325 | 331 | ||
326 | pub(crate) fn as_file(self) -> Option<FileId> { | 332 | pub(crate) fn as_file(self) -> Option<FileId> { |
@@ -333,18 +339,18 @@ impl ModuleSource { | |||
333 | pub(crate) fn file_id(self) -> FileId { | 339 | pub(crate) fn file_id(self) -> FileId { |
334 | match self { | 340 | match self { |
335 | ModuleSource::SourceFile(f) => f, | 341 | ModuleSource::SourceFile(f) => f, |
336 | ModuleSource::Module(ptr) => ptr.file_id(), | 342 | ModuleSource::Module(source_item_id) => source_item_id.file_id, |
337 | } | 343 | } |
338 | } | 344 | } |
339 | 345 | ||
340 | pub(crate) fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { | 346 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { |
341 | match self { | 347 | match self { |
342 | ModuleSource::SourceFile(file_id) => { | 348 | ModuleSource::SourceFile(file_id) => { |
343 | let syntax = db.file_syntax(file_id); | 349 | let syntax = db.file_syntax(file_id); |
344 | ModuleSourceNode::SourceFile(syntax.ast().owned()) | 350 | ModuleSourceNode::SourceFile(syntax.ast().owned()) |
345 | } | 351 | } |
346 | ModuleSource::Module(ptr) => { | 352 | ModuleSource::Module(item_id) => { |
347 | let syntax = db.resolve_syntax_ptr(ptr); | 353 | let syntax = db.file_item(item_id); |
348 | let syntax = syntax.borrowed(); | 354 | let syntax = syntax.borrowed(); |
349 | let module = ast::Module::cast(syntax).unwrap(); | 355 | let module = ast::Module::cast(syntax).unwrap(); |
350 | ModuleSourceNode::Module(module.owned()) | 356 | ModuleSourceNode::Module(module.owned()) |
diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index ae292e964..cdd986ce4 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs | |||
@@ -63,16 +63,17 @@ pub(crate) fn submodules( | |||
63 | db.check_canceled()?; | 63 | db.check_canceled()?; |
64 | let file_id = source.file_id(); | 64 | let file_id = source.file_id(); |
65 | let submodules = match source.resolve(db) { | 65 | let submodules = match source.resolve(db) { |
66 | ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), | 66 | ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), |
67 | ModuleSourceNode::Module(it) => it | 67 | ModuleSourceNode::Module(it) => it |
68 | .borrowed() | 68 | .borrowed() |
69 | .item_list() | 69 | .item_list() |
70 | .map(|it| collect_submodules(file_id, it)) | 70 | .map(|it| collect_submodules(db, file_id, it)) |
71 | .unwrap_or_else(Vec::new), | 71 | .unwrap_or_else(Vec::new), |
72 | }; | 72 | }; |
73 | return Ok(Arc::new(submodules)); | 73 | return Ok(Arc::new(submodules)); |
74 | 74 | ||
75 | fn collect_submodules<'a>( | 75 | fn collect_submodules<'a>( |
76 | db: &impl HirDatabase, | ||
76 | file_id: FileId, | 77 | file_id: FileId, |
77 | root: impl ast::ModuleItemOwner<'a>, | 78 | root: impl ast::ModuleItemOwner<'a>, |
78 | ) -> Vec<Submodule> { | 79 | ) -> Vec<Submodule> { |
@@ -81,7 +82,7 @@ pub(crate) fn submodules( | |||
81 | if m.has_semi() { | 82 | if m.has_semi() { |
82 | Submodule::Declaration(name) | 83 | Submodule::Declaration(name) |
83 | } else { | 84 | } else { |
84 | let src = ModuleSource::new_inline(file_id, m); | 85 | let src = ModuleSource::new_inline(db, file_id, m); |
85 | Submodule::Definition(name, src) | 86 | Submodule::Definition(name, src) |
86 | } | 87 | } |
87 | }) | 88 | }) |