diff options
Diffstat (limited to 'crates/ra_assists/src')
29 files changed, 106 insertions, 321 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index 2ab65ab99..f32072dbd 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::{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, |
@@ -49,14 +50,14 @@ pub(crate) enum Assist { | |||
49 | /// moment, because the LSP API is pretty awkward in this place, and it's much | 50 | /// moment, because the LSP API is pretty awkward in this place, and it's much |
50 | /// easier to just compute the edit eagerly :-) | 51 | /// easier to just compute the edit eagerly :-) |
51 | #[derive(Debug)] | 52 | #[derive(Debug)] |
52 | pub(crate) struct AssistCtx<'a, DB> { | 53 | pub(crate) struct AssistCtx<'a> { |
53 | pub(crate) db: &'a DB, | 54 | pub(crate) db: &'a RootDatabase, |
54 | pub(crate) frange: FileRange, | 55 | pub(crate) frange: FileRange, |
55 | source_file: SourceFile, | 56 | source_file: SourceFile, |
56 | should_compute_edit: bool, | 57 | should_compute_edit: bool, |
57 | } | 58 | } |
58 | 59 | ||
59 | impl<'a, DB> Clone for AssistCtx<'a, DB> { | 60 | impl<'a> Clone for AssistCtx<'a> { |
60 | fn clone(&self) -> Self { | 61 | fn clone(&self) -> Self { |
61 | AssistCtx { | 62 | AssistCtx { |
62 | db: self.db, | 63 | db: self.db, |
@@ -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> { |
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) -> 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> AssistCtx<'a> { | ||
81 | pub(crate) fn add_assist( | 89 | pub(crate) fn add_assist( |
82 | self, | 90 | self, |
83 | id: AssistId, | 91 | id: AssistId, |
@@ -141,7 +149,7 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | |||
141 | pub(crate) fn covering_element(&self) -> SyntaxElement { | 149 | pub(crate) fn covering_element(&self) -> SyntaxElement { |
142 | find_covering_element(self.source_file.syntax(), self.frange.range) | 150 | find_covering_element(self.source_file.syntax(), self.frange.range) |
143 | } | 151 | } |
144 | pub(crate) fn source_binder(&self) -> SourceBinder<'a, DB> { | 152 | pub(crate) fn source_binder(&self) -> SourceBinder<'a, RootDatabase> { |
145 | SourceBinder::new(self.db) | 153 | SourceBinder::new(self.db) |
146 | } | 154 | } |
147 | pub(crate) fn source_analyzer( | 155 | 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 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use crate::{Assist, AssistCtx, AssistId}; | 3 | use crate::{Assist, AssistCtx, AssistId}; |
4 | use hir::db::HirDatabase; | 4 | |
5 | use join_to_string::join; | 5 | use join_to_string::join; |
6 | use ra_syntax::{ | 6 | use ra_syntax::{ |
7 | ast::{self, AstNode}, | 7 | ast::{self, AstNode}, |
@@ -29,7 +29,7 @@ const DERIVE_TRAIT: &str = "derive"; | |||
29 | // | 29 | // |
30 | // } | 30 | // } |
31 | // ``` | 31 | // ``` |
32 | pub(crate) fn add_custom_impl(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 32 | pub(crate) fn add_custom_impl(ctx: AssistCtx) -> Option<Assist> { |
33 | let input = ctx.find_node_at_offset::<ast::AttrInput>()?; | 33 | let input = ctx.find_node_at_offset::<ast::AttrInput>()?; |
34 | let attr = input.syntax().parent().and_then(ast::Attr::cast)?; | 34 | let attr = input.syntax().parent().and_then(ast::Attr::cast)?; |
35 | 35 | ||
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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast::{self, AstNode, AttrsOwner}, | 2 | ast::{self, AstNode, AttrsOwner}, |
4 | SyntaxKind::{COMMENT, WHITESPACE}, | 3 | SyntaxKind::{COMMENT, WHITESPACE}, |
@@ -25,7 +24,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
25 | // y: u32, | 24 | // y: u32, |
26 | // } | 25 | // } |
27 | // ``` | 26 | // ``` |
28 | pub(crate) fn add_derive(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 27 | pub(crate) fn add_derive(ctx: AssistCtx) -> Option<Assist> { |
29 | let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; | 28 | let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; |
30 | let node_start = derive_insertion_offset(&nominal)?; | 29 | let node_start = derive_insertion_offset(&nominal)?; |
31 | ctx.add_assist(AssistId("add_derive"), "Add `#[derive]`", |edit| { | 30 | 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 @@ | |||
1 | use hir::{db::HirDatabase, HirDisplay}; | 1 | use hir::HirDisplay; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode, LetStmt, NameOwner, TypeAscriptionOwner}, | 3 | ast::{self, AstNode, LetStmt, NameOwner, TypeAscriptionOwner}, |
4 | TextRange, | 4 | TextRange, |
@@ -21,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
21 | // let x: i32 = 92; | 21 | // let x: i32 = 92; |
22 | // } | 22 | // } |
23 | // ``` | 23 | // ``` |
24 | pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option<Assist> { |
25 | let stmt = ctx.find_node_at_offset::<LetStmt>()?; | 25 | let stmt = ctx.find_node_at_offset::<LetStmt>()?; |
26 | let expr = stmt.initializer()?; | 26 | let expr = stmt.initializer()?; |
27 | let pat = stmt.pat()?; | 27 | 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 @@ | |||
1 | use format_buf::format; | 1 | use format_buf::format; |
2 | use hir::db::HirDatabase; | 2 | |
3 | use join_to_string::join; | 3 | use join_to_string::join; |
4 | use ra_syntax::{ | 4 | use ra_syntax::{ |
5 | ast::{self, AstNode, NameOwner, TypeParamsOwner}, | 5 | ast::{self, AstNode, NameOwner, TypeParamsOwner}, |
@@ -27,7 +27,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
27 | // | 27 | // |
28 | // } | 28 | // } |
29 | // ``` | 29 | // ``` |
30 | pub(crate) fn add_impl(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 30 | pub(crate) fn add_impl(ctx: AssistCtx) -> Option<Assist> { |
31 | let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; | 31 | let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; |
32 | let name = nominal.name()?; | 32 | let name = nominal.name()?; |
33 | ctx.add_assist(AssistId("add_impl"), format!("Implement {}", name.text().as_str()), |edit| { | 33 | 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 @@ | |||
1 | use hir::{self, db::HirDatabase, ModPath}; | 1 | use hir::{self, ModPath}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, NameOwner}, | 3 | ast::{self, NameOwner}, |
4 | AstNode, Direction, SmolStr, | 4 | AstNode, Direction, SmolStr, |
@@ -50,7 +50,7 @@ pub fn auto_import_text_edit( | |||
50 | // | 50 | // |
51 | // fn process(map: HashMap<String, String>) {} | 51 | // fn process(map: HashMap<String, String>) {} |
52 | // ``` | 52 | // ``` |
53 | pub(crate) fn add_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 53 | pub(crate) fn add_import(ctx: AssistCtx) -> Option<Assist> { |
54 | let path: ast::Path = ctx.find_node_at_offset()?; | 54 | let path: ast::Path = ctx.find_node_at_offset()?; |
55 | // We don't want to mess with use statements | 55 | // We don't want to mess with use statements |
56 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { | 56 | 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 { | |||
43 | // | 43 | // |
44 | // } | 44 | // } |
45 | // ``` | 45 | // ``` |
46 | pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 46 | pub(crate) fn add_missing_impl_members(ctx: AssistCtx) -> Option<Assist> { |
47 | add_missing_impl_members_inner( | 47 | add_missing_impl_members_inner( |
48 | ctx, | 48 | ctx, |
49 | AddMissingImplMembersMode::NoDefaultMethods, | 49 | AddMissingImplMembersMode::NoDefaultMethods, |
@@ -84,7 +84,7 @@ pub(crate) fn add_missing_impl_members(ctx: AssistCtx<impl HirDatabase>) -> Opti | |||
84 | // | 84 | // |
85 | // } | 85 | // } |
86 | // ``` | 86 | // ``` |
87 | pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 87 | pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> Option<Assist> { |
88 | add_missing_impl_members_inner( | 88 | add_missing_impl_members_inner( |
89 | ctx, | 89 | ctx, |
90 | AddMissingImplMembersMode::DefaultMethodsOnly, | 90 | AddMissingImplMembersMode::DefaultMethodsOnly, |
@@ -94,7 +94,7 @@ pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> O | |||
94 | } | 94 | } |
95 | 95 | ||
96 | fn add_missing_impl_members_inner( | 96 | fn add_missing_impl_members_inner( |
97 | ctx: AssistCtx<impl HirDatabase>, | 97 | ctx: AssistCtx, |
98 | mode: AddMissingImplMembersMode, | 98 | mode: AddMissingImplMembersMode, |
99 | assist_id: &'static str, | 99 | assist_id: &'static str, |
100 | label: &'static str, | 100 | 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 @@ | |||
1 | use format_buf::format; | 1 | use format_buf::format; |
2 | use hir::{db::HirDatabase, InFile}; | 2 | use hir::InFile; |
3 | use join_to_string::join; | 3 | use join_to_string::join; |
4 | use ra_syntax::{ | 4 | use ra_syntax::{ |
5 | ast::{ | 5 | ast::{ |
@@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
31 | // } | 31 | // } |
32 | // | 32 | // |
33 | // ``` | 33 | // ``` |
34 | pub(crate) fn add_new(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 34 | pub(crate) fn add_new(ctx: AssistCtx) -> Option<Assist> { |
35 | let strukt = ctx.find_node_at_offset::<ast::StructDef>()?; | 35 | let strukt = ctx.find_node_at_offset::<ast::StructDef>()?; |
36 | 36 | ||
37 | // We want to only apply this to non-union structs with named fields | 37 | // 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 { | |||
128 | // | 128 | // |
129 | // FIXME: change the new fn checking to a more semantic approach when that's more | 129 | // FIXME: change the new fn checking to a more semantic approach when that's more |
130 | // viable (e.g. we process proc macros, etc) | 130 | // viable (e.g. we process proc macros, etc) |
131 | fn find_struct_impl( | 131 | fn find_struct_impl(ctx: &AssistCtx, strukt: &ast::StructDef) -> Option<Option<ast::ImplBlock>> { |
132 | ctx: &AssistCtx<impl HirDatabase>, | ||
133 | strukt: &ast::StructDef, | ||
134 | ) -> Option<Option<ast::ImplBlock>> { | ||
135 | let db = ctx.db; | 132 | let db = ctx.db; |
136 | let module = strukt.syntax().ancestors().find(|node| { | 133 | let module = strukt.syntax().ancestors().find(|node| { |
137 | ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind()) | 134 | 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 @@ | |||
1 | use super::invert_if::invert_boolean_expression; | 1 | use super::invert_if::invert_boolean_expression; |
2 | use hir::db::HirDatabase; | ||
3 | use ra_syntax::ast::{self, AstNode}; | 2 | use ra_syntax::ast::{self, AstNode}; |
4 | 3 | ||
5 | use crate::{Assist, AssistCtx, AssistId}; | 4 | use crate::{Assist, AssistCtx, AssistId}; |
@@ -23,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
23 | // if !(x == 4 && y) {} | 22 | // if !(x == 4 && y) {} |
24 | // } | 23 | // } |
25 | // ``` | 24 | // ``` |
26 | pub(crate) fn apply_demorgan(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option<Assist> { |
27 | let expr = ctx.find_node_at_offset::<ast::BinExpr>()?; | 26 | let expr = ctx.find_node_at_offset::<ast::BinExpr>()?; |
28 | let op = expr.op_kind()?; | 27 | let op = expr.op_kind()?; |
29 | let op_range = expr.op_token()?.text_range(); | 28 | 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..219051063 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use hir::{db::HirDatabase, ModPath}; | 1 | use hir::ModPath; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, AstNode}, | 3 | ast::{self, AstNode}, |
4 | SyntaxNode, | 4 | SyntaxNode, |
@@ -6,8 +6,9 @@ use ra_syntax::{ | |||
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | assist_ctx::{ActionBuilder, Assist, AssistCtx}, | 8 | assist_ctx::{ActionBuilder, Assist, AssistCtx}, |
9 | auto_import_text_edit, AssistId, ImportsLocator, | 9 | auto_import_text_edit, AssistId, |
10 | }; | 10 | }; |
11 | use ra_ide_db::imports_locator::ImportsLocatorIde; | ||
11 | 12 | ||
12 | // Assist: auto_import | 13 | // Assist: auto_import |
13 | // | 14 | // |
@@ -26,10 +27,7 @@ use crate::{ | |||
26 | // let map = HashMap<|>::new(); | 27 | // let map = HashMap<|>::new(); |
27 | // } | 28 | // } |
28 | // ``` | 29 | // ``` |
29 | pub(crate) fn auto_import<F: ImportsLocator>( | 30 | pub(crate) fn auto_import(ctx: AssistCtx) -> Option<Assist> { |
30 | ctx: AssistCtx<impl HirDatabase>, | ||
31 | imports_locator: &mut F, | ||
32 | ) -> Option<Assist> { | ||
33 | let path_to_import: ast::Path = ctx.find_node_at_offset()?; | 31 | let path_to_import: ast::Path = ctx.find_node_at_offset()?; |
34 | let path_to_import_syntax = path_to_import.syntax(); | 32 | let path_to_import_syntax = path_to_import.syntax(); |
35 | if path_to_import_syntax.ancestors().find_map(ast::UseItem::cast).is_some() { | 33 | if path_to_import_syntax.ancestors().find_map(ast::UseItem::cast).is_some() { |
@@ -52,6 +50,8 @@ pub(crate) fn auto_import<F: ImportsLocator>( | |||
52 | return None; | 50 | return None; |
53 | } | 51 | } |
54 | 52 | ||
53 | let mut imports_locator = ImportsLocatorIde::new(ctx.db); | ||
54 | |||
55 | let proposed_imports = imports_locator | 55 | let proposed_imports = imports_locator |
56 | .find_imports(&name_to_import) | 56 | .find_imports(&name_to_import) |
57 | .into_iter() | 57 | .into_iter() |
@@ -81,16 +81,12 @@ fn import_to_action(import: ModPath, position: &SyntaxNode, anchor: &SyntaxNode) | |||
81 | #[cfg(test)] | 81 | #[cfg(test)] |
82 | mod tests { | 82 | mod tests { |
83 | use super::*; | 83 | use super::*; |
84 | use crate::helpers::{ | 84 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
85 | check_assist_with_imports_locator, check_assist_with_imports_locator_not_applicable, | ||
86 | TestImportsLocator, | ||
87 | }; | ||
88 | 85 | ||
89 | #[test] | 86 | #[test] |
90 | fn applicable_when_found_an_import() { | 87 | fn applicable_when_found_an_import() { |
91 | check_assist_with_imports_locator( | 88 | check_assist( |
92 | auto_import, | 89 | auto_import, |
93 | TestImportsLocator::new, | ||
94 | r" | 90 | r" |
95 | <|>PubStruct | 91 | <|>PubStruct |
96 | 92 | ||
@@ -112,9 +108,8 @@ mod tests { | |||
112 | 108 | ||
113 | #[test] | 109 | #[test] |
114 | fn auto_imports_are_merged() { | 110 | fn auto_imports_are_merged() { |
115 | check_assist_with_imports_locator( | 111 | check_assist( |
116 | auto_import, | 112 | auto_import, |
117 | TestImportsLocator::new, | ||
118 | r" | 113 | r" |
119 | use PubMod::PubStruct1; | 114 | use PubMod::PubStruct1; |
120 | 115 | ||
@@ -148,9 +143,8 @@ mod tests { | |||
148 | 143 | ||
149 | #[test] | 144 | #[test] |
150 | fn applicable_when_found_multiple_imports() { | 145 | fn applicable_when_found_multiple_imports() { |
151 | check_assist_with_imports_locator( | 146 | check_assist( |
152 | auto_import, | 147 | auto_import, |
153 | TestImportsLocator::new, | ||
154 | r" | 148 | r" |
155 | PubSt<|>ruct | 149 | PubSt<|>ruct |
156 | 150 | ||
@@ -184,9 +178,8 @@ mod tests { | |||
184 | 178 | ||
185 | #[test] | 179 | #[test] |
186 | fn not_applicable_for_already_imported_types() { | 180 | fn not_applicable_for_already_imported_types() { |
187 | check_assist_with_imports_locator_not_applicable( | 181 | check_assist_not_applicable( |
188 | auto_import, | 182 | auto_import, |
189 | TestImportsLocator::new, | ||
190 | r" | 183 | r" |
191 | use PubMod::PubStruct; | 184 | use PubMod::PubStruct; |
192 | 185 | ||
@@ -201,9 +194,8 @@ mod tests { | |||
201 | 194 | ||
202 | #[test] | 195 | #[test] |
203 | fn not_applicable_for_types_with_private_paths() { | 196 | fn not_applicable_for_types_with_private_paths() { |
204 | check_assist_with_imports_locator_not_applicable( | 197 | check_assist_not_applicable( |
205 | auto_import, | 198 | auto_import, |
206 | TestImportsLocator::new, | ||
207 | r" | 199 | r" |
208 | PrivateStruct<|> | 200 | PrivateStruct<|> |
209 | 201 | ||
@@ -216,9 +208,8 @@ mod tests { | |||
216 | 208 | ||
217 | #[test] | 209 | #[test] |
218 | fn not_applicable_when_no_imports_found() { | 210 | fn not_applicable_when_no_imports_found() { |
219 | check_assist_with_imports_locator_not_applicable( | 211 | check_assist_not_applicable( |
220 | auto_import, | 212 | auto_import, |
221 | TestImportsLocator::new, | ||
222 | " | 213 | " |
223 | PubStruct<|>", | 214 | PubStruct<|>", |
224 | ); | 215 | ); |
@@ -226,9 +217,8 @@ mod tests { | |||
226 | 217 | ||
227 | #[test] | 218 | #[test] |
228 | fn not_applicable_in_import_statements() { | 219 | fn not_applicable_in_import_statements() { |
229 | check_assist_with_imports_locator_not_applicable( | 220 | check_assist_not_applicable( |
230 | auto_import, | 221 | auto_import, |
231 | TestImportsLocator::new, | ||
232 | r" | 222 | r" |
233 | use PubStruct<|>; | 223 | use PubStruct<|>; |
234 | 224 | ||
@@ -240,9 +230,8 @@ mod tests { | |||
240 | 230 | ||
241 | #[test] | 231 | #[test] |
242 | fn function_import() { | 232 | fn function_import() { |
243 | check_assist_with_imports_locator( | 233 | check_assist( |
244 | auto_import, | 234 | auto_import, |
245 | TestImportsLocator::new, | ||
246 | r" | 235 | r" |
247 | test_function<|> | 236 | test_function<|> |
248 | 237 | ||
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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast::{self, NameOwner, VisibilityOwner}, | 2 | ast::{self, NameOwner, VisibilityOwner}, |
4 | AstNode, | 3 | AstNode, |
@@ -22,14 +21,14 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
22 | // ``` | 21 | // ``` |
23 | // pub(crate) fn frobnicate() {} | 22 | // pub(crate) fn frobnicate() {} |
24 | // ``` | 23 | // ``` |
25 | pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn change_visibility(ctx: AssistCtx) -> Option<Assist> { |
26 | if let Some(vis) = ctx.find_node_at_offset::<ast::Visibility>() { | 25 | if let Some(vis) = ctx.find_node_at_offset::<ast::Visibility>() { |
27 | return change_vis(ctx, vis); | 26 | return change_vis(ctx, vis); |
28 | } | 27 | } |
29 | add_vis(ctx) | 28 | add_vis(ctx) |
30 | } | 29 | } |
31 | 30 | ||
32 | fn add_vis(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 31 | fn add_vis(ctx: AssistCtx) -> Option<Assist> { |
33 | let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { | 32 | let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { |
34 | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, | 33 | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, |
35 | _ => false, | 34 | _ => false, |
@@ -75,7 +74,7 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { | |||
75 | .unwrap_or_else(|| node.text_range().start()) | 74 | .unwrap_or_else(|| node.text_range().start()) |
76 | } | 75 | } |
77 | 76 | ||
78 | fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: ast::Visibility) -> Option<Assist> { | 77 | fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option<Assist> { |
79 | if vis.syntax().text() == "pub" { | 78 | if vis.syntax().text() == "pub" { |
80 | return ctx.add_assist( | 79 | return ctx.add_assist( |
81 | AssistId("change_visibility"), | 80 | 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 @@ | |||
1 | use std::{iter::once, ops::RangeInclusive}; | 1 | use std::{iter::once, ops::RangeInclusive}; |
2 | 2 | ||
3 | use hir::db::HirDatabase; | ||
4 | use ra_syntax::{ | 3 | use ra_syntax::{ |
5 | algo::replace_children, | 4 | algo::replace_children, |
6 | ast::{self, edit::IndentLevel, make, Block, Pat::TupleStructPat}, | 5 | ast::{self, edit::IndentLevel, make, Block, Pat::TupleStructPat}, |
@@ -36,7 +35,7 @@ use crate::{ | |||
36 | // bar(); | 35 | // bar(); |
37 | // } | 36 | // } |
38 | // ``` | 37 | // ``` |
39 | pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 38 | pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option<Assist> { |
40 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | 39 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; |
41 | if if_expr.else_branch().is_some() { | 40 | if if_expr.else_branch().is_some() { |
42 | return None; | 41 | 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}; | |||
31 | // } | 31 | // } |
32 | // } | 32 | // } |
33 | // ``` | 33 | // ``` |
34 | pub(crate) fn fill_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 34 | pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option<Assist> { |
35 | let match_expr = ctx.find_node_at_offset::<ast::MatchExpr>()?; | 35 | let match_expr = ctx.find_node_at_offset::<ast::MatchExpr>()?; |
36 | let match_arm_list = match_expr.match_arm_list()?; | 36 | let match_arm_list = match_expr.match_arm_list()?; |
37 | 37 | ||
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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::ast::{AstNode, BinExpr, BinOp}; | 1 | use ra_syntax::ast::{AstNode, BinExpr, BinOp}; |
3 | 2 | ||
4 | use crate::{Assist, AssistCtx, AssistId}; | 3 | use crate::{Assist, AssistCtx, AssistId}; |
@@ -18,7 +17,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
18 | // let _ = 2 + 90; | 17 | // let _ = 2 + 90; |
19 | // } | 18 | // } |
20 | // ``` | 19 | // ``` |
21 | pub(crate) fn flip_binexpr(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 20 | pub(crate) fn flip_binexpr(ctx: AssistCtx) -> Option<Assist> { |
22 | let expr = ctx.find_node_at_offset::<BinExpr>()?; | 21 | let expr = ctx.find_node_at_offset::<BinExpr>()?; |
23 | let lhs = expr.lhs()?.syntax().clone(); | 22 | let lhs = expr.lhs()?.syntax().clone(); |
24 | let rhs = expr.rhs()?.syntax().clone(); | 23 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{algo::non_trivia_sibling, Direction, T}; | 1 | use ra_syntax::{algo::non_trivia_sibling, Direction, T}; |
3 | 2 | ||
4 | use crate::{Assist, AssistCtx, AssistId}; | 3 | use crate::{Assist, AssistCtx, AssistId}; |
@@ -18,7 +17,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
18 | // ((3, 4), (1, 2)); | 17 | // ((3, 4), (1, 2)); |
19 | // } | 18 | // } |
20 | // ``` | 19 | // ``` |
21 | pub(crate) fn flip_comma(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 20 | pub(crate) fn flip_comma(ctx: AssistCtx) -> Option<Assist> { |
22 | let comma = ctx.find_token_at_offset(T![,])?; | 21 | let comma = ctx.find_token_at_offset(T![,])?; |
23 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; | 22 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; |
24 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; | 23 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | algo::non_trivia_sibling, | 2 | algo::non_trivia_sibling, |
4 | ast::{self, AstNode}, | 3 | ast::{self, AstNode}, |
@@ -18,7 +17,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
18 | // ``` | 17 | // ``` |
19 | // fn foo<T: Copy + Clone>() { } | 18 | // fn foo<T: Copy + Clone>() { } |
20 | // ``` | 19 | // ``` |
21 | pub(crate) fn flip_trait_bound(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 20 | pub(crate) fn flip_trait_bound(ctx: AssistCtx) -> Option<Assist> { |
22 | // We want to replicate the behavior of `flip_binexpr` by only suggesting | 21 | // We want to replicate the behavior of `flip_binexpr` by only suggesting |
23 | // the assist when the cursor is on a `+` | 22 | // the assist when the cursor is on a `+` |
24 | let plus = ctx.find_token_at_offset(T![+])?; | 23 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast::{self, AstNode, AstToken}, | 2 | ast::{self, AstNode, AstToken}, |
4 | TextRange, | 3 | TextRange, |
@@ -23,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
23 | // (1 + 2) * 4; | 22 | // (1 + 2) * 4; |
24 | // } | 23 | // } |
25 | // ``` | 24 | // ``` |
26 | pub(crate) fn inline_local_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { |
27 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; | 26 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; |
28 | let bind_pat = match let_stmt.pat()? { | 27 | let bind_pat = match let_stmt.pat()? { |
29 | ast::Pat::BindPat(pat) => pat, | 28 | 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 @@ | |||
1 | use format_buf::format; | 1 | use format_buf::format; |
2 | use hir::db::HirDatabase; | ||
3 | use ra_syntax::{ | 2 | use ra_syntax::{ |
4 | ast::{self, AstNode}, | 3 | ast::{self, AstNode}, |
5 | SyntaxKind::{ | 4 | SyntaxKind::{ |
@@ -28,7 +27,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
28 | // var_name * 4; | 27 | // var_name * 4; |
29 | // } | 28 | // } |
30 | // ``` | 29 | // ``` |
31 | pub(crate) fn introduce_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 30 | pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option<Assist> { |
32 | if ctx.frange.range.is_empty() { | 31 | if ctx.frange.range.is_empty() { |
33 | return None; | 32 | return None; |
34 | } | 33 | } |
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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::ast::{self, AstNode}; | 1 | use ra_syntax::ast::{self, AstNode}; |
3 | use ra_syntax::T; | 2 | use ra_syntax::T; |
4 | 3 | ||
@@ -23,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
23 | // } | 22 | // } |
24 | // ``` | 23 | // ``` |
25 | 24 | ||
26 | pub(crate) fn invert_if(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> { |
27 | let if_keyword = ctx.find_token_at_offset(T![if])?; | 26 | let if_keyword = ctx.find_token_at_offset(T![if])?; |
28 | let expr = ast::IfExpr::cast(if_keyword.parent())?; | 27 | let expr = ast::IfExpr::cast(if_keyword.parent())?; |
29 | let if_range = if_keyword.text_range(); | 28 | 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 @@ | |||
1 | use std::iter::successors; | 1 | use std::iter::successors; |
2 | 2 | ||
3 | use hir::db::HirDatabase; | ||
4 | use ra_syntax::{ | 3 | use ra_syntax::{ |
5 | ast::{self, AstNode}, | 4 | ast::{self, AstNode}, |
6 | Direction, TextUnit, | 5 | Direction, TextUnit, |
@@ -32,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId, TextRange}; | |||
32 | // } | 31 | // } |
33 | // } | 32 | // } |
34 | // ``` | 33 | // ``` |
35 | pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 34 | pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option<Assist> { |
36 | let current_arm = ctx.find_node_at_offset::<ast::MatchArm>()?; | 35 | let current_arm = ctx.find_node_at_offset::<ast::MatchArm>()?; |
37 | // Don't try to handle arms with guards for now - can add support for this later | 36 | // Don't try to handle arms with guards for now - can add support for this later |
38 | if current_arm.guard().is_some() { | 37 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, | 2 | ast::{self, edit, make, AstNode, NameOwner, TypeBoundsOwner}, |
4 | SyntaxElement, | 3 | SyntaxElement, |
@@ -22,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
22 | // f(x) | 21 | // f(x) |
23 | // } | 22 | // } |
24 | // ``` | 23 | // ``` |
25 | pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option<Assist> { |
26 | let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?; | 25 | let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?; |
27 | 26 | ||
28 | let mut type_params = type_param_list.type_params(); | 27 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast, | 2 | ast, |
4 | ast::{AstNode, AstToken, IfExpr, MatchArm}, | 3 | ast::{AstNode, AstToken, IfExpr, MatchArm}, |
@@ -32,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
32 | // } | 31 | // } |
33 | // } | 32 | // } |
34 | // ``` | 33 | // ``` |
35 | pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 34 | pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option<Assist> { |
36 | let match_arm = ctx.find_node_at_offset::<MatchArm>()?; | 35 | let match_arm = ctx.find_node_at_offset::<MatchArm>()?; |
37 | let guard = match_arm.guard()?; | 36 | let guard = match_arm.guard()?; |
38 | let space_before_guard = guard.syntax().prev_sibling_or_token(); | 37 | let space_before_guard = guard.syntax().prev_sibling_or_token(); |
@@ -89,7 +88,7 @@ pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx<impl HirDatabase>) -> Option | |||
89 | // } | 88 | // } |
90 | // } | 89 | // } |
91 | // ``` | 90 | // ``` |
92 | pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 91 | pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option<Assist> { |
93 | let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?; | 92 | let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?; |
94 | let last_match_pat = match_arm.pats().last()?; | 93 | let last_match_pat = match_arm.pats().last()?; |
95 | 94 | ||
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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast, AstToken, | 2 | ast, AstToken, |
4 | SyntaxKind::{RAW_STRING, STRING}, | 3 | SyntaxKind::{RAW_STRING, STRING}, |
@@ -22,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
22 | // r#"Hello, World!"#; | 21 | // r#"Hello, World!"#; |
23 | // } | 22 | // } |
24 | // ``` | 23 | // ``` |
25 | pub(crate) fn make_raw_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn make_raw_string(ctx: AssistCtx) -> Option<Assist> { |
26 | let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?; | 25 | let token = ctx.find_token_at_offset(STRING).and_then(ast::String::cast)?; |
27 | let value = token.value()?; | 26 | let value = token.value()?; |
28 | ctx.add_assist(AssistId("make_raw_string"), "Rewrite as raw string", |edit| { | 27 | ctx.add_assist(AssistId("make_raw_string"), "Rewrite as raw string", |edit| { |
@@ -51,7 +50,7 @@ pub(crate) fn make_raw_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist | |||
51 | // "Hello, \"World!\""; | 50 | // "Hello, \"World!\""; |
52 | // } | 51 | // } |
53 | // ``` | 52 | // ``` |
54 | pub(crate) fn make_usual_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 53 | pub(crate) fn make_usual_string(ctx: AssistCtx) -> Option<Assist> { |
55 | let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?; | 54 | let token = ctx.find_token_at_offset(RAW_STRING).and_then(ast::RawString::cast)?; |
56 | let value = token.value()?; | 55 | let value = token.value()?; |
57 | ctx.add_assist(AssistId("make_usual_string"), "Rewrite as regular string", |edit| { | 56 | ctx.add_assist(AssistId("make_usual_string"), "Rewrite as regular string", |edit| { |
@@ -77,7 +76,7 @@ pub(crate) fn make_usual_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi | |||
77 | // r##"Hello, World!"##; | 76 | // r##"Hello, World!"##; |
78 | // } | 77 | // } |
79 | // ``` | 78 | // ``` |
80 | pub(crate) fn add_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 79 | pub(crate) fn add_hash(ctx: AssistCtx) -> Option<Assist> { |
81 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 80 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
82 | ctx.add_assist(AssistId("add_hash"), "Add # to raw string", |edit| { | 81 | ctx.add_assist(AssistId("add_hash"), "Add # to raw string", |edit| { |
83 | edit.target(token.text_range()); | 82 | edit.target(token.text_range()); |
@@ -101,7 +100,7 @@ pub(crate) fn add_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
101 | // r"Hello, World!"; | 100 | // r"Hello, World!"; |
102 | // } | 101 | // } |
103 | // ``` | 102 | // ``` |
104 | pub(crate) fn remove_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 103 | pub(crate) fn remove_hash(ctx: AssistCtx) -> Option<Assist> { |
105 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 104 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
106 | let text = token.text().as_str(); | 105 | let text = token.text().as_str(); |
107 | if text.starts_with("r\"") { | 106 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_syntax::{ | 1 | use ra_syntax::{ |
3 | ast::{self, AstNode}, | 2 | ast::{self, AstNode}, |
4 | TextUnit, T, | 3 | TextUnit, T, |
@@ -21,7 +20,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
21 | // 92; | 20 | // 92; |
22 | // } | 21 | // } |
23 | // ``` | 22 | // ``` |
24 | pub(crate) fn remove_dbg(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 23 | pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option<Assist> { |
25 | let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; | 24 | let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; |
26 | 25 | ||
27 | if !is_valid_macrocall(¯o_call, "dbg")? { | 26 | 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 @@ | |||
1 | use hir::db::HirDatabase; | ||
2 | use ra_fmt::unwrap_trivial_block; | 1 | use ra_fmt::unwrap_trivial_block; |
3 | use ra_syntax::{ | 2 | use ra_syntax::{ |
4 | ast::{self, make}, | 3 | ast::{self, make}, |
@@ -34,7 +33,7 @@ use ast::edit::IndentLevel; | |||
34 | // } | 33 | // } |
35 | // } | 34 | // } |
36 | // ``` | 35 | // ``` |
37 | pub(crate) fn replace_if_let_with_match(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 36 | pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { |
38 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | 37 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; |
39 | let cond = if_expr.condition()?; | 38 | let cond = if_expr.condition()?; |
40 | let pat = cond.pat()?; | 39 | 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 @@ | |||
1 | use std::iter::successors; | 1 | use std::iter::successors; |
2 | 2 | ||
3 | use hir::db::HirDatabase; | ||
4 | use ra_syntax::{ast, AstNode, TextUnit, T}; | 3 | use ra_syntax::{ast, AstNode, TextUnit, T}; |
5 | 4 | ||
6 | use crate::{Assist, AssistCtx, AssistId}; | 5 | use crate::{Assist, AssistCtx, AssistId}; |
@@ -16,7 +15,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
16 | // ``` | 15 | // ``` |
17 | // use std::{collections::HashMap}; | 16 | // use std::{collections::HashMap}; |
18 | // ``` | 17 | // ``` |
19 | pub(crate) fn split_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 18 | pub(crate) fn split_import(ctx: AssistCtx) -> Option<Assist> { |
20 | let colon_colon = ctx.find_token_at_offset(T![::])?; | 19 | let colon_colon = ctx.find_token_at_offset(T![::])?; |
21 | let path = ast::Path::cast(colon_colon.parent())?; | 20 | let path = ast::Path::cast(colon_colon.parent())?; |
22 | let top_path = successors(Some(path), |it| it.parent_path()).last()?; | 21 | let top_path = successors(Some(path), |it| it.parent_path()).last()?; |
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 625ebc4a2..1343043dd 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs | |||
@@ -9,13 +9,11 @@ mod assist_ctx; | |||
9 | mod marks; | 9 | mod marks; |
10 | #[cfg(test)] | 10 | #[cfg(test)] |
11 | mod doc_tests; | 11 | mod doc_tests; |
12 | #[cfg(test)] | ||
13 | mod test_db; | ||
14 | pub mod ast_transform; | 12 | pub mod ast_transform; |
15 | 13 | ||
16 | use either::Either; | 14 | use either::Either; |
17 | use hir::{db::HirDatabase, ModuleDef}; | ||
18 | use ra_db::FileRange; | 15 | use ra_db::FileRange; |
16 | use ra_ide_db::RootDatabase; | ||
19 | use ra_syntax::{TextRange, TextUnit}; | 17 | use ra_syntax::{TextRange, TextUnit}; |
20 | use ra_text_edit::TextEdit; | 18 | use ra_text_edit::TextEdit; |
21 | 19 | ||
@@ -61,10 +59,7 @@ impl ResolvedAssist { | |||
61 | /// | 59 | /// |
62 | /// Assists are returned in the "unresolved" state, that is only labels are | 60 | /// Assists are returned in the "unresolved" state, that is only labels are |
63 | /// returned, without actual edits. | 61 | /// returned, without actual edits. |
64 | pub fn applicable_assists<H>(db: &H, range: FileRange) -> Vec<AssistLabel> | 62 | pub fn applicable_assists(db: &RootDatabase, range: FileRange) -> Vec<AssistLabel> { |
65 | where | ||
66 | H: HirDatabase + 'static, | ||
67 | { | ||
68 | AssistCtx::with_ctx(db, range, false, |ctx| { | 63 | AssistCtx::with_ctx(db, range, false, |ctx| { |
69 | assists::all() | 64 | assists::all() |
70 | .iter() | 65 | .iter() |
@@ -77,59 +72,11 @@ where | |||
77 | }) | 72 | }) |
78 | } | 73 | } |
79 | 74 | ||
80 | /// A functionality for locating imports for the given name. | ||
81 | /// | ||
82 | /// Currently has to be a trait with the real implementation provided by the ra_ide_api crate, | ||
83 | /// due to the search functionality located there. | ||
84 | /// Later, this trait should be removed completely and the search functionality moved to a separate crate, | ||
85 | /// accessible from the ra_assists crate. | ||
86 | pub trait ImportsLocator { | ||
87 | /// Finds all imports for the given name and the module that contains this name. | ||
88 | fn find_imports(&mut self, name_to_import: &str) -> Vec<ModuleDef>; | ||
89 | } | ||
90 | |||
91 | /// Return all the assists applicable at the given position | ||
92 | /// and additional assists that need the imports locator functionality to work. | ||
93 | /// | ||
94 | /// Assists are returned in the "resolved" state, that is with edit fully | ||
95 | /// computed. | ||
96 | pub fn assists_with_imports_locator<H, F>( | ||
97 | db: &H, | ||
98 | range: FileRange, | ||
99 | mut imports_locator: F, | ||
100 | ) -> Vec<ResolvedAssist> | ||
101 | where | ||
102 | H: HirDatabase + 'static, | ||
103 | F: ImportsLocator, | ||
104 | { | ||
105 | AssistCtx::with_ctx(db, range, true, |ctx| { | ||
106 | let mut assists = assists::all() | ||
107 | .iter() | ||
108 | .map(|f| f(ctx.clone())) | ||
109 | .chain( | ||
110 | assists::all_with_imports_locator() | ||
111 | .iter() | ||
112 | .map(|f| f(ctx.clone(), &mut imports_locator)), | ||
113 | ) | ||
114 | .filter_map(std::convert::identity) | ||
115 | .map(|a| match a { | ||
116 | Assist::Resolved { assist } => assist, | ||
117 | Assist::Unresolved { .. } => unreachable!(), | ||
118 | }) | ||
119 | .collect(); | ||
120 | sort_assists(&mut assists); | ||
121 | assists | ||
122 | }) | ||
123 | } | ||
124 | |||
125 | /// Return all the assists applicable at the given position. | 75 | /// Return all the assists applicable at the given position. |
126 | /// | 76 | /// |
127 | /// Assists are returned in the "resolved" state, that is with edit fully | 77 | /// Assists are returned in the "resolved" state, that is with edit fully |
128 | /// computed. | 78 | /// computed. |
129 | pub fn assists<H>(db: &H, range: FileRange) -> Vec<ResolvedAssist> | 79 | pub fn assists(db: &RootDatabase, range: FileRange) -> Vec<ResolvedAssist> { |
130 | where | ||
131 | H: HirDatabase + 'static, | ||
132 | { | ||
133 | AssistCtx::with_ctx(db, range, true, |ctx| { | 80 | AssistCtx::with_ctx(db, range, true, |ctx| { |
134 | let mut a = assists::all() | 81 | let mut a = assists::all() |
135 | .iter() | 82 | .iter() |
@@ -155,8 +102,7 @@ fn sort_assists(assists: &mut Vec<ResolvedAssist>) { | |||
155 | } | 102 | } |
156 | 103 | ||
157 | mod assists { | 104 | mod assists { |
158 | use crate::{Assist, AssistCtx, ImportsLocator}; | 105 | use crate::{Assist, AssistCtx}; |
159 | use hir::db::HirDatabase; | ||
160 | 106 | ||
161 | mod add_derive; | 107 | mod add_derive; |
162 | mod add_explicit_type; | 108 | mod add_explicit_type; |
@@ -184,7 +130,7 @@ mod assists { | |||
184 | mod move_bounds; | 130 | mod move_bounds; |
185 | mod early_return; | 131 | mod early_return; |
186 | 132 | ||
187 | pub(crate) fn all<DB: HirDatabase>() -> &'static [fn(AssistCtx<DB>) -> Option<Assist>] { | 133 | pub(crate) fn all() -> &'static [fn(AssistCtx) -> Option<Assist>] { |
188 | &[ | 134 | &[ |
189 | add_derive::add_derive, | 135 | add_derive::add_derive, |
190 | add_explicit_type::add_explicit_type, | 136 | add_explicit_type::add_explicit_type, |
@@ -215,79 +161,34 @@ mod assists { | |||
215 | raw_string::make_usual_string, | 161 | raw_string::make_usual_string, |
216 | raw_string::remove_hash, | 162 | raw_string::remove_hash, |
217 | early_return::convert_to_guarded_return, | 163 | early_return::convert_to_guarded_return, |
164 | auto_import::auto_import, | ||
218 | ] | 165 | ] |
219 | } | 166 | } |
220 | |||
221 | pub(crate) fn all_with_imports_locator<'a, DB: HirDatabase, F: ImportsLocator>( | ||
222 | ) -> &'a [fn(AssistCtx<DB>, &mut F) -> Option<Assist>] { | ||
223 | &[auto_import::auto_import] | ||
224 | } | ||
225 | } | 167 | } |
226 | 168 | ||
227 | #[cfg(test)] | 169 | #[cfg(test)] |
228 | mod helpers { | 170 | mod helpers { |
229 | use hir::db::DefDatabase; | ||
230 | use ra_db::{fixture::WithFixture, FileId, FileRange}; | ||
231 | use ra_syntax::TextRange; | ||
232 | use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; | ||
233 | |||
234 | use crate::{test_db::TestDB, Assist, AssistCtx, ImportsLocator}; | ||
235 | use std::sync::Arc; | 171 | use std::sync::Arc; |
236 | 172 | ||
237 | // FIXME remove the `ModuleDefId` reexport from `ra_hir` when this gets removed. | 173 | use ra_db::{fixture::WithFixture, FileId, FileRange, SourceDatabaseExt}; |
238 | pub(crate) struct TestImportsLocator { | 174 | use ra_ide_db::{symbol_index::SymbolsDatabase, RootDatabase}; |
239 | db: Arc<TestDB>, | 175 | use ra_syntax::TextRange; |
240 | test_file_id: FileId, | 176 | use test_utils::{add_cursor, assert_eq_text, extract_offset, extract_range}; |
241 | } | ||
242 | |||
243 | impl TestImportsLocator { | ||
244 | pub(crate) fn new(db: Arc<TestDB>, test_file_id: FileId) -> Self { | ||
245 | TestImportsLocator { db, test_file_id } | ||
246 | } | ||
247 | } | ||
248 | 177 | ||
249 | impl ImportsLocator for TestImportsLocator { | 178 | use crate::{Assist, AssistCtx}; |
250 | fn find_imports(&mut self, name_to_import: &str) -> Vec<hir::ModuleDef> { | ||
251 | let crate_def_map = self.db.crate_def_map(self.db.test_crate()); | ||
252 | let mut findings = Vec::new(); | ||
253 | |||
254 | let mut module_ids_to_process = | ||
255 | crate_def_map.modules_for_file(self.test_file_id).collect::<Vec<_>>(); | ||
256 | |||
257 | while !module_ids_to_process.is_empty() { | ||
258 | let mut more_ids_to_process = Vec::new(); | ||
259 | for local_module_id in module_ids_to_process.drain(..) { | ||
260 | for (name, namespace_data) in | ||
261 | crate_def_map[local_module_id].scope.entries_without_primitives() | ||
262 | { | ||
263 | let found_a_match = &name.to_string() == name_to_import; | ||
264 | vec![namespace_data.types, namespace_data.values] | ||
265 | .into_iter() | ||
266 | .filter_map(std::convert::identity) | ||
267 | .for_each(|(module_def_id, _)| { | ||
268 | if found_a_match { | ||
269 | findings.push(module_def_id.into()); | ||
270 | } | ||
271 | if let hir::ModuleDefId::ModuleId(module_id) = module_def_id { | ||
272 | more_ids_to_process.push(module_id.local_id); | ||
273 | } | ||
274 | }); | ||
275 | } | ||
276 | } | ||
277 | module_ids_to_process = more_ids_to_process; | ||
278 | } | ||
279 | 179 | ||
280 | findings | 180 | pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { |
281 | } | 181 | let (mut db, file_id) = RootDatabase::with_single_file(text); |
182 | // FIXME: ideally, this should be done by the above `RootDatabase::with_single_file`, | ||
183 | // but it looks like this might need specialization? :( | ||
184 | let local_roots = vec![db.file_source_root(file_id)]; | ||
185 | db.set_local_roots(Arc::new(local_roots)); | ||
186 | (db, file_id) | ||
282 | } | 187 | } |
283 | 188 | ||
284 | pub(crate) fn check_assist( | 189 | pub(crate) fn check_assist(assist: fn(AssistCtx) -> Option<Assist>, before: &str, after: &str) { |
285 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | ||
286 | before: &str, | ||
287 | after: &str, | ||
288 | ) { | ||
289 | let (before_cursor_pos, before) = extract_offset(before); | 190 | let (before_cursor_pos, before) = extract_offset(before); |
290 | let (db, file_id) = TestDB::with_single_file(&before); | 191 | let (db, file_id) = with_single_file(&before); |
291 | let frange = | 192 | let frange = |
292 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 193 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
293 | let assist = | 194 | let assist = |
@@ -309,45 +210,13 @@ mod helpers { | |||
309 | assert_eq_text!(after, &actual); | 210 | assert_eq_text!(after, &actual); |
310 | } | 211 | } |
311 | 212 | ||
312 | 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, | ||
315 | before: &str, | ||
316 | after: &str, | ||
317 | ) { | ||
318 | let (before_cursor_pos, before) = extract_offset(before); | ||
319 | let (db, file_id) = TestDB::with_single_file(&before); | ||
320 | let db = Arc::new(db); | ||
321 | let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); | ||
322 | let frange = | ||
323 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | ||
324 | let assist = | ||
325 | AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator)) | ||
326 | .expect("code action is not applicable"); | ||
327 | let action = match assist { | ||
328 | Assist::Unresolved { .. } => unreachable!(), | ||
329 | Assist::Resolved { assist } => assist.get_first_action(), | ||
330 | }; | ||
331 | |||
332 | let actual = action.edit.apply(&before); | ||
333 | let actual_cursor_pos = match action.cursor_position { | ||
334 | None => action | ||
335 | .edit | ||
336 | .apply_to_offset(before_cursor_pos) | ||
337 | .expect("cursor position is affected by the edit"), | ||
338 | Some(off) => off, | ||
339 | }; | ||
340 | let actual = add_cursor(&actual, actual_cursor_pos); | ||
341 | assert_eq_text!(after, &actual); | ||
342 | } | ||
343 | |||
344 | pub(crate) fn check_assist_range( | 213 | pub(crate) fn check_assist_range( |
345 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 214 | assist: fn(AssistCtx) -> Option<Assist>, |
346 | before: &str, | 215 | before: &str, |
347 | after: &str, | 216 | after: &str, |
348 | ) { | 217 | ) { |
349 | let (range, before) = extract_range(before); | 218 | let (range, before) = extract_range(before); |
350 | let (db, file_id) = TestDB::with_single_file(&before); | 219 | let (db, file_id) = with_single_file(&before); |
351 | let frange = FileRange { file_id, range }; | 220 | let frange = FileRange { file_id, range }; |
352 | let assist = | 221 | let assist = |
353 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 222 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); |
@@ -364,12 +233,12 @@ mod helpers { | |||
364 | } | 233 | } |
365 | 234 | ||
366 | pub(crate) fn check_assist_target( | 235 | pub(crate) fn check_assist_target( |
367 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 236 | assist: fn(AssistCtx) -> Option<Assist>, |
368 | before: &str, | 237 | before: &str, |
369 | target: &str, | 238 | target: &str, |
370 | ) { | 239 | ) { |
371 | let (before_cursor_pos, before) = extract_offset(before); | 240 | let (before_cursor_pos, before) = extract_offset(before); |
372 | let (db, file_id) = TestDB::with_single_file(&before); | 241 | let (db, file_id) = with_single_file(&before); |
373 | let frange = | 242 | let frange = |
374 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 243 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
375 | let assist = | 244 | let assist = |
@@ -384,12 +253,12 @@ mod helpers { | |||
384 | } | 253 | } |
385 | 254 | ||
386 | pub(crate) fn check_assist_range_target( | 255 | pub(crate) fn check_assist_range_target( |
387 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 256 | assist: fn(AssistCtx) -> Option<Assist>, |
388 | before: &str, | 257 | before: &str, |
389 | target: &str, | 258 | target: &str, |
390 | ) { | 259 | ) { |
391 | let (range, before) = extract_range(before); | 260 | let (range, before) = extract_range(before); |
392 | let (db, file_id) = TestDB::with_single_file(&before); | 261 | let (db, file_id) = with_single_file(&before); |
393 | let frange = FileRange { file_id, range }; | 262 | let frange = FileRange { file_id, range }; |
394 | let assist = | 263 | let assist = |
395 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); | 264 | AssistCtx::with_ctx(&db, frange, true, assist).expect("code action is not applicable"); |
@@ -403,39 +272,23 @@ mod helpers { | |||
403 | } | 272 | } |
404 | 273 | ||
405 | pub(crate) fn check_assist_not_applicable( | 274 | pub(crate) fn check_assist_not_applicable( |
406 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 275 | assist: fn(AssistCtx) -> Option<Assist>, |
407 | before: &str, | 276 | before: &str, |
408 | ) { | 277 | ) { |
409 | let (before_cursor_pos, before) = extract_offset(before); | 278 | let (before_cursor_pos, before) = extract_offset(before); |
410 | let (db, file_id) = TestDB::with_single_file(&before); | 279 | let (db, file_id) = with_single_file(&before); |
411 | let frange = | 280 | let frange = |
412 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 281 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
413 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 282 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
414 | assert!(assist.is_none()); | 283 | assert!(assist.is_none()); |
415 | } | 284 | } |
416 | 285 | ||
417 | 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, | ||
420 | before: &str, | ||
421 | ) { | ||
422 | let (before_cursor_pos, before) = extract_offset(before); | ||
423 | let (db, file_id) = TestDB::with_single_file(&before); | ||
424 | let db = Arc::new(db); | ||
425 | let mut imports_locator = imports_locator_provider(Arc::clone(&db), file_id); | ||
426 | let frange = | ||
427 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | ||
428 | let assist = | ||
429 | AssistCtx::with_ctx(db.as_ref(), frange, true, |ctx| assist(ctx, &mut imports_locator)); | ||
430 | assert!(assist.is_none()); | ||
431 | } | ||
432 | |||
433 | pub(crate) fn check_assist_range_not_applicable( | 286 | pub(crate) fn check_assist_range_not_applicable( |
434 | assist: fn(AssistCtx<TestDB>) -> Option<Assist>, | 287 | assist: fn(AssistCtx) -> Option<Assist>, |
435 | before: &str, | 288 | before: &str, |
436 | ) { | 289 | ) { |
437 | let (range, before) = extract_range(before); | 290 | let (range, before) = extract_range(before); |
438 | let (db, file_id) = TestDB::with_single_file(&before); | 291 | let (db, file_id) = with_single_file(&before); |
439 | let frange = FileRange { file_id, range }; | 292 | let frange = FileRange { file_id, range }; |
440 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); | 293 | let assist = AssistCtx::with_ctx(&db, frange, true, assist); |
441 | assert!(assist.is_none()); | 294 | assert!(assist.is_none()); |
@@ -444,17 +297,17 @@ mod helpers { | |||
444 | 297 | ||
445 | #[cfg(test)] | 298 | #[cfg(test)] |
446 | mod tests { | 299 | mod tests { |
447 | use ra_db::{fixture::WithFixture, FileRange}; | 300 | use ra_db::FileRange; |
448 | use ra_syntax::TextRange; | 301 | use ra_syntax::TextRange; |
449 | use test_utils::{extract_offset, extract_range}; | 302 | use test_utils::{extract_offset, extract_range}; |
450 | 303 | ||
451 | use crate::test_db::TestDB; | 304 | use crate::helpers; |
452 | 305 | ||
453 | #[test] | 306 | #[test] |
454 | fn assist_order_field_struct() { | 307 | fn assist_order_field_struct() { |
455 | let before = "struct Foo { <|>bar: u32 }"; | 308 | let before = "struct Foo { <|>bar: u32 }"; |
456 | let (before_cursor_pos, before) = extract_offset(before); | 309 | let (before_cursor_pos, before) = extract_offset(before); |
457 | let (db, file_id) = TestDB::with_single_file(&before); | 310 | let (db, file_id) = helpers::with_single_file(&before); |
458 | let frange = | 311 | let frange = |
459 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; | 312 | FileRange { file_id, range: TextRange::offset_len(before_cursor_pos, 0.into()) }; |
460 | let assists = super::assists(&db, frange); | 313 | let assists = super::assists(&db, frange); |
@@ -478,7 +331,7 @@ mod tests { | |||
478 | } | 331 | } |
479 | }"; | 332 | }"; |
480 | let (range, before) = extract_range(before); | 333 | let (range, before) = extract_range(before); |
481 | let (db, file_id) = TestDB::with_single_file(&before); | 334 | let (db, file_id) = helpers::with_single_file(&before); |
482 | let frange = FileRange { file_id, range }; | 335 | let frange = FileRange { file_id, range }; |
483 | let assists = super::assists(&db, frange); | 336 | let assists = super::assists(&db, frange); |
484 | let mut assists = assists.iter(); | 337 | let mut assists = assists.iter(); |
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 @@ | |||
1 | //! Database used for testing `ra_assists`. | ||
2 | |||
3 | use std::sync::Arc; | ||
4 | |||
5 | use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, RelativePath}; | ||
6 | |||
7 | #[salsa::database( | ||
8 | ra_db::SourceDatabaseExtStorage, | ||
9 | ra_db::SourceDatabaseStorage, | ||
10 | hir::db::InternDatabaseStorage, | ||
11 | hir::db::AstDatabaseStorage, | ||
12 | hir::db::DefDatabaseStorage, | ||
13 | hir::db::HirDatabaseStorage | ||
14 | )] | ||
15 | #[derive(Debug, Default)] | ||
16 | pub struct TestDB { | ||
17 | runtime: salsa::Runtime<TestDB>, | ||
18 | } | ||
19 | |||
20 | impl salsa::Database for TestDB { | ||
21 | fn salsa_runtime(&self) -> &salsa::Runtime<Self> { | ||
22 | &self.runtime | ||
23 | } | ||
24 | fn salsa_runtime_mut(&mut self) -> &mut salsa::Runtime<Self> { | ||
25 | &mut self.runtime | ||
26 | } | ||
27 | } | ||
28 | |||
29 | impl std::panic::RefUnwindSafe for TestDB {} | ||
30 | |||
31 | impl FileLoader for TestDB { | ||
32 | fn file_text(&self, file_id: FileId) -> Arc<String> { | ||
33 | FileLoaderDelegate(self).file_text(file_id) | ||
34 | } | ||
35 | fn resolve_relative_path( | ||
36 | &self, | ||
37 | anchor: FileId, | ||
38 | relative_path: &RelativePath, | ||
39 | ) -> Option<FileId> { | ||
40 | FileLoaderDelegate(self).resolve_relative_path(anchor, relative_path) | ||
41 | } | ||
42 | fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> { | ||
43 | FileLoaderDelegate(self).relevant_crates(file_id) | ||
44 | } | ||
45 | } | ||