aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-25 12:16:50 +0000
committerAleksey Kladov <[email protected]>2019-01-25 12:16:50 +0000
commit8cf092d5de113fc218b84421a2db4449a370ccb6 (patch)
tree8fe4173465c56a0ce4dc68ee4b43ef8f7c1293de /crates/ra_db/src
parentae97cd59ff086e7efb6409f14c2f8ae8861596e4 (diff)
:arrow_up salsa
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r--crates/ra_db/src/input.rs42
-rw-r--r--crates/ra_db/src/lib.rs68
2 files changed, 54 insertions, 56 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 9825d52cf..275894252 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -5,11 +5,8 @@
5/// Note that neither this module, nor any other part of the analyzer's core do 5/// Note that neither this module, nor any other part of the analyzer's core do
6/// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how 6/// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how
7/// actual IO is done and lowered to input. 7/// actual IO is done and lowered to input.
8use std::sync::Arc;
9
10use relative_path::RelativePathBuf; 8use relative_path::RelativePathBuf;
11use rustc_hash::FxHashMap; 9use rustc_hash::FxHashMap;
12use salsa;
13 10
14use ra_syntax::SmolStr; 11use ra_syntax::SmolStr;
15use rustc_hash::FxHashSet; 12use rustc_hash::FxHashSet;
@@ -146,45 +143,6 @@ impl CrateGraph {
146 } 143 }
147} 144}
148 145
149#[salsa::query_group]
150pub trait FilesDatabase: salsa::Database {
151 /// Text of the file.
152 #[salsa::input]
153 fn file_text(&self, file_id: FileId) -> Arc<String>;
154 /// Path to a file, relative to the root of its source root.
155 #[salsa::input]
156 fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
157 /// Source root of the file.
158 #[salsa::input]
159 fn file_source_root(&self, file_id: FileId) -> SourceRootId;
160 /// Contents of the source root.
161 #[salsa::input]
162 fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
163 fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
164 /// The set of "local" (that is, from the current workspace) roots.
165 /// Files in local roots are assumed to change frequently.
166 #[salsa::input]
167 fn local_roots(&self) -> Arc<Vec<SourceRootId>>;
168 /// The set of roots for crates.io libraries.
169 /// Files in libraries are assumed to never change.
170 #[salsa::input]
171 fn library_roots(&self) -> Arc<Vec<SourceRootId>>;
172 /// The crate graph.
173 #[salsa::input]
174 fn crate_graph(&self) -> Arc<CrateGraph>;
175}
176
177fn source_root_crates(db: &impl FilesDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> {
178 let root = db.source_root(id);
179 let graph = db.crate_graph();
180 let res = root
181 .files
182 .values()
183 .filter_map(|&it| graph.crate_id_for_crate_root(it))
184 .collect::<Vec<_>>();
185 Arc::new(res)
186}
187
188#[cfg(test)] 146#[cfg(test)]
189mod tests { 147mod tests {
190 use super::{CrateGraph, FileId, SmolStr}; 148 use super::{CrateGraph, FileId, SmolStr};
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index 84759c75a..7e13f70bc 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -4,17 +4,18 @@ mod input;
4mod loc2id; 4mod loc2id;
5pub mod mock; 5pub mod mock;
6 6
7use std::panic; 7use std::{
8 panic, sync::Arc,
9};
8 10
9use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; 11use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc};
12use relative_path::RelativePathBuf;
10 13
11pub use ::salsa as salsa; 14pub use ::salsa as salsa;
12pub use crate::{ 15pub use crate::{
13 cancellation::Canceled, 16 cancellation::Canceled,
14 input::{ 17 input::{
15 FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, 18 FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency,
16 FileTextQuery, FileSourceRootQuery, SourceRootQuery, SourceRootCratesQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery,
17 FileRelativePathQuery
18 }, 19 },
19 loc2id::LocationIntener, 20 loc2id::LocationIntener,
20}; 21};
@@ -50,16 +51,6 @@ pub trait BaseDatabase: salsa::Database + panic::RefUnwindSafe {
50 } 51 }
51} 52}
52 53
53#[salsa::query_group]
54pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase {
55 fn source_file(&self, file_id: FileId) -> TreeArc<SourceFile>;
56}
57
58fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreeArc<SourceFile> {
59 let text = db.file_text(file_id);
60 SourceFile::parse(&*text)
61}
62
63#[derive(Clone, Copy, Debug)] 54#[derive(Clone, Copy, Debug)]
64pub struct FilePosition { 55pub struct FilePosition {
65 pub file_id: FileId, 56 pub file_id: FileId,
@@ -71,3 +62,52 @@ pub struct FileRange {
71 pub file_id: FileId, 62 pub file_id: FileId,
72 pub range: TextRange, 63 pub range: TextRange,
73} 64}
65
66#[salsa::query_group]
67pub trait FilesDatabase: salsa::Database {
68 /// Text of the file.
69 #[salsa::input]
70 fn file_text(&self, file_id: FileId) -> Arc<String>;
71 /// Path to a file, relative to the root of its source root.
72 #[salsa::input]
73 fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
74 /// Source root of the file.
75 #[salsa::input]
76 fn file_source_root(&self, file_id: FileId) -> SourceRootId;
77 /// Contents of the source root.
78 #[salsa::input]
79 fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
80 fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
81 /// The set of "local" (that is, from the current workspace) roots.
82 /// Files in local roots are assumed to change frequently.
83 #[salsa::input]
84 fn local_roots(&self) -> Arc<Vec<SourceRootId>>;
85 /// The set of roots for crates.io libraries.
86 /// Files in libraries are assumed to never change.
87 #[salsa::input]
88 fn library_roots(&self) -> Arc<Vec<SourceRootId>>;
89 /// The crate graph.
90 #[salsa::input]
91 fn crate_graph(&self) -> Arc<CrateGraph>;
92}
93
94fn source_root_crates(db: &impl FilesDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> {
95 let root = db.source_root(id);
96 let graph = db.crate_graph();
97 let res = root
98 .files
99 .values()
100 .filter_map(|&it| graph.crate_id_for_crate_root(it))
101 .collect::<Vec<_>>();
102 Arc::new(res)
103}
104
105#[salsa::query_group]
106pub trait SyntaxDatabase: FilesDatabase + BaseDatabase {
107 fn source_file(&self, file_id: FileId) -> TreeArc<SourceFile>;
108}
109
110fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreeArc<SourceFile> {
111 let text = db.file_text(file_id);
112 SourceFile::parse(&*text)
113}