forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.rs
More file actions
48 lines (39 loc) · 1.28 KB
/
Copy pathdb.rs
File metadata and controls
48 lines (39 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::files::{File, SourceFileId, Utf8Path};
use codespan_reporting as cs;
use salsa;
use smol_str::SmolStr;
use std::rc::Rc;
pub trait Upcast<T: ?Sized> {
fn upcast(&self) -> &T;
}
pub trait UpcastMut<T: ?Sized> {
fn upcast_mut(&mut self) -> &mut T;
}
#[salsa::query_group(SourceDbStorage)]
pub trait SourceDb {
#[salsa::interned]
fn intern_file(&self, file: File) -> SourceFileId;
/// Set with `fn set_file_content(&mut self, file: SourceFileId, content: Rc<str>)
#[salsa::input]
fn file_content(&self, file: SourceFileId) -> Rc<str>;
#[salsa::invoke(file_line_starts_query)]
fn file_line_starts(&self, file: SourceFileId) -> Rc<[usize]>;
#[salsa::invoke(file_name_query)]
fn file_name(&self, file: SourceFileId) -> SmolStr;
}
fn file_line_starts_query(db: &dyn SourceDb, file: SourceFileId) -> Rc<[usize]> {
cs::files::line_starts(&file.content(db)).collect()
}
fn file_name_query(db: &dyn SourceDb, file: SourceFileId) -> SmolStr {
let path = db.lookup_intern_file(file).path;
Utf8Path::new(path.as_str())
.file_name()
.expect("path lacks file name")
.into()
}
#[salsa::database(SourceDbStorage)]
#[derive(Default)]
pub struct TestDb {
storage: salsa::Storage<TestDb>,
}
impl salsa::Database for TestDb {}