diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-25 16:04:48 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-10-25 16:04:48 +0100 |
commit | 5932bd0bb5fcd066a9d16abcd1597b7097978085 (patch) | |
tree | 7b6ea86855fe34c3d45d14262c8cf94e315566e8 /crates/ra_analysis/src/lib.rs | |
parent | 2cb2074c4b7219b32993abdcc7084637c0123d49 (diff) | |
parent | 363adf07b7763cfe7e13fac0ee148361d51834e4 (diff) |
Merge #162
162: Db everywhere r=matklad a=matklad
This PR continues our switch to salsa.
Now *all* state is handled by a single salsa database.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_analysis/src/lib.rs')
-rw-r--r-- | crates/ra_analysis/src/lib.rs | 99 |
1 files changed, 65 insertions, 34 deletions
diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 7078e2d31..a67cac21e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs | |||
@@ -6,23 +6,30 @@ extern crate relative_path; | |||
6 | extern crate rustc_hash; | 6 | extern crate rustc_hash; |
7 | extern crate salsa; | 7 | extern crate salsa; |
8 | 8 | ||
9 | mod input; | ||
9 | mod db; | 10 | mod db; |
10 | mod descriptors; | 11 | mod descriptors; |
11 | mod imp; | 12 | mod imp; |
12 | mod roots; | ||
13 | mod symbol_index; | 13 | mod symbol_index; |
14 | mod completion; | 14 | mod completion; |
15 | 15 | ||
16 | use std::{fmt::Debug, sync::Arc}; | 16 | use std::{ |
17 | fmt, | ||
18 | sync::Arc, | ||
19 | }; | ||
17 | 20 | ||
18 | use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; | 21 | use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; |
19 | use relative_path::{RelativePath, RelativePathBuf}; | 22 | use relative_path::RelativePathBuf; |
20 | use rustc_hash::FxHashMap; | 23 | use rayon::prelude::*; |
21 | 24 | ||
22 | use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}; | 25 | use crate::{ |
26 | imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, | ||
27 | symbol_index::SymbolIndex, | ||
28 | }; | ||
23 | 29 | ||
24 | pub use crate::{ | 30 | pub use crate::{ |
25 | descriptors::FnDescriptor, | 31 | descriptors::FnDescriptor, |
32 | input::{FileId, FileResolver, CrateGraph, CrateId} | ||
26 | }; | 33 | }; |
27 | pub use ra_editor::{ | 34 | pub use ra_editor::{ |
28 | CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, | 35 | CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, |
@@ -43,20 +50,52 @@ impl std::fmt::Display for Canceled { | |||
43 | impl std::error::Error for Canceled { | 50 | impl std::error::Error for Canceled { |
44 | } | 51 | } |
45 | 52 | ||
46 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 53 | #[derive(Default)] |
47 | pub struct FileId(pub u32); | 54 | pub struct AnalysisChange { |
48 | 55 | files_added: Vec<(FileId, String)>, | |
49 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] | 56 | files_changed: Vec<(FileId, String)>, |
50 | pub struct CrateId(pub u32); | 57 | files_removed: Vec<(FileId)>, |
58 | libraries_added: Vec<LibraryData>, | ||
59 | crate_graph: Option<CrateGraph>, | ||
60 | file_resolver: Option<FileResolverImp>, | ||
61 | } | ||
51 | 62 | ||
52 | #[derive(Debug, Clone, Default)] | 63 | impl fmt::Debug for AnalysisChange { |
53 | pub struct CrateGraph { | 64 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
54 | pub crate_roots: FxHashMap<CrateId, FileId>, | 65 | fmt.debug_struct("AnalysisChange") |
66 | .field("files_added", &self.files_added.len()) | ||
67 | .field("files_changed", &self.files_changed.len()) | ||
68 | .field("files_removed", &self.files_removed.len()) | ||
69 | .field("libraries_added", &self.libraries_added.len()) | ||
70 | .field("crate_graph", &self.crate_graph) | ||
71 | .field("file_resolver", &self.file_resolver) | ||
72 | .finish() | ||
73 | } | ||
55 | } | 74 | } |
56 | 75 | ||
57 | pub trait FileResolver: Debug + Send + Sync + 'static { | 76 | |
58 | fn file_stem(&self, file_id: FileId) -> String; | 77 | impl AnalysisChange { |
59 | fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option<FileId>; | 78 | pub fn new() -> AnalysisChange { |
79 | AnalysisChange::default() | ||
80 | } | ||
81 | pub fn add_file(&mut self, file_id: FileId, text: String) { | ||
82 | self.files_added.push((file_id, text)) | ||
83 | } | ||
84 | pub fn change_file(&mut self, file_id: FileId, new_text: String) { | ||
85 | self.files_changed.push((file_id, new_text)) | ||
86 | } | ||
87 | pub fn remove_file(&mut self, file_id: FileId) { | ||
88 | self.files_removed.push(file_id) | ||
89 | } | ||
90 | pub fn add_library(&mut self, data: LibraryData) { | ||
91 | self.libraries_added.push(data) | ||
92 | } | ||
93 | pub fn set_crate_graph(&mut self, graph: CrateGraph) { | ||
94 | self.crate_graph = Some(graph); | ||
95 | } | ||
96 | pub fn set_file_resolver(&mut self, file_resolver: Arc<FileResolver>) { | ||
97 | self.file_resolver = Some(FileResolverImp::new(file_resolver)); | ||
98 | } | ||
60 | } | 99 | } |
61 | 100 | ||
62 | #[derive(Debug)] | 101 | #[derive(Debug)] |
@@ -75,20 +114,8 @@ impl AnalysisHost { | |||
75 | imp: self.imp.analysis(), | 114 | imp: self.imp.analysis(), |
76 | } | 115 | } |
77 | } | 116 | } |
78 | pub fn change_file(&mut self, file_id: FileId, text: Option<String>) { | 117 | pub fn apply_change(&mut self, change: AnalysisChange) { |
79 | self.change_files(::std::iter::once((file_id, text))); | 118 | 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 | } | 119 | } |
93 | } | 120 | } |
94 | 121 | ||
@@ -266,14 +293,18 @@ impl Analysis { | |||
266 | 293 | ||
267 | #[derive(Debug)] | 294 | #[derive(Debug)] |
268 | pub struct LibraryData { | 295 | pub struct LibraryData { |
269 | root: roots::ReadonlySourceRoot, | 296 | files: Vec<(FileId, String)>, |
297 | file_resolver: FileResolverImp, | ||
298 | symbol_index: SymbolIndex, | ||
270 | } | 299 | } |
271 | 300 | ||
272 | impl LibraryData { | 301 | impl LibraryData { |
273 | pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData { | 302 | pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc<FileResolver>) -> LibraryData { |
274 | let file_resolver = FileResolverImp::new(file_resolver); | 303 | let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| { |
275 | let root = roots::ReadonlySourceRoot::new(files, file_resolver); | 304 | let file = File::parse(text); |
276 | LibraryData { root } | 305 | (*file_id, file) |
306 | })); | ||
307 | LibraryData { files, file_resolver: FileResolverImp::new(file_resolver), symbol_index } | ||
277 | } | 308 | } |
278 | } | 309 | } |
279 | 310 | ||