From 2c922ef54958a82ce745e6db6834062f97f21bed Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 16:53:42 +0100 Subject: Start switching assists to a root database --- crates/ra_assists/src/assist_ctx.rs | 16 +++++++--- crates/ra_assists/src/doc_tests.rs | 4 +-- crates/ra_assists/src/lib.rs | 61 +++++++++++++++++-------------------- 3 files changed, 42 insertions(+), 39 deletions(-) (limited to 'crates') 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 @@ //! This module defines `AssistCtx` -- the API surface that is exposed to assists. use either::Either; use hir::{db::HirDatabase, InFile, SourceAnalyzer, SourceBinder}; -use ra_db::FileRange; +use ra_db::{FileRange, SourceDatabase}; use ra_fmt::{leading_indent, reindent}; +use ra_ide_db::RootDatabase; use ra_syntax::{ algo::{self, find_covering_element, find_node_at_offset}, AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextUnit, @@ -67,17 +68,24 @@ impl<'a, DB> Clone for AssistCtx<'a, DB> { } } -impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { - pub(crate) fn with_ctx(db: &DB, frange: FileRange, should_compute_edit: bool, f: F) -> T +impl<'a> AssistCtx<'a, RootDatabase> { + pub(crate) fn with_ctx( + db: &RootDatabase, + frange: FileRange, + should_compute_edit: bool, + f: F, + ) -> T where - F: FnOnce(AssistCtx) -> T, + F: FnOnce(AssistCtx) -> T, { let parse = db.parse(frange.file_id); let ctx = AssistCtx { db, frange, source_file: parse.tree(), should_compute_edit }; f(ctx) } +} +impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { pub(crate) fn add_assist( self, 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; use ra_db::{fixture::WithFixture, FileRange}; use test_utils::{assert_eq_text, extract_range_or_offset}; -use crate::test_db::TestDB; +use ra_ide_db::RootDatabase; fn check(assist_id: &str, before: &str, after: &str) { // 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) { return; } let (selection, before) = extract_range_or_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range: selection.into() }; 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; pub mod ast_transform; use either::Either; -use hir::{db::HirDatabase, ModuleDef}; +use hir::ModuleDef; use ra_db::FileRange; use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; use ra_syntax::{TextRange, TextUnit}; @@ -62,10 +62,7 @@ impl ResolvedAssist { /// /// Assists are returned in the "unresolved" state, that is only labels are /// returned, without actual edits. -pub fn applicable_assists(db: &H, range: FileRange) -> Vec -where - H: HirDatabase + 'static, -{ +pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec { AssistCtx::with_ctx(db, range, false, |ctx| { assists::all() .iter() @@ -126,10 +123,7 @@ pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec< /// /// Assists are returned in the "resolved" state, that is with edit fully /// computed. -pub fn assists(db: &H, range: FileRange) -> Vec -where - H: HirDatabase + 'static, -{ +pub fn assists(db: &RootDatabase, range: FileRange) -> Vec { AssistCtx::with_ctx(db, range, true, |ctx| { let mut a = assists::all() .iter() @@ -231,17 +225,18 @@ mod helpers { use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; - use crate::{test_db::TestDB, Assist, AssistCtx, ImportsLocator}; + use crate::{Assist, AssistCtx, ImportsLocator}; + use ra_ide_db::RootDatabase; use std::sync::Arc; // FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed. pub(crate) struct TestImportsLocator { - db: Arc, + db: Arc, test_file_id: FileId, } impl TestImportsLocator { - pub(crate) fn new(db: Arc, test_file_id: FileId) -> Self { + pub(crate) fn new(db: Arc, test_file_id: FileId) -> Self { TestImportsLocator { db, test_file_id } } } @@ -282,12 +277,12 @@ mod helpers { } pub(crate) fn check_assist( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -310,13 +305,13 @@ mod helpers { } pub(crate) fn check_assist_with_imports_locator( - assist: fn(AssistCtx, &mut F) -> Option, - imports_locator_provider: fn(db: Arc, file_id: FileId) -> F, + assist: fn(AssistCtx, &mut F) -> Option, + imports_locator_provider: fn(db: Arc, file_id: FileId) -> F, before: &str, after: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let db = Arc::new(db); let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); let frange = @@ -342,12 +337,12 @@ mod helpers { } pub(crate) fn check_assist_range( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -364,12 +359,12 @@ mod helpers { } pub(crate) fn check_assist_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -384,12 +379,12 @@ mod helpers { } pub(crate) fn check_assist_range_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); @@ -403,11 +398,11 @@ mod helpers { } pub(crate) fn check_assist_not_applicable( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); @@ -415,12 +410,12 @@ mod helpers { } pub(crate) fn check_assist_with_imports_locator_not_applicable( - assist: fn(AssistCtx, &mut F) -> Option, - imports_locator_provider: fn(db: Arc, file_id: FileId) -> F, + assist: fn(AssistCtx, &mut F) -> Option, + imports_locator_provider: fn(db: Arc, file_id: FileId) -> F, before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let db = Arc::new(db); let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); let frange = @@ -431,11 +426,11 @@ mod helpers { } pub(crate) fn check_assist_range_not_applicable( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); assert!(assist.is_none()); @@ -448,13 +443,13 @@ mod tests { use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; - use crate::test_db::TestDB; + use ra_ide_db::RootDatabase; #[test] fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assists = super::assists(&db, frange); @@ -478,7 +473,7 @@ mod tests { } }"; let (range, before) = extract_range(before); - let (db, file_id) = TestDB::with_single_file(&before); + let (db, file_id) = RootDatabase::with_single_file(&before); let frange = FileRange { file_id, range }; let assists = super::assists(&db, frange); let mut assists = assists.iter(); -- cgit v1.2.3