diff options
Diffstat (limited to 'crates/ra_db')
-rw-r--r-- | crates/ra_db/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_db/src/fixture.rs | 40 | ||||
-rw-r--r-- | crates/ra_db/src/lib.rs | 3 |
3 files changed, 43 insertions, 1 deletions
diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml index 3394ae8ce..bf1f7920c 100644 --- a/crates/ra_db/Cargo.toml +++ b/crates/ra_db/Cargo.toml | |||
@@ -12,3 +12,4 @@ rustc-hash = "1.0" | |||
12 | ra_syntax = { path = "../ra_syntax" } | 12 | ra_syntax = { path = "../ra_syntax" } |
13 | ra_cfg = { path = "../ra_cfg" } | 13 | ra_cfg = { path = "../ra_cfg" } |
14 | ra_prof = { path = "../ra_prof" } | 14 | ra_prof = { path = "../ra_prof" } |
15 | test_utils = { path = "../test_utils" } | ||
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs new file mode 100644 index 000000000..469251fe9 --- /dev/null +++ b/crates/ra_db/src/fixture.rs | |||
@@ -0,0 +1,40 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use ra_cfg::CfgOptions; | ||
6 | |||
7 | use crate::{ | ||
8 | CrateGraph, Edition, FileId, RelativePathBuf, SourceDatabaseExt, SourceRoot, SourceRootId, | ||
9 | }; | ||
10 | |||
11 | pub const WORKSPACE: SourceRootId = SourceRootId(0); | ||
12 | |||
13 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { | ||
14 | fn with_single_file(text: &str) -> (Self, FileId) { | ||
15 | let mut db = Self::default(); | ||
16 | let file_id = with_single_file(&mut db, text); | ||
17 | (db, file_id) | ||
18 | } | ||
19 | } | ||
20 | |||
21 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} | ||
22 | |||
23 | fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId { | ||
24 | let file_id = FileId(0); | ||
25 | let rel_path: RelativePathBuf = "/main.rs".into(); | ||
26 | |||
27 | let mut source_root = SourceRoot::default(); | ||
28 | source_root.insert_file(rel_path.clone(), file_id); | ||
29 | |||
30 | let mut crate_graph = CrateGraph::default(); | ||
31 | crate_graph.add_crate_root(file_id, Edition::Edition2018, CfgOptions::default()); | ||
32 | |||
33 | db.set_file_text(file_id, Arc::new(text.to_string())); | ||
34 | db.set_file_relative_path(file_id, rel_path); | ||
35 | db.set_file_source_root(file_id, WORKSPACE); | ||
36 | db.set_source_root(WORKSPACE, Arc::new(source_root)); | ||
37 | db.set_crate_graph(Arc::new(crate_graph)); | ||
38 | |||
39 | file_id | ||
40 | } | ||
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs index 0d1ab4843..b6bfd531d 100644 --- a/crates/ra_db/src/lib.rs +++ b/crates/ra_db/src/lib.rs | |||
@@ -1,17 +1,18 @@ | |||
1 | //! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api. | 1 | //! ra_db defines basic database traits. The concrete DB is defined by ra_ide_api. |
2 | mod cancellation; | 2 | mod cancellation; |
3 | mod input; | 3 | mod input; |
4 | pub mod fixture; | ||
4 | 5 | ||
5 | use std::{panic, sync::Arc}; | 6 | use std::{panic, sync::Arc}; |
6 | 7 | ||
7 | use ra_prof::profile; | 8 | use ra_prof::profile; |
8 | use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; | 9 | use ra_syntax::{ast, Parse, SourceFile, TextRange, TextUnit}; |
9 | use relative_path::{RelativePath, RelativePathBuf}; | ||
10 | 10 | ||
11 | pub use crate::{ | 11 | pub use crate::{ |
12 | cancellation::Canceled, | 12 | cancellation::Canceled, |
13 | input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, | 13 | input::{CrateGraph, CrateId, Dependency, Edition, FileId, SourceRoot, SourceRootId}, |
14 | }; | 14 | }; |
15 | pub use relative_path::{RelativePath, RelativePathBuf}; | ||
15 | pub use salsa; | 16 | pub use salsa; |
16 | 17 | ||
17 | pub trait CheckCanceled { | 18 | pub trait CheckCanceled { |