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