From 5a56ac4b72e7f57fb20d49bcf531611f144cc0e0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Aug 2018 17:19:27 +0300 Subject: Better limiting --- crates/libanalysis/src/lib.rs | 20 +++++++++++++------- crates/libanalysis/src/symbol_index.rs | 18 ++++++++++++++---- 2 files changed, 27 insertions(+), 11 deletions(-) (limited to 'crates/libanalysis/src') diff --git a/crates/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs index 97b6dfca6..562fab290 100644 --- a/crates/libanalysis/src/lib.rs +++ b/crates/libanalysis/src/lib.rs @@ -94,12 +94,12 @@ impl World { Ok(index.clone()) } - pub fn world_symbols<'a>(&'a self, query: Query) -> impl Iterator + 'a { + pub fn world_symbols<'a>(&'a self, mut query: Query) -> impl Iterator + 'a { self.data.file_map.iter() .flat_map(move |(path, data)| { let path: &'a Path = path.as_path(); - let symbols = data.symbols(path); - query.process(symbols).map(move |s| (path, s)) + let symbols = data.symbols(); + query.process(symbols).into_iter().map(move |s| (path, s)) }) } @@ -125,7 +125,8 @@ impl World { let mut query = Query::new(name.to_string()); 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> { @@ -178,9 +179,14 @@ impl FileData { }) } - fn symbols(&self, path: &Path) -> &FileSymbols { - let syntax = self.syntax(path); + fn syntax_transient(&self) -> ast::File { + 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 - .get_or_init(|| FileSymbols::new(syntax)) + .get_or_init(|| FileSymbols::new(&syntax)) } } diff --git a/crates/libanalysis/src/symbol_index.rs b/crates/libanalysis/src/symbol_index.rs index 88d5c4995..a7ae197e0 100644 --- a/crates/libanalysis/src/symbol_index.rs +++ b/crates/libanalysis/src/symbol_index.rs @@ -35,6 +35,7 @@ pub struct Query { lowercased: String, only_types: bool, exact: bool, + limit: usize, } impl Query { @@ -45,6 +46,7 @@ impl Query { lowercased, only_types: false, exact: false, + limit: usize::max_value() } } @@ -56,10 +58,14 @@ impl Query { self.exact = true; } + pub fn limit(&mut self, limit: usize) { + self.limit = limit + } + pub(crate) fn process<'a>( - &self, + &mut self, file: &'a FileSymbols, - ) -> impl Iterator + 'a { + ) -> Vec<&'a FileSymbol> { fn is_type(kind: SyntaxKind) -> bool { match kind { STRUCT | ENUM | TRAIT | TYPE_ITEM => true, @@ -70,6 +76,9 @@ impl Query { let mut stream = file.map.search(automaton).into_stream(); let mut res = Vec::new(); while let Some((_, idx)) = stream.next() { + if self.limit == 0 { + break; + } let idx = idx as usize; let symbol = &file.symbols[idx]; if self.only_types && !is_type(symbol.kind) { @@ -78,9 +87,10 @@ impl Query { if self.exact && symbol.name != self.query { continue; } - res.push(symbol) + res.push(symbol); + self.limit -= 1; } - res.into_iter() + res } } -- cgit v1.2.3