diff options
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 71 |
1 files changed, 18 insertions, 53 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index 3959d7efe..ddf46e6c4 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -61,7 +61,7 @@ use std::{str::FromStr, sync::Arc}; | |||
61 | 61 | ||
62 | use ra_cfg::CfgOptions; | 62 | use ra_cfg::CfgOptions; |
63 | use rustc_hash::FxHashMap; | 63 | use rustc_hash::FxHashMap; |
64 | use test_utils::{extract_offset, Fixture, CURSOR_MARKER}; | 64 | use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER}; |
65 | use vfs::{file_set::FileSet, VfsPath}; | 65 | use vfs::{file_set::FileSet, VfsPath}; |
66 | 66 | ||
67 | use crate::{ | 67 | use crate::{ |
@@ -74,8 +74,9 @@ pub const WORKSPACE: SourceRootId = SourceRootId(0); | |||
74 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { | 74 | pub trait WithFixture: Default + SourceDatabaseExt + 'static { |
75 | fn with_single_file(text: &str) -> (Self, FileId) { | 75 | fn with_single_file(text: &str) -> (Self, FileId) { |
76 | let mut db = Self::default(); | 76 | let mut db = Self::default(); |
77 | let file_id = with_single_file(&mut db, text); | 77 | let (_, files) = with_files(&mut db, text); |
78 | (db, file_id) | 78 | assert_eq!(files.len(), 1); |
79 | (db, files[0]) | ||
79 | } | 80 | } |
80 | 81 | ||
81 | fn with_files(ra_fixture: &str) -> Self { | 82 | fn with_files(ra_fixture: &str) -> Self { |
@@ -86,9 +87,19 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
86 | } | 87 | } |
87 | 88 | ||
88 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { | 89 | fn with_position(ra_fixture: &str) -> (Self, FilePosition) { |
90 | let (db, file_id, range_or_offset) = Self::with_range_or_offset(ra_fixture); | ||
91 | let offset = match range_or_offset { | ||
92 | RangeOrOffset::Range(_) => panic!(), | ||
93 | RangeOrOffset::Offset(it) => it, | ||
94 | }; | ||
95 | (db, FilePosition { file_id, offset }) | ||
96 | } | ||
97 | |||
98 | fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) { | ||
89 | let mut db = Self::default(); | 99 | let mut db = Self::default(); |
90 | let (pos, _) = with_files(&mut db, ra_fixture); | 100 | let (pos, _) = with_files(&mut db, ra_fixture); |
91 | (db, pos.unwrap()) | 101 | let (file_id, range_or_offset) = pos.unwrap(); |
102 | (db, file_id, range_or_offset) | ||
92 | } | 103 | } |
93 | 104 | ||
94 | fn test_crate(&self) -> CrateId { | 105 | fn test_crate(&self) -> CrateId { |
@@ -102,56 +113,10 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static { | |||
102 | 113 | ||
103 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} | 114 | impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {} |
104 | 115 | ||
105 | fn with_single_file(db: &mut dyn SourceDatabaseExt, ra_fixture: &str) -> FileId { | ||
106 | let file_id = FileId(0); | ||
107 | let mut file_set = vfs::file_set::FileSet::default(); | ||
108 | file_set.insert(file_id, vfs::VfsPath::new_virtual_path("/main.rs".to_string())); | ||
109 | |||
110 | let source_root = SourceRoot::new_local(file_set); | ||
111 | |||
112 | let crate_graph = if let Some(meta) = ra_fixture.lines().find(|it| it.contains("//-")) { | ||
113 | let entry = Fixture::parse_meta_line(meta.trim()); | ||
114 | let meta = match ParsedMeta::from(&entry) { | ||
115 | ParsedMeta::File(it) => it, | ||
116 | }; | ||
117 | |||
118 | let mut crate_graph = CrateGraph::default(); | ||
119 | crate_graph.add_crate_root( | ||
120 | file_id, | ||
121 | meta.edition, | ||
122 | meta.krate.map(|name| { | ||
123 | CrateName::new(&name).expect("Fixture crate name should not contain dashes") | ||
124 | }), | ||
125 | meta.cfg, | ||
126 | meta.env, | ||
127 | Default::default(), | ||
128 | ); | ||
129 | crate_graph | ||
130 | } else { | ||
131 | let mut crate_graph = CrateGraph::default(); | ||
132 | crate_graph.add_crate_root( | ||
133 | file_id, | ||
134 | Edition::Edition2018, | ||
135 | None, | ||
136 | CfgOptions::default(), | ||
137 | Env::default(), | ||
138 | Default::default(), | ||
139 | ); | ||
140 | crate_graph | ||
141 | }; | ||
142 | |||
143 | db.set_file_text(file_id, Arc::new(ra_fixture.to_string())); | ||
144 | db.set_file_source_root(file_id, WORKSPACE); | ||
145 | db.set_source_root(WORKSPACE, Arc::new(source_root)); | ||
146 | db.set_crate_graph(Arc::new(crate_graph)); | ||
147 | |||
148 | file_id | ||
149 | } | ||
150 | |||
151 | fn with_files( | 116 | fn with_files( |
152 | db: &mut dyn SourceDatabaseExt, | 117 | db: &mut dyn SourceDatabaseExt, |
153 | fixture: &str, | 118 | fixture: &str, |
154 | ) -> (Option<FilePosition>, Vec<FileId>) { | 119 | ) -> (Option<(FileId, RangeOrOffset)>, Vec<FileId>) { |
155 | let fixture = Fixture::parse(fixture); | 120 | let fixture = Fixture::parse(fixture); |
156 | 121 | ||
157 | let mut files = Vec::new(); | 122 | let mut files = Vec::new(); |
@@ -193,9 +158,9 @@ fn with_files( | |||
193 | } | 158 | } |
194 | 159 | ||
195 | let text = if entry.text.contains(CURSOR_MARKER) { | 160 | let text = if entry.text.contains(CURSOR_MARKER) { |
196 | let (offset, text) = extract_offset(&entry.text); | 161 | let (range_or_offset, text) = extract_range_or_offset(&entry.text); |
197 | assert!(file_position.is_none()); | 162 | assert!(file_position.is_none()); |
198 | file_position = Some(FilePosition { file_id, offset }); | 163 | file_position = Some((file_id, range_or_offset)); |
199 | text.to_string() | 164 | text.to_string() |
200 | } else { | 165 | } else { |
201 | entry.text.to_string() | 166 | entry.text.to_string() |