From 0d8cf1426955eaee4c48db904ddee2e3abe1e47c Mon Sep 17 00:00:00 2001 From: oxalica Date: Sun, 24 Jul 2022 13:37:22 +0800 Subject: [PATCH] Impl Analysis interface --- src/base.rs | 2 +- src/ide/mod.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/lib.rs | 2 +- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/src/base.rs b/src/base.rs index f7af6f4..e38aaa3 100644 --- a/src/base.rs +++ b/src/base.rs @@ -55,7 +55,7 @@ impl Change { self.file_changes.push((file_id, content)); } - pub fn apply(self, db: &mut dyn SourceDatabase) { + pub(crate) fn apply(self, db: &mut dyn SourceDatabase) { for (file_id, content) in self.file_changes { let content = content.unwrap_or_else(|| String::new().into()); // TODO: Better guess of durability? diff --git a/src/ide/mod.rs b/src/ide/mod.rs index 9385cb7..afab7a4 100644 --- a/src/ide/mod.rs +++ b/src/ide/mod.rs @@ -1,6 +1,6 @@ -use crate::FileId; -use crate::{base::SourceDatabaseStorage, def::DefDatabaseStorage}; +use crate::{base::SourceDatabaseStorage, def::DefDatabaseStorage, Change, FileId, FilePos}; use rowan::TextRange; +use salsa::{Cancelled, Database, Durability, ParallelDatabase}; use std::fmt; mod goto_definition; @@ -12,6 +12,8 @@ pub struct NavigationTarget { pub focus_range: TextRange, } +pub type Cancellable = Result; + #[salsa::database(SourceDatabaseStorage, DefDatabaseStorage)] #[derive(Default)] pub struct RootDatabase { @@ -33,3 +35,47 @@ impl fmt::Debug for RootDatabase { f.debug_struct("RootDatabase").finish_non_exhaustive() } } + +#[derive(Debug, Default)] +pub struct AnalysisHost { + db: RootDatabase, +} + +impl AnalysisHost { + pub fn new() -> Self { + Self::default() + } + + pub fn snapshot(&self) -> Analysis { + Analysis { + db: self.db.snapshot(), + } + } + + pub fn request_cancellation(&mut self) { + self.db.salsa_runtime_mut().synthetic_write(Durability::LOW); + } + + pub fn apply_change(&mut self, change: Change) { + self.request_cancellation(); + change.apply(&mut self.db); + } +} + +#[derive(Debug)] +pub struct Analysis { + db: salsa::Snapshot, +} + +impl Analysis { + fn with_db(&self, f: F) -> Cancellable + where + F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe, + { + Cancelled::catch(|| f(&self.db)) + } + + pub fn goto_definition(&self, pos: FilePos) -> Cancellable> { + self.with_db(|db| goto_definition::goto_definition(db, pos.file_id, pos.value)) + } +} diff --git a/src/lib.rs b/src/lib.rs index efa85bb..169efcc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,4 +6,4 @@ mod ide; mod tests; pub use base::{Change, FileId, FilePos, InFile}; -pub use ide::RootDatabase; +pub use ide::{Analysis, AnalysisHost, RootDatabase};