diff options
-rw-r--r-- | crates/ra_ide_api/src/lib.rs | 75 | ||||
-rw-r--r-- | crates/ra_ide_api/src/navigation_target.rs | 86 |
2 files changed, 88 insertions, 73 deletions
diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index f2439bfd7..2e1768951 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs | |||
@@ -22,6 +22,7 @@ mod db; | |||
22 | mod imp; | 22 | mod imp; |
23 | pub mod mock_analysis; | 23 | pub mod mock_analysis; |
24 | mod symbol_index; | 24 | mod symbol_index; |
25 | mod navigation_target; | ||
25 | 26 | ||
26 | mod completion; | 27 | mod completion; |
27 | mod runnables; | 28 | mod runnables; |
@@ -33,8 +34,7 @@ mod syntax_highlighting; | |||
33 | 34 | ||
34 | use std::{fmt, sync::Arc}; | 35 | use std::{fmt, sync::Arc}; |
35 | 36 | ||
36 | use hir::{Def, ModuleSource, Name}; | 37 | use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit}; |
37 | use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, SyntaxNode, TextRange, TextUnit, AstNode}; | ||
38 | use ra_text_edit::TextEdit; | 38 | use ra_text_edit::TextEdit; |
39 | use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr, BaseDatabase}; | 39 | use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr, BaseDatabase}; |
40 | use rayon::prelude::*; | 40 | use rayon::prelude::*; |
@@ -259,77 +259,6 @@ pub struct NavigationTarget { | |||
259 | } | 259 | } |
260 | 260 | ||
261 | impl NavigationTarget { | 261 | impl NavigationTarget { |
262 | fn from_symbol(symbol: FileSymbol) -> NavigationTarget { | ||
263 | NavigationTarget { | ||
264 | file_id: symbol.file_id, | ||
265 | name: symbol.name.clone(), | ||
266 | kind: symbol.ptr.kind(), | ||
267 | range: symbol.ptr.range(), | ||
268 | ptr: Some(symbol.ptr.clone()), | ||
269 | } | ||
270 | } | ||
271 | |||
272 | fn from_syntax(name: Option<Name>, file_id: FileId, node: &SyntaxNode) -> NavigationTarget { | ||
273 | NavigationTarget { | ||
274 | file_id, | ||
275 | name: name.map(|n| n.to_string().into()).unwrap_or("".into()), | ||
276 | kind: node.kind(), | ||
277 | range: node.range(), | ||
278 | ptr: Some(LocalSyntaxPtr::new(node)), | ||
279 | } | ||
280 | } | ||
281 | // TODO once Def::Item is gone, this should be able to always return a NavigationTarget | ||
282 | fn from_def(db: &db::RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> { | ||
283 | Ok(match def { | ||
284 | Def::Struct(s) => { | ||
285 | let (file_id, node) = s.source(db)?; | ||
286 | Some(NavigationTarget::from_syntax( | ||
287 | s.name(db)?, | ||
288 | file_id.original_file(db), | ||
289 | node.syntax(), | ||
290 | )) | ||
291 | } | ||
292 | Def::Enum(e) => { | ||
293 | let (file_id, node) = e.source(db)?; | ||
294 | Some(NavigationTarget::from_syntax( | ||
295 | e.name(db)?, | ||
296 | file_id.original_file(db), | ||
297 | node.syntax(), | ||
298 | )) | ||
299 | } | ||
300 | Def::EnumVariant(ev) => { | ||
301 | let (file_id, node) = ev.source(db)?; | ||
302 | Some(NavigationTarget::from_syntax( | ||
303 | ev.name(db)?, | ||
304 | file_id.original_file(db), | ||
305 | node.syntax(), | ||
306 | )) | ||
307 | } | ||
308 | Def::Function(f) => { | ||
309 | let (file_id, node) = f.source(db)?; | ||
310 | let name = f.signature(db).name().clone(); | ||
311 | Some(NavigationTarget::from_syntax( | ||
312 | Some(name), | ||
313 | file_id.original_file(db), | ||
314 | node.syntax(), | ||
315 | )) | ||
316 | } | ||
317 | Def::Module(m) => { | ||
318 | let (file_id, source) = m.definition_source(db)?; | ||
319 | let name = m.name(db)?; | ||
320 | match source { | ||
321 | ModuleSource::SourceFile(node) => { | ||
322 | Some(NavigationTarget::from_syntax(name, file_id, node.syntax())) | ||
323 | } | ||
324 | ModuleSource::Module(node) => { | ||
325 | Some(NavigationTarget::from_syntax(name, file_id, node.syntax())) | ||
326 | } | ||
327 | } | ||
328 | } | ||
329 | Def::Item => None, | ||
330 | }) | ||
331 | } | ||
332 | |||
333 | pub fn name(&self) -> &SmolStr { | 262 | pub fn name(&self) -> &SmolStr { |
334 | &self.name | 263 | &self.name |
335 | } | 264 | } |
diff --git a/crates/ra_ide_api/src/navigation_target.rs b/crates/ra_ide_api/src/navigation_target.rs new file mode 100644 index 000000000..bacb7329f --- /dev/null +++ b/crates/ra_ide_api/src/navigation_target.rs | |||
@@ -0,0 +1,86 @@ | |||
1 | use ra_db::{FileId, LocalSyntaxPtr, Cancelable}; | ||
2 | use ra_syntax::{SyntaxNode, AstNode}; | ||
3 | use hir::{Name, Def, ModuleSource}; | ||
4 | |||
5 | use crate::{ | ||
6 | NavigationTarget, | ||
7 | FileSymbol, | ||
8 | db::RootDatabase, | ||
9 | }; | ||
10 | |||
11 | impl NavigationTarget { | ||
12 | pub(crate) fn from_symbol(symbol: FileSymbol) -> NavigationTarget { | ||
13 | NavigationTarget { | ||
14 | file_id: symbol.file_id, | ||
15 | name: symbol.name.clone(), | ||
16 | kind: symbol.ptr.kind(), | ||
17 | range: symbol.ptr.range(), | ||
18 | ptr: Some(symbol.ptr.clone()), | ||
19 | } | ||
20 | } | ||
21 | |||
22 | pub(crate) fn from_syntax( | ||
23 | name: Option<Name>, | ||
24 | file_id: FileId, | ||
25 | node: &SyntaxNode, | ||
26 | ) -> NavigationTarget { | ||
27 | NavigationTarget { | ||
28 | file_id, | ||
29 | name: name.map(|n| n.to_string().into()).unwrap_or("".into()), | ||
30 | kind: node.kind(), | ||
31 | range: node.range(), | ||
32 | ptr: Some(LocalSyntaxPtr::new(node)), | ||
33 | } | ||
34 | } | ||
35 | // TODO once Def::Item is gone, this should be able to always return a NavigationTarget | ||
36 | pub(crate) fn from_def(db: &RootDatabase, def: Def) -> Cancelable<Option<NavigationTarget>> { | ||
37 | Ok(match def { | ||
38 | Def::Struct(s) => { | ||
39 | let (file_id, node) = s.source(db)?; | ||
40 | Some(NavigationTarget::from_syntax( | ||
41 | s.name(db)?, | ||
42 | file_id.original_file(db), | ||
43 | node.syntax(), | ||
44 | )) | ||
45 | } | ||
46 | Def::Enum(e) => { | ||
47 | let (file_id, node) = e.source(db)?; | ||
48 | Some(NavigationTarget::from_syntax( | ||
49 | e.name(db)?, | ||
50 | file_id.original_file(db), | ||
51 | node.syntax(), | ||
52 | )) | ||
53 | } | ||
54 | Def::EnumVariant(ev) => { | ||
55 | let (file_id, node) = ev.source(db)?; | ||
56 | Some(NavigationTarget::from_syntax( | ||
57 | ev.name(db)?, | ||
58 | file_id.original_file(db), | ||
59 | node.syntax(), | ||
60 | )) | ||
61 | } | ||
62 | Def::Function(f) => { | ||
63 | let (file_id, node) = f.source(db)?; | ||
64 | let name = f.signature(db).name().clone(); | ||
65 | Some(NavigationTarget::from_syntax( | ||
66 | Some(name), | ||
67 | file_id.original_file(db), | ||
68 | node.syntax(), | ||
69 | )) | ||
70 | } | ||
71 | Def::Module(m) => { | ||
72 | let (file_id, source) = m.definition_source(db)?; | ||
73 | let name = m.name(db)?; | ||
74 | match source { | ||
75 | ModuleSource::SourceFile(node) => { | ||
76 | Some(NavigationTarget::from_syntax(name, file_id, node.syntax())) | ||
77 | } | ||
78 | ModuleSource::Module(node) => { | ||
79 | Some(NavigationTarget::from_syntax(name, file_id, node.syntax())) | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | Def::Item => None, | ||
84 | }) | ||
85 | } | ||
86 | } | ||