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

77 lines
2.2 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};
use {Query, FileId, JobToken};
2018-08-13 15:10:20 +03:00
#[derive(Debug)]
pub(crate) struct FileSymbols {
symbols: Vec<FileSymbol>,
map: fst::Map,
}
impl FileSymbols {
2018-08-25 11:44:58 +03:00
pub(crate) fn new(file: &File) -> FileSymbols {
2018-08-13 15:10:20 +03:00
let mut symbols = file_symbols(file)
.into_iter()
.map(|s| (s.name.as_str().to_lowercase(), s))
.collect::<Vec<_>>();
symbols.sort_by(|s1, s2| s1.0.cmp(&s2.0));
symbols.dedup_by(|s1, s2| s1.0 == s2.0);
let (names, symbols): (Vec<String>, Vec<FileSymbol>) =
symbols.into_iter().unzip();
let map = fst::Map::from_iter(
names.into_iter().zip(0u64..)
).unwrap();
FileSymbols { symbols, map }
}
}
impl Query {
2018-09-02 20:08:58 +03:00
pub(crate) fn search(
mut self,
indices: &[(FileId, &FileSymbols)],
token: &JobToken,
) -> Vec<(FileId, FileSymbol)> {
let mut op = fst::map::OpBuilder::new();
for (_, file_symbols) in indices.iter() {
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 {
let (file_id, file_symbols) = &indices[indexed_value.index];
let idx = indexed_value.value as usize;
let symbol = &file_symbols.symbols[idx];
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,
}
}