diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-08 19:48:48 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-08 19:48:48 +0000 |
commit | 46f74e33ca53a7897e9020d3de75cc76a6b89d79 (patch) | |
tree | 2bc001c8ecf58b49ac9a0da1f20d5644ce29fb3a /crates/ra_ide_api/src/imp.rs | |
parent | 4f4f7933b1b7ff34f8633b1686b18b2d1b994c47 (diff) | |
parent | 0c62b1bb7a49bf527780ce1f8cade5eb4fbfdb2d (diff) |
Merge #471
471: rename crates to match reality r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/imp.rs')
-rw-r--r-- | crates/ra_ide_api/src/imp.rs | 309 |
1 files changed, 309 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/imp.rs b/crates/ra_ide_api/src/imp.rs new file mode 100644 index 000000000..7c60ab7d6 --- /dev/null +++ b/crates/ra_ide_api/src/imp.rs | |||
@@ -0,0 +1,309 @@ | |||
1 | use std::sync::Arc; | ||
2 | |||
3 | use salsa::Database; | ||
4 | |||
5 | use hir::{ | ||
6 | self, Problem, source_binder, | ||
7 | }; | ||
8 | use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; | ||
9 | use ra_ide_api_light::{self, assists, LocalEdit, Severity}; | ||
10 | use ra_syntax::{ | ||
11 | TextRange, AstNode, SourceFile, | ||
12 | ast::{self, NameOwner}, | ||
13 | algo::find_node_at_offset, | ||
14 | SyntaxKind::*, | ||
15 | }; | ||
16 | |||
17 | use crate::{ | ||
18 | AnalysisChange, | ||
19 | Cancelable, NavigationTarget, | ||
20 | CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, | ||
21 | Query, RootChange, SourceChange, SourceFileEdit, | ||
22 | symbol_index::{LibrarySymbolsQuery, FileSymbol}, | ||
23 | }; | ||
24 | |||
25 | impl db::RootDatabase { | ||
26 | pub(crate) fn apply_change(&mut self, change: AnalysisChange) { | ||
27 | log::info!("apply_change {:?}", change); | ||
28 | // self.gc_syntax_trees(); | ||
29 | if !change.new_roots.is_empty() { | ||
30 | let mut local_roots = Vec::clone(&self.local_roots()); | ||
31 | for (root_id, is_local) in change.new_roots { | ||
32 | self.query_mut(ra_db::SourceRootQuery) | ||
33 | .set(root_id, Default::default()); | ||
34 | if is_local { | ||
35 | local_roots.push(root_id); | ||
36 | } | ||
37 | } | ||
38 | self.query_mut(ra_db::LocalRootsQuery) | ||
39 | .set((), Arc::new(local_roots)); | ||
40 | } | ||
41 | |||
42 | for (root_id, root_change) in change.roots_changed { | ||
43 | self.apply_root_change(root_id, root_change); | ||
44 | } | ||
45 | for (file_id, text) in change.files_changed { | ||
46 | self.query_mut(ra_db::FileTextQuery).set(file_id, text) | ||
47 | } | ||
48 | if !change.libraries_added.is_empty() { | ||
49 | let mut libraries = Vec::clone(&self.library_roots()); | ||
50 | for library in change.libraries_added { | ||
51 | libraries.push(library.root_id); | ||
52 | self.query_mut(ra_db::SourceRootQuery) | ||
53 | .set(library.root_id, Default::default()); | ||
54 | self.query_mut(LibrarySymbolsQuery) | ||
55 | .set_constant(library.root_id, Arc::new(library.symbol_index)); | ||
56 | self.apply_root_change(library.root_id, library.root_change); | ||
57 | } | ||
58 | self.query_mut(ra_db::LibraryRootsQuery) | ||
59 | .set((), Arc::new(libraries)); | ||
60 | } | ||
61 | if let Some(crate_graph) = change.crate_graph { | ||
62 | self.query_mut(ra_db::CrateGraphQuery) | ||
63 | .set((), Arc::new(crate_graph)) | ||
64 | } | ||
65 | } | ||
66 | |||
67 | fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { | ||
68 | let mut source_root = SourceRoot::clone(&self.source_root(root_id)); | ||
69 | for add_file in root_change.added { | ||
70 | self.query_mut(ra_db::FileTextQuery) | ||
71 | .set(add_file.file_id, add_file.text); | ||
72 | self.query_mut(ra_db::FileRelativePathQuery) | ||
73 | .set(add_file.file_id, add_file.path.clone()); | ||
74 | self.query_mut(ra_db::FileSourceRootQuery) | ||
75 | .set(add_file.file_id, root_id); | ||
76 | source_root.files.insert(add_file.path, add_file.file_id); | ||
77 | } | ||
78 | for remove_file in root_change.removed { | ||
79 | self.query_mut(ra_db::FileTextQuery) | ||
80 | .set(remove_file.file_id, Default::default()); | ||
81 | source_root.files.remove(&remove_file.path); | ||
82 | } | ||
83 | self.query_mut(ra_db::SourceRootQuery) | ||
84 | .set(root_id, Arc::new(source_root)); | ||
85 | } | ||
86 | |||
87 | #[allow(unused)] | ||
88 | /// Ideally, we should call this function from time to time to collect heavy | ||
89 | /// syntax trees. However, if we actually do that, everything is recomputed | ||
90 | /// for some reason. Needs investigation. | ||
91 | fn gc_syntax_trees(&mut self) { | ||
92 | self.query(ra_db::SourceFileQuery) | ||
93 | .sweep(salsa::SweepStrategy::default().discard_values()); | ||
94 | self.query(hir::db::SourceFileItemsQuery) | ||
95 | .sweep(salsa::SweepStrategy::default().discard_values()); | ||
96 | self.query(hir::db::FileItemQuery) | ||
97 | .sweep(salsa::SweepStrategy::default().discard_values()); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | impl db::RootDatabase { | ||
102 | /// This returns `Vec` because a module may be included from several places. We | ||
103 | /// don't handle this case yet though, so the Vec has length at most one. | ||
104 | pub(crate) fn parent_module( | ||
105 | &self, | ||
106 | position: FilePosition, | ||
107 | ) -> Cancelable<Vec<NavigationTarget>> { | ||
108 | let module = match source_binder::module_from_position(self, position)? { | ||
109 | None => return Ok(Vec::new()), | ||
110 | Some(it) => it, | ||
111 | }; | ||
112 | let (file_id, ast_module) = match module.declaration_source(self)? { | ||
113 | None => return Ok(Vec::new()), | ||
114 | Some(it) => it, | ||
115 | }; | ||
116 | let name = ast_module.name().unwrap(); | ||
117 | Ok(vec![NavigationTarget { | ||
118 | file_id, | ||
119 | name: name.text().clone(), | ||
120 | range: name.syntax().range(), | ||
121 | kind: MODULE, | ||
122 | ptr: None, | ||
123 | }]) | ||
124 | } | ||
125 | /// Returns `Vec` for the same reason as `parent_module` | ||
126 | pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { | ||
127 | let module = match source_binder::module_from_file_id(self, file_id)? { | ||
128 | Some(it) => it, | ||
129 | None => return Ok(Vec::new()), | ||
130 | }; | ||
131 | let krate = match module.krate(self)? { | ||
132 | Some(it) => it, | ||
133 | None => return Ok(Vec::new()), | ||
134 | }; | ||
135 | Ok(vec![krate.crate_id()]) | ||
136 | } | ||
137 | pub(crate) fn find_all_refs( | ||
138 | &self, | ||
139 | position: FilePosition, | ||
140 | ) -> Cancelable<Vec<(FileId, TextRange)>> { | ||
141 | let file = self.source_file(position.file_id); | ||
142 | // Find the binding associated with the offset | ||
143 | let (binding, descr) = match find_binding(self, &file, position)? { | ||
144 | None => return Ok(Vec::new()), | ||
145 | Some(it) => it, | ||
146 | }; | ||
147 | |||
148 | let mut ret = binding | ||
149 | .name() | ||
150 | .into_iter() | ||
151 | .map(|name| (position.file_id, name.syntax().range())) | ||
152 | .collect::<Vec<_>>(); | ||
153 | ret.extend( | ||
154 | descr | ||
155 | .scopes(self)? | ||
156 | .find_all_refs(binding) | ||
157 | .into_iter() | ||
158 | .map(|ref_desc| (position.file_id, ref_desc.range)), | ||
159 | ); | ||
160 | |||
161 | return Ok(ret); | ||
162 | |||
163 | fn find_binding<'a>( | ||
164 | db: &db::RootDatabase, | ||
165 | source_file: &'a SourceFile, | ||
166 | position: FilePosition, | ||
167 | ) -> Cancelable<Option<(&'a ast::BindPat, hir::Function)>> { | ||
168 | let syntax = source_file.syntax(); | ||
169 | if let Some(binding) = find_node_at_offset::<ast::BindPat>(syntax, position.offset) { | ||
170 | let descr = ctry!(source_binder::function_from_child_node( | ||
171 | db, | ||
172 | position.file_id, | ||
173 | binding.syntax(), | ||
174 | )?); | ||
175 | return Ok(Some((binding, descr))); | ||
176 | }; | ||
177 | let name_ref = ctry!(find_node_at_offset::<ast::NameRef>(syntax, position.offset)); | ||
178 | let descr = ctry!(source_binder::function_from_child_node( | ||
179 | db, | ||
180 | position.file_id, | ||
181 | name_ref.syntax(), | ||
182 | )?); | ||
183 | let scope = descr.scopes(db)?; | ||
184 | let resolved = ctry!(scope.resolve_local_name(name_ref)); | ||
185 | let resolved = resolved.ptr().resolve(source_file); | ||
186 | let binding = ctry!(find_node_at_offset::<ast::BindPat>( | ||
187 | syntax, | ||
188 | resolved.range().end() | ||
189 | )); | ||
190 | Ok(Some((binding, descr))) | ||
191 | } | ||
192 | } | ||
193 | |||
194 | pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { | ||
195 | let syntax = self.source_file(file_id); | ||
196 | |||
197 | let mut res = ra_ide_api_light::diagnostics(&syntax) | ||
198 | .into_iter() | ||
199 | .map(|d| Diagnostic { | ||
200 | range: d.range, | ||
201 | message: d.msg, | ||
202 | severity: d.severity, | ||
203 | fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), | ||
204 | }) | ||
205 | .collect::<Vec<_>>(); | ||
206 | if let Some(m) = source_binder::module_from_file_id(self, file_id)? { | ||
207 | for (name_node, problem) in m.problems(self)? { | ||
208 | let source_root = self.file_source_root(file_id); | ||
209 | let diag = match problem { | ||
210 | Problem::UnresolvedModule { candidate } => { | ||
211 | let create_file = FileSystemEdit::CreateFile { | ||
212 | source_root, | ||
213 | path: candidate.clone(), | ||
214 | }; | ||
215 | let fix = SourceChange { | ||
216 | label: "create module".to_string(), | ||
217 | source_file_edits: Vec::new(), | ||
218 | file_system_edits: vec![create_file], | ||
219 | cursor_position: None, | ||
220 | }; | ||
221 | Diagnostic { | ||
222 | range: name_node.range(), | ||
223 | message: "unresolved module".to_string(), | ||
224 | severity: Severity::Error, | ||
225 | fix: Some(fix), | ||
226 | } | ||
227 | } | ||
228 | Problem::NotDirOwner { move_to, candidate } => { | ||
229 | let move_file = FileSystemEdit::MoveFile { | ||
230 | src: file_id, | ||
231 | dst_source_root: source_root, | ||
232 | dst_path: move_to.clone(), | ||
233 | }; | ||
234 | let create_file = FileSystemEdit::CreateFile { | ||
235 | source_root, | ||
236 | path: move_to.join(candidate), | ||
237 | }; | ||
238 | let fix = SourceChange { | ||
239 | label: "move file and create module".to_string(), | ||
240 | source_file_edits: Vec::new(), | ||
241 | file_system_edits: vec![move_file, create_file], | ||
242 | cursor_position: None, | ||
243 | }; | ||
244 | Diagnostic { | ||
245 | range: name_node.range(), | ||
246 | message: "can't declare module at this location".to_string(), | ||
247 | severity: Severity::Error, | ||
248 | fix: Some(fix), | ||
249 | } | ||
250 | } | ||
251 | }; | ||
252 | res.push(diag) | ||
253 | } | ||
254 | }; | ||
255 | Ok(res) | ||
256 | } | ||
257 | |||
258 | pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> { | ||
259 | let file = self.source_file(frange.file_id); | ||
260 | assists::assists(&file, frange.range) | ||
261 | .into_iter() | ||
262 | .map(|local_edit| SourceChange::from_local_edit(frange.file_id, local_edit)) | ||
263 | .collect() | ||
264 | } | ||
265 | |||
266 | pub(crate) fn rename( | ||
267 | &self, | ||
268 | position: FilePosition, | ||
269 | new_name: &str, | ||
270 | ) -> Cancelable<Vec<SourceFileEdit>> { | ||
271 | let res = self | ||
272 | .find_all_refs(position)? | ||
273 | .iter() | ||
274 | .map(|(file_id, text_range)| SourceFileEdit { | ||
275 | file_id: *file_id, | ||
276 | edit: { | ||
277 | let mut builder = ra_text_edit::TextEditBuilder::default(); | ||
278 | builder.replace(*text_range, new_name.into()); | ||
279 | builder.finish() | ||
280 | }, | ||
281 | }) | ||
282 | .collect::<Vec<_>>(); | ||
283 | Ok(res) | ||
284 | } | ||
285 | pub(crate) fn index_resolve(&self, name_ref: &ast::NameRef) -> Cancelable<Vec<FileSymbol>> { | ||
286 | let name = name_ref.text(); | ||
287 | let mut query = Query::new(name.to_string()); | ||
288 | query.exact(); | ||
289 | query.limit(4); | ||
290 | crate::symbol_index::world_symbols(self, query) | ||
291 | } | ||
292 | } | ||
293 | |||
294 | impl SourceChange { | ||
295 | pub(crate) fn from_local_edit(file_id: FileId, edit: LocalEdit) -> SourceChange { | ||
296 | let file_edit = SourceFileEdit { | ||
297 | file_id, | ||
298 | edit: edit.edit, | ||
299 | }; | ||
300 | SourceChange { | ||
301 | label: edit.label, | ||
302 | source_file_edits: vec![file_edit], | ||
303 | file_system_edits: vec![], | ||
304 | cursor_position: edit | ||
305 | .cursor_position | ||
306 | .map(|offset| FilePosition { offset, file_id }), | ||
307 | } | ||
308 | } | ||
309 | } | ||