diff options
Diffstat (limited to 'crates/ra_analysis/src/lib.rs')
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 104 |
1 files changed, 81 insertions, 23 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 22fff71ab..a1d462528 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -18,9 +18,9 @@ pub mod mock_analysis; | |||
18 | 18 | ||
19 | use std::{fmt, sync::Arc}; | 19 | use std::{fmt, sync::Arc}; |
20 | 20 | ||
21 | use rustc_hash::FxHashMap; | ||
21 | use ra_syntax::{SourceFileNode, TextRange, TextUnit}; | 22 | use ra_syntax::{SourceFileNode, TextRange, TextUnit}; |
22 | use ra_text_edit::AtomTextEdit; | 23 | use ra_text_edit::AtomTextEdit; |
23 | use ra_db::FileResolverImp; | ||
24 | use rayon::prelude::*; | 24 | use rayon::prelude::*; |
25 | use relative_path::RelativePathBuf; | 25 | use relative_path::RelativePathBuf; |
26 | 26 | ||
@@ -39,28 +39,54 @@ pub use hir::FnSignatureInfo; | |||
39 | 39 | ||
40 | pub use ra_db::{ | 40 | pub use ra_db::{ |
41 | Canceled, Cancelable, FilePosition, | 41 | Canceled, Cancelable, FilePosition, |
42 | CrateGraph, CrateId, FileId, FileResolver | 42 | CrateGraph, CrateId, SourceRootId, FileId |
43 | }; | 43 | }; |
44 | 44 | ||
45 | #[derive(Default)] | 45 | #[derive(Default)] |
46 | pub struct AnalysisChange { | 46 | pub struct AnalysisChange { |
47 | files_added: Vec<(FileId, String)>, | 47 | new_roots: Vec<(SourceRootId, bool)>, |
48 | files_changed: Vec<(FileId, String)>, | 48 | roots_changed: FxHashMap<SourceRootId, RootChange>, |
49 | files_removed: Vec<(FileId)>, | 49 | files_changed: Vec<(FileId, Arc<String>)>, |
50 | libraries_added: Vec<LibraryData>, | 50 | libraries_added: Vec<LibraryData>, |
51 | crate_graph: Option<CrateGraph>, | 51 | crate_graph: Option<CrateGraph>, |
52 | file_resolver: Option<FileResolverImp>, | 52 | } |
53 | |||
54 | #[derive(Default)] | ||
55 | struct RootChange { | ||
56 | added: Vec<AddFile>, | ||
57 | removed: Vec<RemoveFile>, | ||
58 | } | ||
59 | |||
60 | #[derive(Debug)] | ||
61 | struct AddFile { | ||
62 | file_id: FileId, | ||
63 | path: RelativePathBuf, | ||
64 | text: Arc<String>, | ||
65 | } | ||
66 | |||
67 | #[derive(Debug)] | ||
68 | struct RemoveFile { | ||
69 | file_id: FileId, | ||
70 | path: RelativePathBuf, | ||
53 | } | 71 | } |
54 | 72 | ||
55 | impl fmt::Debug for AnalysisChange { | 73 | impl fmt::Debug for AnalysisChange { |
56 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | 74 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
57 | fmt.debug_struct("AnalysisChange") | 75 | fmt.debug_struct("AnalysisChange") |
58 | .field("files_added", &self.files_added.len()) | 76 | .field("new_roots", &self.new_roots) |
77 | .field("roots_changed", &self.roots_changed) | ||
59 | .field("files_changed", &self.files_changed.len()) | 78 | .field("files_changed", &self.files_changed.len()) |
60 | .field("files_removed", &self.files_removed.len()) | ||
61 | .field("libraries_added", &self.libraries_added.len()) | 79 | .field("libraries_added", &self.libraries_added.len()) |
62 | .field("crate_graph", &self.crate_graph) | 80 | .field("crate_graph", &self.crate_graph) |
63 | .field("file_resolver", &self.file_resolver) | 81 | .finish() |
82 | } | ||
83 | } | ||
84 | |||
85 | impl fmt::Debug for RootChange { | ||
86 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { | ||
87 | fmt.debug_struct("AnalysisChange") | ||
88 | .field("added", &self.added.len()) | ||
89 | .field("removed", &self.removed.len()) | ||
64 | .finish() | 90 | .finish() |
65 | } | 91 | } |
66 | } | 92 | } |
@@ -69,14 +95,37 @@ impl AnalysisChange { | |||
69 | pub fn new() -> AnalysisChange { | 95 | pub fn new() -> AnalysisChange { |
70 | AnalysisChange::default() | 96 | AnalysisChange::default() |
71 | } | 97 | } |
72 | pub fn add_file(&mut self, file_id: FileId, text: String) { | 98 | pub fn add_root(&mut self, root_id: SourceRootId, is_local: bool) { |
73 | self.files_added.push((file_id, text)) | 99 | self.new_roots.push((root_id, is_local)); |
74 | } | 100 | } |
75 | pub fn change_file(&mut self, file_id: FileId, new_text: String) { | 101 | pub fn add_file( |
102 | &mut self, | ||
103 | root_id: SourceRootId, | ||
104 | file_id: FileId, | ||
105 | path: RelativePathBuf, | ||
106 | text: Arc<String>, | ||
107 | ) { | ||
108 | let file = AddFile { | ||
109 | file_id, | ||
110 | path, | ||
111 | text, | ||
112 | }; | ||
113 | self.roots_changed | ||
114 | .entry(root_id) | ||
115 | .or_default() | ||
116 | .added | ||
117 | .push(file); | ||
118 | } | ||
119 | pub fn change_file(&mut self, file_id: FileId, new_text: Arc<String>) { | ||
76 | self.files_changed.push((file_id, new_text)) | 120 | self.files_changed.push((file_id, new_text)) |
77 | } | 121 | } |
78 | pub fn remove_file(&mut self, file_id: FileId) { | 122 | pub fn remove_file(&mut self, root_id: SourceRootId, file_id: FileId, path: RelativePathBuf) { |
79 | self.files_removed.push(file_id) | 123 | let file = RemoveFile { file_id, path }; |
124 | self.roots_changed | ||
125 | .entry(root_id) | ||
126 | .or_default() | ||
127 | .removed | ||
128 | .push(file); | ||
80 | } | 129 | } |
81 | pub fn add_library(&mut self, data: LibraryData) { | 130 | pub fn add_library(&mut self, data: LibraryData) { |
82 | self.libraries_added.push(data) | 131 | self.libraries_added.push(data) |
@@ -84,9 +133,6 @@ impl AnalysisChange { | |||
84 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { | 133 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { |
85 | self.crate_graph = Some(graph); | 134 | self.crate_graph = Some(graph); |
86 | } | 135 | } |
87 | pub fn set_file_resolver(&mut self, file_resolver: Arc<FileResolver>) { | ||
88 | self.file_resolver = Some(FileResolverImp::new(file_resolver)); | ||
89 | } | ||
90 | } | 136 | } |
91 | 137 | ||
92 | /// `AnalysisHost` stores the current state of the world. | 138 | /// `AnalysisHost` stores the current state of the world. |
@@ -313,20 +359,32 @@ impl Analysis { | |||
313 | 359 | ||
314 | #[derive(Debug)] | 360 | #[derive(Debug)] |
315 | pub struct LibraryData { | 361 | pub struct LibraryData { |
316 | files: Vec<(FileId, String)>, | 362 | root_id: SourceRootId, |
317 | file_resolver: FileResolverImp, | 363 | root_change: RootChange, |
318 | symbol_index: SymbolIndex, | 364 | symbol_index: SymbolIndex, |
319 | } | 365 | } |
320 | 366 | ||
321 | impl LibraryData { | 367 | impl LibraryData { |
322 | pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData { | 368 | pub fn prepare( |
323 | let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| { | 369 | root_id: SourceRootId, |
370 | files: Vec<(FileId, RelativePathBuf, Arc<String>)>, | ||
371 | ) -> LibraryData { | ||
372 | let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, _, text)| { | ||
324 | let file = SourceFileNode::parse(text); | 373 | let file = SourceFileNode::parse(text); |
325 | (*file_id, file) | 374 | (*file_id, file) |
326 | })); | 375 | })); |
376 | let mut root_change = RootChange::default(); | ||
377 | root_change.added = files | ||
378 | .into_iter() | ||
379 | .map(|(file_id, path, text)| AddFile { | ||
380 | file_id, | ||
381 | path, | ||
382 | text, | ||
383 | }) | ||
384 | .collect(); | ||
327 | LibraryData { | 385 | LibraryData { |
328 | files, | 386 | root_id, |
329 | file_resolver: FileResolverImp::new(file_resolver), | 387 | root_change, |
330 | symbol_index, | 388 | symbol_index, |
331 | } | 389 | } |
332 | } | 390 | } |