From 6a459c25edb693c8862f725c1963f25e86f92f45 Mon Sep 17 00:00:00 2001 From: oxalica Date: Sun, 24 Jul 2022 11:01:40 +0800 Subject: [PATCH] Tweak database input and abstract Change --- src/base.rs | 32 ++++++++++++++++++++++++++------ src/lib.rs | 1 + src/tests.rs | 8 +++++--- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/base.rs b/src/base.rs index f7106ba..5592f1c 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,3 +1,4 @@ +use salsa::Durability; use std::sync::Arc; use syntax::Parse; @@ -26,17 +27,36 @@ impl InFile { #[salsa::query_group(SourceDatabaseStorage)] pub trait SourceDatabase { #[salsa::input] - fn file_source_root(&self) -> FileId; - - #[salsa::input] - fn file_content(&self, file_id: FileId) -> Arc<[u8]>; + fn file_content(&self, file_id: FileId) -> Arc; fn parse(&self, file_id: FileId) -> InFile; } fn parse(db: &dyn SourceDatabase, file_id: FileId) -> InFile { let content = db.file_content(file_id); - let content = std::str::from_utf8(&content).unwrap_or_default(); - let parse = syntax::parse_file(content); + let parse = syntax::parse_file(&content); InFile::new(file_id, parse) } + +#[derive(Default, Debug, Clone, PartialEq, Eq)] +pub struct Change { + pub file_changes: Vec<(FileId, Option>)>, +} + +impl Change { + pub fn new() -> Self { + Self::default() + } + + pub fn change_file(&mut self, file_id: FileId, content: Option>) { + self.file_changes.push((file_id, content)); + } + + pub 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? + db.set_file_content_with_durability(file_id, content, Durability::HIGH); + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 15350de..a3d222a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,4 +5,5 @@ mod ide; #[cfg(test)] mod tests; +pub use base::{Change, FileId}; pub use ide::RootDatabase; diff --git a/src/tests.rs b/src/tests.rs index 0964c4c..7c0b976 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,5 +1,6 @@ -use crate::base::{FileId, SourceDatabase, SourceDatabaseStorage}; +use crate::base::{SourceDatabase, SourceDatabaseStorage}; use crate::def::DefDatabaseStorage; +use crate::{Change, FileId}; use rowan::{ast::AstNode, TextSize}; use syntax::NixLanguage; @@ -17,8 +18,9 @@ impl TestDB { pub fn from_file(content: &str) -> (Self, FileId) { let mut db = Self::default(); let root_id = FileId(0); - db.set_file_content(root_id, content.as_bytes().into()); - db.set_file_source_root(root_id); + let mut change = Change::new(); + change.change_file(root_id, Some(content.into())); + change.apply(&mut db); (db, root_id) }