move world-symbols to file_symbols

This commit is contained in:
Aleksey Kladov 2019-01-02 18:08:14 +03:00
parent 29d8bfb9c9
commit a94530afb3
3 changed files with 42 additions and 42 deletions

View File

@ -3,7 +3,6 @@
sync::Arc,
};
use rayon::prelude::*;
use salsa::{Database, ParallelDatabase};
use hir::{
@ -25,7 +24,7 @@
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 @@ pub fn file_syntax(&self, file_id: FileId) -> SourceFileNode {
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))
}
pub(crate) fn module_path(&self, position: FilePosition) -> Cancelable<Option<String>> {
let descr = match source_binder::module_from_position(&*self.db, position)? {
None => return Ok(None),
@ -555,7 +521,7 @@ fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable<Vec<(FileId, FileS
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)
}
}

View File

@ -342,9 +342,7 @@ pub fn folding_ranges(&self, file_id: FileId) -> Vec<Fold> {
ra_editor::folding_ranges(&file)
}
pub fn symbol_search(&self, query: Query) -> Cancelable<Vec<NavigationTarget>> {
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();

View File

@ -10,12 +10,13 @@
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)>,