From fcdf3a52b4b61a39474950486ea0edf5ebf33bea Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 15 Sep 2018 23:42:01 +0300 Subject: everysalsa --- crates/libanalysis/src/queries.rs | 21 ++++++++++++++++----- crates/libanalysis/src/roots.rs | 28 ++++++++++++---------------- crates/libanalysis/src/symbol_index.rs | 12 +++++++++++- crates/libeditor/src/symbols.rs | 2 +- 4 files changed, 40 insertions(+), 23 deletions(-) (limited to 'crates') diff --git a/crates/libanalysis/src/queries.rs b/crates/libanalysis/src/queries.rs index 9d6754fd4..0b60316e6 100644 --- a/crates/libanalysis/src/queries.rs +++ b/crates/libanalysis/src/queries.rs @@ -3,13 +3,11 @@ use libsyntax2::File; use libeditor::LineIndex; use { FileId, - db::{Query, QueryCtx, QueryRegistry, file_text}, + db::{Query, QueryCtx, QueryRegistry}, + symbol_index::SymbolIndex, }; -pub(crate) fn register_queries(reg: &mut QueryRegistry) { - reg.add(FILE_SYNTAX, "FILE_SYNTAX"); - reg.add(FILE_LINES, "FILE_LINES"); -} +pub(crate) use db::{file_text, file_set}; pub(crate) fn file_syntax(ctx: QueryCtx, file_id: FileId) -> File { (&*ctx.get(FILE_SYNTAX, file_id)).clone() @@ -17,6 +15,9 @@ pub(crate) fn file_syntax(ctx: QueryCtx, file_id: FileId) -> File { pub(crate) fn file_lines(ctx: QueryCtx, file_id: FileId) -> Arc { ctx.get(FILE_LINES, file_id) } +pub(crate) fn file_symbols(ctx: QueryCtx, file_id: FileId) -> Arc { + ctx.get(FILE_SYMBOLS, file_id) +} const FILE_SYNTAX: Query = Query(16, |ctx, file_id: &FileId| { let text = file_text(ctx, *file_id); @@ -26,3 +27,13 @@ const FILE_LINES: Query = Query(17, |ctx, file_id: &FileId| { let text = file_text(ctx, *file_id); LineIndex::new(&*text) }); +const FILE_SYMBOLS: Query = Query(18, |ctx, file_id: &FileId| { + let syntax = file_syntax(ctx, *file_id); + SymbolIndex::for_file(*file_id, syntax) +}); + +pub(crate) fn register_queries(reg: &mut QueryRegistry) { + reg.add(FILE_SYNTAX, "FILE_SYNTAX"); + reg.add(FILE_LINES, "FILE_LINES"); + reg.add(FILE_SYMBOLS, "FILE_SYMBOLS"); +} diff --git a/crates/libanalysis/src/roots.rs b/crates/libanalysis/src/roots.rs index 0e7253ba2..191d0d821 100644 --- a/crates/libanalysis/src/roots.rs +++ b/crates/libanalysis/src/roots.rs @@ -22,7 +22,7 @@ pub(crate) trait SourceRoot { fn module_tree(&self) -> Arc; fn lines(&self, file_id: FileId) -> Arc; fn syntax(&self, file_id: FileId) -> File; - fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>); + fn symbols(&self, acc: &mut Vec>); } #[derive(Default, Debug)] @@ -74,20 +74,16 @@ impl SourceRoot for WritableSourceRoot { fn syntax(&self, file_id: FileId) -> File { self.db.make_query(|ctx| ::queries::file_syntax(ctx, file_id)) } - fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>) { - // acc.extend( - // self.file_map - // .iter() - // .map(|(&file_id, data)| symbols(file_id, data)) - // ) + fn symbols<'a>(&'a self, acc: &mut Vec>) { + self.db.make_query(|ctx| { + let file_set = ::queries::file_set(ctx); + let syms = file_set.0.iter() + .map(|file_id| ::queries::file_symbols(ctx, *file_id)); + acc.extend(syms); + }); } } -fn symbols(file_id: FileId, (data, symbols): &(FileData, OnceCell)) -> &SymbolIndex { - let syntax = data.syntax_transient(); - symbols.get_or_init(|| SymbolIndex::for_file(file_id, syntax)) -} - #[derive(Debug)] struct FileData { text: String, @@ -121,7 +117,7 @@ impl FileData { #[derive(Debug)] pub(crate) struct ReadonlySourceRoot { - symbol_index: SymbolIndex, + symbol_index: Arc, file_map: HashMap, module_tree: Arc, } @@ -149,7 +145,7 @@ impl ReadonlySourceRoot { .collect(); ReadonlySourceRoot { - symbol_index, + symbol_index: Arc::new(symbol_index), file_map, module_tree: Arc::new(module_tree), } @@ -176,7 +172,7 @@ impl SourceRoot for ReadonlySourceRoot { fn syntax(&self, file_id: FileId) -> File { self.data(file_id).syntax().clone() } - fn symbols<'a>(&'a self, acc: &mut Vec<&'a SymbolIndex>) { - acc.push(&self.symbol_index) + fn symbols(&self, acc: &mut Vec>) { + acc.push(Arc::clone(&self.symbol_index)) } } diff --git a/crates/libanalysis/src/symbol_index.rs b/crates/libanalysis/src/symbol_index.rs index 4c93761aa..d22187ac0 100644 --- a/crates/libanalysis/src/symbol_index.rs +++ b/crates/libanalysis/src/symbol_index.rs @@ -1,3 +1,7 @@ +use std::{ + sync::Arc, + hash::{Hash, Hasher}, +}; use libeditor::{FileSymbol, file_symbols}; use libsyntax2::{ File, @@ -13,6 +17,12 @@ pub(crate) struct SymbolIndex { map: fst::Map, } +impl Hash for SymbolIndex { + fn hash(&self, hasher: &mut H) { + self.symbols.hash(hasher) + } +} + impl SymbolIndex { pub(crate) fn for_files(files: impl ParallelIterator) -> SymbolIndex { let mut symbols = files @@ -43,7 +53,7 @@ impl SymbolIndex { impl Query { pub(crate) fn search( self, - indices: &[&SymbolIndex], + indices: &[Arc], token: &JobToken, ) -> Vec<(FileId, FileSymbol)> { diff --git a/crates/libeditor/src/symbols.rs b/crates/libeditor/src/symbols.rs index 28b86c004..2f9cc9233 100644 --- a/crates/libeditor/src/symbols.rs +++ b/crates/libeditor/src/symbols.rs @@ -17,7 +17,7 @@ pub struct StructureNode { pub kind: SyntaxKind, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Hash)] pub struct FileSymbol { pub name: SmolStr, pub node_range: TextRange, -- cgit v1.2.3