From 9bb2a742564fc893f5b8e1e605c760798e102765 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Jan 2019 19:55:34 +0300 Subject: embed file_id into FileSymbol --- crates/ra_analysis/src/imp.rs | 22 ++++++++++++--------- crates/ra_analysis/src/lib.rs | 11 +++-------- crates/ra_analysis/src/symbol_index.rs | 36 +++++++++++++++++----------------- 3 files changed, 34 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 771dad475..eae73c2c4 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -165,9 +165,11 @@ impl db::RootDatabase { }; } // If that fails try the index based approach. - for (file_id, symbol) in self.index_resolve(name_ref)? { - rr.add_resolution(file_id, symbol); - } + rr.resolves_to.extend( + self.index_resolve(name_ref)? + .into_iter() + .map(NavigationTarget::from_symbol), + ); return Ok(Some(rr)); } if let Some(name) = find_node_at_offset::(syntax, position.offset) { @@ -352,13 +354,15 @@ impl db::RootDatabase { // Resolve the function's NameRef (NOTE: this isn't entirely accurate). let file_symbols = self.index_resolve(name_ref)?; - for (fn_file_id, fs) in file_symbols { - if fs.ptr.kind() == FN_DEF { - let fn_file = self.source_file(fn_file_id); - let fn_def = fs.ptr.resolve(&fn_file); + for symbol in file_symbols { + if symbol.ptr.kind() == FN_DEF { + let fn_file = self.source_file(symbol.file_id); + let fn_def = symbol.ptr.resolve(&fn_file); let fn_def = ast::FnDef::cast(fn_def.borrowed()).unwrap(); let descr = ctry!(source_binder::function_from_source( - self, fn_file_id, fn_def + self, + symbol.file_id, + fn_def )?); if let Some(descriptor) = descr.signature_info(self) { // If we have a calling expression let's find which argument we are on @@ -438,7 +442,7 @@ impl db::RootDatabase { .collect::>(); Ok(res) } - fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable> { + fn index_resolve(&self, name_ref: ast::NameRef) -> Cancelable> { let name = name_ref.text(); let mut query = Query::new(name.to_string()); query.exact(); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index 61af676b2..ab935954c 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -237,11 +237,11 @@ pub struct NavigationTarget { } impl NavigationTarget { - fn from_symbol(file_id: FileId, symbol: FileSymbol) -> NavigationTarget { + fn from_symbol(symbol: FileSymbol) -> NavigationTarget { NavigationTarget { + file_id: symbol.file_id, name: symbol.name.clone(), kind: symbol.ptr.kind(), - file_id, range: symbol.ptr.range(), ptr: Some(symbol.ptr.clone()), } @@ -278,11 +278,6 @@ impl ReferenceResolution { resolves_to: Vec::new(), } } - - fn add_resolution(&mut self, file_id: FileId, symbol: FileSymbol) { - self.resolves_to - .push(NavigationTarget::from_symbol(file_id, symbol)) - } } /// `AnalysisHost` stores the current state of the world. @@ -380,7 +375,7 @@ impl Analysis { pub fn symbol_search(&self, query: Query) -> Cancelable> { let res = symbol_index::world_symbols(&*self.db, query)? .into_iter() - .map(|(file_id, symbol)| NavigationTarget::from_symbol(file_id, symbol)) + .map(NavigationTarget::from_symbol) .collect(); Ok(res) } diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs index 10d8e8059..b355b14ed 100644 --- a/crates/ra_analysis/src/symbol_index.rs +++ b/crates/ra_analysis/src/symbol_index.rs @@ -58,10 +58,7 @@ fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable Cancelable> { +pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Cancelable> { /// Need to wrap Snapshot to provide `Clone` impl for `map_with` struct Snap(salsa::Snapshot); impl Clone for Snap { @@ -95,7 +92,7 @@ pub(crate) fn world_symbols( #[derive(Default, Debug)] pub(crate) struct SymbolIndex { - symbols: Vec<(FileId, FileSymbol)>, + symbols: Vec, map: fst::Map, } @@ -126,14 +123,18 @@ impl SymbolIndex { file.syntax() .descendants() .filter_map(to_symbol) - .map(move |symbol| (symbol.name.as_str().to_lowercase(), (file_id, symbol))) + .map(move |(name, ptr)| { + ( + name.as_str().to_lowercase(), + FileSymbol { name, ptr, file_id }, + ) + }) .collect::>() }) .collect::>(); symbols.par_sort_by(|s1, s2| s1.0.cmp(&s2.0)); symbols.dedup_by(|s1, s2| s1.0 == s2.0); - let (names, symbols): (Vec, Vec<(FileId, FileSymbol)>) = - symbols.into_iter().unzip(); + let (names, symbols): (Vec, Vec) = symbols.into_iter().unzip(); let map = fst::Map::from_iter(names.into_iter().zip(0u64..)).unwrap(); SymbolIndex { symbols, map } } @@ -144,7 +145,7 @@ impl SymbolIndex { } impl Query { - pub(crate) fn search(self, indices: &[Arc]) -> Vec<(FileId, FileSymbol)> { + pub(crate) fn search(self, indices: &[Arc]) -> Vec { let mut op = fst::map::OpBuilder::new(); for file_symbols in indices.iter() { let automaton = fst::automaton::Subsequence::new(&self.lowercased); @@ -160,14 +161,14 @@ impl Query { let file_symbols = &indices[indexed_value.index]; let idx = indexed_value.value as usize; - let (file_id, symbol) = &file_symbols.symbols[idx]; + let symbol = &file_symbols.symbols[idx]; if self.only_types && !is_type(symbol.ptr.kind()) { continue; } if self.exact && symbol.name != self.query { continue; } - res.push((*file_id, symbol.clone())); + res.push(symbol.clone()); } } res @@ -185,17 +186,16 @@ fn is_type(kind: SyntaxKind) -> bool { /// possible. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub(crate) struct FileSymbol { + pub(crate) file_id: FileId, pub(crate) name: SmolStr, pub(crate) ptr: LocalSyntaxPtr, } -fn to_symbol(node: SyntaxNodeRef) -> Option { - fn decl<'a, N: NameOwner<'a>>(node: N) -> Option { - let name = node.name()?; - Some(FileSymbol { - name: name.text(), - ptr: LocalSyntaxPtr::new(node.syntax()), - }) +fn to_symbol(node: SyntaxNodeRef) -> Option<(SmolStr, LocalSyntaxPtr)> { + fn decl<'a, N: NameOwner<'a>>(node: N) -> Option<(SmolStr, LocalSyntaxPtr)> { + let name = node.name()?.text(); + let ptr = LocalSyntaxPtr::new(node.syntax()); + Some((name, ptr)) } visitor() .visit(decl::) -- cgit v1.2.3