diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-18 22:51:34 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-12-18 22:51:34 +0000 |
commit | 0e1c01cdb8e6b346edd5d68d9cc72cbce1ce9793 (patch) | |
tree | bba7163aa81ceba1dfb26f481b8fd974421aaa8b /crates/ra_hir | |
parent | 9664fbcac5e53d725c6e438ffba2194921321b76 (diff) | |
parent | 1c5ce7a8682c9e14ce726388b88897644721b19b (diff) |
Merge #291
291: File module source does not depend on syntax r=matklad a=matklad
Fixes this problem: https://github.com/rust-analyzer/rust-analyzer/pull/289#issuecomment-448398571
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/lib.rs | 3 | ||||
-rw-r--r-- | crates/ra_hir/src/module.rs | 10 | ||||
-rw-r--r-- | crates/ra_hir/src/module/imp.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/module/nameres.rs | 4 | ||||
-rw-r--r-- | crates/ra_hir/src/query_definitions.rs | 6 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 11 |
6 files changed, 21 insertions, 17 deletions
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 80354a49d..5941a9ea3 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -109,7 +109,8 @@ pub(crate) type SourceFileItemId = Id<SyntaxNode>; | |||
109 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 109 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
110 | pub struct SourceItemId { | 110 | pub struct SourceItemId { |
111 | file_id: FileId, | 111 | file_id: FileId, |
112 | item_id: SourceFileItemId, | 112 | /// None for the whole file. |
113 | item_id: Option<SourceFileItemId>, | ||
113 | } | 114 | } |
114 | 115 | ||
115 | /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. | 116 | /// Maps item's `SyntaxNode`s to `SourceFileItemId` and back. |
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs index c6bb76d56..d5866f6ef 100644 --- a/crates/ra_hir/src/module.rs +++ b/crates/ra_hir/src/module.rs | |||
@@ -271,15 +271,13 @@ pub struct ModuleData { | |||
271 | 271 | ||
272 | impl ModuleSource { | 272 | impl ModuleSource { |
273 | // precondition: item_id **must** point to module | 273 | // precondition: item_id **must** point to module |
274 | fn new(file_id: FileId, item_id: SourceFileItemId) -> ModuleSource { | 274 | fn new(file_id: FileId, item_id: Option<SourceFileItemId>) -> ModuleSource { |
275 | let source_item_id = SourceItemId { file_id, item_id }; | 275 | let source_item_id = SourceItemId { file_id, item_id }; |
276 | ModuleSource(source_item_id) | 276 | ModuleSource(source_item_id) |
277 | } | 277 | } |
278 | 278 | ||
279 | pub(crate) fn new_file(db: &impl HirDatabase, file_id: FileId) -> ModuleSource { | 279 | pub(crate) fn new_file(file_id: FileId) -> ModuleSource { |
280 | let file_items = db.file_items(file_id); | 280 | ModuleSource::new(file_id, None) |
281 | let item_id = file_items.id_of_source_file(); | ||
282 | ModuleSource::new(file_id, item_id) | ||
283 | } | 281 | } |
284 | 282 | ||
285 | pub(crate) fn new_inline( | 283 | pub(crate) fn new_inline( |
@@ -290,7 +288,7 @@ impl ModuleSource { | |||
290 | assert!(!m.has_semi()); | 288 | assert!(!m.has_semi()); |
291 | let file_items = db.file_items(file_id); | 289 | let file_items = db.file_items(file_id); |
292 | let item_id = file_items.id_of(file_id, m.syntax()); | 290 | let item_id = file_items.id_of(file_id, m.syntax()); |
293 | ModuleSource::new(file_id, item_id) | 291 | ModuleSource::new(file_id, Some(item_id)) |
294 | } | 292 | } |
295 | 293 | ||
296 | pub fn file_id(self) -> FileId { | 294 | pub fn file_id(self) -> FileId { |
diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index 9f144e139..4a19842c4 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs | |||
@@ -66,7 +66,7 @@ fn create_module_tree<'a>( | |||
66 | 66 | ||
67 | let source_root = db.source_root(source_root); | 67 | let source_root = db.source_root(source_root); |
68 | for &file_id in source_root.files.iter() { | 68 | for &file_id in source_root.files.iter() { |
69 | let source = ModuleSource::new_file(db, file_id); | 69 | let source = ModuleSource::new_file(file_id); |
70 | if visited.contains(&source) { | 70 | if visited.contains(&source) { |
71 | continue; // TODO: use explicit crate_roots here | 71 | continue; // TODO: use explicit crate_roots here |
72 | } | 72 | } |
@@ -126,7 +126,7 @@ fn build_subtree( | |||
126 | visited, | 126 | visited, |
127 | roots, | 127 | roots, |
128 | Some(link), | 128 | Some(link), |
129 | ModuleSource::new_file(db, file_id), | 129 | ModuleSource::new_file(file_id), |
130 | ), | 130 | ), |
131 | }) | 131 | }) |
132 | .collect::<Cancelable<Vec<_>>>()?; | 132 | .collect::<Cancelable<Vec<_>>>()?; |
diff --git a/crates/ra_hir/src/module/nameres.rs b/crates/ra_hir/src/module/nameres.rs index 9afeade9e..5540b827f 100644 --- a/crates/ra_hir/src/module/nameres.rs +++ b/crates/ra_hir/src/module/nameres.rs | |||
@@ -98,7 +98,7 @@ impl NamedImport { | |||
98 | pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { | 98 | pub fn range(&self, db: &impl HirDatabase, file_id: FileId) -> TextRange { |
99 | let source_item_id = SourceItemId { | 99 | let source_item_id = SourceItemId { |
100 | file_id, | 100 | file_id, |
101 | item_id: self.file_item_id, | 101 | item_id: Some(self.file_item_id), |
102 | }; | 102 | }; |
103 | let syntax = db.file_item(source_item_id); | 103 | let syntax = db.file_item(source_item_id); |
104 | let offset = syntax.borrowed().range().start(); | 104 | let offset = syntax.borrowed().range().start(); |
@@ -281,7 +281,7 @@ where | |||
281 | module_id, | 281 | module_id, |
282 | source_item_id: SourceItemId { | 282 | source_item_id: SourceItemId { |
283 | file_id, | 283 | file_id, |
284 | item_id: item.id, | 284 | item_id: Some(item.id), |
285 | }, | 285 | }, |
286 | }; | 286 | }; |
287 | let def_id = def_loc.id(self.db); | 287 | let def_id = def_loc.id(self.db); |
diff --git a/crates/ra_hir/src/query_definitions.rs b/crates/ra_hir/src/query_definitions.rs index 37c4f9e4f..efaeb1525 100644 --- a/crates/ra_hir/src/query_definitions.rs +++ b/crates/ra_hir/src/query_definitions.rs | |||
@@ -38,7 +38,6 @@ pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> { | |||
38 | pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> { | 38 | pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> { |
39 | let mut res = SourceFileItems::new(file_id); | 39 | let mut res = SourceFileItems::new(file_id); |
40 | let source_file = db.source_file(file_id); | 40 | let source_file = db.source_file(file_id); |
41 | res.alloc(source_file.syntax().owned()); | ||
42 | let source_file = source_file.borrowed(); | 41 | let source_file = source_file.borrowed(); |
43 | source_file | 42 | source_file |
44 | .syntax() | 43 | .syntax() |
@@ -52,7 +51,10 @@ pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFi | |||
52 | } | 51 | } |
53 | 52 | ||
54 | pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { | 53 | pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { |
55 | db.file_items(source_item_id.file_id)[source_item_id.item_id].clone() | 54 | match source_item_id.item_id { |
55 | Some(id) => db.file_items(source_item_id.file_id)[id].clone(), | ||
56 | None => db.source_file(source_item_id.file_id).syntax().owned(), | ||
57 | } | ||
56 | } | 58 | } |
57 | 59 | ||
58 | pub(crate) fn submodules( | 60 | pub(crate) fn submodules( |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 0c16ccc24..ce2a0f2e8 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -20,7 +20,7 @@ use crate::{ | |||
20 | 20 | ||
21 | /// Locates the module by `FileId`. Picks topmost module in the file. | 21 | /// Locates the module by `FileId`. Picks topmost module in the file. |
22 | pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> { | 22 | pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable<Option<Module>> { |
23 | let module_source = ModuleSource::new_file(db, file_id); | 23 | let module_source = ModuleSource::new_file(file_id); |
24 | module_from_source(db, module_source) | 24 | module_from_source(db, module_source) |
25 | } | 25 | } |
26 | 26 | ||
@@ -32,7 +32,7 @@ pub fn module_from_position( | |||
32 | let file = db.source_file(position.file_id); | 32 | let file = db.source_file(position.file_id); |
33 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) { | 33 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) { |
34 | Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), | 34 | Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), |
35 | _ => ModuleSource::new_file(db, position.file_id), | 35 | _ => ModuleSource::new_file(position.file_id), |
36 | }; | 36 | }; |
37 | module_from_source(db, module_source) | 37 | module_from_source(db, module_source) |
38 | } | 38 | } |
@@ -50,7 +50,7 @@ pub fn module_from_child_node( | |||
50 | { | 50 | { |
51 | ModuleSource::new_inline(db, file_id, m) | 51 | ModuleSource::new_inline(db, file_id, m) |
52 | } else { | 52 | } else { |
53 | ModuleSource::new_file(db, file_id) | 53 | ModuleSource::new_file(file_id) |
54 | }; | 54 | }; |
55 | module_from_source(db, module_source) | 55 | module_from_source(db, module_source) |
56 | } | 56 | } |
@@ -76,7 +76,10 @@ pub fn function_from_source( | |||
76 | let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?); | 76 | let module = ctry!(module_from_child_node(db, file_id, fn_def.syntax())?); |
77 | let file_items = db.file_items(file_id); | 77 | let file_items = db.file_items(file_id); |
78 | let item_id = file_items.id_of(file_id, fn_def.syntax()); | 78 | let item_id = file_items.id_of(file_id, fn_def.syntax()); |
79 | let source_item_id = SourceItemId { file_id, item_id }; | 79 | let source_item_id = SourceItemId { |
80 | file_id, | ||
81 | item_id: Some(item_id), | ||
82 | }; | ||
80 | let def_loc = DefLoc { | 83 | let def_loc = DefLoc { |
81 | kind: DefKind::Function, | 84 | kind: DefKind::Function, |
82 | source_root_id: module.source_root_id, | 85 | source_root_id: module.source_root_id, |