From a173e31890c1eb03d9d4c123986baae4154cd4fa Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 16:40:28 +0100 Subject: Make assists use ImportsLocator directly --- crates/ra_assists/Cargo.toml | 1 + crates/ra_assists/src/lib.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 9d3091b91..6973038d4 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml @@ -18,5 +18,6 @@ ra_text_edit = { path = "../ra_text_edit" } ra_fmt = { path = "../ra_fmt" } ra_prof = { path = "../ra_prof" } ra_db = { path = "../ra_db" } +ra_ide_db = { path = "../ra_ide_db" } hir = { path = "../ra_hir", package = "ra_hir" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 625ebc4a2..0ebb8e8b0 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -16,6 +16,7 @@ pub mod ast_transform; use either::Either; use hir::{db::HirDatabase, ModuleDef}; use ra_db::FileRange; +use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; use ra_syntax::{TextRange, TextUnit}; use ra_text_edit::TextEdit; @@ -88,20 +89,19 @@ pub trait ImportsLocator { fn find_imports(&mut self, name_to_import: &str) -> Vec; } +impl ImportsLocator for ImportsLocatorIde<'_> { + fn find_imports(&mut self, name_to_import: &str) -> Vec { + self.find_imports(name_to_import) + } +} + /// Return all the assists applicable at the given position /// and additional assists that need the imports locator functionality to work. /// /// Assists are returned in the "resolved" state, that is with edit fully /// computed. -pub fn assists_with_imports_locator( - db: &H, - range: FileRange, - mut imports_locator: F, -) -> Vec -where - H: HirDatabase + 'static, - F: ImportsLocator, -{ +pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec { + let mut imports_locator = ImportsLocatorIde::new(db); AssistCtx::with_ctx(db, range, true, |ctx| { let mut assists = assists::all() .iter() -- cgit v1.2.3 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/ra_assists') 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 From f8965ffafd5cf467b3f0482ca962ba2bfd090161 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 16:54:22 +0100 Subject: Remove assists TestDB --- crates/ra_assists/src/lib.rs | 2 -- crates/ra_assists/src/test_db.rs | 45 ---------------------------------------- 2 files changed, 47 deletions(-) delete mode 100644 crates/ra_assists/src/test_db.rs (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index a2109b751..be6e06842 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -9,8 +9,6 @@ mod assist_ctx; mod marks; #[cfg(test)] mod doc_tests; -#[cfg(test)] -mod test_db; pub mod ast_transform; use either::Either; diff --git a/crates/ra_assists/src/test_db.rs b/crates/ra_assists/src/test_db.rs deleted file mode 100644 index d5249f308..000000000 --- a/crates/ra_assists/src/test_db.rs +++ /dev/null @@ -1,45 +0,0 @@ -//! Database used for testing `ra_assists`. - -use std::sync::Arc; - -use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; - -#[salsa::database( - ra_db::SourceDatabaseExtStorage, - ra_db::SourceDatabaseStorage, - hir::db::InternDatabaseStorage, - hir::db::AstDatabaseStorage, - hir::db::DefDatabaseStorage, - hir::db::HirDatabaseStorage -)] -#[derive(Debug, Default)] -pub struct TestDB { - runtime: salsa::Runtime, -} - -impl salsa::Database for TestDB { - fn salsa_runtime(&self) -> &salsa::Runtime { - &self.runtime - } - fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime { - &mut self.runtime - } -} - -impl std::panic::RefUnwindSafe for TestDB {} - -impl FileLoader for TestDB { - fn file_text(&self, file_id: FileId) -> Arc { - FileLoaderDelegate(self).file_text(file_id) - } - fn resolve_relative_path( - &self, - anchor: FileId, - relative_path: &RelativePath, - ) -> Option { - FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) - } - fn relevant_crates(&self, file_id: FileId) -> Arc> { - FileLoaderDelegate(self).relevant_crates(file_id) - } -} -- cgit v1.2.3 From cf812c12d1ac7944d1c18877ee93bea02d91e99f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 16:58:57 +0100 Subject: Assists are not generic --- crates/ra_assists/src/assist_ctx.rs | 16 ++++++------- crates/ra_assists/src/assists/add_custom_impl.rs | 4 ++-- crates/ra_assists/src/assists/add_derive.rs | 3 +-- crates/ra_assists/src/assists/add_explicit_type.rs | 4 ++-- crates/ra_assists/src/assists/add_impl.rs | 4 ++-- crates/ra_assists/src/assists/add_import.rs | 4 ++-- .../src/assists/add_missing_impl_members.rs | 6 ++--- crates/ra_assists/src/assists/add_new.rs | 9 +++----- crates/ra_assists/src/assists/apply_demorgan.rs | 3 +-- crates/ra_assists/src/assists/auto_import.rs | 4 ++-- crates/ra_assists/src/assists/change_visibility.rs | 7 +++--- crates/ra_assists/src/assists/early_return.rs | 3 +-- crates/ra_assists/src/assists/fill_match_arms.rs | 2 +- crates/ra_assists/src/assists/flip_binexpr.rs | 3 +-- crates/ra_assists/src/assists/flip_comma.rs | 3 +-- crates/ra_assists/src/assists/flip_trait_bound.rs | 3 +-- .../src/assists/inline_local_variable.rs | 3 +-- .../ra_assists/src/assists/introduce_variable.rs | 3 +-- crates/ra_assists/src/assists/invert_if.rs | 3 +-- crates/ra_assists/src/assists/merge_match_arms.rs | 3 +-- crates/ra_assists/src/assists/move_bounds.rs | 3 +-- crates/ra_assists/src/assists/move_guard.rs | 5 ++-- crates/ra_assists/src/assists/raw_string.rs | 9 ++++---- crates/ra_assists/src/assists/remove_dbg.rs | 3 +-- .../src/assists/replace_if_let_with_match.rs | 3 +-- crates/ra_assists/src/assists/split_import.rs | 3 +-- crates/ra_assists/src/lib.rs | 27 +++++++++------------- 27 files changed, 59 insertions(+), 84 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index b6cac96fa..f32072dbd 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -1,6 +1,6 @@ //! This module defines `AssistCtx` -- the API surface that is exposed to assists. use either::Either; -use hir::{db::HirDatabase, InFile, SourceAnalyzer, SourceBinder}; +use hir::{InFile, SourceAnalyzer, SourceBinder}; use ra_db::{FileRange, SourceDatabase}; use ra_fmt::{leading_indent, reindent}; use ra_ide_db::RootDatabase; @@ -50,14 +50,14 @@ pub(crate) enum Assist { /// moment, because the LSP API is pretty awkward in this place, and it's much /// easier to just compute the edit eagerly :-) #[derive(Debug)] -pub(crate) struct AssistCtx<'a, DB> { - pub(crate) db: &'a DB, +pub(crate) struct AssistCtx<'a> { + pub(crate) db: &'a RootDatabase, pub(crate) frange: FileRange, source_file: SourceFile, should_compute_edit: bool, } -impl<'a, DB> Clone for AssistCtx<'a, DB> { +impl<'a> Clone for AssistCtx<'a> { fn clone(&self) -> Self { AssistCtx { db: self.db, @@ -68,7 +68,7 @@ impl<'a, DB> Clone for AssistCtx<'a, DB> { } } -impl<'a> AssistCtx<'a, RootDatabase> { +impl<'a> AssistCtx<'a> { pub(crate) fn with_ctx( db: &RootDatabase, frange: FileRange, @@ -76,7 +76,7 @@ impl<'a> AssistCtx<'a, RootDatabase> { f: F, ) -> T where - F: FnOnce(AssistCtx) -> T, + F: FnOnce(AssistCtx) -> T, { let parse = db.parse(frange.file_id); @@ -85,7 +85,7 @@ impl<'a> AssistCtx<'a, RootDatabase> { } } -impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { +impl<'a> AssistCtx<'a> { pub(crate) fn add_assist( self, id: AssistId, @@ -149,7 +149,7 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { pub(crate) fn covering_element(&self) -> SyntaxElement { find_covering_element(self.source_file.syntax(), self.frange.range) } - pub(crate) fn source_binder(&self) -> SourceBinder<'a, DB> { + pub(crate) fn source_binder(&self) -> SourceBinder<'a, RootDatabase> { SourceBinder::new(self.db) } pub(crate) fn source_analyzer( diff --git a/crates/ra_assists/src/assists/add_custom_impl.rs b/crates/ra_assists/src/assists/add_custom_impl.rs index f91034967..7fdd816bf 100644 --- a/crates/ra_assists/src/assists/add_custom_impl.rs +++ b/crates/ra_assists/src/assists/add_custom_impl.rs @@ -1,7 +1,7 @@ //! FIXME: write short doc here use crate::{Assist, AssistCtx, AssistId}; -use hir::db::HirDatabase; + use join_to_string::join; use ra_syntax::{ ast::{self, AstNode}, @@ -29,7 +29,7 @@ const DERIVE_TRAIT: &str = "derive"; // // } // ``` -pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option { +pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option { let input = ctx.find_node_at_offset::()?; let attr = input.syntax().parent().and_then(ast::Attr::cast)?; diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs index 6d9af3905..b0d1a0a80 100644 --- a/crates/ra_assists/src/assists/add_derive.rs +++ b/crates/ra_assists/src/assists/add_derive.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode, AttrsOwner}, SyntaxKind::{COMMENT, WHITESPACE}, @@ -25,7 +24,7 @@ use crate::{Assist, AssistCtx, AssistId}; // y: u32, // } // ``` -pub(crate) fn add_derive(ctx: AssistCtx) -> Option { +pub(crate) fn add_derive(ctx: AssistCtx) -> Option { let nominal = ctx.find_node_at_offset::()?; let node_start = derive_insertion_offset(&nominal)?; ctx.add_assist(AssistId("add_derive"), "Add `#[derive]`", |edit| { diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index 2443d5541..2cb9d2f48 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs @@ -1,4 +1,4 @@ -use hir::{db::HirDatabase, HirDisplay}; +use hir::HirDisplay; use ra_syntax::{ ast::{self, AstNode, LetStmt, NameOwner, TypeAscriptionOwner}, TextRange, @@ -21,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; // let x: i32 = 92; // } // ``` -pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option { +pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option { let stmt = ctx.find_node_at_offset::()?; let expr = stmt.initializer()?; let pat = stmt.pat()?; diff --git a/crates/ra_assists/src/assists/add_impl.rs b/crates/ra_assists/src/assists/add_impl.rs index 4b326c837..241b085fd 100644 --- a/crates/ra_assists/src/assists/add_impl.rs +++ b/crates/ra_assists/src/assists/add_impl.rs @@ -1,5 +1,5 @@ use format_buf::format; -use hir::db::HirDatabase; + use join_to_string::join; use ra_syntax::{ ast::{self, AstNode, NameOwner, TypeParamsOwner}, @@ -27,7 +27,7 @@ use crate::{Assist, AssistCtx, AssistId}; // // } // ``` -pub(crate) fn add_impl(ctx: AssistCtx) -> Option { +pub(crate) fn add_impl(ctx: AssistCtx) -> Option { let nominal = ctx.find_node_at_offset::()?; let name = nominal.name()?; ctx.add_assist(AssistId("add_impl"), format!("Implement {}", name.text().as_str()), |edit| { diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index fc038df78..f03dddac8 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs @@ -1,4 +1,4 @@ -use hir::{self, db::HirDatabase, ModPath}; +use hir::{self, ModPath}; use ra_syntax::{ ast::{self, NameOwner}, AstNode, Direction, SmolStr, @@ -50,7 +50,7 @@ pub fn auto_import_text_edit( // // fn process(map: HashMap) {} // ``` -pub(crate) fn add_import(ctx: AssistCtx) -> Option { +pub(crate) fn add_import(ctx: AssistCtx) -> Option { let path: ast::Path = ctx.find_node_at_offset()?; // We don't want to mess with use statements if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index e4c22ad55..448697d31 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -43,7 +43,7 @@ enum AddMissingImplMembersMode { // // } // ``` -pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option { +pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option { add_missing_impl_members_inner( ctx, AddMissingImplMembersMode::NoDefaultMethods, @@ -84,7 +84,7 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Opti // // } // ``` -pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option { +pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option { add_missing_impl_members_inner( ctx, AddMissingImplMembersMode::DefaultMethodsOnly, @@ -94,7 +94,7 @@ pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> O } fn add_missing_impl_members_inner( - ctx: AssistCtx, + ctx: AssistCtx, mode: AddMissingImplMembersMode, assist_id: &'static str, label: &'static str, diff --git a/crates/ra_assists/src/assists/add_new.rs b/crates/ra_assists/src/assists/add_new.rs index 8db63f762..a08639311 100644 --- a/crates/ra_assists/src/assists/add_new.rs +++ b/crates/ra_assists/src/assists/add_new.rs @@ -1,5 +1,5 @@ use format_buf::format; -use hir::{db::HirDatabase, InFile}; +use hir::InFile; use join_to_string::join; use ra_syntax::{ ast::{ @@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // // ``` -pub(crate) fn add_new(ctx: AssistCtx) -> Option { +pub(crate) fn add_new(ctx: AssistCtx) -> Option { let strukt = ctx.find_node_at_offset::()?; // We want to only apply this to non-union structs with named fields @@ -128,10 +128,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String { // // FIXME: change the new fn checking to a more semantic approach when that's more // viable (e.g. we process proc macros, etc) -fn find_struct_impl( - ctx: &AssistCtx, - strukt: &ast::StructDef, -) -> Option> { +fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option> { let db = ctx.db; let module = strukt.syntax().ancestors().find(|node| { ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind()) diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/assists/apply_demorgan.rs index 666dce4e6..dac6280ad 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs @@ -1,5 +1,4 @@ use super::invert_if::invert_boolean_expression; -use hir::db::HirDatabase; use ra_syntax::ast::{self, AstNode}; use crate::{Assist, AssistCtx, AssistId}; @@ -23,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; // if !(x == 4 && y) {} // } // ``` -pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option { +pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option { let expr = ctx.find_node_at_offset::()?; let op = expr.op_kind()?; let op_range = expr.op_token()?.text_range(); diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 2068256b0..48ab336b1 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -1,4 +1,4 @@ -use hir::{db::HirDatabase, ModPath}; +use hir::ModPath; use ra_syntax::{ ast::{self, AstNode}, SyntaxNode, @@ -27,7 +27,7 @@ use crate::{ // } // ``` pub(crate) fn auto_import( - ctx: AssistCtx, + ctx: AssistCtx, imports_locator: &mut F, ) -> Option { let path_to_import: ast::Path = ctx.find_node_at_offset()?; diff --git a/crates/ra_assists/src/assists/change_visibility.rs b/crates/ra_assists/src/assists/change_visibility.rs index fd766bb46..f325b6f92 100644 --- a/crates/ra_assists/src/assists/change_visibility.rs +++ b/crates/ra_assists/src/assists/change_visibility.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, NameOwner, VisibilityOwner}, AstNode, @@ -22,14 +21,14 @@ use crate::{Assist, AssistCtx, AssistId}; // ``` // pub(crate) fn frobnicate() {} // ``` -pub(crate) fn change_visibility(ctx: AssistCtx) -> Option { +pub(crate) fn change_visibility(ctx: AssistCtx) -> Option { if let Some(vis) = ctx.find_node_at_offset::() { return change_vis(ctx, vis); } add_vis(ctx) } -fn add_vis(ctx: AssistCtx) -> Option { +fn add_vis(ctx: AssistCtx) -> Option { let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, _ => false, @@ -75,7 +74,7 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { .unwrap_or_else(|| node.text_range().start()) } -fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option { +fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option { if vis.syntax().text() == "pub" { return ctx.add_assist( AssistId("change_visibility"), diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index 487ee9eef..7d510b055 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs @@ -1,6 +1,5 @@ use std::{iter::once, ops::RangeInclusive}; -use hir::db::HirDatabase; use ra_syntax::{ algo::replace_children, ast::{self, edit::IndentLevel, make, Block, Pat::TupleStructPat}, @@ -36,7 +35,7 @@ use crate::{ // bar(); // } // ``` -pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option { +pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option { let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; if if_expr.else_branch().is_some() { return None; diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs index 01758d23a..0908fc246 100644 --- a/crates/ra_assists/src/assists/fill_match_arms.rs +++ b/crates/ra_assists/src/assists/fill_match_arms.rs @@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // } // ``` -pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option { +pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option { let match_expr = ctx.find_node_at_offset::()?; let match_arm_list = match_expr.match_arm_list()?; diff --git a/crates/ra_assists/src/assists/flip_binexpr.rs b/crates/ra_assists/src/assists/flip_binexpr.rs index 2074087cd..bfcc09e90 100644 --- a/crates/ra_assists/src/assists/flip_binexpr.rs +++ b/crates/ra_assists/src/assists/flip_binexpr.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::ast::{AstNode, BinExpr, BinOp}; use crate::{Assist, AssistCtx, AssistId}; @@ -18,7 +17,7 @@ use crate::{Assist, AssistCtx, AssistId}; // let _ = 2 + 90; // } // ``` -pub(crate) fn flip_binexpr(ctx: AssistCtx) -> Option { +pub(crate) fn flip_binexpr(ctx: AssistCtx) -> Option { let expr = ctx.find_node_at_offset::()?; let lhs = expr.lhs()?.syntax().clone(); let rhs = expr.rhs()?.syntax().clone(); diff --git a/crates/ra_assists/src/assists/flip_comma.rs b/crates/ra_assists/src/assists/flip_comma.rs index dd0c405ed..1dacf29f8 100644 --- a/crates/ra_assists/src/assists/flip_comma.rs +++ b/crates/ra_assists/src/assists/flip_comma.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{algo::non_trivia_sibling, Direction, T}; use crate::{Assist, AssistCtx, AssistId}; @@ -18,7 +17,7 @@ use crate::{Assist, AssistCtx, AssistId}; // ((3, 4), (1, 2)); // } // ``` -pub(crate) fn flip_comma(ctx: AssistCtx) -> Option { +pub(crate) fn flip_comma(ctx: AssistCtx) -> Option { let comma = ctx.find_token_at_offset(T![,])?; let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs index 50b3fa492..f56769624 100644 --- a/crates/ra_assists/src/assists/flip_trait_bound.rs +++ b/crates/ra_assists/src/assists/flip_trait_bound.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ algo::non_trivia_sibling, ast::{self, AstNode}, @@ -18,7 +17,7 @@ use crate::{Assist, AssistCtx, AssistId}; // ``` // fn foo() { } // ``` -pub(crate) fn flip_trait_bound(ctx: AssistCtx) -> Option { +pub(crate) fn flip_trait_bound(ctx: AssistCtx) -> Option { // We want to replicate the behavior of `flip_binexpr` by only suggesting // the assist when the cursor is on a `+` let plus = ctx.find_token_at_offset(T![+])?; diff --git a/crates/ra_assists/src/assists/inline_local_variable.rs b/crates/ra_assists/src/assists/inline_local_variable.rs index 83527d904..91b588243 100644 --- a/crates/ra_assists/src/assists/inline_local_variable.rs +++ b/crates/ra_assists/src/assists/inline_local_variable.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode, AstToken}, TextRange, @@ -23,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; // (1 + 2) * 4; // } // ``` -pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { +pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option { let let_stmt = ctx.find_node_at_offset::()?; let bind_pat = match let_stmt.pat()? { ast::Pat::BindPat(pat) => pat, diff --git a/crates/ra_assists/src/assists/introduce_variable.rs b/crates/ra_assists/src/assists/introduce_variable.rs index 19e211e0f..7312ce687 100644 --- a/crates/ra_assists/src/assists/introduce_variable.rs +++ b/crates/ra_assists/src/assists/introduce_variable.rs @@ -1,5 +1,4 @@ use format_buf::format; -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, SyntaxKind::{ @@ -28,7 +27,7 @@ use crate::{Assist, AssistCtx, AssistId}; // var_name * 4; // } // ``` -pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option { +pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option { if ctx.frange.range.is_empty() { return None; } diff --git a/crates/ra_assists/src/assists/invert_if.rs b/crates/ra_assists/src/assists/invert_if.rs index 16352c040..694c3642c 100644 --- a/crates/ra_assists/src/assists/invert_if.rs +++ b/crates/ra_assists/src/assists/invert_if.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::ast::{self, AstNode}; use ra_syntax::T; @@ -23,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // ``` -pub(crate) fn invert_if(ctx: AssistCtx) -> Option { +pub(crate) fn invert_if(ctx: AssistCtx) -> Option { let if_keyword = ctx.find_token_at_offset(T![if])?; let expr = ast::IfExpr::cast(if_keyword.parent())?; let if_range = if_keyword.text_range(); diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs index 64c9379da..670614dd8 100644 --- a/crates/ra_assists/src/assists/merge_match_arms.rs +++ b/crates/ra_assists/src/assists/merge_match_arms.rs @@ -1,6 +1,5 @@ use std::iter::successors; -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, Direction, TextUnit, @@ -32,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId, TextRange}; // } // } // ``` -pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option { +pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option { let current_arm = ctx.find_node_at_offset::()?; // Don't try to handle arms with guards for now - can add support for this later if current_arm.guard().is_some() { diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index 355adddc3..90793b5fc 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, SyntaxElement, @@ -22,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; // f(x) // } // ``` -pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option { +pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option { let type_param_list = ctx.find_node_at_offset::()?; let mut type_params = type_param_list.type_params(); diff --git a/crates/ra_assists/src/assists/move_guard.rs b/crates/ra_assists/src/assists/move_guard.rs index 41a31e677..2b91ce7c4 100644 --- a/crates/ra_assists/src/assists/move_guard.rs +++ b/crates/ra_assists/src/assists/move_guard.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast, ast::{AstNode, AstToken, IfExpr, MatchArm}, @@ -32,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // } // ``` -pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { +pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { let match_arm = ctx.find_node_at_offset::()?; let guard = match_arm.guard()?; let space_before_guard = guard.syntax().prev_sibling_or_token(); @@ -89,7 +88,7 @@ pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option // } // } // ``` -pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { +pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { let match_arm: MatchArm = ctx.find_node_at_offset::()?; let last_match_pat = match_arm.pats().last()?; diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index e79c51673..2c0a1e126 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast, AstToken, SyntaxKind::{RAW_STRING, STRING}, @@ -22,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; // r#"Hello, World!"#; // } // ``` -pub(crate) fn make_raw_string(ctx: AssistCtx) -> Option { +pub(crate) fn make_raw_string(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?; let value = token.value()?; ctx.add_assist(AssistId("make_raw_string"), "Rewrite as raw string", |edit| { @@ -51,7 +50,7 @@ pub(crate) fn make_raw_string(ctx: AssistCtx) -> Option) -> Option { +pub(crate) fn make_usual_string(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?; let value = token.value()?; ctx.add_assist(AssistId("make_usual_string"), "Rewrite as regular string", |edit| { @@ -77,7 +76,7 @@ pub(crate) fn make_usual_string(ctx: AssistCtx) -> Option) -> Option { +pub(crate) fn add_hash(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; ctx.add_assist(AssistId("add_hash"), "Add # to raw string", |edit| { edit.target(token.text_range()); @@ -101,7 +100,7 @@ pub(crate) fn add_hash(ctx: AssistCtx) -> Option { // r"Hello, World!"; // } // ``` -pub(crate) fn remove_hash(ctx: AssistCtx) -> Option { +pub(crate) fn remove_hash(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; let text = token.text().as_str(); if text.starts_with("r\"") { diff --git a/crates/ra_assists/src/assists/remove_dbg.rs b/crates/ra_assists/src/assists/remove_dbg.rs index cf211ab84..5085649b4 100644 --- a/crates/ra_assists/src/assists/remove_dbg.rs +++ b/crates/ra_assists/src/assists/remove_dbg.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_syntax::{ ast::{self, AstNode}, TextUnit, T, @@ -21,7 +20,7 @@ use crate::{Assist, AssistCtx, AssistId}; // 92; // } // ``` -pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option { +pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option { let macro_call = ctx.find_node_at_offset::()?; if !is_valid_macrocall(¯o_call, "dbg")? { diff --git a/crates/ra_assists/src/assists/replace_if_let_with_match.rs b/crates/ra_assists/src/assists/replace_if_let_with_match.rs index c8b13b7b3..e6cd50bc1 100644 --- a/crates/ra_assists/src/assists/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/assists/replace_if_let_with_match.rs @@ -1,4 +1,3 @@ -use hir::db::HirDatabase; use ra_fmt::unwrap_trivial_block; use ra_syntax::{ ast::{self, make}, @@ -34,7 +33,7 @@ use ast::edit::IndentLevel; // } // } // ``` -pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option { +pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option { let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; let cond = if_expr.condition()?; let pat = cond.pat()?; diff --git a/crates/ra_assists/src/assists/split_import.rs b/crates/ra_assists/src/assists/split_import.rs index 6038c4858..2c3f07a79 100644 --- a/crates/ra_assists/src/assists/split_import.rs +++ b/crates/ra_assists/src/assists/split_import.rs @@ -1,6 +1,5 @@ use std::iter::successors; -use hir::db::HirDatabase; use ra_syntax::{ast, AstNode, TextUnit, T}; use crate::{Assist, AssistCtx, AssistId}; @@ -16,7 +15,7 @@ use crate::{Assist, AssistCtx, AssistId}; // ``` // use std::{collections::HashMap}; // ``` -pub(crate) fn split_import(ctx: AssistCtx) -> Option { +pub(crate) fn split_import(ctx: AssistCtx) -> Option { let colon_colon = ctx.find_token_at_offset(T![::])?; let path = ast::Path::cast(colon_colon.parent())?; let top_path = successors(Some(path), |it| it.parent_path()).last()?; diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index be6e06842..ad8438b6c 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -148,7 +148,6 @@ fn sort_assists(assists: &mut Vec) { mod assists { use crate::{Assist, AssistCtx, ImportsLocator}; - use hir::db::HirDatabase; mod add_derive; mod add_explicit_type; @@ -176,7 +175,7 @@ mod assists { mod move_bounds; mod early_return; - pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option] { + pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option] { &[ add_derive::add_derive, add_explicit_type::add_explicit_type, @@ -210,8 +209,8 @@ mod assists { ] } - pub(crate) fn all_with_imports_locator<'a, DB: HirDatabase, F: ImportsLocator>( - ) -> &'a [fn(AssistCtx, &mut F) -> Option] { + pub(crate) fn all_with_imports_locator<'a, F: ImportsLocator>( + ) -> &'a [fn(AssistCtx, &mut F) -> Option] { &[auto_import::auto_import] } } @@ -274,11 +273,7 @@ mod helpers { } } - pub(crate) fn check_assist( - assist: fn(AssistCtx) -> Option, - before: &str, - after: &str, - ) { + pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option, before: &str, after: &str) { let (before_cursor_pos, before) = extract_offset(before); let (db, file_id) = RootDatabase::with_single_file(&before); let frange = @@ -303,7 +298,7 @@ mod helpers { } pub(crate) fn check_assist_with_imports_locator( - assist: fn(AssistCtx, &mut F) -> Option, + assist: fn(AssistCtx, &mut F) -> Option, imports_locator_provider: fn(db: Arc, file_id: FileId) -> F, before: &str, after: &str, @@ -335,7 +330,7 @@ mod helpers { } pub(crate) fn check_assist_range( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, after: &str, ) { @@ -357,7 +352,7 @@ mod helpers { } pub(crate) fn check_assist_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { @@ -377,7 +372,7 @@ mod helpers { } pub(crate) fn check_assist_range_target( - assist: fn(AssistCtx) -> Option, + assist: fn(AssistCtx) -> Option, before: &str, target: &str, ) { @@ -396,7 +391,7 @@ 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); @@ -408,7 +403,7 @@ mod helpers { } pub(crate) fn check_assist_with_imports_locator_not_applicable( - assist: fn(AssistCtx, &mut F) -> Option, + assist: fn(AssistCtx, &mut F) -> Option, imports_locator_provider: fn(db: Arc, file_id: FileId) -> F, before: &str, ) { @@ -424,7 +419,7 @@ 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); -- cgit v1.2.3 From ad204f7562747150c4f570d7ce648f2539530b76 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 17:17:51 +0100 Subject: Mostly remove ImoportLocator infra --- crates/ra_assists/src/assists/auto_import.rs | 39 +++---- crates/ra_assists/src/lib.rs | 158 +-------------------------- 2 files changed, 20 insertions(+), 177 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 48ab336b1..219051063 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -6,8 +6,9 @@ use ra_syntax::{ use crate::{ assist_ctx::{ActionBuilder, Assist, AssistCtx}, - auto_import_text_edit, AssistId, ImportsLocator, + auto_import_text_edit, AssistId, }; +use ra_ide_db::imports_locator::ImportsLocatorIde; // Assist: auto_import // @@ -26,10 +27,7 @@ use crate::{ // let map = HashMap<|>::new(); // } // ``` -pub(crate) fn auto_import( - ctx: AssistCtx, - imports_locator: &mut F, -) -> Option { +pub(crate) fn auto_import(ctx: AssistCtx) -> Option { let path_to_import: ast::Path = ctx.find_node_at_offset()?; let path_to_import_syntax = path_to_import.syntax(); if path_to_import_syntax.ancestors().find_map(ast::UseItem::cast).is_some() { @@ -52,6 +50,8 @@ pub(crate) fn auto_import( return None; } + let mut imports_locator = ImportsLocatorIde::new(ctx.db); + let proposed_imports = imports_locator .find_imports(&name_to_import) .into_iter() @@ -81,16 +81,12 @@ fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode) #[cfg(test)] mod tests { use super::*; - use crate::helpers::{ - check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable, - TestImportsLocator, - }; + use crate::helpers::{check_assist, check_assist_not_applicable}; #[test] fn applicable_when_found_an_import() { - check_assist_with_imports_locator( + check_assist( auto_import, - TestImportsLocator::new, r" <|>PubStruct @@ -112,9 +108,8 @@ mod tests { #[test] fn auto_imports_are_merged() { - check_assist_with_imports_locator( + check_assist( auto_import, - TestImportsLocator::new, r" use PubMod::PubStruct1; @@ -148,9 +143,8 @@ mod tests { #[test] fn applicable_when_found_multiple_imports() { - check_assist_with_imports_locator( + check_assist( auto_import, - TestImportsLocator::new, r" PubSt<|>ruct @@ -184,9 +178,8 @@ mod tests { #[test] fn not_applicable_for_already_imported_types() { - check_assist_with_imports_locator_not_applicable( + check_assist_not_applicable( auto_import, - TestImportsLocator::new, r" use PubMod::PubStruct; @@ -201,9 +194,8 @@ mod tests { #[test] fn not_applicable_for_types_with_private_paths() { - check_assist_with_imports_locator_not_applicable( + check_assist_not_applicable( auto_import, - TestImportsLocator::new, r" PrivateStruct<|> @@ -216,9 +208,8 @@ mod tests { #[test] fn not_applicable_when_no_imports_found() { - check_assist_with_imports_locator_not_applicable( + check_assist_not_applicable( auto_import, - TestImportsLocator::new, " PubStruct<|>", ); @@ -226,9 +217,8 @@ mod tests { #[test] fn not_applicable_in_import_statements() { - check_assist_with_imports_locator_not_applicable( + check_assist_not_applicable( auto_import, - TestImportsLocator::new, r" use PubStruct<|>; @@ -240,9 +230,8 @@ mod tests { #[test] fn function_import() { - check_assist_with_imports_locator( + check_assist( auto_import, - TestImportsLocator::new, r" test_function<|> diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index ad8438b6c..8285e93a4 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -12,9 +12,8 @@ mod doc_tests; pub mod ast_transform; use either::Either; -use hir::ModuleDef; use ra_db::FileRange; -use ra_ide_db::{imports_locator::ImportsLocatorIde, RootDatabase}; +use ra_ide_db::RootDatabase; use ra_syntax::{TextRange, TextUnit}; use ra_text_edit::TextEdit; @@ -73,50 +72,6 @@ pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec Vec; -} - -impl ImportsLocator for ImportsLocatorIde<'_> { - fn find_imports(&mut self, name_to_import: &str) -> Vec { - self.find_imports(name_to_import) - } -} - -/// Return all the assists applicable at the given position -/// and additional assists that need the imports locator functionality to work. -/// -/// Assists are returned in the "resolved" state, that is with edit fully -/// computed. -pub fn assists_with_imports_locator(db: &RootDatabase, range: FileRange) -> Vec { - let mut imports_locator = ImportsLocatorIde::new(db); - AssistCtx::with_ctx(db, range, true, |ctx| { - let mut assists = assists::all() - .iter() - .map(|f| f(ctx.clone())) - .chain( - assists::all_with_imports_locator() - .iter() - .map(|f| f(ctx.clone(), &mut imports_locator)), - ) - .filter_map(std::convert::identity) - .map(|a| match a { - Assist::Resolved { assist } => assist, - Assist::Unresolved { .. } => unreachable!(), - }) - .collect(); - sort_assists(&mut assists); - assists - }) -} - /// Return all the assists applicable at the given position. /// /// Assists are returned in the "resolved" state, that is with edit fully @@ -147,7 +102,7 @@ fn sort_assists(assists: &mut Vec) { } mod assists { - use crate::{Assist, AssistCtx, ImportsLocator}; + use crate::{Assist, AssistCtx}; mod add_derive; mod add_explicit_type; @@ -206,72 +161,19 @@ mod assists { raw_string::make_usual_string, raw_string::remove_hash, early_return::convert_to_guarded_return, + auto_import::auto_import, ] } - - pub(crate) fn all_with_imports_locator<'a, F: ImportsLocator>( - ) -> &'a [fn(AssistCtx, &mut F) -> Option] { - &[auto_import::auto_import] - } } #[cfg(test)] mod helpers { - use hir::db::DefDatabase; - use ra_db::{fixture::WithFixture, FileId, FileRange}; + use ra_db::{fixture::WithFixture, FileRange}; + use ra_ide_db::RootDatabase; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; - 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, - test_file_id: FileId, - } - - impl TestImportsLocator { - pub(crate) fn new(db: Arc, test_file_id: FileId) -> Self { - TestImportsLocator { db, test_file_id } - } - } - - impl ImportsLocator for TestImportsLocator { - fn find_imports(&mut self, name_to_import: &str) -> Vec { - let crate_def_map = self.db.crate_def_map(self.db.test_crate()); - let mut findings = Vec::new(); - - let mut module_ids_to_process = - crate_def_map.modules_for_file(self.test_file_id).collect::>(); - - while !module_ids_to_process.is_empty() { - let mut more_ids_to_process = Vec::new(); - for local_module_id in module_ids_to_process.drain(..) { - for (name, namespace_data) in - crate_def_map[local_module_id].scope.entries_without_primitives() - { - let found_a_match = &name.to_string() == name_to_import; - vec![namespace_data.types, namespace_data.values] - .into_iter() - .filter_map(std::convert::identity) - .for_each(|(module_def_id, _)| { - if found_a_match { - findings.push(module_def_id.into()); - } - if let hir::ModuleDefId::ModuleId(module_id) = module_def_id { - more_ids_to_process.push(module_id.local_id); - } - }); - } - } - module_ids_to_process = more_ids_to_process; - } - - findings - } - } + use crate::{Assist, AssistCtx}; pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option, before: &str, after: &str) { let (before_cursor_pos, before) = extract_offset(before); @@ -297,38 +199,6 @@ mod helpers { assert_eq_text!(after, &actual); } - pub(crate) fn check_assist_with_imports_locator( - 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) = RootDatabase::with_single_file(&before); - let db = Arc::new(db); - let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); - let frange = - FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; - let assist = - AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator)) - .expect("code action is not applicable"); - let action = match assist { - Assist::Unresolved { .. } => unreachable!(), - Assist::Resolved { assist } => assist.get_first_action(), - }; - - let actual = action.edit.apply(&before); - let actual_cursor_pos = match action.cursor_position { - None => action - .edit - .apply_to_offset(before_cursor_pos) - .expect("cursor position is affected by the edit"), - Some(off) => off, - }; - let actual = add_cursor(&actual, actual_cursor_pos); - assert_eq_text!(after, &actual); - } - pub(crate) fn check_assist_range( assist: fn(AssistCtx) -> Option, before: &str, @@ -402,22 +272,6 @@ mod helpers { assert!(assist.is_none()); } - 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, - before: &str, - ) { - let (before_cursor_pos, before) = extract_offset(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 = - FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; - let assist = - AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator)); - assert!(assist.is_none()); - } - pub(crate) fn check_assist_range_not_applicable( assist: fn(AssistCtx) -> Option, before: &str, -- cgit v1.2.3 From d1e8b8d134da802eecef5cbcd5486bd542ad75b5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 6 Feb 2020 17:42:17 +0100 Subject: Fix tests --- crates/ra_assists/src/lib.rs | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'crates/ra_assists') 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 { #[cfg(test)] mod helpers { - use ra_db::{fixture::WithFixture, FileRange}; - use ra_ide_db::RootDatabase; + use std::sync::Arc; + + use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; + use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; use ra_syntax::TextRange; use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; use crate::{Assist, AssistCtx}; + pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { + let (mut db, file_id) = RootDatabase::with_single_file(text); + // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, + // but it looks like this might need specialization? :( + let local_roots = vec![db.file_source_root(file_id)]; + db.set_local_roots(Arc::new(local_roots)); + (db, file_id) + } + pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option, before: &str, after: &str) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -205,7 +216,7 @@ mod helpers { after: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = 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"); @@ -227,7 +238,7 @@ mod helpers { target: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assist = @@ -247,7 +258,7 @@ mod helpers { target: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = 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"); @@ -265,7 +276,7 @@ mod helpers { before: &str, ) { let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = 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); @@ -277,7 +288,7 @@ mod helpers { before: &str, ) { let (range, before) = extract_range(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = with_single_file(&before); let frange = FileRange { file_id, range }; let assist = AssistCtx::with_ctx(&db, frange, true, assist); assert!(assist.is_none()); @@ -286,17 +297,17 @@ mod helpers { #[cfg(test)] mod tests { - use ra_db::{fixture::WithFixture, FileRange}; + use ra_db::FileRange; use ra_syntax::TextRange; use test_utils::{extract_offset, extract_range}; - use ra_ide_db::RootDatabase; + use crate::helpers; #[test] fn assist_order_field_struct() { let before = "struct Foo { <|>bar: u32 }"; let (before_cursor_pos, before) = extract_offset(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = helpers::with_single_file(&before); let frange = FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; let assists = super::assists(&db, frange); @@ -320,7 +331,7 @@ mod tests { } }"; let (range, before) = extract_range(before); - let (db, file_id) = RootDatabase::with_single_file(&before); + let (db, file_id) = helpers::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