aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/lib.rs')
-rw-r--r--crates/ra_assists/src/lib.rs35
1 files changed, 23 insertions, 12 deletions
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs
index 8285e93a4..1343043dd 100644
--- a/crates/ra_assists/src/lib.rs
+++ b/crates/ra_assists/src/lib.rs
@@ -168,16 +168,27 @@ mod assists {
168 168
169#[cfg(test)] 169#[cfg(test)]
170mod helpers { 170mod helpers {
171 use ra_db::{fixture::WithFixture, FileRange}; 171 use std::sync::Arc;
172 use ra_ide_db::RootDatabase; 172
173 use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt};
174 use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase};
173 use ra_syntax::TextRange; 175 use ra_syntax::TextRange;
174 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; 176 use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
175 177
176 use crate::{Assist, AssistCtx}; 178 use crate::{Assist, AssistCtx};
177 179
180 pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
181 let (mut db, file_id) = RootDatabase::with_single_file(text);
182 // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`,
183 // but it looks like this might need specialization? :(
184 let local_roots = vec![db.file_source_root(file_id)];
185 db.set_local_roots(Arc::new(local_roots));
186 (db, file_id)
187 }
188
178 pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) { 189 pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) {
179 let (before_cursor_pos, before) = extract_offset(before); 190 let (before_cursor_pos, before) = extract_offset(before);
180 let (db, file_id) = RootDatabase::with_single_file(&before); 191 let (db, file_id) = with_single_file(&before);
181 let frange = 192 let frange =
182 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 193 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
183 let assist = 194 let assist =
@@ -205,7 +216,7 @@ mod helpers {
205 after: &str, 216 after: &str,
206 ) { 217 ) {
207 let (range, before) = extract_range(before); 218 let (range, before) = extract_range(before);
208 let (db, file_id) = RootDatabase::with_single_file(&before); 219 let (db, file_id) = with_single_file(&before);
209 let frange = FileRange { file_id, range }; 220 let frange = FileRange { file_id, range };
210 let assist = 221 let assist =
211 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 222 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -227,7 +238,7 @@ mod helpers {
227 target: &str, 238 target: &str,
228 ) { 239 ) {
229 let (before_cursor_pos, before) = extract_offset(before); 240 let (before_cursor_pos, before) = extract_offset(before);
230 let (db, file_id) = RootDatabase::with_single_file(&before); 241 let (db, file_id) = with_single_file(&before);
231 let frange = 242 let frange =
232 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 243 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
233 let assist = 244 let assist =
@@ -247,7 +258,7 @@ mod helpers {
247 target: &str, 258 target: &str,
248 ) { 259 ) {
249 let (range, before) = extract_range(before); 260 let (range, before) = extract_range(before);
250 let (db, file_id) = RootDatabase::with_single_file(&before); 261 let (db, file_id) = with_single_file(&before);
251 let frange = FileRange { file_id, range }; 262 let frange = FileRange { file_id, range };
252 let assist = 263 let assist =
253 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); 264 AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -265,7 +276,7 @@ mod helpers {
265 before: &str, 276 before: &str,
266 ) { 277 ) {
267 let (before_cursor_pos, before) = extract_offset(before); 278 let (before_cursor_pos, before) = extract_offset(before);
268 let (db, file_id) = RootDatabase::with_single_file(&before); 279 let (db, file_id) = with_single_file(&before);
269 let frange = 280 let frange =
270 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 281 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
271 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 282 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
@@ -277,7 +288,7 @@ mod helpers {
277 before: &str, 288 before: &str,
278 ) { 289 ) {
279 let (range, before) = extract_range(before); 290 let (range, before) = extract_range(before);
280 let (db, file_id) = RootDatabase::with_single_file(&before); 291 let (db, file_id) = with_single_file(&before);
281 let frange = FileRange { file_id, range }; 292 let frange = FileRange { file_id, range };
282 let assist = AssistCtx::with_ctx(&db, frange, true, assist); 293 let assist = AssistCtx::with_ctx(&db, frange, true, assist);
283 assert!(assist.is_none()); 294 assert!(assist.is_none());
@@ -286,17 +297,17 @@ mod helpers {
286 297
287#[cfg(test)] 298#[cfg(test)]
288mod tests { 299mod tests {
289 use ra_db::{fixture::WithFixture, FileRange}; 300 use ra_db::FileRange;
290 use ra_syntax::TextRange; 301 use ra_syntax::TextRange;
291 use test_utils::{extract_offset, extract_range}; 302 use test_utils::{extract_offset, extract_range};
292 303
293 use ra_ide_db::RootDatabase; 304 use crate::helpers;
294 305
295 #[test] 306 #[test]
296 fn assist_order_field_struct() { 307 fn assist_order_field_struct() {
297 let before = "struct Foo { <|>bar: u32 }"; 308 let before = "struct Foo { <|>bar: u32 }";
298 let (before_cursor_pos, before) = extract_offset(before); 309 let (before_cursor_pos, before) = extract_offset(before);
299 let (db, file_id) = RootDatabase::with_single_file(&before); 310 let (db, file_id) = helpers::with_single_file(&before);
300 let frange = 311 let frange =
301 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; 312 FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
302 let assists = super::assists(&db, frange); 313 let assists = super::assists(&db, frange);
@@ -320,7 +331,7 @@ mod tests {
320 } 331 }
321 }"; 332 }";
322 let (range, before) = extract_range(before); 333 let (range, before) = extract_range(before);
323 let (db, file_id) = RootDatabase::with_single_file(&before); 334 let (db, file_id) = helpers::with_single_file(&before);
324 let frange = FileRange { file_id, range }; 335 let frange = FileRange { file_id, range };
325 let assists = super::assists(&db, frange); 336 let assists = super::assists(&db, frange);
326 let mut assists = assists.iter(); 337 let mut assists = assists.iter();