From 9ae455ea52bf0bc60476fdb3d50d05f5873040c1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 5 Sep 2019 22:36:04 +0300 Subject: make source_root API more abstract --- crates/ra_db/src/input.rs | 16 ++++++++++++++-- crates/ra_db/src/lib.rs | 5 ++--- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index ad8e10c52..d1ee3c036 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -5,7 +5,7 @@ /// Note that neither this module, nor any other part of the analyzer's core do /// actual IO. See `vfs` and `project_model` in the `ra_lsp_server` crate for how /// actual IO is done and lowered to input. -use relative_path::RelativePathBuf; +use relative_path::{RelativePath, RelativePathBuf}; use rustc_hash::FxHashMap; use ra_syntax::SmolStr; @@ -36,7 +36,7 @@ pub struct SourceRoot { /// Libraries are considered mostly immutable, this assumption is used to /// optimize salsa's query structure pub is_library: bool, - pub files: FxHashMap, + files: FxHashMap, } impl SourceRoot { @@ -46,6 +46,18 @@ impl SourceRoot { pub fn new_library() -> SourceRoot { SourceRoot { is_library: true, ..SourceRoot::new() } } + pub fn file_by_relative_path(&self, path: &RelativePath) -> Option { + self.files.get(path).copied() + } + pub fn insert_file(&mut self, path: RelativePathBuf, file_id: FileId) { + self.files.insert(path, file_id); + } + pub fn remove_file(&mut self, path: &RelativePath) { + self.files.remove(path); + } + pub fn walk(&self) -> impl Iterator + '_ { + self.files.values().copied() + } } /// `CrateGraph` is a bit of information which turns a set of text files into a diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index b82d1bda0..c54791b7a 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs @@ -12,7 +12,7 @@ pub use crate::{ cancellation::Canceled, input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, }; -pub use ::salsa; +pub use salsa; pub trait CheckCanceled { /// Aborts current query if there are pending changes. @@ -93,8 +93,7 @@ pub trait SourceDatabase: CheckCanceled + std::fmt::Debug { fn source_root_crates(db: &impl SourceDatabase, id: SourceRootId) -> Arc> { let root = db.source_root(id); let graph = db.crate_graph(); - let res = - root.files.values().filter_map(|&it| graph.crate_id_for_crate_root(it)).collect::>(); + let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::>(); Arc::new(res) } -- cgit v1.2.3