get rid of AnalysisImpl

This commit is contained in:
Aleksey Kladov 2019-01-02 19:20:56 +03:00
parent e9b47dbb36
commit 08d1537468
2 changed files with 80 additions and 101 deletions

View file

@ -1,9 +1,6 @@
use std::{ use std::sync::Arc;
fmt,
sync::Arc,
};
use salsa::{Database, ParallelDatabase}; use salsa::Database;
use hir::{ use hir::{
self, FnSignatureInfo, Problem, source_binder, self, FnSignatureInfo, Problem, source_binder,
@ -21,18 +18,12 @@ use ra_syntax::{
use crate::{ use crate::{
AnalysisChange, AnalysisChange,
Cancelable, NavigationTarget, Cancelable, NavigationTarget,
completion::{CompletionItem, completions},
CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit,
Query, ReferenceResolution, RootChange, SourceChange, SourceFileEdit, Query, ReferenceResolution, RootChange, SourceChange, SourceFileEdit,
symbol_index::{LibrarySymbolsQuery, FileSymbol}, symbol_index::{LibrarySymbolsQuery, FileSymbol},
}; };
impl db::RootDatabase { impl db::RootDatabase {
pub(crate) fn analysis(&self) -> AnalysisImpl {
AnalysisImpl {
db: self.snapshot(),
}
}
pub(crate) fn apply_change(&mut self, change: AnalysisChange) { pub(crate) fn apply_change(&mut self, change: AnalysisChange) {
log::info!("apply_change {:?}", change); log::info!("apply_change {:?}", change);
// self.gc_syntax_trees(); // self.gc_syntax_trees();
@ -108,20 +99,9 @@ impl db::RootDatabase {
} }
} }
pub(crate) struct AnalysisImpl { impl db::RootDatabase {
pub(crate) db: salsa::Snapshot<db::RootDatabase>,
}
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 {
pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> { pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> {
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), None => return Ok(None),
Some(it) => it, Some(it) => it,
}; };
@ -143,12 +123,15 @@ impl AnalysisImpl {
/// This returns `Vec` because a module may be included from several places. We /// 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. /// don't handle this case yet though, so the Vec has length at most one.
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> { pub(crate) fn parent_module(
let descr = match source_binder::module_from_position(&*self.db, position)? { &self,
position: FilePosition,
) -> Cancelable<Vec<NavigationTarget>> {
let descr = match source_binder::module_from_position(self, position)? {
None => return Ok(Vec::new()), None => return Ok(Vec::new()),
Some(it) => it, 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()), None => return Ok(Vec::new()),
Some(it) => it, Some(it) => it,
}; };
@ -162,39 +145,33 @@ impl AnalysisImpl {
Ok(vec![NavigationTarget { file_id, symbol }]) Ok(vec![NavigationTarget { file_id, symbol }])
} }
/// Returns `Vec` for the same reason as `parent_module` /// Returns `Vec` for the same reason as `parent_module`
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
let descr = match source_binder::module_from_file_id(&*self.db, file_id)? { let descr = match source_binder::module_from_file_id(self, file_id)? {
None => return Ok(Vec::new()), None => return Ok(Vec::new()),
Some(it) => it, Some(it) => it,
}; };
let root = descr.crate_root(); let root = descr.crate_root();
let file_id = root.file_id(); 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); let crate_id = crate_graph.crate_id_for_crate_root(file_id);
Ok(crate_id.into_iter().collect()) Ok(crate_id.into_iter().collect())
} }
pub fn crate_root(&self, crate_id: CrateId) -> FileId { pub(crate) fn crate_root(&self, crate_id: CrateId) -> FileId {
self.db.crate_graph().crate_root(crate_id) self.crate_graph().crate_root(crate_id)
} }
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> { pub(crate) fn approximately_resolve_symbol(
let completions = completions(&self.db, position)?;
Ok(completions.map(|it| it.into()))
}
pub fn approximately_resolve_symbol(
&self, &self,
position: FilePosition, position: FilePosition,
) -> Cancelable<Option<ReferenceResolution>> { ) -> Cancelable<Option<ReferenceResolution>> {
let file = self.db.source_file(position.file_id); let file = self.source_file(position.file_id);
let syntax = file.syntax(); let syntax = file.syntax();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) { if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
let mut rr = ReferenceResolution::new(name_ref.syntax().range()); let mut rr = ReferenceResolution::new(name_ref.syntax().range());
if let Some(fn_descr) = source_binder::function_from_child_node( if let Some(fn_descr) =
&*self.db, source_binder::function_from_child_node(self, position.file_id, name_ref.syntax())?
position.file_id, {
name_ref.syntax(), let scope = fn_descr.scopes(self);
)? {
let scope = fn_descr.scopes(&*self.db);
// First try to resolve the symbol locally // First try to resolve the symbol locally
if let Some(entry) = scope.resolve_local_name(name_ref) { if let Some(entry) = scope.resolve_local_name(name_ref) {
rr.add_resolution( rr.add_resolution(
@ -219,7 +196,7 @@ impl AnalysisImpl {
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) { if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
if module.has_semi() { if module.has_semi() {
if let Some(child_module) = 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 file_id = child_module.file_id();
let name = match child_module.name() { let name = match child_module.name() {
@ -240,10 +217,13 @@ impl AnalysisImpl {
Ok(None) Ok(None)
} }
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { pub(crate) fn find_all_refs(
let file = self.db.source_file(position.file_id); &self,
position: FilePosition,
) -> Cancelable<Vec<(FileId, TextRange)>> {
let file = self.source_file(position.file_id);
// Find the binding associated with the offset // 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()), None => return Ok(Vec::new()),
Some(it) => it, Some(it) => it,
}; };
@ -255,7 +235,7 @@ impl AnalysisImpl {
.collect::<Vec<_>>(); .collect::<Vec<_>>();
ret.extend( ret.extend(
descr descr
.scopes(&*self.db) .scopes(self)
.find_all_refs(binding) .find_all_refs(binding)
.into_iter() .into_iter()
.map(|ref_desc| (position.file_id, ref_desc.range)), .map(|ref_desc| (position.file_id, ref_desc.range)),
@ -293,8 +273,8 @@ impl AnalysisImpl {
Ok(Some((binding, descr))) Ok(Some((binding, descr)))
} }
} }
pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> { pub(crate) fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
let file = self.db.source_file(nav.file_id); let file = self.source_file(nav.file_id);
let result = match (nav.symbol.description(&file), nav.symbol.docs(&file)) { let result = match (nav.symbol.description(&file), nav.symbol.docs(&file)) {
(Some(desc), Some(docs)) => { (Some(desc), Some(docs)) => {
Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs) Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs)
@ -307,8 +287,8 @@ impl AnalysisImpl {
Ok(result) Ok(result)
} }
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
let syntax = self.db.source_file(file_id); let syntax = self.source_file(file_id);
let mut res = ra_editor::diagnostics(&syntax) let mut res = ra_editor::diagnostics(&syntax)
.into_iter() .into_iter()
@ -319,9 +299,9 @@ impl AnalysisImpl {
fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)), fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
if let Some(m) = source_binder::module_from_file_id(&*self.db, file_id)? { if let Some(m) = source_binder::module_from_file_id(self, file_id)? {
for (name_node, problem) in m.problems(&*self.db) { for (name_node, problem) in m.problems(self) {
let source_root = self.db.file_source_root(file_id); let source_root = self.file_source_root(file_id);
let diag = match problem { let diag = match problem {
Problem::UnresolvedModule { candidate } => { Problem::UnresolvedModule { candidate } => {
let create_file = FileSystemEdit::CreateFile { let create_file = FileSystemEdit::CreateFile {
@ -371,8 +351,8 @@ impl AnalysisImpl {
Ok(res) Ok(res)
} }
pub fn assists(&self, frange: FileRange) -> Vec<SourceChange> { pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> {
let file = self.db.source_file(frange.file_id); let file = self.source_file(frange.file_id);
let offset = frange.range.start(); let offset = frange.range.start();
let actions = vec![ let actions = vec![
ra_editor::flip_comma(&file, offset).map(|f| f()), ra_editor::flip_comma(&file, offset).map(|f| f()),
@ -389,11 +369,11 @@ impl AnalysisImpl {
.collect() .collect()
} }
pub fn resolve_callable( pub(crate) fn resolve_callable(
&self, &self,
position: FilePosition, position: FilePosition,
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> { ) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
let file = self.db.source_file(position.file_id); let file = self.source_file(position.file_id);
let syntax = file.syntax(); let syntax = file.syntax();
// Find the calling expression and it's NameRef // Find the calling expression and it's NameRef
@ -404,12 +384,12 @@ impl AnalysisImpl {
let file_symbols = self.index_resolve(name_ref)?; let file_symbols = self.index_resolve(name_ref)?;
for (fn_file_id, fs) in file_symbols { for (fn_file_id, fs) in file_symbols {
if fs.kind == FN_DEF { 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()) { if let Some(fn_def) = find_node_at_offset(fn_file.syntax(), fs.node_range.start()) {
let descr = ctry!(source_binder::function_from_source( 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 // If we have a calling expression let's find which argument we are on
let mut current_parameter = None; let mut current_parameter = None;
@ -456,20 +436,20 @@ impl AnalysisImpl {
Ok(None) Ok(None)
} }
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> { pub(crate) fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
let file = self.db.source_file(frange.file_id); let file = self.source_file(frange.file_id);
let syntax = file.syntax(); let syntax = file.syntax();
let node = find_covering_node(syntax, frange.range); let node = find_covering_node(syntax, frange.range);
let parent_fn = ctry!(node.ancestors().find_map(FnDef::cast)); let parent_fn = ctry!(node.ancestors().find_map(FnDef::cast));
let function = ctry!(source_binder::function_from_source( let function = ctry!(source_binder::function_from_source(
&*self.db, self,
frange.file_id, frange.file_id,
parent_fn parent_fn
)?); )?);
let infer = function.infer(&*self.db)?; let infer = function.infer(self)?;
Ok(infer.type_of_node(node).map(|t| t.to_string())) Ok(infer.type_of_node(node).map(|t| t.to_string()))
} }
pub fn rename( pub(crate) fn rename(
&self, &self,
position: FilePosition, position: FilePosition,
new_name: &str, new_name: &str,
@ -493,7 +473,7 @@ impl AnalysisImpl {
let mut query = Query::new(name.to_string()); let mut query = Query::new(name.to_string());
query.exact(); query.exact();
query.limit(4); query.limit(4);
crate::symbol_index::world_symbols(&*self.db, query) crate::symbol_index::world_symbols(self, query)
} }
} }

View file

@ -27,11 +27,9 @@ use ra_syntax::{SourceFileNode, TextRange, TextUnit, SmolStr, SyntaxKind};
use ra_text_edit::TextEdit; use ra_text_edit::TextEdit;
use rayon::prelude::*; use rayon::prelude::*;
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use salsa::ParallelDatabase;
use crate::{ use crate::symbol_index::{SymbolIndex, FileSymbol};
imp::AnalysisImpl,
symbol_index::{SymbolIndex, FileSymbol},
};
pub use crate::{ pub use crate::{
completion::{CompletionItem, CompletionItemKind, InsertText}, completion::{CompletionItem, CompletionItemKind, InsertText},
@ -161,7 +159,7 @@ impl AnalysisHost {
/// semantic information. /// semantic information.
pub fn analysis(&self) -> Analysis { pub fn analysis(&self) -> Analysis {
Analysis { Analysis {
imp: self.db.analysis(), db: self.db.snapshot(),
} }
} }
/// Applies changes to the current state of the world. If there are /// 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)`). /// `Analysis` are canceled (most method return `Err(Canceled)`).
#[derive(Debug)] #[derive(Debug)]
pub struct Analysis { pub struct Analysis {
pub(crate) imp: AnalysisImpl, db: salsa::Snapshot<db::RootDatabase>,
} }
impl Analysis { impl Analysis {
pub fn file_text(&self, file_id: FileId) -> Arc<String> { pub fn file_text(&self, file_id: FileId) -> Arc<String> {
self.imp.db.file_text(file_id) self.db.file_text(file_id)
} }
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode { 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<LineIndex> { pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
self.imp.db.file_lines(file_id) self.db.file_lines(file_id)
} }
pub fn extend_selection(&self, frange: FileRange) -> TextRange { 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<TextUnit> { pub fn matching_brace(&self, file: &SourceFileNode, offset: TextUnit) -> Option<TextUnit> {
ra_editor::matching_brace(file, offset) ra_editor::matching_brace(file, offset)
} }
pub fn syntax_tree(&self, file_id: FileId) -> String { 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) ra_editor::syntax_tree(&file)
} }
pub fn join_lines(&self, frange: FileRange) -> SourceChange { 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)) SourceChange::from_local_edit(frange.file_id, ra_editor::join_lines(&file, frange.range))
} }
pub fn on_enter(&self, position: FilePosition) -> Option<SourceChange> { pub fn on_enter(&self, position: FilePosition) -> Option<SourceChange> {
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 edit = ra_editor::on_enter(&file, position.offset)?;
let res = SourceChange::from_local_edit(position.file_id, edit); let res = SourceChange::from_local_edit(position.file_id, edit);
Some(res) Some(res)
} }
pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> { pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
let file = self.imp.db.source_file(position.file_id); let file = self.db.source_file(position.file_id);
Some(SourceChange::from_local_edit( Some(SourceChange::from_local_edit(
position.file_id, position.file_id,
ra_editor::on_eq_typed(&file, position.offset)?, ra_editor::on_eq_typed(&file, position.offset)?,
)) ))
} }
pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> { pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
let file = self.imp.db.source_file(file_id); let file = self.db.source_file(file_id);
ra_editor::file_structure(&file) ra_editor::file_structure(&file)
} }
pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> { pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> {
let file = self.imp.db.source_file(file_id); let file = self.db.source_file(file_id);
ra_editor::folding_ranges(&file) ra_editor::folding_ranges(&file)
} }
pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<NavigationTarget>> { pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<NavigationTarget>> {
let res = symbol_index::world_symbols(&*self.imp.db, query)? let res = symbol_index::world_symbols(&*self.db, query)?
.into_iter() .into_iter()
.map(|(file_id, symbol)| NavigationTarget { file_id, symbol }) .map(|(file_id, symbol)| NavigationTarget { file_id, symbol })
.collect(); .collect();
@ -352,57 +350,58 @@ impl Analysis {
&self, &self,
position: FilePosition, position: FilePosition,
) -> Cancelable<Option<ReferenceResolution>> { ) -> Cancelable<Option<ReferenceResolution>> {
self.imp.approximately_resolve_symbol(position) self.db.approximately_resolve_symbol(position)
} }
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> { pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
self.imp.find_all_refs(position) self.db.find_all_refs(position)
} }
pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> { pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
self.imp.doc_text_for(nav) self.db.doc_text_for(nav)
} }
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> { pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
self.imp.parent_module(position) self.db.parent_module(position)
} }
pub fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> { pub fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> {
self.imp.module_path(position) self.db.module_path(position)
} }
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
self.imp.crate_for(file_id) self.db.crate_for(file_id)
} }
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> { pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
Ok(self.imp.crate_root(crate_id)) Ok(self.db.crate_root(crate_id))
} }
pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> { pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> {
let file = self.imp.db.source_file(file_id); let file = self.db.source_file(file_id);
Ok(runnables::runnables(self, &file, file_id)) Ok(runnables::runnables(self, &file, file_id))
} }
pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> { pub fn highlight(&self, file_id: FileId) -> Cancelable<Vec<HighlightedRange>> {
syntax_highlighting::highlight(&*self.imp.db, file_id) syntax_highlighting::highlight(&*self.db, file_id)
} }
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> { pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
self.imp.completions(position) let completions = completion::completions(&self.db, position)?;
Ok(completions.map(|it| it.into()))
} }
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<SourceChange>> { pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<SourceChange>> {
Ok(self.imp.assists(frange)) Ok(self.db.assists(frange))
} }
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> { pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
self.imp.diagnostics(file_id) self.db.diagnostics(file_id)
} }
pub fn resolve_callable( pub fn resolve_callable(
&self, &self,
position: FilePosition, position: FilePosition,
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> { ) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
self.imp.resolve_callable(position) self.db.resolve_callable(position)
} }
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> { pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
self.imp.type_of(frange) self.db.type_of(frange)
} }
pub fn rename( pub fn rename(
&self, &self,
position: FilePosition, position: FilePosition,
new_name: &str, new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> { ) -> Cancelable<Vec<SourceFileEdit>> {
self.imp.rename(position, new_name) self.db.rename(position, new_name)
} }
} }