Skip to content

Commit 2c922ef

Browse files
committed
Start switching assists to a root database
1 parent a173e31 commit 2c922ef

File tree

3 files changed

+42
-39
lines changed

3 files changed

+42
-39
lines changed

crates/ra_assists/src/assist_ctx.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! This module defines `AssistCtx` -- the API surface that is exposed to assists.
22
use either::Either;
33
use hir::{db::HirDatabase, InFile, SourceAnalyzer, SourceBinder};
4-
use ra_db::FileRange;
4+
use ra_db::{FileRange, SourceDatabase};
55
use ra_fmt::{leading_indent, reindent};
6+
use ra_ide_db::RootDatabase;
67
use ra_syntax::{
78
algo::{self, find_covering_element, find_node_at_offset},
89
AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit,
@@ -67,17 +68,24 @@ impl<'a, DB> Clone for AssistCtx<'a, DB> {
6768
}
6869
}
6970

70-
impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
71-
pub(crate) fn with_ctx<F, T>(db: &DB, frange: FileRange, should_compute_edit: bool, f: F) -> T
71+
impl<'a> AssistCtx<'a, RootDatabase> {
72+
pub(crate) fn with_ctx<F, T>(
73+
db: &RootDatabase,
74+
frange: FileRange,
75+
should_compute_edit: bool,
76+
f: F,
77+
) -> T
7278
where
73-
F: FnOnce(AssistCtx<DB>) -> T,
79+
F: FnOnce(AssistCtx<RootDatabase>) -> T,
7480
{
7581
let parse = db.parse(frange.file_id);
7682

7783
let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit };
7884
f(ctx)
7985
}
86+
}
8087

88+
impl<'a, DB: HirDatabase> AssistCtx<'a, DB> {
8189
pub(crate) fn add_assist(
8290
self,
8391
id: AssistId,

crates/ra_assists/src/doc_tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ mod generated;
88
use ra_db::{fixture::WithFixture, FileRange};
99
use test_utils::{assert_eq_text, extract_range_or_offset};
1010

11-
use crate::test_db::TestDB;
11+
use ra_ide_db::RootDatabase;
1212

1313
fn check(assist_id: &str, before: &str, after: &str) {
1414
// FIXME we cannot get the imports search functionality here yet, but still need to generate a test and a doc for an assist
1515
if assist_id == "auto_import" {
1616
return;
1717
}
1818
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);
2020
let frange = FileRange { file_id, range: selection.into() };
2121

2222
let assist = crate::assists(&db, frange)

crates/ra_assists/src/lib.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod test_db;
1414
pub mod ast_transform;
1515

1616
use either::Either;
17-
use hir::{db::HirDatabase, ModuleDef};
17+
use hir::ModuleDef;
1818
use ra_db::FileRange;
1919
use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase};
2020
use ra_syntax::{TextRange, TextUnit};
@@ -62,10 +62,7 @@ impl ResolvedAssist {
6262
///
6363
/// Assists are returned in the "unresolved" state, that is only labels are
6464
/// returned, without actual edits.
65-
pub fn applicable_assists<H>(db: &H, range: FileRange) -> Vec<AssistLabel>
66-
where
67-
H: HirDatabase + 'static,
68-
{
65+
pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> {
6966
AssistCtx::with_ctx(db, range, false, |ctx| {
7067
assists::all()
7168
.iter()
@@ -126,10 +123,7 @@ pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec<
126123
///
127124
/// Assists are returned in the "resolved" state, that is with edit fully
128125
/// computed.
129-
pub fn assists<H>(db: &H, range: FileRange) -> Vec<ResolvedAssist>
130-
where
131-
H: HirDatabase + 'static,
132-
{
126+
pub fn assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> {
133127
AssistCtx::with_ctx(db, range, true, |ctx| {
134128
let mut a = assists::all()
135129
.iter()
@@ -231,17 +225,18 @@ mod helpers {
231225
use ra_syntax::TextRange;
232226
use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range};
233227

234-
use crate::{test_db::TestDB, Assist, AssistCtx, ImportsLocator};
228+
use crate::{Assist, AssistCtx, ImportsLocator};
229+
use ra_ide_db::RootDatabase;
235230
use std::sync::Arc;
236231

237232
// FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed.
238233
pub(crate) struct TestImportsLocator {
239-
db: Arc<TestDB>,
234+
db: Arc<RootDatabase>,
240235
test_file_id: FileId,
241236
}
242237

243238
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 {
245240
TestImportsLocator { db, test_file_id }
246241
}
247242
}
@@ -282,12 +277,12 @@ mod helpers {
282277
}
283278

284279
pub(crate) fn check_assist(
285-
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
280+
assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>,
286281
before: &str,
287282
after: &str,
288283
) {
289284
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);
291286
let frange =
292287
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
293288
let assist =
@@ -310,13 +305,13 @@ mod helpers {
310305
}
311306

312307
pub(crate) fn check_assist_with_imports_locator<F: ImportsLocator>(
313-
assist: fn(AssistCtx<TestDB>, &mut F) -> Option<Assist>,
314-
imports_locator_provider: fn(db: Arc<TestDB>, file_id: FileId) -> F,
308+
assist: fn(AssistCtx<RootDatabase>, &mut F) -> Option<Assist>,
309+
imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F,
315310
before: &str,
316311
after: &str,
317312
) {
318313
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);
320315
let db = Arc::new(db);
321316
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
322317
let frange =
@@ -342,12 +337,12 @@ mod helpers {
342337
}
343338

344339
pub(crate) fn check_assist_range(
345-
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
340+
assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>,
346341
before: &str,
347342
after: &str,
348343
) {
349344
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);
351346
let frange = FileRange { file_id, range };
352347
let assist =
353348
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -364,12 +359,12 @@ mod helpers {
364359
}
365360

366361
pub(crate) fn check_assist_target(
367-
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
362+
assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>,
368363
before: &str,
369364
target: &str,
370365
) {
371366
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);
373368
let frange =
374369
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
375370
let assist =
@@ -384,12 +379,12 @@ mod helpers {
384379
}
385380

386381
pub(crate) fn check_assist_range_target(
387-
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
382+
assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>,
388383
before: &str,
389384
target: &str,
390385
) {
391386
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);
393388
let frange = FileRange { file_id, range };
394389
let assist =
395390
AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable");
@@ -403,24 +398,24 @@ mod helpers {
403398
}
404399

405400
pub(crate) fn check_assist_not_applicable(
406-
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
401+
assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>,
407402
before: &str,
408403
) {
409404
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);
411406
let frange =
412407
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
413408
let assist = AssistCtx::with_ctx(&db, frange, true, assist);
414409
assert!(assist.is_none());
415410
}
416411

417412
pub(crate) fn check_assist_with_imports_locator_not_applicable<F: ImportsLocator>(
418-
assist: fn(AssistCtx<TestDB>, &mut F) -> Option<Assist>,
419-
imports_locator_provider: fn(db: Arc<TestDB>, file_id: FileId) -> F,
413+
assist: fn(AssistCtx<RootDatabase>, &mut F) -> Option<Assist>,
414+
imports_locator_provider: fn(db: Arc<RootDatabase>, file_id: FileId) -> F,
420415
before: &str,
421416
) {
422417
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);
424419
let db = Arc::new(db);
425420
let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id);
426421
let frange =
@@ -431,11 +426,11 @@ mod helpers {
431426
}
432427

433428
pub(crate) fn check_assist_range_not_applicable(
434-
assist: fn(AssistCtx<TestDB>) -> Option<Assist>,
429+
assist: fn(AssistCtx<RootDatabase>) -> Option<Assist>,
435430
before: &str,
436431
) {
437432
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);
439434
let frange = FileRange { file_id, range };
440435
let assist = AssistCtx::with_ctx(&db, frange, true, assist);
441436
assert!(assist.is_none());
@@ -448,13 +443,13 @@ mod tests {
448443
use ra_syntax::TextRange;
449444
use test_utils::{extract_offset, extract_range};
450445

451-
use crate::test_db::TestDB;
446+
use ra_ide_db::RootDatabase;
452447

453448
#[test]
454449
fn assist_order_field_struct() {
455450
let before = "struct Foo { <|>bar: u32 }";
456451
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);
458453
let frange =
459454
FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) };
460455
let assists = super::assists(&db, frange);
@@ -478,7 +473,7 @@ mod tests {
478473
}
479474
}";
480475
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);
482477
let frange = FileRange { file_id, range };
483478
let assists = super::assists(&db, frange);
484479
let mut assists = assists.iter();

0 commit comments

Comments
 (0)