1
1
mirror of https://github.com/oxalica/nil.git synced 2024-11-22 19:49:20 +03:00

Impl Analysis interface

This commit is contained in:
oxalica 2022-07-24 13:37:22 +08:00
parent 2ff5eddeaa
commit 0d8cf14269
3 changed files with 50 additions and 4 deletions

View File

@ -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?

View File

@ -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<T> = Result<T, Cancelled>;
#[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<RootDatabase>,
}
impl Analysis {
fn with_db<F, T>(&self, f: F) -> Cancellable<T>
where
F: FnOnce(&RootDatabase) -> T + std::panic::UnwindSafe,
{
Cancelled::catch(|| f(&self.db))
}
pub fn goto_definition(&self, pos: FilePos) -> Cancellable<Option<NavigationTarget>> {
self.with_db(|db| goto_definition::goto_definition(db, pos.file_id, pos.value))
}
}

View File

@ -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};