aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/symbol_index.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/symbol_index.rs')
-rw-r--r--crates/ra_analysis/src/symbol_index.rs56
1 files changed, 34 insertions, 22 deletions
diff --git a/crates/ra_analysis/src/symbol_index.rs b/crates/ra_analysis/src/symbol_index.rs
index b355b14ed..e2b1c88fe 100644
--- a/crates/ra_analysis/src/symbol_index.rs
+++ b/crates/ra_analysis/src/symbol_index.rs
@@ -20,6 +20,7 @@
20//! file in the current workspace, and run a query aginst the union of all 20//! file in the current workspace, and run a query aginst the union of all
21//! thouse fsts. 21//! thouse fsts.
22use std::{ 22use std::{
23 cmp::Ordering,
23 hash::{Hash, Hasher}, 24 hash::{Hash, Hasher},
24 sync::Arc, 25 sync::Arc,
25}; 26};
@@ -27,11 +28,11 @@ use std::{
27use fst::{self, Streamer}; 28use fst::{self, Streamer};
28use ra_syntax::{ 29use ra_syntax::{
29 SyntaxNodeRef, SourceFileNode, SmolStr, 30 SyntaxNodeRef, SourceFileNode, SmolStr,
30 algo::visit::{visitor, Visitor}, 31 algo::{visit::{visitor, Visitor}, find_covering_node},
31 SyntaxKind::{self, *}, 32 SyntaxKind::{self, *},
32 ast::{self, NameOwner}, 33 ast::{self, NameOwner},
33}; 34};
34use ra_db::{SyntaxDatabase, SourceRootId, FilesDatabase, LocalSyntaxPtr}; 35use ra_db::{SourceRootId, FilesDatabase, LocalSyntaxPtr};
35use salsa::ParallelDatabase; 36use salsa::ParallelDatabase;
36use rayon::prelude::*; 37use rayon::prelude::*;
37 38
@@ -41,7 +42,7 @@ use crate::{
41}; 42};
42 43
43salsa::query_group! { 44salsa::query_group! {
44 pub(crate) trait SymbolsDatabase: SyntaxDatabase { 45 pub(crate) trait SymbolsDatabase: hir::db::HirDatabase {
45 fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> { 46 fn file_symbols(file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
46 type FileSymbolsQuery; 47 type FileSymbolsQuery;
47 } 48 }
@@ -52,10 +53,23 @@ salsa::query_group! {
52 } 53 }
53} 54}
54 55
55fn file_symbols(db: &impl SyntaxDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> { 56fn file_symbols(db: &impl SymbolsDatabase, file_id: FileId) -> Cancelable<Arc<SymbolIndex>> {
56 db.check_canceled()?; 57 db.check_canceled()?;
57 let syntax = db.source_file(file_id); 58 let source_file = db.source_file(file_id);
58 Ok(Arc::new(SymbolIndex::for_file(file_id, syntax))) 59 let mut symbols = source_file
60 .syntax()
61 .descendants()
62 .filter_map(to_symbol)
63 .map(move |(name, ptr)| FileSymbol { name, ptr, file_id })
64 .collect::<Vec<_>>();
65
66 for (name, text_range) in hir::source_binder::macro_symbols(db, file_id)? {
67 let node = find_covering_node(source_file.syntax(), text_range);
68 let ptr = LocalSyntaxPtr::new(node);
69 symbols.push(FileSymbol { file_id, name, ptr })
70 }
71
72 Ok(Arc::new(SymbolIndex::new(symbols)))
59} 73}
60 74
61pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Cancelable<Vec<FileSymbol>> { 75pub(crate) fn world_symbols(db: &RootDatabase, query: Query) -> Cancelable<Vec<FileSymbol>> {
@@ -111,6 +125,17 @@ impl Hash for SymbolIndex {
111} 125}
112 126
113impl SymbolIndex { 127impl SymbolIndex {
128 fn new(mut symbols: Vec<FileSymbol>) -> SymbolIndex {
129 fn cmp(s1: &FileSymbol, s2: &FileSymbol) -> Ordering {
130 unicase::Ascii::new(s1.name.as_str()).cmp(&unicase::Ascii::new(s2.name.as_str()))
131 }
132 symbols.par_sort_by(cmp);
133 symbols.dedup_by(|s1, s2| cmp(s1, s2) == Ordering::Equal);
134 let names = symbols.iter().map(|it| it.name.as_str().to_lowercase());
135 let map = fst::Map::from_iter(names.into_iter().zip(0u64..)).unwrap();
136 SymbolIndex { symbols, map }
137 }
138
114 pub(crate) fn len(&self) -> usize { 139 pub(crate) fn len(&self) -> usize {
115 self.symbols.len() 140 self.symbols.len()
116 } 141 }
@@ -118,29 +143,16 @@ impl SymbolIndex {
118 pub(crate) fn for_files( 143 pub(crate) fn for_files(
119 files: impl ParallelIterator<Item = (FileId, SourceFileNode)>, 144 files: impl ParallelIterator<Item = (FileId, SourceFileNode)>,
120 ) -> SymbolIndex { 145 ) -> SymbolIndex {
121 let mut symbols = files 146 let symbols = files
122 .flat_map(|(file_id, file)| { 147 .flat_map(|(file_id, file)| {
123 file.syntax() 148 file.syntax()
124 .descendants() 149 .descendants()
125 .filter_map(to_symbol) 150 .filter_map(to_symbol)
126 .map(move |(name, ptr)| { 151 .map(move |(name, ptr)| FileSymbol { name, ptr, file_id })
127 (
128 name.as_str().to_lowercase(),
129 FileSymbol { name, ptr, file_id },
130 )
131 })
132 .collect::<Vec<_>>() 152 .collect::<Vec<_>>()
133 }) 153 })
134 .collect::<Vec<_>>(); 154 .collect::<Vec<_>>();
135 symbols.par_sort_by(|s1, s2| s1.0.cmp(&s2.0)); 155 SymbolIndex::new(symbols)
136 symbols.dedup_by(|s1, s2| s1.0 == s2.0);
137 let (names, symbols): (Vec<String>, Vec<FileSymbol>) = symbols.into_iter().unzip();
138 let map = fst::Map::from_iter(names.into_iter().zip(0u64..)).unwrap();
139 SymbolIndex { symbols, map }
140 }
141
142 pub(crate) fn for_file(file_id: FileId, file: SourceFileNode) -> SymbolIndex {
143 SymbolIndex::for_files(rayon::iter::once((file_id, file)))
144 } 156 }
145} 157}
146 158