aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db/src/lib.rs')
-rw-r--r--crates/ra_db/src/lib.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
new file mode 100644
index 000000000..53805aada
--- /dev/null
+++ b/crates/ra_db/src/lib.rs
@@ -0,0 +1,83 @@
1//! ra_db defines basic database traits. Concrete DB is defined by ra_analysis.
2mod syntax_ptr;
3mod file_resolver;
4mod input;
5mod loc2id;
6pub mod mock;
7
8use std::sync::Arc;
9use ra_editor::LineIndex;
10use ra_syntax::{TextUnit, SourceFileNode};
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
13pub struct Canceled;
14
15pub type Cancelable<T> = Result<T, Canceled>;
16
17impl std::fmt::Display for Canceled {
18 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 fmt.write_str("Canceled")
20 }
21}
22
23impl std::error::Error for Canceled {}
24
25pub use crate::{
26 syntax_ptr::LocalSyntaxPtr,
27 file_resolver::{FileResolver, FileResolverImp},
28 input::{
29 FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, WORKSPACE,
30 FileTextQuery, FileSourceRootQuery, SourceRootQuery, LibrariesQuery, CrateGraphQuery,
31 },
32 loc2id::{LocationIntener, NumericId},
33};
34
35#[macro_export]
36macro_rules! impl_numeric_id {
37 ($id:ident) => {
38 impl $crate::NumericId for $id {
39 fn from_u32(id: u32) -> Self {
40 $id(id)
41 }
42 fn to_u32(self) -> u32 {
43 self.0
44 }
45 }
46 };
47}
48
49pub trait BaseDatabase: salsa::Database {
50 fn check_canceled(&self) -> Cancelable<()> {
51 if self.salsa_runtime().is_current_revision_canceled() {
52 Err(Canceled)
53 } else {
54 Ok(())
55 }
56 }
57}
58
59salsa::query_group! {
60 pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase {
61 fn source_file(file_id: FileId) -> SourceFileNode {
62 type SourceFileQuery;
63 }
64 fn file_lines(file_id: FileId) -> Arc<LineIndex> {
65 type FileLinesQuery;
66 }
67 }
68}
69
70fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode {
71 let text = db.file_text(file_id);
72 SourceFileNode::parse(&*text)
73}
74fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
75 let text = db.file_text(file_id);
76 Arc::new(LineIndex::new(&*text))
77}
78
79#[derive(Clone, Copy, Debug)]
80pub struct FilePosition {
81 pub file_id: FileId,
82 pub offset: TextUnit,
83}