diff options
Diffstat (limited to 'crates/ra_assists/src')
-rw-r--r-- | crates/ra_assists/src/assist_ctx.rs | 16 | ||||
-rw-r--r-- | crates/ra_assists/src/doc_tests.rs | 4 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 61 |
3 files changed, 42 insertions, 39 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 2ab65ab99..b6cac96fa 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -1,8 +1,9 @@ | |||
1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. | 1 | //! This module defines `AssistCtx` -- the API surface that is exposed to assists. |
2 | use either::Either; | 2 | use either::Either; |
3 | use hir::{db::HirDatabase, InFile, SourceAnalyzer, SourceBinder}; | 3 | use hir::{db::HirDatabase, InFile, SourceAnalyzer, SourceBinder}; |
4 | use ra_db::FileRange; | 4 | use ra_db::{FileRange, SourceDatabase}; |
5 | use ra_fmt::{leading_indent, reindent}; | 5 | use ra_fmt::{leading_indent, reindent}; |
6 | use ra_ide_db::RootDatabase; | ||
6 | use ra_syntax::{ | 7 | use ra_syntax::{ |
7 | algo::{self, find_covering_element, find_node_at_offset}, | 8 | algo::{self, find_covering_element, find_node_at_offset}, |
8 | AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit, | 9 | AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit, |
@@ -67,17 +68,24 @@ impl<'a, DB> Clone for AssistCtx<'a, DB> { | |||
67 | } | 68 | } |
68 | } | 69 | } |
69 | 70 | ||
70 | impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | 71 | impl<'a> AssistCtx<'a, RootDatabase> { |
71 | pub(crate) fn with_ctx<F, T>(db: &DB, frange: FileRange, should_compute_edit: bool, f: F) -> T | 72 | pub(crate) fn with_ctx<F, T>( |
73 | db: &RootDatabase, | ||
74 | frange: FileRange, | ||
75 | should_compute_edit: bool, | ||
76 | f: F, | ||
77 | ) -> T | ||
72 | where | 78 | where |
73 | F: FnOnce(AssistCtx<DB>) -> T, | 79 | F: FnOnce(AssistCtx<RootDatabase>) -> T, |
74 | { | 80 | { |
75 | let parse = db.parse(frange.file_id); | 81 | let parse = db.parse(frange.file_id); |
76 | 82 | ||
77 | let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }; | 83 | let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }; |
78 | f(ctx) | 84 | f(ctx) |
79 | } | 85 | } |
86 | } | ||
80 | 87 | ||
88 | impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | ||
81 | pub(crate) fn add_assist( | 89 | pub(crate) fn add_assist( |
82 | self, | 90 | self, |
83 | id: AssistId, | 91 | id: AssistId, |
diff --git a/crates/ra_assists/src/doc_tests.rs b/crates/ra_assists/src/doc_tests.rs index 65d51428b..370b64225 100644 --- a/crates/ra_assists/src/doc_tests.rs +++ b/crates/ra_assists/src/doc_tests.rs | |||
@@ -8,7 +8,7 @@ mod generated; | |||
8 | use ra_db::{fixture::WithFixture, FileRange}; | 8 | use ra_db::{fixture::WithFixture, FileRange}; |
9 | use test_utils::{assert_eq_text, extract_range_or_offset}; | 9 | use test_utils::{assert_eq_text, extract_range_or_offset}; |
10 | 10 | ||
11 | use crate::test_db::TestDB; | 11 | use ra_ide_db::RootDatabase; |
12 | 12 | ||
13 | fn check(assist_id: &str, before: &str, after: &str) { | 13 | fn check(assist_id: &str, before: &str, after: &str) { |
14 | // FIXME we cannot get the imports search functionality here yet, but still need to generate a test and a doc for an assist | 14 | // FIXME we cannot get the imports search functionality here yet, but still need to generate a test and a doc for an assist |
@@ -16,7 +16,7 @@ fn check(assist_id: &str, before: &str, after: &str) { | |||
16 | return; | 16 | return; |
17 | } | 17 | } |
18 | let (selection, before) = extract_range_or_offset(before); | 18 | let (selection, before) = extract_range_or_offset(before); |
19 | let (db, file_id) = TestDB::with_single_file(&before); | 19 | let (db, file_id) = RootDatabase::with_single_file(&before); |
20 | let frange = FileRange { file_id, range: selection.into() }; | 20 | let frange = FileRange { file_id, range: selection.into() }; |
21 | 21 | ||
22 | let assist = crate::assists(&db, frange) | 22 | let assist = crate::assists(&db, frange) |
diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 0ebb8e8b0..a2109b751 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -14,7 +14,7 @@ mod test_db; | |||
14 | pub mod ast_transform; | 14 | pub mod ast_transform; |
15 | 15 | ||
16 | use either::Either; | 16 | use either::Either; |
17 | use hir::{db::HirDatabase, ModuleDef}; | 17 | use hir::ModuleDef; |
18 | use ra_db::FileRange; | 18 | use ra_db::FileRange; |
19 | use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; | 19 | use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; |
20 | use ra_syntax::{TextRange, TextUnit}; | 20 | use ra_syntax::{TextRange, TextUnit}; |
@@ -62,10 +62,7 @@ impl ResolvedAssist { | |||
62 | /// | 62 | /// |
63 | /// Assists are returned in the "unresolved" state, that is only labels are | 63 | /// Assists are returned in the "unresolved" state, that is only labels are |
64 | /// returned, without actual edits. | 64 | /// returned, without actual edits. |
65 | pub fn applicable_assists<H>(db: &H, range: FileRange) -> Vec<AssistLabel> | 65 | pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { |
66 | where | ||
67 | H: HirDatabase + 'static, | ||
68 | { | ||
69 | AssistCtx::with_ctx(db, range, false, |ctx| { | 66 | AssistCtx::with_ctx(db, range, false, |ctx| { |
70 | assists::all() | 67 | assists::all() |
71 | .iter() | 68 | .iter() |
@@ -126,10 +123,7 @@ pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec< | |||
126 | /// | 123 | /// |
127 | /// Assists are returned in the "resolved" state, that is with edit fully | 124 | /// Assists are returned in the "resolved" state, that is with edit fully |
128 | /// computed. | 125 | /// computed. |
129 | pub fn assists<H>(db: &H, range: FileRange) -> Vec<ResolvedAssist> | 126 | pub fn assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { |
130 | where | ||
131 | H: HirDatabase + 'static, | ||
132 | { | ||
133 | AssistCtx::with_ctx(db, range, true, |ctx| { | 127 | AssistCtx::with_ctx(db, range, true, |ctx| { |
134 | let mut a = assists::all() | 128 | let mut a = assists::all() |
135 | .iter() | 129 | .iter() |
@@ -231,17 +225,18 @@ mod helpers { | |||
231 | use ra_syntax::TextRange; | 225 | use ra_syntax::TextRange; |
232 | use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; | 226 | use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; |
233 | 227 | ||
234 | use crate::{test_db::TestDB, Assist, AssistCtx, ImportsLocator}; | 228 | use crate::{Assist, AssistCtx, ImportsLocator}; |
229 | use ra_ide_db::RootDatabase; | ||
235 | use std::sync::Arc; | 230 | use std::sync::Arc; |
236 | 231 | ||
237 | // FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed. | 232 | // FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed. |
238 | pub(crate) struct TestImportsLocator { | 233 | pub(crate) struct TestImportsLocator { |
239 | db: Arc<TestDB>, | 234 | db: Arc<RootDatabase>, |
240 | test_file_id: FileId, | 235 | test_file_id: FileId, |
241 | } | 236 | } |
242 | 237 | ||
243 | impl TestImportsLocator { | 238 | impl TestImportsLocator { |
244 | pub(crate) fn new(db: Arc<TestDB>, test_file_id: FileId) -> Self { | 239 | pub(crate) fn new(db: Arc<RootDatabase>, test_file_id: FileId) -> Self { |
245 | TestImportsLocator { db, test_file_id } | 240 | TestImportsLocator { db, test_file_id } |
246 | } | 241 | } |
247 | } | 242 | } |
@@ -282,12 +277,12 @@ mod helpers { | |||
282 | } | 277 | } |
283 | 278 | ||
284 | pub(crate) fn check_assist( | 279 | pub(crate) fn check_assist( |
285 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 280 | assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>, |
286 | before: &str, | 281 | before: &str, |
287 | after: &str, | 282 | after: &str, |
288 | ) { | 283 | ) { |
289 | let (before_cursor_pos, before) = extract_offset(before); | 284 | let (before_cursor_pos, before) = extract_offset(before); |
290 | let (db, file_id) = TestDB::with_single_file(&before); | 285 | let (db, file_id) = RootDatabase::with_single_file(&before); |
291 | let frange = | 286 | let frange = |
292 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 287 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
293 | let assist = | 288 | let assist = |
@@ -310,13 +305,13 @@ mod helpers { | |||
310 | } | 305 | } |
311 | 306 | ||
312 | pub(crate) fn check_assist_with_imports_locator<F: ImportsLocator>( | 307 | pub(crate) fn check_assist_with_imports_locator<F: ImportsLocator>( |
313 | assist: fn(AssistCtx<TestDB>, &mut F) -> Option<Assist>, | 308 | assist: fn(AssistCtx<RootDatabase>, &mut F) -> Option<Assist>, |
314 | imports_locator_provider: fn(db: Arc<TestDB>, file_id: FileId) -> F, | 309 | imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F, |
315 | before: &str, | 310 | before: &str, |
316 | after: &str, | 311 | after: &str, |
317 | ) { | 312 | ) { |
318 | let (before_cursor_pos, before) = extract_offset(before); | 313 | let (before_cursor_pos, before) = extract_offset(before); |
319 | let (db, file_id) = TestDB::with_single_file(&before); | 314 | let (db, file_id) = RootDatabase::with_single_file(&before); |
320 | let db = Arc::new(db); | 315 | let db = Arc::new(db); |
321 | let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); | 316 | let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); |
322 | let frange = | 317 | let frange = |
@@ -342,12 +337,12 @@ mod helpers { | |||
342 | } | 337 | } |
343 | 338 | ||
344 | pub(crate) fn check_assist_range( | 339 | pub(crate) fn check_assist_range( |
345 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 340 | assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>, |
346 | before: &str, | 341 | before: &str, |
347 | after: &str, | 342 | after: &str, |
348 | ) { | 343 | ) { |
349 | let (range, before) = extract_range(before); | 344 | let (range, before) = extract_range(before); |
350 | let (db, file_id) = TestDB::with_single_file(&before); | 345 | let (db, file_id) = RootDatabase::with_single_file(&before); |
351 | let frange = FileRange { file_id, range }; | 346 | let frange = FileRange { file_id, range }; |
352 | let assist = | 347 | let assist = |
353 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 348 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); |
@@ -364,12 +359,12 @@ mod helpers { | |||
364 | } | 359 | } |
365 | 360 | ||
366 | pub(crate) fn check_assist_target( | 361 | pub(crate) fn check_assist_target( |
367 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 362 | assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>, |
368 | before: &str, | 363 | before: &str, |
369 | target: &str, | 364 | target: &str, |
370 | ) { | 365 | ) { |
371 | let (before_cursor_pos, before) = extract_offset(before); | 366 | let (before_cursor_pos, before) = extract_offset(before); |
372 | let (db, file_id) = TestDB::with_single_file(&before); | 367 | let (db, file_id) = RootDatabase::with_single_file(&before); |
373 | let frange = | 368 | let frange = |
374 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 369 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
375 | let assist = | 370 | let assist = |
@@ -384,12 +379,12 @@ mod helpers { | |||
384 | } | 379 | } |
385 | 380 | ||
386 | pub(crate) fn check_assist_range_target( | 381 | pub(crate) fn check_assist_range_target( |
387 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 382 | assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>, |
388 | before: &str, | 383 | before: &str, |
389 | target: &str, | 384 | target: &str, |
390 | ) { | 385 | ) { |
391 | let (range, before) = extract_range(before); | 386 | let (range, before) = extract_range(before); |
392 | let (db, file_id) = TestDB::with_single_file(&before); | 387 | let (db, file_id) = RootDatabase::with_single_file(&before); |
393 | let frange = FileRange { file_id, range }; | 388 | let frange = FileRange { file_id, range }; |
394 | let assist = | 389 | let assist = |
395 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 390 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); |
@@ -403,11 +398,11 @@ mod helpers { | |||
403 | } | 398 | } |
404 | 399 | ||
405 | pub(crate) fn check_assist_not_applicable( | 400 | pub(crate) fn check_assist_not_applicable( |
406 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 401 | assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>, |
407 | before: &str, | 402 | before: &str, |
408 | ) { | 403 | ) { |
409 | let (before_cursor_pos, before) = extract_offset(before); | 404 | let (before_cursor_pos, before) = extract_offset(before); |
410 | let (db, file_id) = TestDB::with_single_file(&before); | 405 | let (db, file_id) = RootDatabase::with_single_file(&before); |
411 | let frange = | 406 | let frange = |
412 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 407 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
413 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 408 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
@@ -415,12 +410,12 @@ mod helpers { | |||
415 | } | 410 | } |
416 | 411 | ||
417 | pub(crate) fn check_assist_with_imports_locator_not_applicable<F: ImportsLocator>( | 412 | pub(crate) fn check_assist_with_imports_locator_not_applicable<F: ImportsLocator>( |
418 | assist: fn(AssistCtx<TestDB>, &mut F) -> Option<Assist>, | 413 | assist: fn(AssistCtx<RootDatabase>, &mut F) -> Option<Assist>, |
419 | imports_locator_provider: fn(db: Arc<TestDB>, file_id: FileId) -> F, | 414 | imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F, |
420 | before: &str, | 415 | before: &str, |
421 | ) { | 416 | ) { |
422 | let (before_cursor_pos, before) = extract_offset(before); | 417 | let (before_cursor_pos, before) = extract_offset(before); |
423 | let (db, file_id) = TestDB::with_single_file(&before); | 418 | let (db, file_id) = RootDatabase::with_single_file(&before); |
424 | let db = Arc::new(db); | 419 | let db = Arc::new(db); |
425 | let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); | 420 | let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); |
426 | let frange = | 421 | let frange = |
@@ -431,11 +426,11 @@ mod helpers { | |||
431 | } | 426 | } |
432 | 427 | ||
433 | pub(crate) fn check_assist_range_not_applicable( | 428 | pub(crate) fn check_assist_range_not_applicable( |
434 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 429 | assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>, |
435 | before: &str, | 430 | before: &str, |
436 | ) { | 431 | ) { |
437 | let (range, before) = extract_range(before); | 432 | let (range, before) = extract_range(before); |
438 | let (db, file_id) = TestDB::with_single_file(&before); | 433 | let (db, file_id) = RootDatabase::with_single_file(&before); |
439 | let frange = FileRange { file_id, range }; | 434 | let frange = FileRange { file_id, range }; |
440 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 435 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
441 | assert!(assist.is_none()); | 436 | assert!(assist.is_none()); |
@@ -448,13 +443,13 @@ mod tests { | |||
448 | use ra_syntax::TextRange; | 443 | use ra_syntax::TextRange; |
449 | use test_utils::{extract_offset, extract_range}; | 444 | use test_utils::{extract_offset, extract_range}; |
450 | 445 | ||
451 | use crate::test_db::TestDB; | 446 | use ra_ide_db::RootDatabase; |
452 | 447 | ||
453 | #[test] | 448 | #[test] |
454 | fn assist_order_field_struct() { | 449 | fn assist_order_field_struct() { |
455 | let before = "struct Foo { <|>bar: u32 }"; | 450 | let before = "struct Foo { <|>bar: u32 }"; |
456 | let (before_cursor_pos, before) = extract_offset(before); | 451 | let (before_cursor_pos, before) = extract_offset(before); |
457 | let (db, file_id) = TestDB::with_single_file(&before); | 452 | let (db, file_id) = RootDatabase::with_single_file(&before); |
458 | let frange = | 453 | let frange = |
459 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 454 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
460 | let assists = super::assists(&db, frange); | 455 | let assists = super::assists(&db, frange); |
@@ -478,7 +473,7 @@ mod tests { | |||
478 | } | 473 | } |
479 | }"; | 474 | }"; |
480 | let (range, before) = extract_range(before); | 475 | let (range, before) = extract_range(before); |
481 | let (db, file_id) = TestDB::with_single_file(&before); | 476 | let (db, file_id) = RootDatabase::with_single_file(&before); |
482 | let frange = FileRange { file_id, range }; | 477 | let frange = FileRange { file_id, range }; |
483 | let assists = super::assists(&db, frange); | 478 | let assists = super::assists(&db, frange); |
484 | let mut assists = assists.iter(); | 479 | let mut assists = assists.iter(); |