diff options
Diffstat (limited to 'crates/ra_analysis/src/descriptors/mod.rs')
-rw-r--r-- | crates/ra_analysis/src/descriptors/mod.rs | 137 |
1 files changed, 0 insertions, 137 deletions
diff --git a/crates/ra_analysis/src/descriptors/mod.rs b/crates/ra_analysis/src/descriptors/mod.rs deleted file mode 100644 index 7a1bcf447..000000000 --- a/crates/ra_analysis/src/descriptors/mod.rs +++ /dev/null | |||
@@ -1,137 +0,0 @@ | |||
1 | pub(crate) mod function; | ||
2 | pub(crate) mod module; | ||
3 | mod path; | ||
4 | |||
5 | use std::sync::Arc; | ||
6 | |||
7 | use ra_syntax::{ | ||
8 | ast::{self, FnDefNode, AstNode}, | ||
9 | TextRange, SyntaxNode, | ||
10 | }; | ||
11 | |||
12 | use crate::{ | ||
13 | FileId, | ||
14 | db::SyntaxDatabase, | ||
15 | descriptors::function::{resolve_local_name, FnId, FnScopes}, | ||
16 | descriptors::module::{ | ||
17 | ModuleId, ModuleTree, ModuleSource, ModuleDescriptor, | ||
18 | nameres::{ItemMap, InputModuleItems, FileItems} | ||
19 | }, | ||
20 | input::SourceRootId, | ||
21 | loc2id::{IdDatabase, DefId, DefLoc}, | ||
22 | syntax_ptr::LocalSyntaxPtr, | ||
23 | Cancelable, | ||
24 | }; | ||
25 | |||
26 | pub(crate) use self::path::{Path, PathKind}; | ||
27 | pub(crate) use self::module::nameres::FileItemId; | ||
28 | |||
29 | salsa::query_group! { | ||
30 | pub(crate) trait DescriptorDatabase: SyntaxDatabase + IdDatabase { | ||
31 | fn fn_scopes(fn_id: FnId) -> Arc<FnScopes> { | ||
32 | type FnScopesQuery; | ||
33 | use fn function::imp::fn_scopes; | ||
34 | } | ||
35 | |||
36 | fn _file_items(file_id: FileId) -> Arc<FileItems> { | ||
37 | type FileItemsQuery; | ||
38 | storage dependencies; | ||
39 | use fn module::nameres::file_items; | ||
40 | } | ||
41 | |||
42 | fn _file_item(file_id: FileId, file_item_id: FileItemId) -> SyntaxNode { | ||
43 | type FileItemQuery; | ||
44 | storage dependencies; | ||
45 | use fn module::nameres::file_item; | ||
46 | } | ||
47 | |||
48 | fn _input_module_items(source_root_id: SourceRootId, module_id: ModuleId) -> Cancelable<Arc<InputModuleItems>> { | ||
49 | type InputModuleItemsQuery; | ||
50 | use fn module::nameres::input_module_items; | ||
51 | } | ||
52 | fn _item_map(source_root_id: SourceRootId) -> Cancelable<Arc<ItemMap>> { | ||
53 | type ItemMapQuery; | ||
54 | use fn module::nameres::item_map; | ||
55 | } | ||
56 | fn _module_tree(source_root_id: SourceRootId) -> Cancelable<Arc<ModuleTree>> { | ||
57 | type ModuleTreeQuery; | ||
58 | use fn module::imp::module_tree; | ||
59 | } | ||
60 | fn _fn_syntax(fn_id: FnId) -> FnDefNode { | ||
61 | type FnSyntaxQuery; | ||
62 | // Don't retain syntax trees in memory | ||
63 | storage dependencies; | ||
64 | use fn function::imp::fn_syntax; | ||
65 | } | ||
66 | fn _submodules(source: ModuleSource) -> Cancelable<Arc<Vec<module::imp::Submodule>>> { | ||
67 | type SubmodulesQuery; | ||
68 | use fn module::imp::submodules; | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | pub(crate) enum Def { | ||
74 | Module(ModuleDescriptor), | ||
75 | Item, | ||
76 | } | ||
77 | |||
78 | impl DefId { | ||
79 | pub(crate) fn resolve(self, db: &impl DescriptorDatabase) -> Cancelable<Def> { | ||
80 | let loc = db.id_maps().def_loc(self); | ||
81 | let res = match loc { | ||
82 | DefLoc::Module { id, source_root } => { | ||
83 | let descr = ModuleDescriptor::new(db, source_root, id)?; | ||
84 | Def::Module(descr) | ||
85 | } | ||
86 | DefLoc::Item { .. } => Def::Item, | ||
87 | }; | ||
88 | Ok(res) | ||
89 | } | ||
90 | } | ||
91 | |||
92 | #[derive(Debug)] | ||
93 | pub struct ReferenceDescriptor { | ||
94 | pub range: TextRange, | ||
95 | pub name: String, | ||
96 | } | ||
97 | |||
98 | #[derive(Debug)] | ||
99 | pub struct DeclarationDescriptor<'a> { | ||
100 | pat: ast::BindPat<'a>, | ||
101 | pub range: TextRange, | ||
102 | } | ||
103 | |||
104 | impl<'a> DeclarationDescriptor<'a> { | ||
105 | pub fn new(pat: ast::BindPat) -> DeclarationDescriptor { | ||
106 | let range = pat.syntax().range(); | ||
107 | |||
108 | DeclarationDescriptor { pat, range } | ||
109 | } | ||
110 | |||
111 | pub fn find_all_refs(&self) -> Vec<ReferenceDescriptor> { | ||
112 | let name_ptr = LocalSyntaxPtr::new(self.pat.syntax()); | ||
113 | |||
114 | let fn_def = match self.pat.syntax().ancestors().find_map(ast::FnDef::cast) { | ||
115 | Some(def) => def, | ||
116 | None => return Default::default(), | ||
117 | }; | ||
118 | |||
119 | let fn_scopes = FnScopes::new(fn_def); | ||
120 | |||
121 | let refs: Vec<_> = fn_def | ||
122 | .syntax() | ||
123 | .descendants() | ||
124 | .filter_map(ast::NameRef::cast) | ||
125 | .filter(|name_ref| match resolve_local_name(*name_ref, &fn_scopes) { | ||
126 | None => false, | ||
127 | Some(entry) => entry.ptr() == name_ptr, | ||
128 | }) | ||
129 | .map(|name_ref| ReferenceDescriptor { | ||
130 | name: name_ref.syntax().text().to_string(), | ||
131 | range: name_ref.syntax().range(), | ||
132 | }) | ||
133 | .collect(); | ||
134 | |||
135 | refs | ||
136 | } | ||
137 | } | ||