406: Simplify r=matklad a=matklad

Get rid of `AnalysisImpl` wrapper around salsa database. It was useful before we migrated by salsa, but it's long have been just a useless boilerplate. 

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2019-01-02 16:42:29 +00:00
commit 4f30c45933
4 changed files with 203 additions and 218 deletions

View File

@ -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);
}

View File

@ -1,16 +1,12 @@
use std::{
fmt,
sync::Arc,
};
use std::sync::Arc;
use rayon::prelude::*;
use salsa::{Database, ParallelDatabase};
use salsa::Database;
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},
@ -22,38 +18,25 @@ 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, SymbolIndex, SymbolsDatabase, FileSymbol},
symbol_index::{LibrarySymbolsQuery, FileSymbol},
};
#[derive(Debug, Default)]
pub(crate) struct AnalysisHostImpl {
db: db::RootDatabase,
}
impl AnalysisHostImpl {
pub fn analysis(&self) -> AnalysisImpl {
AnalysisImpl {
db: self.db.snapshot(),
}
}
pub fn apply_change(&mut self, change: AnalysisChange) {
impl db::RootDatabase {
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));
}
@ -61,53 +44,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));
}
@ -116,74 +90,18 @@ 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());
}
}
pub(crate) struct AnalysisImpl {
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 fn file_text(&self, file_id: FileId) -> Arc<String> {
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<LineIndex> {
self.db.file_lines(file_id)
}
pub fn world_symbols(&self, query: Query) -> Cancelable<Vec<(FileId, FileSymbol)>> {
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
struct Snap(salsa::Snapshot<db::RootDatabase>);
impl Clone for Snap {
fn clone(&self) -> Snap {
Snap(self.0.snapshot())
}
}
let buf: Vec<Arc<SymbolIndex>> = 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))
}
impl db::RootDatabase {
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),
Some(it) => it,
};
@ -205,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<Vec<NavigationTarget>> {
let descr = match source_binder::module_from_position(&*self.db, position)? {
pub(crate) fn parent_module(
&self,
position: FilePosition,
) -> Cancelable<Vec<NavigationTarget>> {
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,
};
@ -224,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<Vec<CrateId>> {
let descr = match source_binder::module_from_file_id(&*self.db, file_id)? {
pub(crate) fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
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(crate) fn crate_root(&self, crate_id: CrateId) -> FileId {
self.crate_graph().crate_root(crate_id)
}
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
let completions = completions(&self.db, position)?;
Ok(completions.map(|it| it.into()))
}
pub fn approximately_resolve_symbol(
pub(crate) fn approximately_resolve_symbol(
&self,
position: FilePosition,
) -> Cancelable<Option<ReferenceResolution>> {
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::<ast::NameRef>(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(
@ -281,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() {
@ -302,10 +217,13 @@ impl AnalysisImpl {
Ok(None)
}
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
let file = self.db.source_file(position.file_id);
pub(crate) fn find_all_refs(
&self,
position: FilePosition,
) -> Cancelable<Vec<(FileId, TextRange)>> {
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,
};
@ -317,7 +235,7 @@ impl AnalysisImpl {
.collect::<Vec<_>>();
ret.extend(
descr
.scopes(&*self.db)
.scopes(self)
.find_all_refs(binding)
.into_iter()
.map(|ref_desc| (position.file_id, ref_desc.range)),
@ -355,8 +273,8 @@ impl AnalysisImpl {
Ok(Some((binding, descr)))
}
}
pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
let file = self.db.source_file(nav.file_id);
pub(crate) fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
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)
@ -369,8 +287,8 @@ impl AnalysisImpl {
Ok(result)
}
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
let syntax = self.db.source_file(file_id);
pub(crate) fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
let syntax = self.source_file(file_id);
let mut res = ra_editor::diagnostics(&syntax)
.into_iter()
@ -381,9 +299,9 @@ impl AnalysisImpl {
fix: d.fix.map(|fix| SourceChange::from_local_edit(file_id, fix)),
})
.collect::<Vec<_>>();
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 {
@ -433,8 +351,8 @@ impl AnalysisImpl {
Ok(res)
}
pub fn assists(&self, frange: FileRange) -> Vec<SourceChange> {
let file = self.file_syntax(frange.file_id);
pub(crate) fn assists(&self, frange: FileRange) -> Vec<SourceChange> {
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()),
@ -451,11 +369,11 @@ impl AnalysisImpl {
.collect()
}
pub fn resolve_callable(
pub(crate) fn resolve_callable(
&self,
position: FilePosition,
) -> 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();
// Find the calling expression and it's NameRef
@ -466,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;
@ -518,20 +436,20 @@ impl AnalysisImpl {
Ok(None)
}
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
let file = self.db.source_file(frange.file_id);
pub(crate) fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
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,
@ -555,7 +473,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, query)
}
}

View File

@ -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::{AnalysisHostImpl, AnalysisImpl},
symbol_index::{SymbolIndex, FileSymbol},
};
use crate::symbol_index::{SymbolIndex, FileSymbol};
pub use crate::{
completion::{CompletionItem, CompletionItemKind, InsertText},
@ -44,7 +42,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)]
@ -150,27 +148,6 @@ impl AnalysisChange {
}
}
/// `AnalysisHost` stores the current state of the world.
#[derive(Debug, Default)]
pub struct AnalysisHost {
imp: AnalysisHostImpl,
}
impl AnalysisHost {
/// Returns a snapshot of the current state, which you can query for
/// semantic information.
pub fn analysis(&self) -> Analysis {
Analysis {
imp: self.imp.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)
}
}
#[derive(Debug)]
pub struct SourceChange {
pub label: String,
@ -287,124 +264,178 @@ 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
/// `Analysis` are canceled (most method return `Err(Canceled)`).
#[derive(Debug)]
pub struct Analysis {
pub(crate) imp: AnalysisImpl,
db: salsa::Snapshot<db::RootDatabase>,
}
impl Analysis {
/// Gets the text of the source file.
pub fn file_text(&self, file_id: FileId) -> Arc<String> {
self.imp.file_text(file_id)
self.db.file_text(file_id)
}
/// Gets the syntax tree of the file.
pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
self.imp.file_syntax(file_id).clone()
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<LineIndex> {
self.imp.file_line_index(file_id)
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.imp.db, frange)
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<TextUnit> {
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.imp.file_syntax(file_id);
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.imp.file_syntax(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))
}
/// 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<SourceChange> {
let file = self.imp.file_syntax(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)
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<SourceChange> {
let file = self.imp.file_syntax(position.file_id);
Some(SourceChange::from_local_edit(
position.file_id,
ra_editor::on_eq_typed(&file, position.offset)?,
))
let file = self.db.source_file(position.file_id);
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<StructureNode> {
let file = self.imp.file_syntax(file_id);
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<Fold> {
let file = self.imp.file_syntax(file_id);
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<Vec<NavigationTarget>> {
let res = self
.imp
.world_symbols(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<Option<ReferenceResolution>> {
self.imp.approximately_resolve_symbol(position)
self.db.approximately_resolve_symbol(position)
}
/// Finds all usages of the reference at point.
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
self.imp.find_all_refs(position)
self.db.find_all_refs(position)
}
/// Returns documentation string for a given target.
pub fn doc_text_for(&self, nav: NavigationTarget) -> Cancelable<Option<String>> {
self.imp.doc_text_for(nav)
self.db.doc_text_for(nav)
}
/// Returns a `mod name;` declaration whihc created the current module.
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
self.imp.parent_module(position)
self.db.parent_module(position)
}
/// Returns `::` separated path to the current module from the crate root.
pub fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> {
self.imp.module_path(position)
self.db.module_path(position)
}
/// Returns crates this file belongs too.
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
self.imp.crate_for(file_id)
self.db.crate_for(file_id)
}
/// Returns the root file of the given crate.
pub fn crate_root(&self, crate_id: CrateId) -> Cancelable<FileId> {
Ok(self.imp.crate_root(crate_id))
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<Vec<Runnable>> {
let file = self.imp.file_syntax(file_id);
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<Vec<HighlightedRange>> {
syntax_highlighting::highlight(&*self.imp.db, file_id)
syntax_highlighting::highlight(&*self.db, file_id)
}
/// Computes completions at the given position.
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()))
}
/// Computes assists (aks code actons aka intentions) for the given
/// position.
pub fn assists(&self, frange: FileRange) -> Cancelable<Vec<SourceChange>> {
Ok(self.imp.assists(frange))
Ok(self.db.assists(frange))
}
/// Computes the set of diagnostics for the given file.
pub fn diagnostics(&self, file_id: FileId) -> Cancelable<Vec<Diagnostic>> {
self.imp.diagnostics(file_id)
self.db.diagnostics(file_id)
}
/// Computes parameter information for the given call expression.
pub fn resolve_callable(
&self,
position: FilePosition,
) -> Cancelable<Option<(FnSignatureInfo, Option<usize>)>> {
self.imp.resolve_callable(position)
self.db.resolve_callable(position)
}
/// Computes the type of the expression at the given position.
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
self.imp.type_of(frange)
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,
new_name: &str,
) -> Cancelable<Vec<SourceFileEdit>> {
self.imp.rename(position, new_name)
self.db.rename(position, new_name)
}
}

View File

@ -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<Arc<Sym
Ok(Arc::new(SymbolIndex::for_file(file_id, syntax)))
}
pub(crate) fn world_symbols(
db: &RootDatabase,
query: Query,
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
/// Need to wrap Snapshot to provide `Clone` impl for `map_with`
struct Snap(salsa::Snapshot<RootDatabase>);
impl Clone for Snap {
fn clone(&self) -> Snap {
Snap(self.0.snapshot())
}
}
let buf: Vec<Arc<SymbolIndex>> = 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)>,