diff options
author | Aleksey Kladov <[email protected]> | 2018-11-28 00:25:20 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-28 00:25:20 +0000 |
commit | 11168c464cd962af3336a2cc68295496066edd6c (patch) | |
tree | 2c3b0bceea0dcf092ae8bf9d16c1508295606b09 /crates/ra_analysis/src | |
parent | b2de95879a8d48cc4077895376b0aaed1e972169 (diff) |
move db basics to ra_db
This should allow to move hir to a separate crate
Diffstat (limited to 'crates/ra_analysis/src')
-rw-r--r-- | crates/ra_analysis/src/completion/mod.rs | 3 | ||||
-rw-r--r-- | crates/ra_analysis/src/db.rs | 69 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/db.rs | 5 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/function/scope.rs | 3 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/module/imp.rs | 4 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/module/mod.rs | 2 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/module/nameres.rs | 5 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/query_definitions.rs | 2 | ||||
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 101 | ||||
-rw-r--r-- | crates/ra_analysis/src/input.rs | 75 | ||||
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 22 | ||||
-rw-r--r-- | crates/ra_analysis/src/loc2id.rs | 102 | ||||
-rw-r--r-- | crates/ra_analysis/src/symbol_index.rs | 3 | ||||
-rw-r--r-- | crates/ra_analysis/src/syntax_ptr.rs | 48 |
14 files changed, 52 insertions, 392 deletions
diff --git a/crates/ra_analysis/src/completion/mod.rs b/crates/ra_analysis/src/completion/mod.rs index 844dabb19..538b51633 100644 --- a/crates/ra_analysis/src/completion/mod.rs +++ b/crates/ra_analysis/src/completion/mod.rs | |||
@@ -7,10 +7,11 @@ use ra_syntax::{ | |||
7 | AstNode, AtomEdit, | 7 | AstNode, AtomEdit, |
8 | SyntaxNodeRef, | 8 | SyntaxNodeRef, |
9 | }; | 9 | }; |
10 | use ra_db::SyntaxDatabase; | ||
10 | use rustc_hash::{FxHashMap}; | 11 | use rustc_hash::{FxHashMap}; |
11 | 12 | ||
12 | use crate::{ | 13 | use crate::{ |
13 | db::{self, SyntaxDatabase}, | 14 | db, |
14 | hir, | 15 | hir, |
15 | Cancelable, FilePosition | 16 | Cancelable, FilePosition |
16 | }; | 17 | }; |
diff --git a/crates/ra_analysis/src/db.rs b/crates/ra_analysis/src/db.rs index e0b7afac5..1b2dd4b3d 100644 --- a/crates/ra_analysis/src/db.rs +++ b/crates/ra_analysis/src/db.rs | |||
@@ -1,15 +1,13 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | #[cfg(test)] | 2 | #[cfg(test)] |
3 | use parking_lot::Mutex; | 3 | use parking_lot::Mutex; |
4 | use ra_editor::LineIndex; | ||
5 | use ra_syntax::{SourceFileNode}; | ||
6 | use salsa::{self, Database}; | 4 | use salsa::{self, Database}; |
5 | use ra_db::{LocationIntener, BaseDatabase}; | ||
7 | 6 | ||
8 | use crate::{ | 7 | use crate::{ |
9 | hir, | 8 | hir, |
10 | symbol_index, | 9 | symbol_index, |
11 | loc2id::{IdMaps, LocationIntener, DefId, DefLoc, FnId}, | 10 | loc2id::{IdMaps, DefId, DefLoc, FnId}, |
12 | Cancelable, Canceled, FileId, | ||
13 | }; | 11 | }; |
14 | 12 | ||
15 | #[derive(Debug)] | 13 | #[derive(Debug)] |
@@ -47,11 +45,11 @@ impl Default for RootDatabase { | |||
47 | runtime: salsa::Runtime::default(), | 45 | runtime: salsa::Runtime::default(), |
48 | id_maps: Default::default(), | 46 | id_maps: Default::default(), |
49 | }; | 47 | }; |
50 | db.query_mut(crate::input::SourceRootQuery) | 48 | db.query_mut(ra_db::SourceRootQuery) |
51 | .set(crate::input::WORKSPACE, Default::default()); | 49 | .set(ra_db::WORKSPACE, Default::default()); |
52 | db.query_mut(crate::input::CrateGraphQuery) | 50 | db.query_mut(ra_db::CrateGraphQuery) |
53 | .set((), Default::default()); | 51 | .set((), Default::default()); |
54 | db.query_mut(crate::input::LibrariesQuery) | 52 | db.query_mut(ra_db::LibrariesQuery) |
55 | .set((), Default::default()); | 53 | .set((), Default::default()); |
56 | db | 54 | db |
57 | } | 55 | } |
@@ -67,22 +65,7 @@ impl salsa::ParallelDatabase for RootDatabase { | |||
67 | } | 65 | } |
68 | } | 66 | } |
69 | 67 | ||
70 | pub(crate) trait BaseDatabase: salsa::Database { | 68 | impl BaseDatabase for RootDatabase {} |
71 | fn id_maps(&self) -> &IdMaps; | ||
72 | fn check_canceled(&self) -> Cancelable<()> { | ||
73 | if self.salsa_runtime().is_current_revision_canceled() { | ||
74 | Err(Canceled) | ||
75 | } else { | ||
76 | Ok(()) | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
81 | impl BaseDatabase for RootDatabase { | ||
82 | fn id_maps(&self) -> &IdMaps { | ||
83 | &self.id_maps | ||
84 | } | ||
85 | } | ||
86 | 69 | ||
87 | impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase { | 70 | impl AsRef<LocationIntener<DefLoc, DefId>> for RootDatabase { |
88 | fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> { | 71 | fn as_ref(&self) -> &LocationIntener<DefLoc, DefId> { |
@@ -121,16 +104,16 @@ impl RootDatabase { | |||
121 | 104 | ||
122 | salsa::database_storage! { | 105 | salsa::database_storage! { |
123 | pub(crate) struct RootDatabaseStorage for RootDatabase { | 106 | pub(crate) struct RootDatabaseStorage for RootDatabase { |
124 | impl crate::input::FilesDatabase { | 107 | impl ra_db::FilesDatabase { |
125 | fn file_text() for crate::input::FileTextQuery; | 108 | fn file_text() for ra_db::FileTextQuery; |
126 | fn file_source_root() for crate::input::FileSourceRootQuery; | 109 | fn file_source_root() for ra_db::FileSourceRootQuery; |
127 | fn source_root() for crate::input::SourceRootQuery; | 110 | fn source_root() for ra_db::SourceRootQuery; |
128 | fn libraries() for crate::input::LibrariesQuery; | 111 | fn libraries() for ra_db::LibrariesQuery; |
129 | fn crate_graph() for crate::input::CrateGraphQuery; | 112 | fn crate_graph() for ra_db::CrateGraphQuery; |
130 | } | 113 | } |
131 | impl SyntaxDatabase { | 114 | impl ra_db::SyntaxDatabase { |
132 | fn source_file() for SourceFileQuery; | 115 | fn source_file() for ra_db::SourceFileQuery; |
133 | fn file_lines() for FileLinesQuery; | 116 | fn file_lines() for ra_db::FileLinesQuery; |
134 | } | 117 | } |
135 | impl symbol_index::SymbolsDatabase { | 118 | impl symbol_index::SymbolsDatabase { |
136 | fn file_symbols() for symbol_index::FileSymbolsQuery; | 119 | fn file_symbols() for symbol_index::FileSymbolsQuery; |
@@ -148,23 +131,3 @@ salsa::database_storage! { | |||
148 | } | 131 | } |
149 | } | 132 | } |
150 | } | 133 | } |
151 | |||
152 | salsa::query_group! { | ||
153 | pub(crate) trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { | ||
154 | fn source_file(file_id: FileId) -> SourceFileNode { | ||
155 | type SourceFileQuery; | ||
156 | } | ||
157 | fn file_lines(file_id: FileId) -> Arc<LineIndex> { | ||
158 | type FileLinesQuery; | ||
159 | } | ||
160 | } | ||
161 | } | ||
162 | |||
163 | fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { | ||
164 | let text = db.file_text(file_id); | ||
165 | SourceFileNode::parse(&*text) | ||
166 | } | ||
167 | fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
168 | let text = db.file_text(file_id); | ||
169 | Arc::new(LineIndex::new(&*text)) | ||
170 | } | ||
diff --git a/crates/ra_analysis/src/hir/db.rs b/crates/ra_analysis/src/hir/db.rs index bf0dc393a..0ae2086ff 100644 --- a/crates/ra_analysis/src/hir/db.rs +++ b/crates/ra_analysis/src/hir/db.rs | |||
@@ -4,10 +4,10 @@ use ra_syntax::{ | |||
4 | SyntaxNode, | 4 | SyntaxNode, |
5 | ast::FnDefNode, | 5 | ast::FnDefNode, |
6 | }; | 6 | }; |
7 | use ra_db::{SourceRootId, LocationIntener, SyntaxDatabase}; | ||
7 | 8 | ||
8 | use crate::{ | 9 | use crate::{ |
9 | FileId, | 10 | FileId, |
10 | db::SyntaxDatabase, | ||
11 | hir::{ | 11 | hir::{ |
12 | SourceFileItems, SourceItemId, | 12 | SourceFileItems, SourceItemId, |
13 | query_definitions, | 13 | query_definitions, |
@@ -15,8 +15,7 @@ use crate::{ | |||
15 | module::{ModuleId, ModuleTree, ModuleSource, | 15 | module::{ModuleId, ModuleTree, ModuleSource, |
16 | nameres::{ItemMap, InputModuleItems}}, | 16 | nameres::{ItemMap, InputModuleItems}}, |
17 | }, | 17 | }, |
18 | input::SourceRootId, | 18 | loc2id::{DefLoc, DefId, FnId}, |
19 | loc2id::{DefLoc, DefId, FnId, LocationIntener}, | ||
20 | Cancelable, | 19 | Cancelable, |
21 | }; | 20 | }; |
22 | 21 | ||
diff --git a/crates/ra_analysis/src/hir/function/scope.rs b/crates/ra_analysis/src/hir/function/scope.rs index 76b2fea68..ed789fede 100644 --- a/crates/ra_analysis/src/hir/function/scope.rs +++ b/crates/ra_analysis/src/hir/function/scope.rs | |||
@@ -5,9 +5,10 @@ use ra_syntax::{ | |||
5 | algo::generate, | 5 | algo::generate, |
6 | ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, | 6 | ast::{self, ArgListOwner, LoopBodyOwner, NameOwner}, |
7 | }; | 7 | }; |
8 | use ra_db::LocalSyntaxPtr; | ||
8 | 9 | ||
9 | use crate::{ | 10 | use crate::{ |
10 | syntax_ptr::LocalSyntaxPtr, | 11 | |
11 | arena::{Arena, Id}, | 12 | arena::{Arena, Id}, |
12 | }; | 13 | }; |
13 | 14 | ||
diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index d51ca2d59..c8f7ed58d 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs | |||
@@ -6,11 +6,11 @@ use ra_syntax::{ | |||
6 | }; | 6 | }; |
7 | use relative_path::RelativePathBuf; | 7 | use relative_path::RelativePathBuf; |
8 | use rustc_hash::{FxHashMap, FxHashSet}; | 8 | use rustc_hash::{FxHashMap, FxHashSet}; |
9 | use ra_db::{SourceRoot, SourceRootId, FileResolverImp}; | ||
9 | 10 | ||
10 | use crate::{ | 11 | use crate::{ |
11 | hir::HirDatabase, | 12 | hir::HirDatabase, |
12 | input::{SourceRoot, SourceRootId}, | 13 | Cancelable, FileId, |
13 | Cancelable, FileId, FileResolverImp, | ||
14 | }; | 14 | }; |
15 | 15 | ||
16 | use super::{ | 16 | use super::{ |
diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 893ec3a10..683cb5d4c 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs | |||
@@ -10,12 +10,12 @@ use ra_syntax::{ | |||
10 | ast::{self, AstNode, NameOwner}, | 10 | ast::{self, AstNode, NameOwner}, |
11 | SmolStr, SyntaxNode, | 11 | SmolStr, SyntaxNode, |
12 | }; | 12 | }; |
13 | use ra_db::SourceRootId; | ||
13 | use relative_path::RelativePathBuf; | 14 | use relative_path::RelativePathBuf; |
14 | 15 | ||
15 | use crate::{ | 16 | use crate::{ |
16 | FileId, FilePosition, Cancelable, | 17 | FileId, FilePosition, Cancelable, |
17 | hir::{Path, PathKind, HirDatabase, SourceItemId}, | 18 | hir::{Path, PathKind, HirDatabase, SourceItemId}, |
18 | input::SourceRootId, | ||
19 | arena::{Arena, Id}, | 19 | arena::{Arena, Id}, |
20 | loc2id::{DefLoc, DefId}, | 20 | loc2id::{DefLoc, DefId}, |
21 | }; | 21 | }; |
diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index f48f51c8d..5c87e7af2 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs | |||
@@ -19,12 +19,12 @@ use std::{ | |||
19 | }; | 19 | }; |
20 | 20 | ||
21 | use rustc_hash::FxHashMap; | 21 | use rustc_hash::FxHashMap; |
22 | |||
23 | use ra_syntax::{ | 22 | use ra_syntax::{ |
24 | TextRange, | 23 | TextRange, |
25 | SmolStr, SyntaxKind::{self, *}, | 24 | SmolStr, SyntaxKind::{self, *}, |
26 | ast::{self, AstNode} | 25 | ast::{self, AstNode} |
27 | }; | 26 | }; |
27 | use ra_db::SourceRootId; | ||
28 | 28 | ||
29 | use crate::{ | 29 | use crate::{ |
30 | Cancelable, FileId, | 30 | Cancelable, FileId, |
@@ -35,7 +35,6 @@ use crate::{ | |||
35 | HirDatabase, | 35 | HirDatabase, |
36 | module::{ModuleId, ModuleTree}, | 36 | module::{ModuleId, ModuleTree}, |
37 | }, | 37 | }, |
38 | input::SourceRootId, | ||
39 | }; | 38 | }; |
40 | 39 | ||
41 | /// Item map is the result of the name resolution. Item map contains, for each | 40 | /// Item map is the result of the name resolution. Item map contains, for each |
@@ -342,11 +341,11 @@ where | |||
342 | 341 | ||
343 | #[cfg(test)] | 342 | #[cfg(test)] |
344 | mod tests { | 343 | mod tests { |
344 | use ra_db::FilesDatabase; | ||
345 | use crate::{ | 345 | use crate::{ |
346 | AnalysisChange, | 346 | AnalysisChange, |
347 | mock_analysis::{MockAnalysis, analysis_and_position}, | 347 | mock_analysis::{MockAnalysis, analysis_and_position}, |
348 | hir::{self, HirDatabase}, | 348 | hir::{self, HirDatabase}, |
349 | input::FilesDatabase, | ||
350 | }; | 349 | }; |
351 | use super::*; | 350 | use super::*; |
352 | 351 | ||
diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs index 6570ca994..fbdf8eb67 100644 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ b/crates/ra_analysis/src/hir/query_definitions.rs | |||
@@ -8,6 +8,7 @@ use ra_syntax::{ | |||
8 | AstNode, SyntaxNode, SmolStr, | 8 | AstNode, SyntaxNode, SmolStr, |
9 | ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} | 9 | ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} |
10 | }; | 10 | }; |
11 | use ra_db::SourceRootId; | ||
11 | 12 | ||
12 | use crate::{ | 13 | use crate::{ |
13 | FileId, Cancelable, | 14 | FileId, Cancelable, |
@@ -21,7 +22,6 @@ use crate::{ | |||
21 | nameres::{InputModuleItems, ItemMap, Resolver}, | 22 | nameres::{InputModuleItems, ItemMap, Resolver}, |
22 | }, | 23 | }, |
23 | }, | 24 | }, |
24 | input::SourceRootId, | ||
25 | }; | 25 | }; |
26 | 26 | ||
27 | /// Resolve `FnId` to the corresponding `SyntaxNode` | 27 | /// Resolve `FnId` to the corresponding `SyntaxNode` |
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index c86bc111a..9a8694221 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -1,6 +1,5 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fmt, | 2 | fmt, |
3 | hash::{Hash, Hasher}, | ||
4 | sync::Arc, | 3 | sync::Arc, |
5 | }; | 4 | }; |
6 | 5 | ||
@@ -11,84 +10,24 @@ use ra_syntax::{ | |||
11 | SyntaxKind::*, | 10 | SyntaxKind::*, |
12 | SyntaxNodeRef, TextRange, TextUnit, | 11 | SyntaxNodeRef, TextRange, TextUnit, |
13 | }; | 12 | }; |
13 | use ra_db::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE, SyntaxDatabase, SourceFileQuery}; | ||
14 | use rayon::prelude::*; | 14 | use rayon::prelude::*; |
15 | use relative_path::RelativePath; | ||
16 | use rustc_hash::FxHashSet; | 15 | use rustc_hash::FxHashSet; |
17 | use salsa::{Database, ParallelDatabase}; | 16 | use salsa::{Database, ParallelDatabase}; |
18 | 17 | ||
19 | use crate::{ | 18 | use crate::{ |
20 | completion::{completions, CompletionItem}, | 19 | completion::{completions, CompletionItem}, |
21 | db::{self, SourceFileQuery, SyntaxDatabase}, | 20 | db, |
22 | hir::{ | 21 | hir::{ |
23 | self, | 22 | self, |
24 | FnSignatureInfo, | 23 | FnSignatureInfo, |
25 | Problem, | 24 | Problem, |
26 | }, | 25 | }, |
27 | input::{FilesDatabase, SourceRoot, SourceRootId, WORKSPACE}, | ||
28 | symbol_index::{SymbolIndex, SymbolsDatabase}, | 26 | symbol_index::{SymbolIndex, SymbolsDatabase}, |
29 | AnalysisChange, Cancelable, CrateGraph, CrateId, Diagnostic, FileId, FileResolver, | 27 | AnalysisChange, Cancelable, CrateId, Diagnostic, FileId, |
30 | FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, | 28 | FileSystemEdit, FilePosition, Query, SourceChange, SourceFileNodeEdit, |
31 | }; | 29 | }; |
32 | 30 | ||
33 | #[derive(Clone, Debug)] | ||
34 | pub(crate) struct FileResolverImp { | ||
35 | inner: Arc<FileResolver>, | ||
36 | } | ||
37 | |||
38 | impl PartialEq for FileResolverImp { | ||
39 | fn eq(&self, other: &FileResolverImp) -> bool { | ||
40 | self.inner() == other.inner() | ||
41 | } | ||
42 | } | ||
43 | |||
44 | impl Eq for FileResolverImp {} | ||
45 | |||
46 | impl Hash for FileResolverImp { | ||
47 | fn hash<H: Hasher>(&self, hasher: &mut H) { | ||
48 | self.inner().hash(hasher); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | impl FileResolverImp { | ||
53 | pub(crate) fn new(inner: Arc<FileResolver>) -> FileResolverImp { | ||
54 | FileResolverImp { inner } | ||
55 | } | ||
56 | pub(crate) fn file_stem(&self, file_id: FileId) -> String { | ||
57 | self.inner.file_stem(file_id) | ||
58 | } | ||
59 | pub(crate) fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> { | ||
60 | self.inner.resolve(file_id, path) | ||
61 | } | ||
62 | pub(crate) fn debug_path(&self, file_id: FileId) -> Option<std::path::PathBuf> { | ||
63 | self.inner.debug_path(file_id) | ||
64 | } | ||
65 | fn inner(&self) -> *const FileResolver { | ||
66 | &*self.inner | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl Default for FileResolverImp { | ||
71 | fn default() -> FileResolverImp { | ||
72 | #[derive(Debug)] | ||
73 | struct DummyResolver; | ||
74 | impl FileResolver for DummyResolver { | ||
75 | fn file_stem(&self, _file_: FileId) -> String { | ||
76 | panic!("file resolver not set") | ||
77 | } | ||
78 | fn resolve( | ||
79 | &self, | ||
80 | _file_id: FileId, | ||
81 | _path: &::relative_path::RelativePath, | ||
82 | ) -> Option<FileId> { | ||
83 | panic!("file resolver not set") | ||
84 | } | ||
85 | } | ||
86 | FileResolverImp { | ||
87 | inner: Arc::new(DummyResolver), | ||
88 | } | ||
89 | } | ||
90 | } | ||
91 | |||
92 | #[derive(Debug, Default)] | 31 | #[derive(Debug, Default)] |
93 | pub(crate) struct AnalysisHostImpl { | 32 | pub(crate) struct AnalysisHostImpl { |
94 | db: db::RootDatabase, | 33 | db: db::RootDatabase, |
@@ -105,7 +44,7 @@ impl AnalysisHostImpl { | |||
105 | 44 | ||
106 | for (file_id, text) in change.files_changed { | 45 | for (file_id, text) in change.files_changed { |
107 | self.db | 46 | self.db |
108 | .query_mut(crate::input::FileTextQuery) | 47 | .query_mut(ra_db::FileTextQuery) |
109 | .set(file_id, Arc::new(text)) | 48 | .set(file_id, Arc::new(text)) |
110 | } | 49 | } |
111 | if !(change.files_added.is_empty() && change.files_removed.is_empty()) { | 50 | if !(change.files_added.is_empty() && change.files_removed.is_empty()) { |
@@ -115,22 +54,22 @@ impl AnalysisHostImpl { | |||
115 | let mut source_root = SourceRoot::clone(&self.db.source_root(WORKSPACE)); | 54 | let mut source_root = SourceRoot::clone(&self.db.source_root(WORKSPACE)); |
116 | for (file_id, text) in change.files_added { | 55 | for (file_id, text) in change.files_added { |
117 | self.db | 56 | self.db |
118 | .query_mut(crate::input::FileTextQuery) | 57 | .query_mut(ra_db::FileTextQuery) |
119 | .set(file_id, Arc::new(text)); | 58 | .set(file_id, Arc::new(text)); |
120 | self.db | 59 | self.db |
121 | .query_mut(crate::input::FileSourceRootQuery) | 60 | .query_mut(ra_db::FileSourceRootQuery) |
122 | .set(file_id, crate::input::WORKSPACE); | 61 | .set(file_id, ra_db::WORKSPACE); |
123 | source_root.files.insert(file_id); | 62 | source_root.files.insert(file_id); |
124 | } | 63 | } |
125 | for file_id in change.files_removed { | 64 | for file_id in change.files_removed { |
126 | self.db | 65 | self.db |
127 | .query_mut(crate::input::FileTextQuery) | 66 | .query_mut(ra_db::FileTextQuery) |
128 | .set(file_id, Arc::new(String::new())); | 67 | .set(file_id, Arc::new(String::new())); |
129 | source_root.files.remove(&file_id); | 68 | source_root.files.remove(&file_id); |
130 | } | 69 | } |
131 | source_root.file_resolver = file_resolver; | 70 | source_root.file_resolver = file_resolver; |
132 | self.db | 71 | self.db |
133 | .query_mut(crate::input::SourceRootQuery) | 72 | .query_mut(ra_db::SourceRootQuery) |
134 | .set(WORKSPACE, Arc::new(source_root)) | 73 | .set(WORKSPACE, Arc::new(source_root)) |
135 | } | 74 | } |
136 | if !change.libraries_added.is_empty() { | 75 | if !change.libraries_added.is_empty() { |
@@ -147,10 +86,10 @@ impl AnalysisHostImpl { | |||
147 | library.file_resolver.debug_path(file_id) | 86 | library.file_resolver.debug_path(file_id) |
148 | ); | 87 | ); |
149 | self.db | 88 | self.db |
150 | .query_mut(crate::input::FileSourceRootQuery) | 89 | .query_mut(ra_db::FileSourceRootQuery) |
151 | .set_constant(file_id, source_root_id); | 90 | .set_constant(file_id, source_root_id); |
152 | self.db | 91 | self.db |
153 | .query_mut(crate::input::FileTextQuery) | 92 | .query_mut(ra_db::FileTextQuery) |
154 | .set_constant(file_id, Arc::new(text)); | 93 | .set_constant(file_id, Arc::new(text)); |
155 | } | 94 | } |
156 | let source_root = SourceRoot { | 95 | let source_root = SourceRoot { |
@@ -158,19 +97,19 @@ impl AnalysisHostImpl { | |||
158 | file_resolver: library.file_resolver, | 97 | file_resolver: library.file_resolver, |
159 | }; | 98 | }; |
160 | self.db | 99 | self.db |
161 | .query_mut(crate::input::SourceRootQuery) | 100 | .query_mut(ra_db::SourceRootQuery) |
162 | .set(source_root_id, Arc::new(source_root)); | 101 | .set(source_root_id, Arc::new(source_root)); |
163 | self.db | 102 | self.db |
164 | .query_mut(crate::symbol_index::LibrarySymbolsQuery) | 103 | .query_mut(crate::symbol_index::LibrarySymbolsQuery) |
165 | .set(source_root_id, Arc::new(library.symbol_index)); | 104 | .set(source_root_id, Arc::new(library.symbol_index)); |
166 | } | 105 | } |
167 | self.db | 106 | self.db |
168 | .query_mut(crate::input::LibrariesQuery) | 107 | .query_mut(ra_db::LibrariesQuery) |
169 | .set((), Arc::new(libraries)); | 108 | .set((), Arc::new(libraries)); |
170 | } | 109 | } |
171 | if let Some(crate_graph) = change.crate_graph { | 110 | if let Some(crate_graph) = change.crate_graph { |
172 | self.db | 111 | self.db |
173 | .query_mut(crate::input::CrateGraphQuery) | 112 | .query_mut(ra_db::CrateGraphQuery) |
174 | .set((), Arc::new(crate_graph)) | 113 | .set((), Arc::new(crate_graph)) |
175 | } | 114 | } |
176 | } | 115 | } |
@@ -261,7 +200,7 @@ impl AnalysisImpl { | |||
261 | Ok(crate_id.into_iter().collect()) | 200 | Ok(crate_id.into_iter().collect()) |
262 | } | 201 | } |
263 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { | 202 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { |
264 | self.db.crate_graph().crate_roots[&crate_id] | 203 | self.db.crate_graph().crate_root(crate_id) |
265 | } | 204 | } |
266 | pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> { | 205 | pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> { |
267 | completions(&self.db, position) | 206 | completions(&self.db, position) |
@@ -546,16 +485,6 @@ impl SourceChange { | |||
546 | } | 485 | } |
547 | } | 486 | } |
548 | 487 | ||
549 | impl CrateGraph { | ||
550 | fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { | ||
551 | let (&crate_id, _) = self | ||
552 | .crate_roots | ||
553 | .iter() | ||
554 | .find(|(_crate_id, &root_id)| root_id == file_id)?; | ||
555 | Some(crate_id) | ||
556 | } | ||
557 | } | ||
558 | |||
559 | enum FnCallNode<'a> { | 488 | enum FnCallNode<'a> { |
560 | CallExpr(ast::CallExpr<'a>), | 489 | CallExpr(ast::CallExpr<'a>), |
561 | MethodCallExpr(ast::MethodCallExpr<'a>), | 490 | MethodCallExpr(ast::MethodCallExpr<'a>), |
diff --git a/crates/ra_analysis/src/input.rs b/crates/ra_analysis/src/input.rs deleted file mode 100644 index e601cd58a..000000000 --- a/crates/ra_analysis/src/input.rs +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | use std::{fmt, sync::Arc}; | ||
2 | |||
3 | use relative_path::RelativePath; | ||
4 | use rustc_hash::FxHashMap; | ||
5 | use rustc_hash::FxHashSet; | ||
6 | use salsa; | ||
7 | |||
8 | use crate::FileResolverImp; | ||
9 | |||
10 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
11 | pub struct FileId(pub u32); | ||
12 | |||
13 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
14 | pub struct CrateId(pub u32); | ||
15 | |||
16 | #[derive(Debug, Clone, Default, PartialEq, Eq)] | ||
17 | pub struct CrateGraph { | ||
18 | pub(crate) crate_roots: FxHashMap<CrateId, FileId>, | ||
19 | } | ||
20 | |||
21 | impl CrateGraph { | ||
22 | pub fn new() -> CrateGraph { | ||
23 | CrateGraph::default() | ||
24 | } | ||
25 | pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId { | ||
26 | let crate_id = CrateId(self.crate_roots.len() as u32); | ||
27 | let prev = self.crate_roots.insert(crate_id, file_id); | ||
28 | assert!(prev.is_none()); | ||
29 | crate_id | ||
30 | } | ||
31 | } | ||
32 | |||
33 | pub trait FileResolver: fmt::Debug + Send + Sync + 'static { | ||
34 | fn file_stem(&self, file_id: FileId) -> String; | ||
35 | fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>; | ||
36 | fn debug_path(&self, _1file_id: FileId) -> Option<std::path::PathBuf> { | ||
37 | None | ||
38 | } | ||
39 | } | ||
40 | |||
41 | salsa::query_group! { | ||
42 | pub(crate) trait FilesDatabase: salsa::Database { | ||
43 | fn file_text(file_id: FileId) -> Arc<String> { | ||
44 | type FileTextQuery; | ||
45 | storage input; | ||
46 | } | ||
47 | fn file_source_root(file_id: FileId) -> SourceRootId { | ||
48 | type FileSourceRootQuery; | ||
49 | storage input; | ||
50 | } | ||
51 | fn source_root(id: SourceRootId) -> Arc<SourceRoot> { | ||
52 | type SourceRootQuery; | ||
53 | storage input; | ||
54 | } | ||
55 | fn libraries() -> Arc<Vec<SourceRootId>> { | ||
56 | type LibrariesQuery; | ||
57 | storage input; | ||
58 | } | ||
59 | fn crate_graph() -> Arc<CrateGraph> { | ||
60 | type CrateGraphQuery; | ||
61 | storage input; | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | |||
66 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
67 | pub(crate) struct SourceRootId(pub(crate) u32); | ||
68 | |||
69 | #[derive(Default, Clone, Debug, PartialEq, Eq)] | ||
70 | pub(crate) struct SourceRoot { | ||
71 | pub(crate) file_resolver: FileResolverImp, | ||
72 | pub(crate) files: FxHashSet<FileId>, | ||
73 | } | ||
74 | |||
75 | pub(crate) const WORKSPACE: SourceRootId = SourceRootId(0); | ||
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index c0e43544e..012d36b8e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -19,8 +19,6 @@ macro_rules! ctry { | |||
19 | } | 19 | } |
20 | 20 | ||
21 | mod arena; | 21 | mod arena; |
22 | mod syntax_ptr; | ||
23 | mod input; | ||
24 | mod db; | 22 | mod db; |
25 | mod loc2id; | 23 | mod loc2id; |
26 | mod imp; | 24 | mod imp; |
@@ -32,35 +30,27 @@ pub mod mock_analysis; | |||
32 | use std::{fmt, sync::Arc}; | 30 | use std::{fmt, sync::Arc}; |
33 | 31 | ||
34 | use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; | 32 | use ra_syntax::{AtomEdit, SourceFileNode, TextRange, TextUnit}; |
33 | use ra_db::FileResolverImp; | ||
35 | use rayon::prelude::*; | 34 | use rayon::prelude::*; |
36 | use relative_path::RelativePathBuf; | 35 | use relative_path::RelativePathBuf; |
37 | 36 | ||
38 | use crate::{ | 37 | use crate::{ |
39 | imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, | 38 | imp::{AnalysisHostImpl, AnalysisImpl}, |
40 | symbol_index::SymbolIndex, | 39 | symbol_index::SymbolIndex, |
41 | }; | 40 | }; |
42 | 41 | ||
43 | pub use crate::{ | 42 | pub use crate::{ |
44 | completion::CompletionItem, | 43 | completion::CompletionItem, |
45 | hir::FnSignatureInfo, | 44 | hir::FnSignatureInfo, |
46 | input::{CrateGraph, CrateId, FileId, FileResolver}, | ||
47 | }; | 45 | }; |
48 | pub use ra_editor::{ | 46 | pub use ra_editor::{ |
49 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, | 47 | FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, RunnableKind, StructureNode, |
50 | }; | 48 | }; |
51 | 49 | ||
52 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | 50 | pub use ra_db::{ |
53 | pub struct Canceled; | 51 | Canceled, Cancelable, |
54 | 52 | CrateGraph, CrateId, FileId, FileResolver | |
55 | pub type Cancelable<T> = Result<T, Canceled>; | 53 | }; |
56 | |||
57 | impl std::fmt::Display for Canceled { | ||
58 | fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
59 | fmt.write_str("Canceled") | ||
60 | } | ||
61 | } | ||
62 | |||
63 | impl std::error::Error for Canceled {} | ||
64 | 54 | ||
65 | #[derive(Default)] | 55 | #[derive(Default)] |
66 | pub struct AnalysisChange { | 56 | pub struct AnalysisChange { |
diff --git a/crates/ra_analysis/src/loc2id.rs b/crates/ra_analysis/src/loc2id.rs index 2aa141130..7956431ab 100644 --- a/crates/ra_analysis/src/loc2id.rs +++ b/crates/ra_analysis/src/loc2id.rs | |||
@@ -1,74 +1,10 @@ | |||
1 | use parking_lot::Mutex; | 1 | use ra_db::SourceRootId; |
2 | |||
3 | use std::hash::Hash; | ||
4 | |||
5 | use rustc_hash::FxHashMap; | ||
6 | 2 | ||
7 | use crate::{ | 3 | use crate::{ |
8 | hir::{SourceItemId, ModuleId}, | 4 | hir::{SourceItemId, ModuleId}, |
9 | input::SourceRootId, | ||
10 | }; | 5 | }; |
11 | 6 | ||
12 | /// There are two principle ways to refer to things: | 7 | use ra_db::{NumericId, LocationIntener}; |
13 | /// - by their locatinon (module in foo/bar/baz.rs at line 42) | ||
14 | /// - by their numeric id (module `ModuleId(42)`) | ||
15 | /// | ||
16 | /// The first one is more powerful (you can actually find the thing in question | ||
17 | /// by id), but the second one is so much more compact. | ||
18 | /// | ||
19 | /// `Loc2IdMap` allows us to have a cake an eat it as well: by maintaining a | ||
20 | /// bidirectional mapping between positional and numeric ids, we can use compact | ||
21 | /// representation wich still allows us to get the actual item | ||
22 | #[derive(Debug)] | ||
23 | struct Loc2IdMap<LOC, ID> | ||
24 | where | ||
25 | ID: NumericId, | ||
26 | LOC: Clone + Eq + Hash, | ||
27 | { | ||
28 | loc2id: FxHashMap<LOC, ID>, | ||
29 | id2loc: FxHashMap<ID, LOC>, | ||
30 | } | ||
31 | |||
32 | impl<LOC, ID> Default for Loc2IdMap<LOC, ID> | ||
33 | where | ||
34 | ID: NumericId, | ||
35 | LOC: Clone + Eq + Hash, | ||
36 | { | ||
37 | fn default() -> Self { | ||
38 | Loc2IdMap { | ||
39 | loc2id: FxHashMap::default(), | ||
40 | id2loc: FxHashMap::default(), | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | impl<LOC, ID> Loc2IdMap<LOC, ID> | ||
46 | where | ||
47 | ID: NumericId, | ||
48 | LOC: Clone + Eq + Hash, | ||
49 | { | ||
50 | pub fn loc2id(&mut self, loc: &LOC) -> ID { | ||
51 | match self.loc2id.get(loc) { | ||
52 | Some(id) => return id.clone(), | ||
53 | None => (), | ||
54 | } | ||
55 | let id = self.loc2id.len(); | ||
56 | assert!(id < u32::max_value() as usize); | ||
57 | let id = ID::from_u32(id as u32); | ||
58 | self.loc2id.insert(loc.clone(), id.clone()); | ||
59 | self.id2loc.insert(id.clone(), loc.clone()); | ||
60 | id | ||
61 | } | ||
62 | |||
63 | pub fn id2loc(&self, id: ID) -> LOC { | ||
64 | self.id2loc[&id].clone() | ||
65 | } | ||
66 | } | ||
67 | |||
68 | pub(crate) trait NumericId: Clone + Eq + Hash { | ||
69 | fn from_u32(id: u32) -> Self; | ||
70 | fn to_u32(self) -> u32; | ||
71 | } | ||
72 | 8 | ||
73 | macro_rules! impl_numeric_id { | 9 | macro_rules! impl_numeric_id { |
74 | ($id:ident) => { | 10 | ($id:ident) => { |
@@ -131,37 +67,3 @@ pub(crate) struct IdMaps { | |||
131 | pub(crate) fns: LocationIntener<SourceItemId, FnId>, | 67 | pub(crate) fns: LocationIntener<SourceItemId, FnId>, |
132 | pub(crate) defs: LocationIntener<DefLoc, DefId>, | 68 | pub(crate) defs: LocationIntener<DefLoc, DefId>, |
133 | } | 69 | } |
134 | |||
135 | #[derive(Debug)] | ||
136 | pub(crate) struct LocationIntener<LOC, ID> | ||
137 | where | ||
138 | ID: NumericId, | ||
139 | LOC: Clone + Eq + Hash, | ||
140 | { | ||
141 | map: Mutex<Loc2IdMap<LOC, ID>>, | ||
142 | } | ||
143 | |||
144 | impl<LOC, ID> Default for LocationIntener<LOC, ID> | ||
145 | where | ||
146 | ID: NumericId, | ||
147 | LOC: Clone + Eq + Hash, | ||
148 | { | ||
149 | fn default() -> Self { | ||
150 | LocationIntener { | ||
151 | map: Default::default(), | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | impl<LOC, ID> LocationIntener<LOC, ID> | ||
157 | where | ||
158 | ID: NumericId, | ||
159 | LOC: Clone + Eq + Hash, | ||
160 | { | ||
161 | fn loc2id(&self, loc: &LOC) -> ID { | ||
162 | self.map.lock().loc2id(loc) | ||
163 | } | ||
164 | fn id2loc(&self, id: ID) -> LOC { | ||
165 | self.map.lock().id2loc(id) | ||
166 | } | ||
167 | } | ||
diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index 747b34e38..b48a37229 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs | |||
@@ -9,13 +9,12 @@ use ra_syntax::{ | |||
9 | SourceFileNode, | 9 | SourceFileNode, |
10 | SyntaxKind::{self, *}, | 10 | SyntaxKind::{self, *}, |
11 | }; | 11 | }; |
12 | use ra_db::{SyntaxDatabase, SourceRootId}; | ||
12 | use rayon::prelude::*; | 13 | use rayon::prelude::*; |
13 | 14 | ||
14 | use crate::{ | 15 | use crate::{ |
15 | Cancelable, | 16 | Cancelable, |
16 | FileId, Query, | 17 | FileId, Query, |
17 | db::SyntaxDatabase, | ||
18 | input::SourceRootId, | ||
19 | }; | 18 | }; |
20 | 19 | ||
21 | salsa::query_group! { | 20 | salsa::query_group! { |
diff --git a/crates/ra_analysis/src/syntax_ptr.rs b/crates/ra_analysis/src/syntax_ptr.rs deleted file mode 100644 index f4b05fc19..000000000 --- a/crates/ra_analysis/src/syntax_ptr.rs +++ /dev/null | |||
@@ -1,48 +0,0 @@ | |||
1 | use ra_syntax::{SourceFileNode, SyntaxKind, SyntaxNode, SyntaxNodeRef, TextRange}; | ||
2 | |||
3 | /// A pionter to a syntax node inside a file. | ||
4 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
5 | pub(crate) struct LocalSyntaxPtr { | ||
6 | range: TextRange, | ||
7 | kind: SyntaxKind, | ||
8 | } | ||
9 | |||
10 | impl LocalSyntaxPtr { | ||
11 | pub(crate) fn new(node: SyntaxNodeRef) -> LocalSyntaxPtr { | ||
12 | LocalSyntaxPtr { | ||
13 | range: node.range(), | ||
14 | kind: node.kind(), | ||
15 | } | ||
16 | } | ||
17 | |||
18 | pub(crate) fn resolve(self, file: &SourceFileNode) -> SyntaxNode { | ||
19 | let mut curr = file.syntax(); | ||
20 | loop { | ||
21 | if curr.range() == self.range && curr.kind() == self.kind { | ||
22 | return curr.owned(); | ||
23 | } | ||
24 | curr = curr | ||
25 | .children() | ||
26 | .find(|it| self.range.is_subrange(&it.range())) | ||
27 | .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) | ||
28 | } | ||
29 | } | ||
30 | |||
31 | pub(crate) fn range(self) -> TextRange { | ||
32 | self.range | ||
33 | } | ||
34 | } | ||
35 | |||
36 | #[test] | ||
37 | fn test_local_syntax_ptr() { | ||
38 | use ra_syntax::{ast, AstNode}; | ||
39 | let file = SourceFileNode::parse("struct Foo { f: u32, }"); | ||
40 | let field = file | ||
41 | .syntax() | ||
42 | .descendants() | ||
43 | .find_map(ast::NamedFieldDef::cast) | ||
44 | .unwrap(); | ||
45 | let ptr = LocalSyntaxPtr::new(field.syntax()); | ||
46 | let field_syntax = ptr.resolve(&file); | ||
47 | assert_eq!(field.syntax(), field_syntax); | ||
48 | } | ||