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.rs81
1 files changed, 57 insertions, 24 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs
index 7078e2d31..4a1ae3b64 100644
--- a/crates/ra_analysis/src/lib.rs
+++ b/crates/ra_analysis/src/lib.rs
@@ -9,17 +9,23 @@ extern crate salsa;
9mod db; 9mod db;
10mod descriptors; 10mod descriptors;
11mod imp; 11mod imp;
12mod roots;
13mod symbol_index; 12mod symbol_index;
14mod completion; 13mod completion;
15 14
16use std::{fmt::Debug, sync::Arc}; 15use std::{
16 fmt::Debug,
17 sync::Arc,
18 collections::BTreeMap,
19};
17 20
18use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; 21use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
19use relative_path::{RelativePath, RelativePathBuf}; 22use relative_path::{RelativePath, RelativePathBuf};
20use rustc_hash::FxHashMap; 23use rayon::prelude::*;
21 24
22use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}; 25use crate::{
26 imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp},
27 symbol_index::SymbolIndex,
28};
23 29
24pub use crate::{ 30pub use crate::{
25 descriptors::FnDescriptor, 31 descriptors::FnDescriptor,
@@ -49,9 +55,9 @@ pub struct FileId(pub u32);
49#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 55#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
50pub struct CrateId(pub u32); 56pub struct CrateId(pub u32);
51 57
52#[derive(Debug, Clone, Default)] 58#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
53pub struct CrateGraph { 59pub struct CrateGraph {
54 pub crate_roots: FxHashMap<CrateId, FileId>, 60 pub crate_roots: BTreeMap<CrateId, FileId>,
55} 61}
56 62
57pub trait FileResolver: Debug + Send + Sync + 'static { 63pub trait FileResolver: Debug + Send + Sync + 'static {
@@ -59,6 +65,41 @@ pub trait FileResolver: Debug + Send + Sync + 'static {
59 fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>; 65 fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>;
60} 66}
61 67
68#[derive(Debug, Default)]
69pub struct AnalysisChange {
70 files_added: Vec<(FileId, String)>,
71 files_changed: Vec<(FileId, String)>,
72 files_removed: Vec<(FileId)>,
73 libraries_added: Vec<LibraryData>,
74 crate_graph: Option<CrateGraph>,
75 file_resolver: Option<FileResolverImp>,
76}
77
78
79impl AnalysisChange {
80 pub fn new() -> AnalysisChange {
81 AnalysisChange::default()
82 }
83 pub fn add_file(&mut self, file_id: FileId, text: String) {
84 self.files_added.push((file_id, text))
85 }
86 pub fn change_file(&mut self, file_id: FileId, new_text: String) {
87 self.files_changed.push((file_id, new_text))
88 }
89 pub fn remove_file(&mut self, file_id: FileId) {
90 self.files_removed.push(file_id)
91 }
92 pub fn add_library(&mut self, data: LibraryData) {
93 self.libraries_added.push(data)
94 }
95 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
96 self.crate_graph = Some(graph);
97 }
98 pub fn set_file_resolver(&mut self, file_resolver: Arc<FileResolver>) {
99 self.file_resolver = Some(FileResolverImp::new(file_resolver));
100 }
101}
102
62#[derive(Debug)] 103#[derive(Debug)]
63pub struct AnalysisHost { 104pub struct AnalysisHost {
64 imp: AnalysisHostImpl, 105 imp: AnalysisHostImpl,
@@ -75,20 +116,8 @@ impl AnalysisHost {
75 imp: self.imp.analysis(), 116 imp: self.imp.analysis(),
76 } 117 }
77 } 118 }
78 pub fn change_file(&mut self, file_id: FileId, text: Option<String>) { 119 pub fn apply_change(&mut self, change: AnalysisChange) {
79 self.change_files(::std::iter::once((file_id, text))); 120 self.imp.apply_change(change)
80 }
81 pub fn change_files(&mut self, mut changes: impl Iterator<Item = (FileId, Option<String>)>) {
82 self.imp.change_files(&mut changes)
83 }
84 pub fn set_file_resolver(&mut self, resolver: Arc<FileResolver>) {
85 self.imp.set_file_resolver(FileResolverImp::new(resolver));
86 }
87 pub fn set_crate_graph(&mut self, graph: CrateGraph) {
88 self.imp.set_crate_graph(graph)
89 }
90 pub fn add_library(&mut self, data: LibraryData) {
91 self.imp.add_library(data.root)
92 } 121 }
93} 122}
94 123
@@ -266,14 +295,18 @@ impl Analysis {
266 295
267#[derive(Debug)] 296#[derive(Debug)]
268pub struct LibraryData { 297pub struct LibraryData {
269 root: roots::ReadonlySourceRoot, 298 files: Vec<(FileId, String)>,
299 file_resolver: FileResolverImp,
300 symbol_index: SymbolIndex,
270} 301}
271 302
272impl LibraryData { 303impl LibraryData {
273 pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData { 304 pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData {
274 let file_resolver = FileResolverImp::new(file_resolver); 305 let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| {
275 let root = roots::ReadonlySourceRoot::new(files, file_resolver); 306 let file = File::parse(text);
276 LibraryData { root } 307 (*file_id, file)
308 }));
309 LibraryData { files, file_resolver: FileResolverImp::new(file_resolver), symbol_index }
277 } 310 }
278} 311}
279 312