aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/db
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-10-07 11:18:25 +0100
committerAleksey Kladov <[email protected]>2018-10-15 19:19:49 +0100
commitd8aee31a600a8a8a56ddee0ee2ff1c5d5ba2320b (patch)
tree2610e72a444870434fcca6f1d69751800b957543 /crates/ra_analysis/src/db
parent93d77e9b22c38a3587f3b7d5c3c6d517b66f3314 (diff)
start salsa migration
Diffstat (limited to 'crates/ra_analysis/src/db')
-rw-r--r--crates/ra_analysis/src/db/mod.rs226
1 files changed, 167 insertions, 59 deletions
diff --git a/crates/ra_analysis/src/db/mod.rs b/crates/ra_analysis/src/db/mod.rs
index 4eb7d922d..4b3e0fc90 100644
--- a/crates/ra_analysis/src/db/mod.rs
+++ b/crates/ra_analysis/src/db/mod.rs
@@ -1,85 +1,193 @@
1mod imp;
2
3use std::{ 1use std::{
2 fmt,
4 sync::Arc, 3 sync::Arc,
4 hash::{Hash, Hasher},
5 collections::HashSet,
5}; 6};
6use im;
7use salsa; 7use salsa;
8use crate::{FileId, imp::FileResolverImp}; 8use ra_syntax::File;
9use ra_editor::{LineIndex};
10use crate::{
11 symbol_index::SymbolIndex,
12 FileId, FileResolverImp
13};
9 14
10#[derive(Debug, Default, Clone)] 15#[derive(Default)]
11pub(crate) struct State { 16pub(crate) struct RootDatabase {
12 pub(crate) file_map: im::HashMap<FileId, Arc<String>>, 17 runtime: salsa::runtime::Runtime<RootDatabase>,
13 pub(crate) file_resolver: FileResolverImp
14} 18}
15 19
16#[derive(Debug)] 20impl fmt::Debug for RootDatabase {
17pub(crate) struct Db { 21 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
18 imp: imp::Db, 22 fmt.write_str("RootDatabase { ... }")
23 }
19} 24}
20 25
21#[derive(Clone, Copy)] 26impl salsa::Database for RootDatabase {
22pub(crate) struct QueryCtx<'a> { 27 fn salsa_runtime(&self) -> &salsa::runtime::Runtime<RootDatabase> {
23 imp: &'a salsa::QueryCtx<State, imp::Data>, 28 &self.runtime
29 }
24} 30}
25 31
26pub(crate) struct Query<T, R>(pub(crate) u16, pub(crate) fn(QueryCtx, &T) -> R); 32salsa::database_storage! {
27 33 pub(crate) struct RootDatabaseStorage for RootDatabase {
28pub(crate) struct QueryRegistry { 34 impl FilesDatabase {
29 imp: imp::QueryRegistry, 35 fn file_text() for FileTextQuery;
36 fn file_set() for FileSetQuery;
37 }
38 impl SyntaxDatabase {
39 fn file_syntax() for FileSyntaxQuery;
40 fn file_lines() for FileLinesQuery;
41 fn file_symbols() for FileSymbolsQuery;
42 }
43 }
30} 44}
31 45
32impl Default for Db { 46salsa::query_group! {
33 fn default() -> Db { 47 pub(crate) trait FilesDatabase: salsa::Database {
34 Db::new() 48 fn file_text(file_id: FileId) -> Arc<String> {
49 type FileTextQuery;
50 storage input;
51 }
52 fn file_set(key: ()) -> Arc<FileSet> {
53 type FileSetQuery;
54 storage input;
55 }
35 } 56 }
36} 57}
37 58
38impl Db { 59#[derive(Default, Debug)]
39 pub(crate) fn new() -> Db { 60pub(crate) struct FileSet {
40 let reg = QueryRegistry::new(); 61 pub(crate) files: HashSet<FileId>,
41 Db { imp: imp::Db::new(reg.imp) } 62 pub(crate) resolver: FileResolverImp,
42 }
43 pub(crate) fn state(&self) -> &State {
44 self.imp.imp.ground_data()
45 }
46 pub(crate) fn with_changes(&self, new_state: State, changed_files: &[FileId], resolver_changed: bool) -> Db {
47 Db { imp: self.imp.with_changes(new_state, changed_files, resolver_changed) }
48 }
49 pub(crate) fn make_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> R {
50 let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
51 f(ctx)
52 }
53 #[allow(unused)]
54 pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) {
55 let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
56 let res = f(ctx);
57 let trace = self.imp.extract_trace(ctx.imp);
58 (res, trace)
59 }
60} 63}
61 64
62impl<'a> QueryCtx<'a> { 65impl PartialEq for FileSet {
63 pub(crate) fn get<Q: imp::EvalQuery>(&self, q: Q, params: Q::Params) -> Arc<Q::Output> { 66 fn eq(&self, other: &FileSet) -> bool {
64 q.get(self, params) 67 self.files == other.files
65 } 68 }
66} 69}
67 70
68pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> { 71impl Eq for FileSet {
69 imp::file_text(ctx, file_id)
70} 72}
71 73
72pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> { 74impl Hash for FileSet {
73 imp::file_set(ctx) 75 fn hash<H: Hasher>(&self, hasher: &mut H) {
74} 76 let mut files = self.files.iter().cloned().collect::<Vec<_>>();
75impl QueryRegistry { 77 files.sort();
76 fn new() -> QueryRegistry { 78 files.hash(hasher);
77 let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() };
78 crate::queries::register_queries(&mut reg);
79 crate::module_map::register_queries(&mut reg);
80 reg
81 } 79 }
82 pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) { 80}
83 self.imp.add(q, name) 81
82salsa::query_group! {
83 pub(crate) trait SyntaxDatabase: FilesDatabase {
84 fn file_syntax(file_id: FileId) -> File {
85 type FileSyntaxQuery;
86 }
87 fn file_lines(file_id: FileId) -> Arc<LineIndex> {
88 type FileLinesQuery;
89 }
90 fn file_symbols(file_id: FileId) -> Arc<SymbolIndex> {
91 type FileSymbolsQuery;
92 }
84 } 93 }
85} 94}
95
96fn file_syntax(db: &impl SyntaxDatabase, file_id: FileId) -> File {
97 let text = db.file_text(file_id);
98 File::parse(&*text)
99}
100fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
101 let text = db.file_text(file_id);
102 Arc::new(LineIndex::new(&*text))
103}
104fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<SymbolIndex> {
105 let syntax = db.file_syntax(file_id);
106 Arc::new(SymbolIndex::for_file(file_id, syntax))
107}
108
109// mod imp;
110
111// use std::{
112// sync::Arc,
113// };
114// use im;
115// use salsa;
116// use {FileId, imp::FileResolverImp};
117
118// #[derive(Debug, Default, Clone)]
119// pub(crate) struct State {
120// pub(crate) file_map: im::HashMap<FileId, Arc<String>>,
121// pub(crate) file_resolver: FileResolverImp
122// }
123
124// #[derive(Debug)]
125// pub(crate) struct Db {
126// imp: imp::Db,
127// }
128
129// #[derive(Clone, Copy)]
130// pub(crate) struct QueryCtx<'a> {
131// imp: &'a salsa::QueryCtx<State, imp::Data>,
132// }
133
134// pub(crate) struct Query<T, R>(pub(crate) u16, pub(crate) fn(QueryCtx, &T) -> R);
135
136// pub(crate) struct QueryRegistry {
137// imp: imp::QueryRegistry,
138// }
139
140// impl Default for Db {
141// fn default() -> Db {
142// Db::new()
143// }
144// }
145
146// impl Db {
147// pub(crate) fn new() -> Db {
148// let reg = QueryRegistry::new();
149// Db { imp: imp::Db::new(reg.imp) }
150// }
151// pub(crate) fn state(&self) -> &State {
152// self.imp.imp.ground_data()
153// }
154// pub(crate) fn with_changes(&self, new_state: State, changed_files: &[FileId], resolver_changed: bool) -> Db {
155// Db { imp: self.imp.with_changes(new_state, changed_files, resolver_changed) }
156// }
157// pub(crate) fn make_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> R {
158// let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
159// f(ctx)
160// }
161// #[allow(unused)]
162// pub(crate) fn trace_query<F: FnOnce(QueryCtx) -> R, R>(&self, f: F) -> (R, Vec<&'static str>) {
163// let ctx = QueryCtx { imp: &self.imp.imp.query_ctx() };
164// let res = f(ctx);
165// let trace = self.imp.extract_trace(ctx.imp);
166// (res, trace)
167// }
168// }
169
170// impl<'a> QueryCtx<'a> {
171// pub(crate) fn get<Q: imp::EvalQuery>(&self, q: Q, params: Q::Params) -> Arc<Q::Output> {
172// q.get(self, params)
173// }
174// }
175
176// pub(crate) fn file_text(ctx: QueryCtx, file_id: FileId) -> Arc<String> {
177// imp::file_text(ctx, file_id)
178// }
179
180// pub(crate) fn file_set(ctx: QueryCtx) -> Arc<(Vec<FileId>, FileResolverImp)> {
181// imp::file_set(ctx)
182// }
183// impl QueryRegistry {
184// fn new() -> QueryRegistry {
185// let mut reg = QueryRegistry { imp: imp::QueryRegistry::new() };
186// ::queries::register_queries(&mut reg);
187// ::module_map::register_queries(&mut reg);
188// reg
189// }
190// pub(crate) fn add<Q: imp::EvalQuery>(&mut self, q: Q, name: &'static str) {
191// self.imp.add(q, name)
192// }
193// }