diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-11-28 01:10:58 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2018-11-28 01:10:58 +0000 |
commit | 95c0c8f3986c8b3bcf0052d34d3ace09ebb9fa1b (patch) | |
tree | 0e5aa7337c000dd8c6ef3a7fedba68abf7feca8a /crates/ra_db/src/lib.rs | |
parent | 9f08341aa486ea59cb488635f19e960523568fb8 (diff) | |
parent | 59e29aef633e906837f8fed604435976a46be691 (diff) |
Merge #247
247: Hir r=matklad a=matklad
This doesn't achive anything new, just a big refactoring.
The main change is that Descriptors are now called `hir`, and live in a separate crate.
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_db/src/lib.rs')
-rw-r--r-- | crates/ra_db/src/lib.rs | 89 |
1 files changed, 89 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..33cb0e2ec --- /dev/null +++ b/crates/ra_db/src/lib.rs | |||
@@ -0,0 +1,89 @@ | |||
1 | //! ra_db defines basic database traits. Concrete DB is defined by ra_analysis. | ||
2 | |||
3 | extern crate ra_editor; | ||
4 | extern crate ra_syntax; | ||
5 | extern crate relative_path; | ||
6 | extern crate rustc_hash; | ||
7 | extern crate salsa; | ||
8 | |||
9 | mod syntax_ptr; | ||
10 | mod file_resolver; | ||
11 | mod input; | ||
12 | mod loc2id; | ||
13 | |||
14 | use std::sync::Arc; | ||
15 | use ra_editor::LineIndex; | ||
16 | use ra_syntax::{TextUnit, SourceFileNode}; | ||
17 | |||
18 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] | ||
19 | pub struct Canceled; | ||
20 | |||
21 | pub type Cancelable<T> = Result<T, Canceled>; | ||
22 | |||
23 | impl std::fmt::Display for Canceled { | ||
24 | fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
25 | fmt.write_str("Canceled") | ||
26 | } | ||
27 | } | ||
28 | |||
29 | impl std::error::Error for Canceled {} | ||
30 | |||
31 | pub use crate::{ | ||
32 | syntax_ptr::LocalSyntaxPtr, | ||
33 | file_resolver::{FileResolver, FileResolverImp}, | ||
34 | input::{ | ||
35 | FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, WORKSPACE, | ||
36 | FileTextQuery, FileSourceRootQuery, SourceRootQuery, LibrariesQuery, CrateGraphQuery, | ||
37 | }, | ||
38 | loc2id::{LocationIntener, NumericId}, | ||
39 | }; | ||
40 | |||
41 | #[macro_export] | ||
42 | macro_rules! impl_numeric_id { | ||
43 | ($id:ident) => { | ||
44 | impl $crate::NumericId for $id { | ||
45 | fn from_u32(id: u32) -> Self { | ||
46 | $id(id) | ||
47 | } | ||
48 | fn to_u32(self) -> u32 { | ||
49 | self.0 | ||
50 | } | ||
51 | } | ||
52 | }; | ||
53 | } | ||
54 | |||
55 | pub trait BaseDatabase: salsa::Database { | ||
56 | fn check_canceled(&self) -> Cancelable<()> { | ||
57 | if self.salsa_runtime().is_current_revision_canceled() { | ||
58 | Err(Canceled) | ||
59 | } else { | ||
60 | Ok(()) | ||
61 | } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | salsa::query_group! { | ||
66 | pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase { | ||
67 | fn source_file(file_id: FileId) -> SourceFileNode { | ||
68 | type SourceFileQuery; | ||
69 | } | ||
70 | fn file_lines(file_id: FileId) -> Arc<LineIndex> { | ||
71 | type FileLinesQuery; | ||
72 | } | ||
73 | } | ||
74 | } | ||
75 | |||
76 | fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> SourceFileNode { | ||
77 | let text = db.file_text(file_id); | ||
78 | SourceFileNode::parse(&*text) | ||
79 | } | ||
80 | fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> { | ||
81 | let text = db.file_text(file_id); | ||
82 | Arc::new(LineIndex::new(&*text)) | ||
83 | } | ||
84 | |||
85 | #[derive(Clone, Copy, Debug)] | ||
86 | pub struct FilePosition { | ||
87 | pub file_id: FileId, | ||
88 | pub offset: TextUnit, | ||
89 | } | ||