diff options
author | Aleksey Kladov <[email protected]> | 2018-12-20 10:47:32 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-20 11:12:47 +0000 |
commit | bb2bafb606cb38f0a9ec8bb44693c149ba4288cd (patch) | |
tree | de330adb9f09ab79cde7d90be8645423eebb84bc /crates/ra_db/src | |
parent | d8c6b8d999079fdd601b07b91fc46f40f17ee4f1 (diff) |
docs for input queries
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/input.rs | 56 |
1 files changed, 45 insertions, 11 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index cccf37cc2..095730521 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -1,3 +1,6 @@ | |||
1 | /// This modules specifies the input to rust-analyzer. In some sense, this is | ||
2 | /// **the** most important module, because all other fancy stuff is strickly | ||
3 | /// derived from this input. | ||
1 | use std::sync::Arc; | 4 | use std::sync::Arc; |
2 | 5 | ||
3 | use rustc_hash::{FxHashMap}; | 6 | use rustc_hash::{FxHashMap}; |
@@ -5,20 +8,48 @@ use relative_path::RelativePathBuf; | |||
5 | use ra_syntax::SmolStr; | 8 | use ra_syntax::SmolStr; |
6 | use salsa; | 9 | use salsa; |
7 | 10 | ||
8 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | 11 | /// `FileId` is an integer which uniquely identifies a file. File paths are |
9 | pub struct SourceRootId(pub u32); | 12 | /// messy and system-dependent, so most of the code should work directly with |
10 | 13 | /// `FileId`, without inspecting the path. The mapping between `FileId` and path | |
14 | /// and `SourceRoot` is constant. File rename is represented as a pair of | ||
15 | /// deletion/creation. | ||
11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 16 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
12 | pub struct FileId(pub u32); | 17 | pub struct FileId(pub u32); |
13 | 18 | ||
14 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 19 | /// Files are grouped into source roots. A source root is a directory on the |
15 | pub struct CrateId(pub u32); | 20 | /// file systems which is watched for changes. Typically it corresponds to a |
21 | /// Cargo package. Source roots *might* be nested: in this case, file belongs to | ||
22 | /// the nearest enclosing source root. Path to files are always relative to a | ||
23 | /// source root, and analyzer does not know the root path of the source root at | ||
24 | /// all. So, a file from one source root can't refere a file in another source | ||
25 | /// root by path. | ||
26 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
27 | pub struct SourceRootId(pub u32); | ||
28 | |||
29 | #[derive(Default, Clone, Debug, PartialEq, Eq)] | ||
30 | pub struct SourceRoot { | ||
31 | pub files: FxHashMap<RelativePathBuf, FileId>, | ||
32 | } | ||
16 | 33 | ||
34 | /// `CrateGraph` is a bit of information which turns a set of text files into a | ||
35 | /// number of Rust crates. Each Crate is the `FileId` of it's root module, the | ||
36 | /// set of cfg flags (not yet implemented) and the set of dependencies. Note | ||
37 | /// that, due to cfg's, there might be several crates for a single `FileId`! As | ||
38 | /// in the rust-lang proper, a crate does not have a name. Instead, names are | ||
39 | /// specified on dependency edges. That is, a crate might be known under | ||
40 | /// different names in different dependant crates. | ||
41 | /// | ||
42 | /// Note that `CrateGraph` is build-system agnostic: it's a concept of the Rust | ||
43 | /// langauge proper, not a concept of the build system. In practice, we get | ||
44 | /// `CrateGraph` by lowering `cargo metadata` output. | ||
17 | #[derive(Debug, Clone, Default, PartialEq, Eq)] | 45 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
18 | pub struct CrateGraph { | 46 | pub struct CrateGraph { |
19 | arena: FxHashMap<CrateId, CrateData>, | 47 | arena: FxHashMap<CrateId, CrateData>, |
20 | } | 48 | } |
21 | 49 | ||
50 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | ||
51 | pub struct CrateId(pub u32); | ||
52 | |||
22 | #[derive(Debug, Clone, PartialEq, Eq)] | 53 | #[derive(Debug, Clone, PartialEq, Eq)] |
23 | struct CrateData { | 54 | struct CrateData { |
24 | file_id: FileId, | 55 | file_id: FileId, |
@@ -57,7 +88,7 @@ impl CrateGraph { | |||
57 | assert!(prev.is_none()); | 88 | assert!(prev.is_none()); |
58 | crate_id | 89 | crate_id |
59 | } | 90 | } |
60 | //FIXME: check that we don't have cycles here. | 91 | // FIXME: check that we don't have cycles here. |
61 | // Just a simple depth first search from `to` should work, | 92 | // Just a simple depth first search from `to` should work, |
62 | // the graph is small. | 93 | // the graph is small. |
63 | pub fn add_dep(&mut self, from: CrateId, name: SmolStr, to: CrateId) { | 94 | pub fn add_dep(&mut self, from: CrateId, name: SmolStr, to: CrateId) { |
@@ -83,6 +114,7 @@ impl CrateGraph { | |||
83 | 114 | ||
84 | salsa::query_group! { | 115 | salsa::query_group! { |
85 | pub trait FilesDatabase: salsa::Database { | 116 | pub trait FilesDatabase: salsa::Database { |
117 | /// Text of the file. | ||
86 | fn file_text(file_id: FileId) -> Arc<String> { | 118 | fn file_text(file_id: FileId) -> Arc<String> { |
87 | type FileTextQuery; | 119 | type FileTextQuery; |
88 | storage input; | 120 | storage input; |
@@ -92,30 +124,32 @@ salsa::query_group! { | |||
92 | type FileRelativePathQuery; | 124 | type FileRelativePathQuery; |
93 | storage input; | 125 | storage input; |
94 | } | 126 | } |
127 | /// Source root of the file. | ||
95 | fn file_source_root(file_id: FileId) -> SourceRootId { | 128 | fn file_source_root(file_id: FileId) -> SourceRootId { |
96 | type FileSourceRootQuery; | 129 | type FileSourceRootQuery; |
97 | storage input; | 130 | storage input; |
98 | } | 131 | } |
132 | /// Contents of the source root. | ||
99 | fn source_root(id: SourceRootId) -> Arc<SourceRoot> { | 133 | fn source_root(id: SourceRootId) -> Arc<SourceRoot> { |
100 | type SourceRootQuery; | 134 | type SourceRootQuery; |
101 | storage input; | 135 | storage input; |
102 | } | 136 | } |
137 | /// The set of "local" (that is, from the current workspace) roots. | ||
138 | /// Files in local roots are assumed to change frequently. | ||
103 | fn local_roots() -> Arc<Vec<SourceRootId>> { | 139 | fn local_roots() -> Arc<Vec<SourceRootId>> { |
104 | type LocalRootsQuery; | 140 | type LocalRootsQuery; |
105 | storage input; | 141 | storage input; |
106 | } | 142 | } |
143 | /// The set of roots for crates.io libraries. | ||
144 | /// Files in libraries are assumed to never change. | ||
107 | fn library_roots() -> Arc<Vec<SourceRootId>> { | 145 | fn library_roots() -> Arc<Vec<SourceRootId>> { |
108 | type LibraryRootsQuery; | 146 | type LibraryRootsQuery; |
109 | storage input; | 147 | storage input; |
110 | } | 148 | } |
149 | /// The crate graph. | ||
111 | fn crate_graph() -> Arc<CrateGraph> { | 150 | fn crate_graph() -> Arc<CrateGraph> { |
112 | type CrateGraphQuery; | 151 | type CrateGraphQuery; |
113 | storage input; | 152 | storage input; |
114 | } | 153 | } |
115 | } | 154 | } |
116 | } | 155 | } |
117 | |||
118 | #[derive(Default, Clone, Debug, PartialEq, Eq)] | ||
119 | pub struct SourceRoot { | ||
120 | pub files: FxHashMap<RelativePathBuf, FileId>, | ||
121 | } | ||