From a94530afb354d2f9a3e3864c678aa496c8de6a23 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 18:08:14 +0300 Subject: move world-symbols to file_symbols --- crates/ra_analysis/src/imp.rs | 38 ++---------------------------- crates/ra_analysis/src/lib.rs | 4 +--- crates/ra_analysis/src/symbol_index.rs | 42 +++++++++++++++++++++++++++++++--- 3 files changed, 42 insertions(+), 42 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 8071554a7..9ac52b5c3 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -3,7 +3,6 @@ use std::{ sync::Arc, }; -use rayon::prelude::*; use salsa::{Database, ParallelDatabase}; use hir::{ @@ -25,7 +24,7 @@ use crate::{ completion::{CompletionItem, completions}, CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, Query, ReferenceResolution, RootChange, SourceChange, SourceFileEdit, - symbol_index::{LibrarySymbolsQuery, SymbolIndex, SymbolsDatabase, FileSymbol}, + symbol_index::{LibrarySymbolsQuery, FileSymbol}, }; #[derive(Debug, Default)] @@ -149,39 +148,6 @@ impl AnalysisImpl { pub fn file_line_index(&self, file_id: FileId) -> Arc { self.db.file_lines(file_id) } - pub fn world_symbols(&self, query: Query) -> Cancelable> { - /// Need to wrap Snapshot to provide `Clone` impl for `map_with` - struct Snap(salsa::Snapshot); - impl Clone for Snap { - fn clone(&self) -> Snap { - Snap(self.0.snapshot()) - } - } - - let buf: Vec> = if query.libs { - let snap = Snap(self.db.snapshot()); - self.db - .library_roots() - .par_iter() - .map_with(snap, |db, &lib_id| db.0.library_symbols(lib_id)) - .collect() - } else { - let mut files = Vec::new(); - for &root in self.db.local_roots().iter() { - let sr = self.db.source_root(root); - files.extend(sr.files.values().map(|&it| it)) - } - - let snap = Snap(self.db.snapshot()); - files - .par_iter() - .map_with(snap, |db, &file_id| db.0.file_symbols(file_id)) - .filter_map(|it| it.ok()) - .collect() - }; - Ok(query.search(&buf)) - } - pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable> { let descr = match source_binder::module_from_position(&*self.db, position)? { None => return Ok(None), @@ -555,7 +521,7 @@ impl AnalysisImpl { let mut query = Query::new(name.to_string()); query.exact(); query.limit(4); - self.world_symbols(query) + crate::symbol_index::world_symbols(&*self.db, query) } } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 9576453ab..03550832e 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -342,9 +342,7 @@ impl Analysis { ra_editor::folding_ranges(&file) } pub fn symbol_search(&self, query: Query) -> Cancelable> { - let res = self - .imp - .world_symbols(query)? + let res = symbol_index::world_symbols(&*self.imp.db, query)? .into_iter() .map(|(file_id, symbol)| NavigationTarget { file_id, symbol }) .collect(); diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index 56a84a850..ddcf3d052 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -10,12 +10,13 @@ use ra_syntax::{ SyntaxKind::{self, *}, ast::{self, NameOwner, DocCommentsOwner}, }; -use ra_db::{SyntaxDatabase, SourceRootId}; +use ra_db::{SyntaxDatabase, SourceRootId, FilesDatabase}; +use salsa::ParallelDatabase; use rayon::prelude::*; use crate::{ - Cancelable, - FileId, Query, + Cancelable, FileId, Query, + db::RootDatabase, }; salsa::query_group! { @@ -36,6 +37,41 @@ fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable Cancelable> { + /// Need to wrap Snapshot to provide `Clone` impl for `map_with` + struct Snap(salsa::Snapshot); + impl Clone for Snap { + fn clone(&self) -> Snap { + Snap(self.0.snapshot()) + } + } + + let buf: Vec> = if query.libs { + let snap = Snap(db.snapshot()); + db.library_roots() + .par_iter() + .map_with(snap, |db, &lib_id| db.0.library_symbols(lib_id)) + .collect() + } else { + let mut files = Vec::new(); + for &root in db.local_roots().iter() { + let sr = db.source_root(root); + files.extend(sr.files.values().map(|&it| it)) + } + + let snap = Snap(db.snapshot()); + files + .par_iter() + .map_with(snap, |db, &file_id| db.0.file_symbols(file_id)) + .filter_map(|it| it.ok()) + .collect() + }; + Ok(query.search(&buf)) +} + #[derive(Default, Debug)] pub(crate) struct SymbolIndex { symbols: Vec<(FileId, FileSymbol)>, -- cgit v1.2.3 From 2f22c861a925b6851540430ede0b87fe83c5374c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 18:11:49 +0300 Subject: remove some methods from analysis impl --- crates/ra_analysis/src/imp.rs | 13 ++----------- crates/ra_analysis/src/lib.rs | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 22 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 9ac52b5c3..248b77f5a 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -9,7 +9,7 @@ use hir::{ self, FnSignatureInfo, Problem, source_binder, }; use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; -use ra_editor::{self, find_node_at_offset, LineIndex, LocalEdit, Severity}; +use ra_editor::{self, find_node_at_offset, LocalEdit, Severity}; use ra_syntax::{ algo::find_covering_node, ast::{self, ArgListOwner, Expr, FnDef, NameOwner}, @@ -139,15 +139,6 @@ impl fmt::Debug for AnalysisImpl { } impl AnalysisImpl { - pub fn file_text(&self, file_id: FileId) -> Arc { - self.db.file_text(file_id) - } - pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { - self.db.source_file(file_id) - } - pub fn file_line_index(&self, file_id: FileId) -> Arc { - self.db.file_lines(file_id) - } pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable> { let descr = match source_binder::module_from_position(&*self.db, position)? { None => return Ok(None), @@ -400,7 +391,7 @@ impl AnalysisImpl { } pub fn assists(&self, frange: FileRange) -> Vec { - let file = self.file_syntax(frange.file_id); + let file = self.db.source_file(frange.file_id); let offset = frange.range.start(); let actions = vec![ ra_editor::flip_comma(&file, offset).map(|f| f()), diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 03550832e..ef08a721c 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -44,7 +44,7 @@ pub use hir::FnSignatureInfo; pub use ra_db::{ Canceled, Cancelable, FilePosition, FileRange, - CrateGraph, CrateId, SourceRootId, FileId + CrateGraph, CrateId, SourceRootId, FileId, SyntaxDatabase, FilesDatabase }; #[derive(Default)] @@ -298,13 +298,13 @@ pub struct Analysis { impl Analysis { pub fn file_text(&self, file_id: FileId) -> Arc { - self.imp.file_text(file_id) + self.imp.db.file_text(file_id) } pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { - self.imp.file_syntax(file_id).clone() + self.imp.db.source_file(file_id).clone() } pub fn file_line_index(&self, file_id: FileId) -> Arc { - self.imp.file_line_index(file_id) + self.imp.db.file_lines(file_id) } pub fn extend_selection(&self, frange: FileRange) -> TextRange { extend_selection::extend_selection(&self.imp.db, frange) @@ -313,32 +313,32 @@ impl Analysis { ra_editor::matching_brace(file, offset) } pub fn syntax_tree(&self, file_id: FileId) -> String { - let file = self.imp.file_syntax(file_id); + let file = self.imp.db.source_file(file_id); ra_editor::syntax_tree(&file) } pub fn join_lines(&self, frange: FileRange) -> SourceChange { - let file = self.imp.file_syntax(frange.file_id); + let file = self.imp.db.source_file(frange.file_id); SourceChange::from_local_edit(frange.file_id, ra_editor::join_lines(&file, frange.range)) } pub fn on_enter(&self, position: FilePosition) -> Option { - let file = self.imp.file_syntax(position.file_id); + let file = self.imp.db.source_file(position.file_id); let edit = ra_editor::on_enter(&file, position.offset)?; let res = SourceChange::from_local_edit(position.file_id, edit); Some(res) } pub fn on_eq_typed(&self, position: FilePosition) -> Option { - let file = self.imp.file_syntax(position.file_id); + let file = self.imp.db.source_file(position.file_id); Some(SourceChange::from_local_edit( position.file_id, ra_editor::on_eq_typed(&file, position.offset)?, )) } pub fn file_structure(&self, file_id: FileId) -> Vec { - let file = self.imp.file_syntax(file_id); + let file = self.imp.db.source_file(file_id); ra_editor::file_structure(&file) } pub fn folding_ranges(&self, file_id: FileId) -> Vec { - let file = self.imp.file_syntax(file_id); + let file = self.imp.db.source_file(file_id); ra_editor::folding_ranges(&file) } pub fn symbol_search(&self, query: Query) -> Cancelable> { @@ -373,7 +373,7 @@ impl Analysis { Ok(self.imp.crate_root(crate_id)) } pub fn runnables(&self, file_id: FileId) -> Cancelable> { - let file = self.imp.file_syntax(file_id); + let file = self.imp.db.source_file(file_id); Ok(runnables::runnables(self, &file, file_id)) } pub fn highlight(&self, file_id: FileId) -> Cancelable> { -- cgit v1.2.3 From e9b47dbb36d36b8fd7500e06581fd0ef687cc582 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 18:41:41 +0300 Subject: remove AnalysisHostImpl --- crates/ra_analysis/src/imp.rs | 63 +++++++++++++++---------------------------- crates/ra_analysis/src/lib.rs | 8 +++--- 2 files changed, 26 insertions(+), 45 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 248b77f5a..d9a3f97e9 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -27,32 +27,25 @@ use crate::{ symbol_index::{LibrarySymbolsQuery, FileSymbol}, }; -#[derive(Debug, Default)] -pub(crate) struct AnalysisHostImpl { - db: db::RootDatabase, -} - -impl AnalysisHostImpl { - pub fn analysis(&self) -> AnalysisImpl { +impl db::RootDatabase { + pub(crate) fn analysis(&self) -> AnalysisImpl { AnalysisImpl { - db: self.db.snapshot(), + db: self.snapshot(), } } - pub fn apply_change(&mut self, change: AnalysisChange) { + pub(crate) fn apply_change(&mut self, change: AnalysisChange) { log::info!("apply_change {:?}", change); // self.gc_syntax_trees(); if !change.new_roots.is_empty() { - let mut local_roots = Vec::clone(&self.db.local_roots()); + let mut local_roots = Vec::clone(&self.local_roots()); for (root_id, is_local) in change.new_roots { - self.db - .query_mut(ra_db::SourceRootQuery) + self.query_mut(ra_db::SourceRootQuery) .set(root_id, Default::default()); if is_local { local_roots.push(root_id); } } - self.db - .query_mut(ra_db::LocalRootsQuery) + self.query_mut(ra_db::LocalRootsQuery) .set((), Arc::new(local_roots)); } @@ -60,53 +53,44 @@ impl AnalysisHostImpl { self.apply_root_change(root_id, root_change); } for (file_id, text) in change.files_changed { - self.db.query_mut(ra_db::FileTextQuery).set(file_id, text) + self.query_mut(ra_db::FileTextQuery).set(file_id, text) } if !change.libraries_added.is_empty() { - let mut libraries = Vec::clone(&self.db.library_roots()); + let mut libraries = Vec::clone(&self.library_roots()); for library in change.libraries_added { libraries.push(library.root_id); - self.db - .query_mut(ra_db::SourceRootQuery) + self.query_mut(ra_db::SourceRootQuery) .set(library.root_id, Default::default()); - self.db - .query_mut(LibrarySymbolsQuery) + self.query_mut(LibrarySymbolsQuery) .set_constant(library.root_id, Arc::new(library.symbol_index)); self.apply_root_change(library.root_id, library.root_change); } - self.db - .query_mut(ra_db::LibraryRootsQuery) + self.query_mut(ra_db::LibraryRootsQuery) .set((), Arc::new(libraries)); } if let Some(crate_graph) = change.crate_graph { - self.db - .query_mut(ra_db::CrateGraphQuery) + self.query_mut(ra_db::CrateGraphQuery) .set((), Arc::new(crate_graph)) } } fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { - let mut source_root = SourceRoot::clone(&self.db.source_root(root_id)); + let mut source_root = SourceRoot::clone(&self.source_root(root_id)); for add_file in root_change.added { - self.db - .query_mut(ra_db::FileTextQuery) + self.query_mut(ra_db::FileTextQuery) .set(add_file.file_id, add_file.text); - self.db - .query_mut(ra_db::FileRelativePathQuery) + self.query_mut(ra_db::FileRelativePathQuery) .set(add_file.file_id, add_file.path.clone()); - self.db - .query_mut(ra_db::FileSourceRootQuery) + self.query_mut(ra_db::FileSourceRootQuery) .set(add_file.file_id, root_id); source_root.files.insert(add_file.path, add_file.file_id); } for remove_file in root_change.removed { - self.db - .query_mut(ra_db::FileTextQuery) + self.query_mut(ra_db::FileTextQuery) .set(remove_file.file_id, Default::default()); source_root.files.remove(&remove_file.path); } - self.db - .query_mut(ra_db::SourceRootQuery) + self.query_mut(ra_db::SourceRootQuery) .set(root_id, Arc::new(source_root)); } @@ -115,14 +99,11 @@ impl AnalysisHostImpl { /// syntax trees. However, if we actually do that, everything is recomputed /// for some reason. Needs investigation. fn gc_syntax_trees(&mut self) { - self.db - .query(ra_db::SourceFileQuery) + self.query(ra_db::SourceFileQuery) .sweep(salsa::SweepStrategy::default().discard_values()); - self.db - .query(hir::db::SourceFileItemsQuery) + self.query(hir::db::SourceFileItemsQuery) .sweep(salsa::SweepStrategy::default().discard_values()); - self.db - .query(hir::db::FileItemQuery) + self.query(hir::db::FileItemQuery) .sweep(salsa::SweepStrategy::default().discard_values()); } } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index ef08a721c..03994b7c4 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -29,7 +29,7 @@ use rayon::prelude::*; use relative_path::RelativePathBuf; use crate::{ - imp::{AnalysisHostImpl, AnalysisImpl}, + imp::AnalysisImpl, symbol_index::{SymbolIndex, FileSymbol}, }; @@ -153,7 +153,7 @@ impl AnalysisChange { /// `AnalysisHost` stores the current state of the world. #[derive(Debug, Default)] pub struct AnalysisHost { - imp: AnalysisHostImpl, + db: db::RootDatabase, } impl AnalysisHost { @@ -161,13 +161,13 @@ impl AnalysisHost { /// semantic information. pub fn analysis(&self) -> Analysis { Analysis { - imp: self.imp.analysis(), + imp: self.db.analysis(), } } /// Applies changes to the current state of the world. If there are /// outstanding snapshots, they will be canceled. pub fn apply_change(&mut self, change: AnalysisChange) { - self.imp.apply_change(change) + self.db.apply_change(change) } } -- cgit v1.2.3 From 08d1537468933910f22aa1cd517cae6b9f97d3ff Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 19:20:56 +0300 Subject: get rid of AnalysisImpl --- crates/ra_analysis/src/imp.rs | 118 ++++++++++++++++++------------------------ crates/ra_analysis/src/lib.rs | 63 +++++++++++----------- 2 files changed, 80 insertions(+), 101 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index d9a3f97e9..0faf8b85d 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -1,9 +1,6 @@ -use std::{ - fmt, - sync::Arc, -}; +use std::sync::Arc; -use salsa::{Database, ParallelDatabase}; +use salsa::Database; use hir::{ self, FnSignatureInfo, Problem, source_binder, @@ -21,18 +18,12 @@ use ra_syntax::{ use crate::{ AnalysisChange, Cancelable, NavigationTarget, - completion::{CompletionItem, completions}, CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, Query, ReferenceResolution, RootChange, SourceChange, SourceFileEdit, symbol_index::{LibrarySymbolsQuery, FileSymbol}, }; impl db::RootDatabase { - pub(crate) fn analysis(&self) -> AnalysisImpl { - AnalysisImpl { - db: self.snapshot(), - } - } pub(crate) fn apply_change(&mut self, change: AnalysisChange) { log::info!("apply_change {:?}", change); // self.gc_syntax_trees(); @@ -108,20 +99,9 @@ impl db::RootDatabase { } } -pub(crate) struct AnalysisImpl { - pub(crate) db: salsa::Snapshot, -} - -impl fmt::Debug for AnalysisImpl { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - let db: &db::RootDatabase = &self.db; - fmt.debug_struct("AnalysisImpl").field("db", db).finish() - } -} - -impl AnalysisImpl { +impl db::RootDatabase { pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable> { - let descr = match source_binder::module_from_position(&*self.db, position)? { + let descr = match source_binder::module_from_position(self, position)? { None => return Ok(None), Some(it) => it, }; @@ -143,12 +123,15 @@ impl AnalysisImpl { /// This returns `Vec` because a module may be included from several places. We /// don't handle this case yet though, so the Vec has length at most one. - pub fn parent_module(&self, position: FilePosition) -> Cancelable> { - let descr = match source_binder::module_from_position(&*self.db, position)? { + pub(crate) fn parent_module( + &self, + position: FilePosition, + ) -> Cancelable> { + let descr = match source_binder::module_from_position(self, position)? { None => return Ok(Vec::new()), Some(it) => it, }; - let (file_id, decl) = match descr.parent_link_source(&*self.db) { + let (file_id, decl) = match descr.parent_link_source(self) { None => return Ok(Vec::new()), Some(it) => it, }; @@ -162,39 +145,33 @@ impl AnalysisImpl { Ok(vec![NavigationTarget { file_id, symbol }]) } /// Returns `Vec` for the same reason as `parent_module` - pub fn crate_for(&self, file_id: FileId) -> Cancelable> { - let descr = match source_binder::module_from_file_id(&*self.db, file_id)? { + pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable> { + let descr = match source_binder::module_from_file_id(self, file_id)? { None => return Ok(Vec::new()), Some(it) => it, }; let root = descr.crate_root(); let file_id = root.file_id(); - let crate_graph = self.db.crate_graph(); + let crate_graph = self.crate_graph(); let crate_id = crate_graph.crate_id_for_crate_root(file_id); Ok(crate_id.into_iter().collect()) } - pub fn crate_root(&self, crate_id: CrateId) -> FileId { - self.db.crate_graph().crate_root(crate_id) - } - pub fn completions(&self, position: FilePosition) -> Cancelable>> { - let completions = completions(&self.db, position)?; - Ok(completions.map(|it| it.into())) + pub(crate) fn crate_root(&self, crate_id: CrateId) -> FileId { + self.crate_graph().crate_root(crate_id) } - pub fn approximately_resolve_symbol( + pub(crate) fn approximately_resolve_symbol( &self, position: FilePosition, ) -> Cancelable> { - let file = self.db.source_file(position.file_id); + let file = self.source_file(position.file_id); let syntax = file.syntax(); if let Some(name_ref) = find_node_at_offset::(syntax, position.offset) { let mut rr = ReferenceResolution::new(name_ref.syntax().range()); - if let Some(fn_descr) = source_binder::function_from_child_node( - &*self.db, - position.file_id, - name_ref.syntax(), - )? { - let scope = fn_descr.scopes(&*self.db); + if let Some(fn_descr) = + source_binder::function_from_child_node(self, position.file_id, name_ref.syntax())? + { + let scope = fn_descr.scopes(self); // First try to resolve the symbol locally if let Some(entry) = scope.resolve_local_name(name_ref) { rr.add_resolution( @@ -219,7 +196,7 @@ impl AnalysisImpl { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if module.has_semi() { if let Some(child_module) = - source_binder::module_from_declaration(&*self.db, position.file_id, module)? + source_binder::module_from_declaration(self, position.file_id, module)? { let file_id = child_module.file_id(); let name = match child_module.name() { @@ -240,10 +217,13 @@ impl AnalysisImpl { Ok(None) } - pub fn find_all_refs(&self, position: FilePosition) -> Cancelable> { - let file = self.db.source_file(position.file_id); + pub(crate) fn find_all_refs( + &self, + position: FilePosition, + ) -> Cancelable> { + let file = self.source_file(position.file_id); // Find the binding associated with the offset - let (binding, descr) = match find_binding(&self.db, &file, position)? { + let (binding, descr) = match find_binding(self, &file, position)? { None => return Ok(Vec::new()), Some(it) => it, }; @@ -255,7 +235,7 @@ impl AnalysisImpl { .collect::>(); ret.extend( descr - .scopes(&*self.db) + .scopes(self) .find_all_refs(binding) .into_iter() .map(|ref_desc| (position.file_id, ref_desc.range)), @@ -293,8 +273,8 @@ impl AnalysisImpl { Ok(Some((binding, descr))) } } - pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable> { - let file = self.db.source_file(nav.file_id); + pub(crate) fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable> { + let file = self.source_file(nav.file_id); let result = match (nav.symbol.description(&file), nav.symbol.docs(&file)) { (Some(desc), Some(docs)) => { Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs) @@ -307,8 +287,8 @@ impl AnalysisImpl { Ok(result) } - pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { - let syntax = self.db.source_file(file_id); + pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable> { + let syntax = self.source_file(file_id); let mut res = ra_editor::diagnostics(&syntax) .into_iter() @@ -319,9 +299,9 @@ impl AnalysisImpl { fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), }) .collect::>(); - if let Some(m) = source_binder::module_from_file_id(&*self.db, file_id)? { - for (name_node, problem) in m.problems(&*self.db) { - let source_root = self.db.file_source_root(file_id); + if let Some(m) = source_binder::module_from_file_id(self, file_id)? { + for (name_node, problem) in m.problems(self) { + let source_root = self.file_source_root(file_id); let diag = match problem { Problem::UnresolvedModule { candidate } => { let create_file = FileSystemEdit::CreateFile { @@ -371,8 +351,8 @@ impl AnalysisImpl { Ok(res) } - pub fn assists(&self, frange: FileRange) -> Vec { - let file = self.db.source_file(frange.file_id); + pub(crate) fn assists(&self, frange: FileRange) -> Vec { + let file = self.source_file(frange.file_id); let offset = frange.range.start(); let actions = vec![ ra_editor::flip_comma(&file, offset).map(|f| f()), @@ -389,11 +369,11 @@ impl AnalysisImpl { .collect() } - pub fn resolve_callable( + pub(crate) fn resolve_callable( &self, position: FilePosition, ) -> Cancelable)>> { - let file = self.db.source_file(position.file_id); + let file = self.source_file(position.file_id); let syntax = file.syntax(); // Find the calling expression and it's NameRef @@ -404,12 +384,12 @@ impl AnalysisImpl { let file_symbols = self.index_resolve(name_ref)?; for (fn_file_id, fs) in file_symbols { if fs.kind == FN_DEF { - let fn_file = self.db.source_file(fn_file_id); + let fn_file = self.source_file(fn_file_id); if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) { let descr = ctry!(source_binder::function_from_source( - &*self.db, fn_file_id, fn_def + self, fn_file_id, fn_def )?); - if let Some(descriptor) = descr.signature_info(&*self.db) { + if let Some(descriptor) = descr.signature_info(self) { // If we have a calling expression let's find which argument we are on let mut current_parameter = None; @@ -456,20 +436,20 @@ impl AnalysisImpl { Ok(None) } - pub fn type_of(&self, frange: FileRange) -> Cancelable> { - let file = self.db.source_file(frange.file_id); + pub(crate) fn type_of(&self, frange: FileRange) -> Cancelable> { + let file = self.source_file(frange.file_id); let syntax = file.syntax(); let node = find_covering_node(syntax, frange.range); let parent_fn = ctry!(node.ancestors().find_map(FnDef::cast)); let function = ctry!(source_binder::function_from_source( - &*self.db, + self, frange.file_id, parent_fn )?); - let infer = function.infer(&*self.db)?; + let infer = function.infer(self)?; Ok(infer.type_of_node(node).map(|t| t.to_string())) } - pub fn rename( + pub(crate) fn rename( &self, position: FilePosition, new_name: &str, @@ -493,7 +473,7 @@ impl AnalysisImpl { let mut query = Query::new(name.to_string()); query.exact(); query.limit(4); - crate::symbol_index::world_symbols(&*self.db, query) + crate::symbol_index::world_symbols(self, query) } } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 03994b7c4..efb483103 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -27,11 +27,9 @@ use ra_syntax::{SourceFileNode, TextRange, TextUnit, SmolStr, SyntaxKind}; use ra_text_edit::TextEdit; use rayon::prelude::*; use relative_path::RelativePathBuf; +use salsa::ParallelDatabase; -use crate::{ - imp::AnalysisImpl, - symbol_index::{SymbolIndex, FileSymbol}, -}; +use crate::symbol_index::{SymbolIndex, FileSymbol}; pub use crate::{ completion::{CompletionItem, CompletionItemKind, InsertText}, @@ -161,7 +159,7 @@ impl AnalysisHost { /// semantic information. pub fn analysis(&self) -> Analysis { Analysis { - imp: self.db.analysis(), + db: self.db.snapshot(), } } /// Applies changes to the current state of the world. If there are @@ -293,56 +291,56 @@ impl ReferenceResolution { /// `Analysis` are canceled (most method return `Err(Canceled)`). #[derive(Debug)] pub struct Analysis { - pub(crate) imp: AnalysisImpl, + db: salsa::Snapshot, } impl Analysis { pub fn file_text(&self, file_id: FileId) -> Arc { - self.imp.db.file_text(file_id) + self.db.file_text(file_id) } pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { - self.imp.db.source_file(file_id).clone() + self.db.source_file(file_id).clone() } pub fn file_line_index(&self, file_id: FileId) -> Arc { - self.imp.db.file_lines(file_id) + self.db.file_lines(file_id) } pub fn extend_selection(&self, frange: FileRange) -> TextRange { - extend_selection::extend_selection(&self.imp.db, frange) + extend_selection::extend_selection(&self.db, frange) } pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option { ra_editor::matching_brace(file, offset) } pub fn syntax_tree(&self, file_id: FileId) -> String { - let file = self.imp.db.source_file(file_id); + let file = self.db.source_file(file_id); ra_editor::syntax_tree(&file) } pub fn join_lines(&self, frange: FileRange) -> SourceChange { - let file = self.imp.db.source_file(frange.file_id); + let file = self.db.source_file(frange.file_id); SourceChange::from_local_edit(frange.file_id, ra_editor::join_lines(&file, frange.range)) } pub fn on_enter(&self, position: FilePosition) -> Option { - let file = self.imp.db.source_file(position.file_id); + let file = self.db.source_file(position.file_id); let edit = ra_editor::on_enter(&file, position.offset)?; let res = SourceChange::from_local_edit(position.file_id, edit); Some(res) } pub fn on_eq_typed(&self, position: FilePosition) -> Option { - let file = self.imp.db.source_file(position.file_id); + let file = self.db.source_file(position.file_id); Some(SourceChange::from_local_edit( position.file_id, ra_editor::on_eq_typed(&file, position.offset)?, )) } pub fn file_structure(&self, file_id: FileId) -> Vec { - let file = self.imp.db.source_file(file_id); + let file = self.db.source_file(file_id); ra_editor::file_structure(&file) } pub fn folding_ranges(&self, file_id: FileId) -> Vec { - let file = self.imp.db.source_file(file_id); + let file = self.db.source_file(file_id); ra_editor::folding_ranges(&file) } pub fn symbol_search(&self, query: Query) -> Cancelable> { - let res = symbol_index::world_symbols(&*self.imp.db, query)? + let res = symbol_index::world_symbols(&*self.db, query)? .into_iter() .map(|(file_id, symbol)| NavigationTarget { file_id, symbol }) .collect(); @@ -352,57 +350,58 @@ impl Analysis { &self, position: FilePosition, ) -> Cancelable> { - self.imp.approximately_resolve_symbol(position) + self.db.approximately_resolve_symbol(position) } pub fn find_all_refs(&self, position: FilePosition) -> Cancelable> { - self.imp.find_all_refs(position) + self.db.find_all_refs(position) } pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable> { - self.imp.doc_text_for(nav) + self.db.doc_text_for(nav) } pub fn parent_module(&self, position: FilePosition) -> Cancelable> { - self.imp.parent_module(position) + self.db.parent_module(position) } pub fn module_path(&self, position: FilePosition) -> Cancelable> { - self.imp.module_path(position) + self.db.module_path(position) } pub fn crate_for(&self, file_id: FileId) -> Cancelable> { - self.imp.crate_for(file_id) + self.db.crate_for(file_id) } pub fn crate_root(&self, crate_id: CrateId) -> Cancelable { - Ok(self.imp.crate_root(crate_id)) + Ok(self.db.crate_root(crate_id)) } pub fn runnables(&self, file_id: FileId) -> Cancelable> { - let file = self.imp.db.source_file(file_id); + let file = self.db.source_file(file_id); Ok(runnables::runnables(self, &file, file_id)) } pub fn highlight(&self, file_id: FileId) -> Cancelable> { - syntax_highlighting::highlight(&*self.imp.db, file_id) + syntax_highlighting::highlight(&*self.db, file_id) } pub fn completions(&self, position: FilePosition) -> Cancelable>> { - self.imp.completions(position) + let completions = completion::completions(&self.db, position)?; + Ok(completions.map(|it| it.into())) } pub fn assists(&self, frange: FileRange) -> Cancelable> { - Ok(self.imp.assists(frange)) + Ok(self.db.assists(frange)) } pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { - self.imp.diagnostics(file_id) + self.db.diagnostics(file_id) } pub fn resolve_callable( &self, position: FilePosition, ) -> Cancelable)>> { - self.imp.resolve_callable(position) + self.db.resolve_callable(position) } pub fn type_of(&self, frange: FileRange) -> Cancelable> { - self.imp.type_of(frange) + self.db.type_of(frange) } pub fn rename( &self, position: FilePosition, new_name: &str, ) -> Cancelable> { - self.imp.rename(position, new_name) + self.db.rename(position, new_name) } } -- cgit v1.2.3 From 5d483cb9980b6a74d02bd5d257b7d30e18955ef9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 19:40:41 +0300 Subject: doctrings --- crates/ra_analysis/src/lib.rs | 90 +++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 28 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index efb483103..b4669dfff 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -148,27 +148,6 @@ impl AnalysisChange { } } -/// `AnalysisHost` stores the current state of the world. -#[derive(Debug, Default)] -pub struct AnalysisHost { - db: db::RootDatabase, -} - -impl AnalysisHost { - /// Returns a snapshot of the current state, which you can query for - /// semantic information. - pub fn analysis(&self) -> Analysis { - Analysis { - db: self.db.snapshot(), - } - } - /// Applies changes to the current state of the world. If there are - /// outstanding snapshots, they will be canceled. - pub fn apply_change(&mut self, change: AnalysisChange) { - self.db.apply_change(change) - } -} - #[derive(Debug)] pub struct SourceChange { pub label: String, @@ -285,6 +264,27 @@ impl ReferenceResolution { } } +/// `AnalysisHost` stores the current state of the world. +#[derive(Debug, Default)] +pub struct AnalysisHost { + db: db::RootDatabase, +} + +impl AnalysisHost { + /// Returns a snapshot of the current state, which you can query for + /// semantic information. + pub fn analysis(&self) -> Analysis { + Analysis { + db: self.db.snapshot(), + } + } + /// Applies changes to the current state of the world. If there are + /// outstanding snapshots, they will be canceled. + pub fn apply_change(&mut self, change: AnalysisChange) { + self.db.apply_change(change) + } +} + /// Analysis is a snapshot of a world state at a moment in time. It is the main /// entry point for asking semantic information about the world. When the world /// state is advanced using `AnalysisHost::apply_change` method, all existing @@ -295,107 +295,141 @@ pub struct Analysis { } impl Analysis { + /// Gets the text of the source file. pub fn file_text(&self, file_id: FileId) -> Arc { self.db.file_text(file_id) } + /// Gets the syntax tree of the file. pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { self.db.source_file(file_id).clone() } + /// Gets the file's `LineIndex`: data structure to convert between absolute + /// offsets and line/column representation. pub fn file_line_index(&self, file_id: FileId) -> Arc { self.db.file_lines(file_id) } + /// Selects the next syntactic nodes encopasing the range. pub fn extend_selection(&self, frange: FileRange) -> TextRange { extend_selection::extend_selection(&self.db, frange) } + /// Returns position of the mathcing brace (all types of braces are + /// supported). pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option { ra_editor::matching_brace(file, offset) } + /// Returns a syntax tree represented as `String`, for debug purposes. + // FIXME: use a better name here. pub fn syntax_tree(&self, file_id: FileId) -> String { let file = self.db.source_file(file_id); ra_editor::syntax_tree(&file) } + /// Returns an edit to remove all newlines in the range, cleaning up minor + /// stuff like trailing commas. pub fn join_lines(&self, frange: FileRange) -> SourceChange { let file = self.db.source_file(frange.file_id); SourceChange::from_local_edit(frange.file_id, ra_editor::join_lines(&file, frange.range)) } + /// Returns an edit which should be applied when opening a new line, fixing + /// up minor stuff like continuing the comment. pub fn on_enter(&self, position: FilePosition) -> Option { let file = self.db.source_file(position.file_id); let edit = ra_editor::on_enter(&file, position.offset)?; - let res = SourceChange::from_local_edit(position.file_id, edit); - Some(res) + Some(SourceChange::from_local_edit(position.file_id, edit)) } + /// Returns an edit which should be applied after `=` was typed. Primaraly, + /// this works when adding `let =`. + // FIXME: use a snippet completion instead of this hack here. pub fn on_eq_typed(&self, position: FilePosition) -> Option { let file = self.db.source_file(position.file_id); - Some(SourceChange::from_local_edit( - position.file_id, - ra_editor::on_eq_typed(&file, position.offset)?, - )) + let edit = ra_editor::on_eq_typed(&file, position.offset)?; + Some(SourceChange::from_local_edit(position.file_id, edit)) } + /// Returns a tree representation of symbols in the file. Useful to draw a + /// file outline. pub fn file_structure(&self, file_id: FileId) -> Vec { let file = self.db.source_file(file_id); ra_editor::file_structure(&file) } + /// Returns the set of folding ranges. pub fn folding_ranges(&self, file_id: FileId) -> Vec { let file = self.db.source_file(file_id); ra_editor::folding_ranges(&file) } + /// Fuzzy searches for a symbol. pub fn symbol_search(&self, query: Query) -> Cancelable> { - let res = symbol_index::world_symbols(&*self.db, query)? + let res = symbol_index::world_symbols(self.db, query)? .into_iter() .map(|(file_id, symbol)| NavigationTarget { file_id, symbol }) .collect(); Ok(res) } + /// Resolves reference to definition, but does not gurantee correctness. pub fn approximately_resolve_symbol( &self, position: FilePosition, ) -> Cancelable> { self.db.approximately_resolve_symbol(position) } + /// Finds all usages of the reference at point. pub fn find_all_refs(&self, position: FilePosition) -> Cancelable> { self.db.find_all_refs(position) } + /// Returns documentation string for a given target. pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable> { self.db.doc_text_for(nav) } + /// Returns a `mod name;` declaration whihc created the current module. pub fn parent_module(&self, position: FilePosition) -> Cancelable> { self.db.parent_module(position) } + /// Returns `::` separated path to the current module from the crate root. pub fn module_path(&self, position: FilePosition) -> Cancelable> { self.db.module_path(position) } + /// Returns crates this file belongs too. pub fn crate_for(&self, file_id: FileId) -> Cancelable> { self.db.crate_for(file_id) } + /// Returns the root file of the given crate. pub fn crate_root(&self, crate_id: CrateId) -> Cancelable { Ok(self.db.crate_root(crate_id)) } + /// Returns the set of possible targets to run for the current file. pub fn runnables(&self, file_id: FileId) -> Cancelable> { let file = self.db.source_file(file_id); Ok(runnables::runnables(self, &file, file_id)) } + /// Computes syntax highlighting for the given file. pub fn highlight(&self, file_id: FileId) -> Cancelable> { syntax_highlighting::highlight(&*self.db, file_id) } + /// Computes completions at the given position. pub fn completions(&self, position: FilePosition) -> Cancelable>> { let completions = completion::completions(&self.db, position)?; Ok(completions.map(|it| it.into())) } + /// Computes assists (aks code actons aka intentions) for the given + /// position. pub fn assists(&self, frange: FileRange) -> Cancelable> { Ok(self.db.assists(frange)) } + /// Computes the set of diagnostics for the given file. pub fn diagnostics(&self, file_id: FileId) -> Cancelable> { self.db.diagnostics(file_id) } + /// Computes parameter information for the given call expression. pub fn resolve_callable( &self, position: FilePosition, ) -> Cancelable)>> { self.db.resolve_callable(position) } + /// Computes the type of the expression at the given position. pub fn type_of(&self, frange: FileRange) -> Cancelable> { self.db.type_of(frange) } + /// Returns the edit required to rename reference at the position to the new + /// name. pub fn rename( &self, position: FilePosition, -- cgit v1.2.3 From ef08b6c084fc2676655fe6dfb693ab097d3f9fd9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 2 Jan 2019 19:41:57 +0300 Subject: fix compilation --- crates/ra_analysis/src/completion.rs | 2 +- crates/ra_analysis/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/ra_analysis') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index b4ee092b5..739ca6ae8 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -58,6 +58,6 @@ fn check_completion(code: &str, expected_completions: &str, kind: CompletionKind } else { single_file_with_position(code) }; - let completions = completions(&analysis.imp.db, position).unwrap().unwrap(); + let completions = completions(&analysis.db, position).unwrap().unwrap(); completions.assert_match(expected_completions, kind); } diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index b4669dfff..54eb2f4d3 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -357,7 +357,7 @@ impl Analysis { } /// Fuzzy searches for a symbol. pub fn symbol_search(&self, query: Query) -> Cancelable> { - let res = symbol_index::world_symbols(self.db, query)? + let res = symbol_index::world_symbols(&*self.db, query)? .into_iter() .map(|(file_id, symbol)| NavigationTarget { file_id, symbol }) .collect(); -- cgit v1.2.3