diff options
Diffstat (limited to 'crates/libanalysis/src/imp.rs')
-rw-r--r-- | crates/libanalysis/src/imp.rs | 65 |
1 files changed, 45 insertions, 20 deletions
diff --git a/crates/libanalysis/src/imp.rs b/crates/libanalysis/src/imp.rs index 47b0d79ff..3e65ee14a 100644 --- a/crates/libanalysis/src/imp.rs +++ b/crates/libanalysis/src/imp.rs | |||
@@ -7,6 +7,7 @@ use std::{ | |||
7 | collections::{HashSet, VecDeque}, | 7 | collections::{HashSet, VecDeque}, |
8 | }; | 8 | }; |
9 | 9 | ||
10 | use relative_path::RelativePath; | ||
10 | use libeditor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit}; | 11 | use libeditor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit}; |
11 | use libsyntax2::{ | 12 | use libsyntax2::{ |
12 | TextUnit, TextRange, SmolStr, File, AstNode, | 13 | TextUnit, TextRange, SmolStr, File, AstNode, |
@@ -21,6 +22,40 @@ use { | |||
21 | roots::{SourceRoot, ReadonlySourceRoot, WritableSourceRoot}, | 22 | roots::{SourceRoot, ReadonlySourceRoot, WritableSourceRoot}, |
22 | }; | 23 | }; |
23 | 24 | ||
25 | |||
26 | #[derive(Clone, Debug)] | ||
27 | pub(crate) struct FileResolverImp { | ||
28 | inner: Arc<FileResolver> | ||
29 | } | ||
30 | |||
31 | impl FileResolverImp { | ||
32 | pub(crate) fn new(inner: Arc<FileResolver>) -> FileResolverImp { | ||
33 | FileResolverImp { inner } | ||
34 | } | ||
35 | pub(crate) fn file_stem(&self, file_id: FileId) -> String { | ||
36 | self.inner.file_stem(file_id) | ||
37 | } | ||
38 | pub(crate) fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId> { | ||
39 | self.inner.resolve(file_id, path) | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl Default for FileResolverImp { | ||
44 | fn default() -> FileResolverImp { | ||
45 | #[derive(Debug)] | ||
46 | struct DummyResolver; | ||
47 | impl FileResolver for DummyResolver { | ||
48 | fn file_stem(&self, _file_: FileId) -> String { | ||
49 | panic!("file resolver not set") | ||
50 | } | ||
51 | fn resolve(&self, _file_id: FileId, _path: &::relative_path::RelativePath) -> Option<FileId> { | ||
52 | panic!("file resolver not set") | ||
53 | } | ||
54 | } | ||
55 | FileResolverImp { inner: Arc::new(DummyResolver) } | ||
56 | } | ||
57 | } | ||
58 | |||
24 | #[derive(Debug)] | 59 | #[derive(Debug)] |
25 | pub(crate) struct AnalysisHostImpl { | 60 | pub(crate) struct AnalysisHostImpl { |
26 | data: Arc<WorldData> | 61 | data: Arc<WorldData> |
@@ -32,13 +67,9 @@ impl AnalysisHostImpl { | |||
32 | data: Arc::new(WorldData::default()), | 67 | data: Arc::new(WorldData::default()), |
33 | } | 68 | } |
34 | } | 69 | } |
35 | pub fn analysis( | 70 | pub fn analysis(&self) -> AnalysisImpl { |
36 | &self, | ||
37 | file_resolver: Arc<dyn FileResolver>, | ||
38 | ) -> AnalysisImpl { | ||
39 | AnalysisImpl { | 71 | AnalysisImpl { |
40 | needs_reindex: AtomicBool::new(false), | 72 | needs_reindex: AtomicBool::new(false), |
41 | file_resolver, | ||
42 | data: self.data.clone(), | 73 | data: self.data.clone(), |
43 | } | 74 | } |
44 | } | 75 | } |
@@ -48,6 +79,11 @@ impl AnalysisHostImpl { | |||
48 | data.root.update(file_id, text); | 79 | data.root.update(file_id, text); |
49 | } | 80 | } |
50 | } | 81 | } |
82 | pub fn set_file_resolver(&mut self, resolver: FileResolverImp) { | ||
83 | let data = self.data_mut(); | ||
84 | data.file_resolver = resolver.clone(); | ||
85 | data.root.set_file_resolver(resolver); | ||
86 | } | ||
51 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { | 87 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { |
52 | let mut visited = HashSet::new(); | 88 | let mut visited = HashSet::new(); |
53 | for &file_id in graph.crate_roots.values() { | 89 | for &file_id in graph.crate_roots.values() { |
@@ -67,7 +103,6 @@ impl AnalysisHostImpl { | |||
67 | 103 | ||
68 | pub(crate) struct AnalysisImpl { | 104 | pub(crate) struct AnalysisImpl { |
69 | needs_reindex: AtomicBool, | 105 | needs_reindex: AtomicBool, |
70 | file_resolver: Arc<dyn FileResolver>, | ||
71 | data: Arc<WorldData>, | 106 | data: Arc<WorldData>, |
72 | } | 107 | } |
73 | 108 | ||
@@ -81,7 +116,6 @@ impl Clone for AnalysisImpl { | |||
81 | fn clone(&self) -> AnalysisImpl { | 116 | fn clone(&self) -> AnalysisImpl { |
82 | AnalysisImpl { | 117 | AnalysisImpl { |
83 | needs_reindex: AtomicBool::new(self.needs_reindex.load(SeqCst)), | 118 | needs_reindex: AtomicBool::new(self.needs_reindex.load(SeqCst)), |
84 | file_resolver: Arc::clone(&self.file_resolver), | ||
85 | data: Arc::clone(&self.data), | 119 | data: Arc::clone(&self.data), |
86 | } | 120 | } |
87 | } | 121 | } |
@@ -117,11 +151,7 @@ impl AnalysisImpl { | |||
117 | let module_map = root.module_map(); | 151 | let module_map = root.module_map(); |
118 | let id = module_map.file2module(file_id); | 152 | let id = module_map.file2module(file_id); |
119 | module_map | 153 | module_map |
120 | .parent_modules( | 154 | .parent_modules(id, &|file_id| root.syntax(file_id)) |
121 | id, | ||
122 | &*self.file_resolver, | ||
123 | &|file_id| root.syntax(file_id), | ||
124 | ) | ||
125 | .into_iter() | 155 | .into_iter() |
126 | .map(|(id, name, node)| { | 156 | .map(|(id, name, node)| { |
127 | let id = module_map.module2file(id); | 157 | let id = module_map.module2file(id); |
@@ -149,11 +179,7 @@ impl AnalysisImpl { | |||
149 | } | 179 | } |
150 | let mid = module_map.file2module(id); | 180 | let mid = module_map.file2module(id); |
151 | let parents = module_map | 181 | let parents = module_map |
152 | .parent_module_ids( | 182 | .parent_module_ids(mid, &|file_id| self.file_syntax(file_id)) |
153 | mid, | ||
154 | &*self.file_resolver, | ||
155 | &|file_id| self.file_syntax(file_id), | ||
156 | ) | ||
157 | .into_iter() | 183 | .into_iter() |
158 | .map(|id| module_map.module2file(id)) | 184 | .map(|id| module_map.module2file(id)) |
159 | .filter(|&id| visited.insert(id)); | 185 | .filter(|&id| visited.insert(id)); |
@@ -213,7 +239,6 @@ impl AnalysisImpl { | |||
213 | 239 | ||
214 | module_map.problems( | 240 | module_map.problems( |
215 | file_id, | 241 | file_id, |
216 | &*self.file_resolver, | ||
217 | &|file_id| self.file_syntax(file_id), | 242 | &|file_id| self.file_syntax(file_id), |
218 | |name_node, problem| { | 243 | |name_node, problem| { |
219 | let diag = match problem { | 244 | let diag = match problem { |
@@ -291,7 +316,6 @@ impl AnalysisImpl { | |||
291 | module_map | 316 | module_map |
292 | .child_module_by_name( | 317 | .child_module_by_name( |
293 | id, name.as_str(), | 318 | id, name.as_str(), |
294 | &*self.file_resolver, | ||
295 | &|file_id| self.file_syntax(file_id), | 319 | &|file_id| self.file_syntax(file_id), |
296 | ) | 320 | ) |
297 | .into_iter() | 321 | .into_iter() |
@@ -306,8 +330,9 @@ impl AnalysisImpl { | |||
306 | } | 330 | } |
307 | } | 331 | } |
308 | 332 | ||
309 | #[derive(Clone, Default, Debug)] | 333 | #[derive(Default, Clone, Debug)] |
310 | struct WorldData { | 334 | struct WorldData { |
335 | file_resolver: FileResolverImp, | ||
311 | crate_graph: CrateGraph, | 336 | crate_graph: CrateGraph, |
312 | root: WritableSourceRoot, | 337 | root: WritableSourceRoot, |
313 | libs: Vec<Arc<ReadonlySourceRoot>>, | 338 | libs: Vec<Arc<ReadonlySourceRoot>>, |