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

Tweak database input and abstract Change

This commit is contained in:
oxalica 2022-07-24 11:01:40 +08:00
parent f10a5ef89d
commit 6a459c25ed
3 changed files with 32 additions and 9 deletions

View File

@ -1,3 +1,4 @@
use salsa::Durability;
use std::sync::Arc;
use syntax::Parse;
@ -26,17 +27,36 @@ impl<T> InFile<T> {
#[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<str>;
fn parse(&self, file_id: FileId) -> InFile<Parse>;
}
fn parse(db: &dyn SourceDatabase, file_id: FileId) -> InFile<Parse> {
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<Arc<str>>)>,
}
impl Change {
pub fn new() -> Self {
Self::default()
}
pub fn change_file(&mut self, file_id: FileId, content: Option<Arc<str>>) {
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);
}
}
}

View File

@ -5,4 +5,5 @@ mod ide;
#[cfg(test)]
mod tests;
pub use base::{Change, FileId};
pub use ide::RootDatabase;

View File

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