Better limiting

This commit is contained in:
Aleksey Kladov 2018-08-13 17:19:27 +03:00
parent 8ae56fa6d0
commit 5a56ac4b72
3 changed files with 29 additions and 12 deletions

View file

@ -94,12 +94,12 @@ impl World {
Ok(index.clone()) Ok(index.clone())
} }
pub fn world_symbols<'a>(&'a self, query: Query) -> impl Iterator<Item=(&'a Path, &'a FileSymbol)> + 'a { pub fn world_symbols<'a>(&'a self, mut query: Query) -> impl Iterator<Item=(&'a Path, &'a FileSymbol)> + 'a {
self.data.file_map.iter() self.data.file_map.iter()
.flat_map(move |(path, data)| { .flat_map(move |(path, data)| {
let path: &'a Path = path.as_path(); let path: &'a Path = path.as_path();
let symbols = data.symbols(path); let symbols = data.symbols();
query.process(symbols).map(move |s| (path, s)) query.process(symbols).into_iter().map(move |s| (path, s))
}) })
} }
@ -125,7 +125,8 @@ impl World {
let mut query = Query::new(name.to_string()); let mut query = Query::new(name.to_string());
query.exact(); query.exact();
Ok(self.world_symbols(query).take(4).collect()) query.limit(4);
Ok(self.world_symbols(query).collect())
} }
fn file_data(&self, path: &Path) -> Result<Arc<FileData>> { fn file_data(&self, path: &Path) -> Result<Arc<FileData>> {
@ -178,9 +179,14 @@ impl FileData {
}) })
} }
fn symbols(&self, path: &Path) -> &FileSymbols { fn syntax_transient(&self) -> ast::File {
let syntax = self.syntax(path); self.syntax.get().map(|s| s.clone())
.unwrap_or_else(|| ast::File::parse(&self.text))
}
fn symbols(&self) -> &FileSymbols {
let syntax = self.syntax_transient();
self.symbols self.symbols
.get_or_init(|| FileSymbols::new(syntax)) .get_or_init(|| FileSymbols::new(&syntax))
} }
} }

View file

@ -35,6 +35,7 @@ pub struct Query {
lowercased: String, lowercased: String,
only_types: bool, only_types: bool,
exact: bool, exact: bool,
limit: usize,
} }
impl Query { impl Query {
@ -45,6 +46,7 @@ impl Query {
lowercased, lowercased,
only_types: false, only_types: false,
exact: false, exact: false,
limit: usize::max_value()
} }
} }
@ -56,10 +58,14 @@ impl Query {
self.exact = true; self.exact = true;
} }
pub fn limit(&mut self, limit: usize) {
self.limit = limit
}
pub(crate) fn process<'a>( pub(crate) fn process<'a>(
&self, &mut self,
file: &'a FileSymbols, file: &'a FileSymbols,
) -> impl Iterator<Item=&'a FileSymbol> + 'a { ) -> Vec<&'a FileSymbol> {
fn is_type(kind: SyntaxKind) -> bool { fn is_type(kind: SyntaxKind) -> bool {
match kind { match kind {
STRUCT | ENUM | TRAIT | TYPE_ITEM => true, STRUCT | ENUM | TRAIT | TYPE_ITEM => true,
@ -70,6 +76,9 @@ impl Query {
let mut stream = file.map.search(automaton).into_stream(); let mut stream = file.map.search(automaton).into_stream();
let mut res = Vec::new(); let mut res = Vec::new();
while let Some((_, idx)) = stream.next() { while let Some((_, idx)) = stream.next() {
if self.limit == 0 {
break;
}
let idx = idx as usize; let idx = idx as usize;
let symbol = &file.symbols[idx]; let symbol = &file.symbols[idx];
if self.only_types && !is_type(symbol.kind) { if self.only_types && !is_type(symbol.kind) {
@ -78,9 +87,10 @@ impl Query {
if self.exact && symbol.name != self.query { if self.exact && symbol.name != self.query {
continue; continue;
} }
res.push(symbol) res.push(symbol);
self.limit -= 1;
} }
res.into_iter() res
} }
} }

View file

@ -110,10 +110,11 @@ pub fn handle_workspace_symbol(
if !all_symbols { if !all_symbols {
q.only_types(); q.only_types();
} }
q.limit(128);
q q
}; };
for (path, symbol) in world.world_symbols(query).take(128) { for (path, symbol) in world.world_symbols(query) {
let line_index = world.file_line_index(path)?; let line_index = world.file_line_index(path)?;
let info = SymbolInformation { let info = SymbolInformation {
name: symbol.name.to_string(), name: symbol.name.to_string(),