diff options
Diffstat (limited to 'crates/ra_db/src/lib.rs')
-rw-r--r-- | crates/ra_db/src/lib.rs | 83 |
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. | ||
2 | mod syntax_ptr; | ||
3 | mod file_resolver; | ||
4 | mod input; | ||
5 | mod loc2id; | ||
6 | pub mod mock; | ||
7 | |||
8 | use std::sync::Arc; | ||
9 | use ra_editor::LineIndex; | ||
10 | use ra_syntax::{TextUnit, SourceFileNode}; | ||
11 | |||
12 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
13 | pub struct Canceled; | ||
14 | |||
15 | pub type Cancelable<T> = Result<T, Canceled>; | ||
16 | |||
17 | impl 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 | |||
23 | impl std::error::Error for Canceled {} | ||
24 | |||
25 | pub 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] | ||
36 | macro_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 | |||
49 | pub 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 | |||
59 | salsa::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 | |||
70 | fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { | ||
71 | let text = db.file_text(file_id); | ||
72 | SourceFileNode::parse(&*text) | ||
73 | } | ||
74 | fn 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)] | ||
80 | pub struct FilePosition { | ||
81 | pub file_id: FileId, | ||
82 | pub offset: TextUnit, | ||
83 | } | ||