From ee4d904cfb1b604bc8627491e05980ac43cd59e3 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Oct 2018 10:57:55 +0300 Subject: Store all the data in the Salsa Database --- crates/ra_analysis/src/lib.rs | 81 ++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'crates/ra_analysis/src/lib.rs') 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; mod db; mod descriptors; mod imp; -mod roots; mod symbol_index; mod completion; -use std::{fmt::Debug, sync::Arc}; +use std::{ + fmt::Debug, + sync::Arc, + collections::BTreeMap, +}; use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; use relative_path::{RelativePath, RelativePathBuf}; -use rustc_hash::FxHashMap; +use rayon::prelude::*; -use crate::imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}; +use crate::{ + imp::{AnalysisHostImpl, AnalysisImpl, FileResolverImp}, + symbol_index::SymbolIndex, +}; pub use crate::{ descriptors::FnDescriptor, @@ -49,9 +55,9 @@ pub struct FileId(pub u32); #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct CrateId(pub u32); -#[derive(Debug, Clone, Default)] +#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] pub struct CrateGraph { - pub crate_roots: FxHashMap, + pub crate_roots: BTreeMap, } pub trait FileResolver: Debug + Send + Sync + 'static { @@ -59,6 +65,41 @@ pub trait FileResolver: Debug + Send + Sync + 'static { fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option; } +#[derive(Debug, Default)] +pub struct AnalysisChange { + files_added: Vec<(FileId, String)>, + files_changed: Vec<(FileId, String)>, + files_removed: Vec<(FileId)>, + libraries_added: Vec, + crate_graph: Option, + file_resolver: Option, +} + + +impl AnalysisChange { + pub fn new() -> AnalysisChange { + AnalysisChange::default() + } + pub fn add_file(&mut self, file_id: FileId, text: String) { + self.files_added.push((file_id, text)) + } + pub fn change_file(&mut self, file_id: FileId, new_text: String) { + self.files_changed.push((file_id, new_text)) + } + pub fn remove_file(&mut self, file_id: FileId) { + self.files_removed.push(file_id) + } + pub fn add_library(&mut self, data: LibraryData) { + self.libraries_added.push(data) + } + pub fn set_crate_graph(&mut self, graph: CrateGraph) { + self.crate_graph = Some(graph); + } + pub fn set_file_resolver(&mut self, file_resolver: Arc) { + self.file_resolver = Some(FileResolverImp::new(file_resolver)); + } +} + #[derive(Debug)] pub struct AnalysisHost { imp: AnalysisHostImpl, @@ -75,20 +116,8 @@ impl AnalysisHost { imp: self.imp.analysis(), } } - pub fn change_file(&mut self, file_id: FileId, text: Option) { - self.change_files(::std::iter::once((file_id, text))); - } - pub fn change_files(&mut self, mut changes: impl Iterator)>) { - self.imp.change_files(&mut changes) - } - pub fn set_file_resolver(&mut self, resolver: Arc) { - self.imp.set_file_resolver(FileResolverImp::new(resolver)); - } - pub fn set_crate_graph(&mut self, graph: CrateGraph) { - self.imp.set_crate_graph(graph) - } - pub fn add_library(&mut self, data: LibraryData) { - self.imp.add_library(data.root) + pub fn apply_change(&mut self, change: AnalysisChange) { + self.imp.apply_change(change) } } @@ -266,14 +295,18 @@ impl Analysis { #[derive(Debug)] pub struct LibraryData { - root: roots::ReadonlySourceRoot, + files: Vec<(FileId, String)>, + file_resolver: FileResolverImp, + symbol_index: SymbolIndex, } impl LibraryData { pub fn prepare(files: Vec<(FileId, String)>, file_resolver: Arc) -> LibraryData { - let file_resolver = FileResolverImp::new(file_resolver); - let root = roots::ReadonlySourceRoot::new(files, file_resolver); - LibraryData { root } + let symbol_index = SymbolIndex::for_files(files.par_iter().map(|(file_id, text)| { + let file = File::parse(text); + (*file_id, file) + })); + LibraryData { files, file_resolver: FileResolverImp::new(file_resolver), symbol_index } } } -- cgit v1.2.3 From 56df0fc83c753b7fb8829438c8d3017ef1bf450c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Oct 2018 16:03:49 +0300 Subject: Improve logging --- crates/ra_analysis/src/lib.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'crates/ra_analysis/src/lib.rs') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 4a1ae3b64..703938cf9 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -13,7 +13,7 @@ mod symbol_index; mod completion; use std::{ - fmt::Debug, + fmt, sync::Arc, collections::BTreeMap, }; @@ -60,12 +60,12 @@ pub struct CrateGraph { pub crate_roots: BTreeMap, } -pub trait FileResolver: Debug + Send + Sync + 'static { +pub trait FileResolver: fmt::Debug + Send + Sync + 'static { fn file_stem(&self, file_id: FileId) -> String; fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option; } -#[derive(Debug, Default)] +#[derive(Default)] pub struct AnalysisChange { files_added: Vec<(FileId, String)>, files_changed: Vec<(FileId, String)>, @@ -75,6 +75,19 @@ pub struct AnalysisChange { file_resolver: Option, } +impl fmt::Debug for AnalysisChange { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt.debug_struct("AnalysisChange") + .field("files_added", &self.files_added.len()) + .field("files_changed", &self.files_changed.len()) + .field("files_removed", &self.files_removed.len()) + .field("libraries_added", &self.libraries_added.len()) + .field("crate_graph", &self.crate_graph) + .field("file_resolver", &self.file_resolver) + .finish() + } +} + impl AnalysisChange { pub fn new() -> AnalysisChange { -- cgit v1.2.3 From e0eb33605a917d2e7289debe0c915e75286b834f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Oct 2018 17:40:24 +0300 Subject: Encapsulate CrateGraph a bit --- crates/ra_analysis/src/lib.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'crates/ra_analysis/src/lib.rs') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 703938cf9..af7894cd0 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -15,9 +15,9 @@ mod completion; use std::{ fmt, sync::Arc, - collections::BTreeMap, }; +use rustc_hash::FxHashMap; use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; use relative_path::{RelativePath, RelativePathBuf}; use rayon::prelude::*; @@ -55,9 +55,21 @@ pub struct FileId(pub u32); #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct CrateId(pub u32); -#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] +#[derive(Debug, Clone, Default, PartialEq, Eq)] pub struct CrateGraph { - pub crate_roots: BTreeMap, + crate_roots: FxHashMap, +} + +impl CrateGraph { + pub fn new() -> CrateGraph { + CrateGraph::default() + } + pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId{ + let crate_id = CrateId(self.crate_roots.len() as u32); + let prev = self.crate_roots.insert(crate_id, file_id); + assert!(prev.is_none()); + crate_id + } } pub trait FileResolver: fmt::Debug + Send + Sync + 'static { -- cgit v1.2.3 From d19001957172287afee90bd4fc3d037d2ce6f5dd Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Oct 2018 17:52:50 +0300 Subject: Move input to top-level --- crates/ra_analysis/src/lib.rs | 33 +++------------------------------ 1 file changed, 3 insertions(+), 30 deletions(-) (limited to 'crates/ra_analysis/src/lib.rs') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index af7894cd0..a67cac21e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -6,6 +6,7 @@ extern crate relative_path; extern crate rustc_hash; extern crate salsa; +mod input; mod db; mod descriptors; mod imp; @@ -17,9 +18,8 @@ use std::{ sync::Arc, }; -use rustc_hash::FxHashMap; use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; -use relative_path::{RelativePath, RelativePathBuf}; +use relative_path::RelativePathBuf; use rayon::prelude::*; use crate::{ @@ -29,6 +29,7 @@ use crate::{ pub use crate::{ descriptors::FnDescriptor, + input::{FileId, FileResolver, CrateGraph, CrateId} }; pub use ra_editor::{ CompletionItem, FileSymbol, Fold, FoldKind, HighlightedRange, LineIndex, Runnable, @@ -49,34 +50,6 @@ impl std::fmt::Display for Canceled { impl std::error::Error for Canceled { } -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct FileId(pub u32); - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct CrateId(pub u32); - -#[derive(Debug, Clone, Default, PartialEq, Eq)] -pub struct CrateGraph { - crate_roots: FxHashMap, -} - -impl CrateGraph { - pub fn new() -> CrateGraph { - CrateGraph::default() - } - pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId{ - let crate_id = CrateId(self.crate_roots.len() as u32); - let prev = self.crate_roots.insert(crate_id, file_id); - assert!(prev.is_none()); - crate_id - } -} - -pub trait FileResolver: fmt::Debug + Send + Sync + 'static { - fn file_stem(&self, file_id: FileId) -> String; - fn resolve(&self, file_id: FileId, path: &RelativePath) -> Option; -} - #[derive(Default)] pub struct AnalysisChange { files_added: Vec<(FileId, String)>, -- cgit v1.2.3