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