aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src/lib.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-25 12:34:10 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-25 12:34:10 +0000
commit021e691997efc35c983808ee3470a060a3ec3e96 (patch)
treea3f49352542d3c6803e318cd2f155cc4543062c5 /crates/ra_db/src/lib.rs
parent04ce89313369a1606b057b3be1368d72b8a5cd63 (diff)
parent08c12e424d5d3fb4e11f081a07b9c265dc7a96b6 (diff)
Merge #639
639: Update salsa r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_db/src/lib.rs')
-rw-r--r--crates/ra_db/src/lib.rs68
1 files changed, 54 insertions, 14 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index 84759c75a..7e13f70bc 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -4,17 +4,18 @@ mod input;
4mod loc2id; 4mod loc2id;
5pub mod mock; 5pub mod mock;
6 6
7use std::panic; 7use std::{
8 panic, sync::Arc,
9};
8 10
9use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc}; 11use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc};
12use relative_path::RelativePathBuf;
10 13
11pub use ::salsa as salsa; 14pub use ::salsa as salsa;
12pub use crate::{ 15pub use crate::{
13 cancellation::Canceled, 16 cancellation::Canceled,
14 input::{ 17 input::{
15 FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency, 18 FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency,
16 FileTextQuery, FileSourceRootQuery, SourceRootQuery, SourceRootCratesQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery,
17 FileRelativePathQuery
18 }, 19 },
19 loc2id::LocationIntener, 20 loc2id::LocationIntener,
20}; 21};
@@ -50,16 +51,6 @@ pub trait BaseDatabase: salsa::Database + panic::RefUnwindSafe {
50 } 51 }
51} 52}
52 53
53#[salsa::query_group]
54pub trait SyntaxDatabase: crate::input::FilesDatabase + BaseDatabase {
55 fn source_file(&self, file_id: FileId) -> TreeArc<SourceFile>;
56}
57
58fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreeArc<SourceFile> {
59 let text = db.file_text(file_id);
60 SourceFile::parse(&*text)
61}
62
63#[derive(Clone, Copy, Debug)] 54#[derive(Clone, Copy, Debug)]
64pub struct FilePosition { 55pub struct FilePosition {
65 pub file_id: FileId, 56 pub file_id: FileId,
@@ -71,3 +62,52 @@ pub struct FileRange {
71 pub file_id: FileId, 62 pub file_id: FileId,
72 pub range: TextRange, 63 pub range: TextRange,
73} 64}
65
66#[salsa::query_group]
67pub trait FilesDatabase: salsa::Database {
68 /// Text of the file.
69 #[salsa::input]
70 fn file_text(&self, file_id: FileId) -> Arc<String>;
71 /// Path to a file, relative to the root of its source root.
72 #[salsa::input]
73 fn file_relative_path(&self, file_id: FileId) -> RelativePathBuf;
74 /// Source root of the file.
75 #[salsa::input]
76 fn file_source_root(&self, file_id: FileId) -> SourceRootId;
77 /// Contents of the source root.
78 #[salsa::input]
79 fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
80 fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
81 /// The set of "local" (that is, from the current workspace) roots.
82 /// Files in local roots are assumed to change frequently.
83 #[salsa::input]
84 fn local_roots(&self) -> Arc<Vec<SourceRootId>>;
85 /// The set of roots for crates.io libraries.
86 /// Files in libraries are assumed to never change.
87 #[salsa::input]
88 fn library_roots(&self) -> Arc<Vec<SourceRootId>>;
89 /// The crate graph.
90 #[salsa::input]
91 fn crate_graph(&self) -> Arc<CrateGraph>;
92}
93
94fn source_root_crates(db: &impl FilesDatabase, id: SourceRootId) -> Arc<Vec<CrateId>> {
95 let root = db.source_root(id);
96 let graph = db.crate_graph();
97 let res = root
98 .files
99 .values()
100 .filter_map(|&it| graph.crate_id_for_crate_root(it))
101 .collect::<Vec<_>>();
102 Arc::new(res)
103}
104
105#[salsa::query_group]
106pub trait SyntaxDatabase: FilesDatabase + BaseDatabase {
107 fn source_file(&self, file_id: FileId) -> TreeArc<SourceFile>;
108}
109
110fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreeArc<SourceFile> {
111 let text = db.file_text(file_id);
112 SourceFile::parse(&*text)
113}