Files
rust/crates/libanalysis/src/symbol_index.rs

86 lines
2.6 KiB
Rust
Raw Normal View History

2018-08-13 15:10:20 +03:00
use libeditor::{FileSymbol, file_symbols};
use libsyntax2::{
2018-08-25 11:44:58 +03:00
File,
2018-08-13 15:10:20 +03:00
SyntaxKind::{self, *},
};
2018-09-02 20:08:58 +03:00
use fst::{self, Streamer};
2018-09-03 19:46:30 +03:00
use rayon::prelude::*;
2018-09-02 20:08:58 +03:00
use {Query, FileId, JobToken};
2018-08-13 15:10:20 +03:00
#[derive(Debug)]
2018-09-03 19:46:30 +03:00
pub(crate) struct SymbolIndex {
2018-09-02 23:36:23 +03:00
symbols: Vec<(FileId, FileSymbol)>,
2018-08-13 15:10:20 +03:00
map: fst::Map,
}
2018-09-03 19:46:30 +03:00
impl SymbolIndex {
pub(crate) fn for_files(files: impl ParallelIterator<Item=(FileId, File)>) -> SymbolIndex {
let mut symbols = files
.flat_map(|(file_id, file)| {
file_symbols(&file)
.into_iter()
.map(move |symbol| {
(symbol.name.as_str().to_lowercase(), (file_id, symbol))
})
.collect::<Vec<_>>()
})
2018-08-13 15:10:20 +03:00
.collect::<Vec<_>>();
2018-09-03 19:46:30 +03:00
symbols.par_sort_by(|s1, s2| s1.0.cmp(&s2.0));
2018-08-13 15:10:20 +03:00
symbols.dedup_by(|s1, s2| s1.0 == s2.0);
2018-09-02 23:36:23 +03:00
let (names, symbols): (Vec<String>, Vec<(FileId, FileSymbol)>) =
2018-09-03 19:46:30 +03:00
symbols.into_iter().unzip();
2018-08-13 15:10:20 +03:00
let map = fst::Map::from_iter(
names.into_iter().zip(0u64..)
).unwrap();
2018-09-03 19:46:30 +03:00
SymbolIndex { symbols, map }
}
pub(crate) fn for_file(file_id: FileId, file: File) -> SymbolIndex {
SymbolIndex::for_files(::rayon::iter::once((file_id, file)))
2018-08-13 15:10:20 +03:00
}
}
impl Query {
2018-09-02 20:08:58 +03:00
pub(crate) fn search(
mut self,
2018-09-03 19:46:30 +03:00
indices: &[&SymbolIndex],
2018-09-02 20:08:58 +03:00
token: &JobToken,
) -> Vec<(FileId, FileSymbol)> {
let mut op = fst::map::OpBuilder::new();
2018-09-02 23:36:23 +03:00
for file_symbols in indices.iter() {
2018-09-02 20:08:58 +03:00
let automaton = fst::automaton::Subsequence::new(&self.lowercased);
op = op.add(file_symbols.map.search(automaton))
2018-08-13 15:10:20 +03:00
}
2018-09-02 20:08:58 +03:00
let mut stream = op.union();
2018-08-13 16:07:05 +03:00
let mut res = Vec::new();
2018-09-02 20:08:58 +03:00
while let Some((_, indexed_values)) = stream.next() {
if self.limit == 0 || token.is_canceled() {
2018-08-13 17:19:27 +03:00
break;
}
2018-09-02 20:08:58 +03:00
for indexed_value in indexed_values {
2018-09-02 23:36:23 +03:00
let file_symbols = &indices[indexed_value.index];
2018-09-02 20:08:58 +03:00
let idx = indexed_value.value as usize;
2018-09-02 23:36:23 +03:00
let (file_id, symbol) = &file_symbols.symbols[idx];
2018-09-02 20:08:58 +03:00
if self.only_types && !is_type(symbol.kind) {
continue;
}
if self.exact && symbol.name != self.query {
continue;
}
res.push((*file_id, symbol.clone()));
self.limit -= 1;
2018-08-13 16:07:05 +03:00
}
}
2018-08-13 17:19:27 +03:00
res
2018-08-13 15:10:20 +03:00
}
}
2018-09-02 20:08:58 +03:00
fn is_type(kind: SyntaxKind) -> bool {
match kind {
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_DEF => true,
_ => false,
}
}