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

67 lines
1.8 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-08-13 16:07:05 +03:00
use fst::{self, IntoStreamer, Streamer};
2018-08-30 13:12:49 +03:00
use Query;
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-08-17 15:37:17 +03:00
pub(crate) fn process(
2018-08-13 17:19:27 +03:00
&mut self,
2018-08-17 15:37:17 +03:00
file: &FileSymbols,
) -> Vec<FileSymbol> {
2018-08-13 15:10:20 +03:00
fn is_type(kind: SyntaxKind) -> bool {
match kind {
2018-08-13 18:27:26 +03:00
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_DEF => true,
2018-08-13 15:10:20 +03:00
_ => false,
}
}
2018-08-13 16:07:05 +03:00
let automaton = fst::automaton::Subsequence::new(&self.lowercased);
let mut stream = file.map.search(automaton).into_stream();
let mut res = Vec::new();
while let Some((_, idx)) = stream.next() {
2018-08-13 17:19:27 +03:00
if self.limit == 0 {
break;
}
2018-08-13 16:07:05 +03:00
let idx = idx as usize;
let symbol = &file.symbols[idx];
if self.only_types && !is_type(symbol.kind) {
continue;
}
if self.exact && symbol.name != self.query {
continue;
}
2018-08-17 15:37:17 +03:00
res.push(symbol.clone());
2018-08-13 17:19:27 +03:00
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
}
}