construct index from symbols directly

This commit is contained in:
Aleksey Kladov 2019-01-03 20:45:29 +03:00
parent 6c0bca5984
commit ebd7c04faa
3 changed files with 27 additions and 13 deletions

View file

@ -20,6 +20,7 @@
//! file in the current workspace, and run a query aginst the union of all
//! thouse fsts.
use std::{
cmp::Ordering,
hash::{Hash, Hasher},
sync::Arc,
};
@ -111,6 +112,17 @@ impl Hash for SymbolIndex {
}
impl SymbolIndex {
fn new(mut symbols: Vec<FileSymbol>) -> SymbolIndex {
fn cmp(s1: &FileSymbol, s2: &FileSymbol) -> Ordering {
unicase::Ascii::new(s1.name.as_str()).cmp(&unicase::Ascii::new(s2.name.as_str()))
}
symbols.par_sort_by(cmp);
symbols.dedup_by(|s1, s2| cmp(s1, s2) == Ordering::Equal);
let names = symbols.iter().map(|it| it.name.as_str().to_lowercase());
let map = fst::Map::from_iter(names.into_iter().zip(0u64..)).unwrap();
SymbolIndex { symbols, map }
}
pub(crate) fn len(&self) -> usize {
self.symbols.len()
}
@ -118,28 +130,19 @@ impl SymbolIndex {
pub(crate) fn for_files(
files: impl ParallelIterator<Item = (FileId, SourceFileNode)>,
) -> SymbolIndex {
let mut symbols = files
let symbols = files
.flat_map(|(file_id, file)| {
file.syntax()
.descendants()
.filter_map(to_symbol)
.map(move |(name, ptr)| {
(
name.as_str().to_lowercase(),
FileSymbol { name, ptr, file_id },
)
})
.map(move |(name, ptr)| FileSymbol { name, ptr, file_id })
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
symbols.par_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();
SymbolIndex { symbols, map }
SymbolIndex::new(symbols)
}
pub(crate) fn for_file(file_id: FileId, file: SourceFileNode) -> SymbolIndex {
fn for_file(file_id: FileId, file: SourceFileNode) -> SymbolIndex {
SymbolIndex::for_files(rayon::iter::once((file_id, file)))
}
}