diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-10-27 14:38:28 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-10-27 14:38:28 +0000 |
commit | cf309b6a5f2d51e9509568ab82446cc3eae29f94 (patch) | |
tree | 54c4a26d7a6688a89515cdc97ec02c1a9f9103bf /crates/ra_assists | |
parent | ad950830d0902aaacfb5a76355a203626eb93b5f (diff) | |
parent | cda6355de23825c201d02e6062cb2dd414e98bf9 (diff) |
Merge #2094
2094: simplify AssistCtx API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_assists')
22 files changed, 113 insertions, 185 deletions
diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index c52736679..c5e9056af 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs | |||
@@ -82,12 +82,12 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | |||
82 | f(ctx) | 82 | f(ctx) |
83 | } | 83 | } |
84 | 84 | ||
85 | pub(crate) fn add_action( | 85 | pub(crate) fn add_assist( |
86 | &mut self, | 86 | mut self, |
87 | id: AssistId, | 87 | id: AssistId, |
88 | label: impl Into<String>, | 88 | label: impl Into<String>, |
89 | f: impl FnOnce(&mut AssistBuilder), | 89 | f: impl FnOnce(&mut AssistBuilder), |
90 | ) -> &mut Self { | 90 | ) -> Option<Assist> { |
91 | let label = AssistLabel { label: label.into(), id }; | 91 | let label = AssistLabel { label: label.into(), id }; |
92 | match &mut self.assist { | 92 | match &mut self.assist { |
93 | Assist::Unresolved(labels) => labels.push(label), | 93 | Assist::Unresolved(labels) => labels.push(label), |
@@ -100,10 +100,6 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { | |||
100 | labels_actions.push((label, action)); | 100 | labels_actions.push((label, action)); |
101 | } | 101 | } |
102 | } | 102 | } |
103 | self | ||
104 | } | ||
105 | |||
106 | pub(crate) fn build(self) -> Option<Assist> { | ||
107 | Some(self.assist) | 103 | Some(self.assist) |
108 | } | 104 | } |
109 | 105 | ||
diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs index d1e925b71..764b17bd8 100644 --- a/crates/ra_assists/src/assists/add_derive.rs +++ b/crates/ra_assists/src/assists/add_derive.rs | |||
@@ -25,10 +25,10 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
25 | // y: u32, | 25 | // y: u32, |
26 | // } | 26 | // } |
27 | // ``` | 27 | // ``` |
28 | pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 28 | pub(crate) fn add_derive(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
29 | let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; | 29 | let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?; |
30 | let node_start = derive_insertion_offset(&nominal)?; | 30 | let node_start = derive_insertion_offset(&nominal)?; |
31 | ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| { | 31 | ctx.add_assist(AssistId("add_derive"), "add `#[derive]`", |edit| { |
32 | let derive_attr = nominal | 32 | let derive_attr = nominal |
33 | .attrs() | 33 | .attrs() |
34 | .filter_map(|x| x.as_simple_call()) | 34 | .filter_map(|x| x.as_simple_call()) |
@@ -44,9 +44,7 @@ pub(crate) fn add_derive(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
44 | }; | 44 | }; |
45 | edit.target(nominal.syntax().text_range()); | 45 | edit.target(nominal.syntax().text_range()); |
46 | edit.set_cursor(offset) | 46 | edit.set_cursor(offset) |
47 | }); | 47 | }) |
48 | |||
49 | ctx.build() | ||
50 | } | 48 | } |
51 | 49 | ||
52 | // Insert `derive` after doc comments. | 50 | // Insert `derive` after doc comments. |
diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index ffbdc0b62..ddda1a0f2 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs | |||
@@ -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(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn add_explicit_type(ctx: AssistCtx<impl HirDatabase>) -> 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()?; |
@@ -47,11 +47,10 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
47 | return None; | 47 | return None; |
48 | } | 48 | } |
49 | 49 | ||
50 | ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| { | 50 | ctx.add_assist(AssistId("add_explicit_type"), "add explicit type", |edit| { |
51 | edit.target(pat_range); | 51 | edit.target(pat_range); |
52 | edit.insert(name_range.end(), format!(": {}", ty.display(db))); | 52 | edit.insert(name_range.end(), format!(": {}", ty.display(db))); |
53 | }); | 53 | }) |
54 | ctx.build() | ||
55 | } | 54 | } |
56 | 55 | ||
57 | /// Returns true if any type parameter is unknown | 56 | /// Returns true if any type parameter is unknown |
diff --git a/crates/ra_assists/src/assists/add_impl.rs b/crates/ra_assists/src/assists/add_impl.rs index fd3588d24..7da0cfd0d 100644 --- a/crates/ra_assists/src/assists/add_impl.rs +++ b/crates/ra_assists/src/assists/add_impl.rs | |||
@@ -27,10 +27,10 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
27 | // | 27 | // |
28 | // } | 28 | // } |
29 | // ``` | 29 | // ``` |
30 | pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 30 | pub(crate) fn add_impl(ctx: AssistCtx<impl HirDatabase>) -> 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_action(AssistId("add_impl"), "add impl", |edit| { | 33 | ctx.add_assist(AssistId("add_impl"), "add impl", |edit| { |
34 | edit.target(nominal.syntax().text_range()); | 34 | edit.target(nominal.syntax().text_range()); |
35 | let type_params = nominal.type_param_list(); | 35 | let type_params = nominal.type_param_list(); |
36 | let start_offset = nominal.syntax().text_range().end(); | 36 | let start_offset = nominal.syntax().text_range().end(); |
@@ -54,9 +54,7 @@ pub(crate) fn add_impl(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
54 | edit.set_cursor(start_offset + TextUnit::of_str(&buf)); | 54 | edit.set_cursor(start_offset + TextUnit::of_str(&buf)); |
55 | buf.push_str("\n}"); | 55 | buf.push_str("\n}"); |
56 | edit.insert(start_offset, buf); | 56 | edit.insert(start_offset, buf); |
57 | }); | 57 | }) |
58 | |||
59 | ctx.build() | ||
60 | } | 58 | } |
61 | 59 | ||
62 | #[cfg(test)] | 60 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index 149d1403f..c522d6a5a 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs | |||
@@ -39,7 +39,7 @@ pub fn auto_import_text_edit( | |||
39 | } | 39 | } |
40 | } | 40 | } |
41 | 41 | ||
42 | pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 42 | pub(crate) fn add_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
43 | let path: ast::Path = ctx.find_node_at_offset()?; | 43 | let path: ast::Path = ctx.find_node_at_offset()?; |
44 | // We don't want to mess with use statements | 44 | // We don't want to mess with use statements |
45 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { | 45 | if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { |
@@ -52,38 +52,18 @@ pub(crate) fn add_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
52 | return None; | 52 | return None; |
53 | } | 53 | } |
54 | 54 | ||
55 | if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) { | 55 | let module = path.syntax().ancestors().find_map(ast::Module::cast); |
56 | if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) { | 56 | let position = match module.and_then(|it| it.item_list()) { |
57 | ctx.add_action( | 57 | Some(item_list) => item_list.syntax().clone(), |
58 | AssistId("add_import"), | 58 | None => { |
59 | format!("import {} in mod {}", fmt_segments(&segments), name.text()), | 59 | let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; |
60 | |edit| { | 60 | current_file.syntax().clone() |
61 | apply_auto_import( | ||
62 | item_list.syntax(), | ||
63 | &path, | ||
64 | &segments, | ||
65 | edit.text_edit_builder(), | ||
66 | ); | ||
67 | }, | ||
68 | ); | ||
69 | } | 61 | } |
70 | } else { | 62 | }; |
71 | let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; | ||
72 | ctx.add_action( | ||
73 | AssistId("add_import"), | ||
74 | format!("import {} in the current file", fmt_segments(&segments)), | ||
75 | |edit| { | ||
76 | apply_auto_import( | ||
77 | current_file.syntax(), | ||
78 | &path, | ||
79 | &segments, | ||
80 | edit.text_edit_builder(), | ||
81 | ); | ||
82 | }, | ||
83 | ); | ||
84 | } | ||
85 | 63 | ||
86 | ctx.build() | 64 | ctx.add_assist(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { |
65 | apply_auto_import(&position, &path, &segments, edit.text_edit_builder()); | ||
66 | }) | ||
87 | } | 67 | } |
88 | 68 | ||
89 | fn collect_path_segments_raw( | 69 | fn collect_path_segments_raw( |
@@ -595,9 +575,10 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option<Vec<SmolStr>> { | |||
595 | 575 | ||
596 | #[cfg(test)] | 576 | #[cfg(test)] |
597 | mod tests { | 577 | mod tests { |
598 | use super::*; | ||
599 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 578 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
600 | 579 | ||
580 | use super::*; | ||
581 | |||
601 | #[test] | 582 | #[test] |
602 | fn test_auto_import_add_use_no_anchor() { | 583 | fn test_auto_import_add_use_no_anchor() { |
603 | check_assist( | 584 | check_assist( |
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 2585f3045..41de23921 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -91,7 +91,7 @@ pub(crate) fn add_missing_default_members(ctx: AssistCtx<impl HirDatabase>) -> O | |||
91 | } | 91 | } |
92 | 92 | ||
93 | fn add_missing_impl_members_inner( | 93 | fn add_missing_impl_members_inner( |
94 | mut ctx: AssistCtx<impl HirDatabase>, | 94 | ctx: AssistCtx<impl HirDatabase>, |
95 | mode: AddMissingImplMembersMode, | 95 | mode: AddMissingImplMembersMode, |
96 | assist_id: &'static str, | 96 | assist_id: &'static str, |
97 | label: &'static str, | 97 | label: &'static str, |
@@ -133,7 +133,7 @@ fn add_missing_impl_members_inner( | |||
133 | return None; | 133 | return None; |
134 | } | 134 | } |
135 | 135 | ||
136 | ctx.add_action(AssistId(assist_id), label, |edit| { | 136 | ctx.add_assist(AssistId(assist_id), label, |edit| { |
137 | let n_existing_items = impl_item_list.impl_items().count(); | 137 | let n_existing_items = impl_item_list.impl_items().count(); |
138 | let items = missing_items | 138 | let items = missing_items |
139 | .into_iter() | 139 | .into_iter() |
@@ -150,9 +150,7 @@ fn add_missing_impl_members_inner( | |||
150 | 150 | ||
151 | edit.replace_ast(impl_item_list, new_impl_item_list); | 151 | edit.replace_ast(impl_item_list, new_impl_item_list); |
152 | edit.set_cursor(cursor_position); | 152 | edit.set_cursor(cursor_position); |
153 | }); | 153 | }) |
154 | |||
155 | ctx.build() | ||
156 | } | 154 | } |
157 | 155 | ||
158 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 156 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/assists/apply_demorgan.rs index 8d5984a58..068da1774 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs | |||
@@ -23,7 +23,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
23 | // if !(x == 4 && y) {} | 23 | // if !(x == 4 && y) {} |
24 | // } | 24 | // } |
25 | // ``` | 25 | // ``` |
26 | pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 26 | pub(crate) fn apply_demorgan(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
27 | let expr = ctx.find_node_at_offset::<ast::BinExpr>()?; | 27 | let expr = ctx.find_node_at_offset::<ast::BinExpr>()?; |
28 | let op = expr.op_kind()?; | 28 | let op = expr.op_kind()?; |
29 | let op_range = expr.op_token()?.text_range(); | 29 | let op_range = expr.op_token()?.text_range(); |
@@ -39,13 +39,12 @@ pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Ass | |||
39 | let not_lhs = undo_negation(lhs)?; | 39 | let not_lhs = undo_negation(lhs)?; |
40 | let not_rhs = undo_negation(rhs)?; | 40 | let not_rhs = undo_negation(rhs)?; |
41 | 41 | ||
42 | ctx.add_action(AssistId("apply_demorgan"), "apply demorgan's law", |edit| { | 42 | ctx.add_assist(AssistId("apply_demorgan"), "apply demorgan's law", |edit| { |
43 | edit.target(op_range); | 43 | edit.target(op_range); |
44 | edit.replace(op_range, opposite_op); | 44 | edit.replace(op_range, opposite_op); |
45 | edit.replace(lhs_range, format!("!({}", not_lhs)); | 45 | edit.replace(lhs_range, format!("!({}", not_lhs)); |
46 | edit.replace(rhs_range, format!("{})", not_rhs)); | 46 | edit.replace(rhs_range, format!("{})", not_rhs)); |
47 | }); | 47 | }) |
48 | ctx.build() | ||
49 | } | 48 | } |
50 | 49 | ||
51 | // Return the opposite text for a given logical operator, if it makes sense | 50 | // Return the opposite text for a given logical operator, if it makes sense |
diff --git a/crates/ra_assists/src/assists/change_visibility.rs b/crates/ra_assists/src/assists/change_visibility.rs index 770ea04fa..132c9dc1d 100644 --- a/crates/ra_assists/src/assists/change_visibility.rs +++ b/crates/ra_assists/src/assists/change_visibility.rs | |||
@@ -29,7 +29,7 @@ pub(crate) fn change_visibility(ctx: AssistCtx<impl HirDatabase>) -> Option<Assi | |||
29 | add_vis(ctx) | 29 | add_vis(ctx) |
30 | } | 30 | } |
31 | 31 | ||
32 | fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 32 | fn add_vis(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
33 | let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { | 33 | let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { |
34 | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, | 34 | T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, |
35 | _ => false, | 35 | _ => false, |
@@ -57,13 +57,11 @@ fn add_vis(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
57 | (vis_offset(field.syntax()), ident.text_range()) | 57 | (vis_offset(field.syntax()), ident.text_range()) |
58 | }; | 58 | }; |
59 | 59 | ||
60 | ctx.add_action(AssistId("change_visibility"), "make pub(crate)", |edit| { | 60 | ctx.add_assist(AssistId("change_visibility"), "make pub(crate)", |edit| { |
61 | edit.target(target); | 61 | edit.target(target); |
62 | edit.insert(offset, "pub(crate) "); | 62 | edit.insert(offset, "pub(crate) "); |
63 | edit.set_cursor(offset); | 63 | edit.set_cursor(offset); |
64 | }); | 64 | }) |
65 | |||
66 | ctx.build() | ||
67 | } | 65 | } |
68 | 66 | ||
69 | fn vis_offset(node: &SyntaxNode) -> TextUnit { | 67 | fn vis_offset(node: &SyntaxNode) -> TextUnit { |
@@ -77,24 +75,20 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { | |||
77 | .unwrap_or_else(|| node.text_range().start()) | 75 | .unwrap_or_else(|| node.text_range().start()) |
78 | } | 76 | } |
79 | 77 | ||
80 | fn change_vis(mut ctx: AssistCtx<impl HirDatabase>, vis: ast::Visibility) -> Option<Assist> { | 78 | fn change_vis(ctx: AssistCtx<impl HirDatabase>, vis: ast::Visibility) -> Option<Assist> { |
81 | if vis.syntax().text() == "pub" { | 79 | if vis.syntax().text() == "pub" { |
82 | ctx.add_action(AssistId("change_visibility"), "change to pub(crate)", |edit| { | 80 | return ctx.add_assist(AssistId("change_visibility"), "change to pub(crate)", |edit| { |
83 | edit.target(vis.syntax().text_range()); | 81 | edit.target(vis.syntax().text_range()); |
84 | edit.replace(vis.syntax().text_range(), "pub(crate)"); | 82 | edit.replace(vis.syntax().text_range(), "pub(crate)"); |
85 | edit.set_cursor(vis.syntax().text_range().start()) | 83 | edit.set_cursor(vis.syntax().text_range().start()) |
86 | }); | 84 | }); |
87 | |||
88 | return ctx.build(); | ||
89 | } | 85 | } |
90 | if vis.syntax().text() == "pub(crate)" { | 86 | if vis.syntax().text() == "pub(crate)" { |
91 | ctx.add_action(AssistId("change_visibility"), "change to pub", |edit| { | 87 | return ctx.add_assist(AssistId("change_visibility"), "change to pub", |edit| { |
92 | edit.target(vis.syntax().text_range()); | 88 | edit.target(vis.syntax().text_range()); |
93 | edit.replace(vis.syntax().text_range(), "pub"); | 89 | edit.replace(vis.syntax().text_range(), "pub"); |
94 | edit.set_cursor(vis.syntax().text_range().start()); | 90 | edit.set_cursor(vis.syntax().text_range().start()); |
95 | }); | 91 | }); |
96 | |||
97 | return ctx.build(); | ||
98 | } | 92 | } |
99 | None | 93 | None |
100 | } | 94 | } |
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index 75822dc65..e839d831e 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs | |||
@@ -35,7 +35,7 @@ use crate::{ | |||
35 | // bar(); | 35 | // bar(); |
36 | // } | 36 | // } |
37 | // ``` | 37 | // ``` |
38 | pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 38 | pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
39 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | 39 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; |
40 | let expr = if_expr.condition()?.expr()?; | 40 | let expr = if_expr.condition()?.expr()?; |
41 | let then_block = if_expr.then_branch()?.block()?; | 41 | let then_block = if_expr.then_branch()?.block()?; |
@@ -75,7 +75,7 @@ pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> | |||
75 | then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; | 75 | then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; |
76 | let cursor_position = ctx.frange.range.start(); | 76 | let cursor_position = ctx.frange.range.start(); |
77 | 77 | ||
78 | ctx.add_action(AssistId("convert_to_guarded_return"), "convert to guarded return", |edit| { | 78 | ctx.add_assist(AssistId("convert_to_guarded_return"), "convert to guarded return", |edit| { |
79 | let if_indent_level = IndentLevel::from_node(&if_expr.syntax()); | 79 | let if_indent_level = IndentLevel::from_node(&if_expr.syntax()); |
80 | let new_if_expr = | 80 | let new_if_expr = |
81 | if_indent_level.increase_indent(make::if_expression(&expr, early_expression)); | 81 | if_indent_level.increase_indent(make::if_expression(&expr, early_expression)); |
@@ -105,8 +105,7 @@ pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx<impl HirDatabase>) -> | |||
105 | edit.target(if_expr.syntax().text_range()); | 105 | edit.target(if_expr.syntax().text_range()); |
106 | edit.replace_ast(parent_block, ast::Block::cast(new_block).unwrap()); | 106 | edit.replace_ast(parent_block, ast::Block::cast(new_block).unwrap()); |
107 | edit.set_cursor(cursor_position); | 107 | edit.set_cursor(cursor_position); |
108 | }); | 108 | }) |
109 | ctx.build() | ||
110 | } | 109 | } |
111 | 110 | ||
112 | #[cfg(test)] | 111 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs index c62c0efbe..2b74f355c 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(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 34 | pub(crate) fn fill_match_arms(ctx: AssistCtx<impl HirDatabase>) -> 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 | ||
@@ -53,7 +53,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
53 | }; | 53 | }; |
54 | let variant_list = enum_def.variant_list()?; | 54 | let variant_list = enum_def.variant_list()?; |
55 | 55 | ||
56 | ctx.add_action(AssistId("fill_match_arms"), "fill match arms", |edit| { | 56 | ctx.add_assist(AssistId("fill_match_arms"), "fill match arms", |edit| { |
57 | let indent_level = IndentLevel::from_node(match_arm_list.syntax()); | 57 | let indent_level = IndentLevel::from_node(match_arm_list.syntax()); |
58 | 58 | ||
59 | let new_arm_list = { | 59 | let new_arm_list = { |
@@ -67,9 +67,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
67 | edit.target(match_expr.syntax().text_range()); | 67 | edit.target(match_expr.syntax().text_range()); |
68 | edit.set_cursor(expr.syntax().text_range().start()); | 68 | edit.set_cursor(expr.syntax().text_range().start()); |
69 | edit.replace_ast(match_arm_list, new_arm_list); | 69 | edit.replace_ast(match_arm_list, new_arm_list); |
70 | }); | 70 | }) |
71 | |||
72 | ctx.build() | ||
73 | } | 71 | } |
74 | 72 | ||
75 | fn is_trivial(arm: &ast::MatchArm) -> bool { | 73 | fn is_trivial(arm: &ast::MatchArm) -> bool { |
diff --git a/crates/ra_assists/src/assists/flip_binexpr.rs b/crates/ra_assists/src/assists/flip_binexpr.rs index 9765d5ddd..386045eb0 100644 --- a/crates/ra_assists/src/assists/flip_binexpr.rs +++ b/crates/ra_assists/src/assists/flip_binexpr.rs | |||
@@ -18,7 +18,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
18 | // let _ = 2 + 90; | 18 | // let _ = 2 + 90; |
19 | // } | 19 | // } |
20 | // ``` | 20 | // ``` |
21 | pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 21 | pub(crate) fn flip_binexpr(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
22 | let expr = ctx.find_node_at_offset::<BinExpr>()?; | 22 | let expr = ctx.find_node_at_offset::<BinExpr>()?; |
23 | let lhs = expr.lhs()?.syntax().clone(); | 23 | let lhs = expr.lhs()?.syntax().clone(); |
24 | let rhs = expr.rhs()?.syntax().clone(); | 24 | let rhs = expr.rhs()?.syntax().clone(); |
@@ -34,16 +34,14 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assis | |||
34 | return None; | 34 | return None; |
35 | } | 35 | } |
36 | 36 | ||
37 | ctx.add_action(AssistId("flip_binexpr"), "flip binary expression", |edit| { | 37 | ctx.add_assist(AssistId("flip_binexpr"), "flip binary expression", |edit| { |
38 | edit.target(op_range); | 38 | edit.target(op_range); |
39 | if let FlipAction::FlipAndReplaceOp(new_op) = action { | 39 | if let FlipAction::FlipAndReplaceOp(new_op) = action { |
40 | edit.replace(op_range, new_op); | 40 | edit.replace(op_range, new_op); |
41 | } | 41 | } |
42 | edit.replace(lhs.text_range(), rhs.text()); | 42 | edit.replace(lhs.text_range(), rhs.text()); |
43 | edit.replace(rhs.text_range(), lhs.text()); | 43 | edit.replace(rhs.text_range(), lhs.text()); |
44 | }); | 44 | }) |
45 | |||
46 | ctx.build() | ||
47 | } | 45 | } |
48 | 46 | ||
49 | enum FlipAction { | 47 | enum FlipAction { |
diff --git a/crates/ra_assists/src/assists/flip_comma.rs b/crates/ra_assists/src/assists/flip_comma.rs index 53ba8011d..9be1c1dc6 100644 --- a/crates/ra_assists/src/assists/flip_comma.rs +++ b/crates/ra_assists/src/assists/flip_comma.rs | |||
@@ -18,7 +18,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
18 | // ((3, 4), (1, 2)); | 18 | // ((3, 4), (1, 2)); |
19 | // } | 19 | // } |
20 | // ``` | 20 | // ``` |
21 | pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 21 | pub(crate) fn flip_comma(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
22 | let comma = ctx.find_token_at_offset(T![,])?; | 22 | let comma = ctx.find_token_at_offset(T![,])?; |
23 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; | 23 | let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; |
24 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; | 24 | let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; |
@@ -29,13 +29,11 @@ pub(crate) fn flip_comma(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
29 | return None; | 29 | return None; |
30 | } | 30 | } |
31 | 31 | ||
32 | ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { | 32 | ctx.add_assist(AssistId("flip_comma"), "flip comma", |edit| { |
33 | edit.target(comma.text_range()); | 33 | edit.target(comma.text_range()); |
34 | edit.replace(prev.text_range(), next.to_string()); | 34 | edit.replace(prev.text_range(), next.to_string()); |
35 | edit.replace(next.text_range(), prev.to_string()); | 35 | edit.replace(next.text_range(), prev.to_string()); |
36 | }); | 36 | }) |
37 | |||
38 | ctx.build() | ||
39 | } | 37 | } |
40 | 38 | ||
41 | #[cfg(test)] | 39 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs index 1625b241f..6017b39dd 100644 --- a/crates/ra_assists/src/assists/flip_trait_bound.rs +++ b/crates/ra_assists/src/assists/flip_trait_bound.rs | |||
@@ -18,7 +18,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
18 | // ``` | 18 | // ``` |
19 | // fn foo<T: Copy + Clone>() { } | 19 | // fn foo<T: Copy + Clone>() { } |
20 | // ``` | 20 | // ``` |
21 | pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 21 | pub(crate) fn flip_trait_bound(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
22 | // We want to replicate the behavior of `flip_binexpr` by only suggesting | 22 | // We want to replicate the behavior of `flip_binexpr` by only suggesting |
23 | // the assist when the cursor is on a `+` | 23 | // the assist when the cursor is on a `+` |
24 | let plus = ctx.find_token_at_offset(T![+])?; | 24 | let plus = ctx.find_token_at_offset(T![+])?; |
@@ -33,13 +33,11 @@ pub(crate) fn flip_trait_bound(mut ctx: AssistCtx<impl HirDatabase>) -> Option<A | |||
33 | non_trivia_sibling(plus.clone().into(), Direction::Next)?, | 33 | non_trivia_sibling(plus.clone().into(), Direction::Next)?, |
34 | ); | 34 | ); |
35 | 35 | ||
36 | ctx.add_action(AssistId("flip_trait_bound"), "flip trait bound", |edit| { | 36 | ctx.add_assist(AssistId("flip_trait_bound"), "flip trait bound", |edit| { |
37 | edit.target(plus.text_range()); | 37 | edit.target(plus.text_range()); |
38 | edit.replace(before.text_range(), after.to_string()); | 38 | edit.replace(before.text_range(), after.to_string()); |
39 | edit.replace(after.text_range(), before.to_string()); | 39 | edit.replace(after.text_range(), before.to_string()); |
40 | }); | 40 | }) |
41 | |||
42 | ctx.build() | ||
43 | } | 41 | } |
44 | 42 | ||
45 | #[cfg(test)] | 43 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/inline_local_variable.rs b/crates/ra_assists/src/assists/inline_local_variable.rs index fe8fa2a86..f43910574 100644 --- a/crates/ra_assists/src/assists/inline_local_variable.rs +++ b/crates/ra_assists/src/assists/inline_local_variable.rs | |||
@@ -23,7 +23,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
23 | // (1 + 2) * 4; | 23 | // (1 + 2) * 4; |
24 | // } | 24 | // } |
25 | // ``` | 25 | // ``` |
26 | pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 26 | pub(crate) fn inline_local_varialbe(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
27 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; | 27 | let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; |
28 | let bind_pat = match let_stmt.pat()? { | 28 | let bind_pat = match let_stmt.pat()? { |
29 | ast::Pat::BindPat(pat) => pat, | 29 | ast::Pat::BindPat(pat) => pat, |
@@ -93,7 +93,7 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
93 | let init_str = initializer_expr.syntax().text().to_string(); | 93 | let init_str = initializer_expr.syntax().text().to_string(); |
94 | let init_in_paren = format!("({})", &init_str); | 94 | let init_in_paren = format!("({})", &init_str); |
95 | 95 | ||
96 | ctx.add_action( | 96 | ctx.add_assist( |
97 | AssistId("inline_local_variable"), | 97 | AssistId("inline_local_variable"), |
98 | "inline local variable", | 98 | "inline local variable", |
99 | move |edit: &mut AssistBuilder| { | 99 | move |edit: &mut AssistBuilder| { |
@@ -107,9 +107,7 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
107 | } | 107 | } |
108 | edit.set_cursor(delete_range.start()) | 108 | edit.set_cursor(delete_range.start()) |
109 | }, | 109 | }, |
110 | ); | 110 | ) |
111 | |||
112 | ctx.build() | ||
113 | } | 111 | } |
114 | 112 | ||
115 | #[cfg(test)] | 113 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/introduce_variable.rs b/crates/ra_assists/src/assists/introduce_variable.rs index 8245dc99f..0623d4475 100644 --- a/crates/ra_assists/src/assists/introduce_variable.rs +++ b/crates/ra_assists/src/assists/introduce_variable.rs | |||
@@ -28,7 +28,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
28 | // var_name * 4; | 28 | // var_name * 4; |
29 | // } | 29 | // } |
30 | // ``` | 30 | // ``` |
31 | pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 31 | pub(crate) fn introduce_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
32 | if ctx.frange.range.is_empty() { | 32 | if ctx.frange.range.is_empty() { |
33 | return None; | 33 | return None; |
34 | } | 34 | } |
@@ -43,7 +43,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
43 | if indent.kind() != WHITESPACE { | 43 | if indent.kind() != WHITESPACE { |
44 | return None; | 44 | return None; |
45 | } | 45 | } |
46 | ctx.add_action(AssistId("introduce_variable"), "introduce variable", move |edit| { | 46 | ctx.add_assist(AssistId("introduce_variable"), "introduce variable", move |edit| { |
47 | let mut buf = String::new(); | 47 | let mut buf = String::new(); |
48 | 48 | ||
49 | let cursor_offset = if wrap_in_block { | 49 | let cursor_offset = if wrap_in_block { |
@@ -88,9 +88,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option | |||
88 | } | 88 | } |
89 | } | 89 | } |
90 | edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); | 90 | edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); |
91 | }); | 91 | }) |
92 | |||
93 | ctx.build() | ||
94 | } | 92 | } |
95 | 93 | ||
96 | /// Check whether the node is a valid expression which can be extracted to a variable. | 94 | /// Check whether the node is a valid expression which can be extracted to a variable. |
diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs index b0c4ee78b..e9f2cae91 100644 --- a/crates/ra_assists/src/assists/merge_match_arms.rs +++ b/crates/ra_assists/src/assists/merge_match_arms.rs | |||
@@ -26,7 +26,7 @@ use ra_syntax::ast::{AstNode, MatchArm}; | |||
26 | // } | 26 | // } |
27 | // } | 27 | // } |
28 | // ``` | 28 | // ``` |
29 | pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 29 | pub(crate) fn merge_match_arms(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
30 | let current_arm = ctx.find_node_at_offset::<MatchArm>()?; | 30 | let current_arm = ctx.find_node_at_offset::<MatchArm>()?; |
31 | 31 | ||
32 | // We check if the following match arm matches this one. We could, but don't, | 32 | // We check if the following match arm matches this one. We could, but don't, |
@@ -52,7 +52,7 @@ pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<A | |||
52 | 52 | ||
53 | let cursor_to_end = current_arm.syntax().text_range().end() - ctx.frange.range.start(); | 53 | let cursor_to_end = current_arm.syntax().text_range().end() - ctx.frange.range.start(); |
54 | 54 | ||
55 | ctx.add_action(AssistId("merge_match_arms"), "merge match arms", |edit| { | 55 | ctx.add_assist(AssistId("merge_match_arms"), "merge match arms", |edit| { |
56 | fn contains_placeholder(a: &MatchArm) -> bool { | 56 | fn contains_placeholder(a: &MatchArm) -> bool { |
57 | a.pats().any(|x| match x { | 57 | a.pats().any(|x| match x { |
58 | ra_syntax::ast::Pat::PlaceholderPat(..) => true, | 58 | ra_syntax::ast::Pat::PlaceholderPat(..) => true, |
@@ -80,9 +80,7 @@ pub(crate) fn merge_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<A | |||
80 | edit.target(current_arm.syntax().text_range()); | 80 | edit.target(current_arm.syntax().text_range()); |
81 | edit.replace(TextRange::from_to(start, end), arm); | 81 | edit.replace(TextRange::from_to(start, end), arm); |
82 | edit.set_cursor(start + offset); | 82 | edit.set_cursor(start + offset); |
83 | }); | 83 | }) |
84 | |||
85 | ctx.build() | ||
86 | } | 84 | } |
87 | 85 | ||
88 | #[cfg(test)] | 86 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/move_bounds.rs b/crates/ra_assists/src/assists/move_bounds.rs index edf2897c6..3145d7625 100644 --- a/crates/ra_assists/src/assists/move_bounds.rs +++ b/crates/ra_assists/src/assists/move_bounds.rs | |||
@@ -22,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
22 | // f(x) | 22 | // f(x) |
23 | // } | 23 | // } |
24 | // ``` | 24 | // ``` |
25 | pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
26 | let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?; | 26 | let type_param_list = ctx.find_node_at_offset::<ast::TypeParamList>()?; |
27 | 27 | ||
28 | let mut type_params = type_param_list.type_params(); | 28 | let mut type_params = type_param_list.type_params(); |
@@ -46,38 +46,30 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx<impl HirDatabase>) | |||
46 | _ => return None, | 46 | _ => return None, |
47 | }; | 47 | }; |
48 | 48 | ||
49 | ctx.add_action( | 49 | ctx.add_assist(AssistId("move_bounds_to_where_clause"), "move_bounds_to_where_clause", |edit| { |
50 | AssistId("move_bounds_to_where_clause"), | 50 | let new_params = type_param_list |
51 | "move_bounds_to_where_clause", | 51 | .type_params() |
52 | |edit| { | 52 | .filter(|it| it.type_bound_list().is_some()) |
53 | let new_params = type_param_list | 53 | .map(|type_param| { |
54 | .type_params() | 54 | let without_bounds = type_param.remove_bounds(); |
55 | .filter(|it| it.type_bound_list().is_some()) | 55 | (type_param, without_bounds) |
56 | .map(|type_param| { | 56 | }); |
57 | let without_bounds = type_param.remove_bounds(); | 57 | |
58 | (type_param, without_bounds) | 58 | let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); |
59 | }); | 59 | edit.replace_ast(type_param_list.clone(), new_type_param_list); |
60 | 60 | ||
61 | let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); | 61 | let where_clause = { |
62 | edit.replace_ast(type_param_list.clone(), new_type_param_list); | 62 | let predicates = type_param_list.type_params().filter_map(build_predicate); |
63 | 63 | make::where_clause(predicates) | |
64 | let where_clause = { | 64 | }; |
65 | let predicates = type_param_list.type_params().filter_map(build_predicate); | 65 | |
66 | make::where_clause(predicates) | 66 | let to_insert = match anchor.prev_sibling_or_token() { |
67 | }; | 67 | Some(ref elem) if elem.kind() == WHITESPACE => format!("{} ", where_clause.syntax()), |
68 | 68 | _ => format!(" {}", where_clause.syntax()), | |
69 | let to_insert = match anchor.prev_sibling_or_token() { | 69 | }; |
70 | Some(ref elem) if elem.kind() == WHITESPACE => { | 70 | edit.insert(anchor.text_range().start(), to_insert); |
71 | format!("{} ", where_clause.syntax()) | 71 | edit.target(type_param_list.syntax().text_range()); |
72 | } | 72 | }) |
73 | _ => format!(" {}", where_clause.syntax()), | ||
74 | }; | ||
75 | edit.insert(anchor.text_range().start(), to_insert); | ||
76 | edit.target(type_param_list.syntax().text_range()); | ||
77 | }, | ||
78 | ); | ||
79 | |||
80 | ctx.build() | ||
81 | } | 73 | } |
82 | 74 | ||
83 | fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { | 75 | fn build_predicate(param: ast::TypeParam) -> Option<ast::WherePred> { |
diff --git a/crates/ra_assists/src/assists/move_guard.rs b/crates/ra_assists/src/assists/move_guard.rs index e820a73c8..b49ec6172 100644 --- a/crates/ra_assists/src/assists/move_guard.rs +++ b/crates/ra_assists/src/assists/move_guard.rs | |||
@@ -32,7 +32,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
32 | // } | 32 | // } |
33 | // } | 33 | // } |
34 | // ``` | 34 | // ``` |
35 | pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 35 | pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
36 | let match_arm = ctx.find_node_at_offset::<MatchArm>()?; | 36 | let match_arm = ctx.find_node_at_offset::<MatchArm>()?; |
37 | let guard = match_arm.guard()?; | 37 | let guard = match_arm.guard()?; |
38 | let space_before_guard = guard.syntax().prev_sibling_or_token(); | 38 | let space_before_guard = guard.syntax().prev_sibling_or_token(); |
@@ -41,7 +41,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op | |||
41 | let arm_expr = match_arm.expr()?; | 41 | let arm_expr = match_arm.expr()?; |
42 | let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); | 42 | let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); |
43 | 43 | ||
44 | ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { | 44 | ctx.add_assist(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { |
45 | edit.target(guard.syntax().text_range()); | 45 | edit.target(guard.syntax().text_range()); |
46 | let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { | 46 | let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { |
47 | Some(tok) => { | 47 | Some(tok) => { |
@@ -61,8 +61,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op | |||
61 | edit.set_cursor( | 61 | edit.set_cursor( |
62 | arm_expr.syntax().text_range().start() + TextUnit::from(3) - offseting_amount, | 62 | arm_expr.syntax().text_range().start() + TextUnit::from(3) - offseting_amount, |
63 | ); | 63 | ); |
64 | }); | 64 | }) |
65 | ctx.build() | ||
66 | } | 65 | } |
67 | 66 | ||
68 | // Assist: move_arm_cond_to_match_guard | 67 | // Assist: move_arm_cond_to_match_guard |
@@ -90,7 +89,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx<impl HirDatabase>) -> Op | |||
90 | // } | 89 | // } |
91 | // } | 90 | // } |
92 | // ``` | 91 | // ``` |
93 | pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 92 | pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
94 | let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?; | 93 | let match_arm: MatchArm = ctx.find_node_at_offset::<MatchArm>()?; |
95 | let last_match_pat = match_arm.pats().last()?; | 94 | let last_match_pat = match_arm.pats().last()?; |
96 | 95 | ||
@@ -110,7 +109,7 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) | |||
110 | 109 | ||
111 | let buf = format!(" if {}", cond.syntax().text()); | 110 | let buf = format!(" if {}", cond.syntax().text()); |
112 | 111 | ||
113 | ctx.add_action( | 112 | ctx.add_assist( |
114 | AssistId("move_arm_cond_to_match_guard"), | 113 | AssistId("move_arm_cond_to_match_guard"), |
115 | "move condition to match guard", | 114 | "move condition to match guard", |
116 | |edit| { | 115 | |edit| { |
@@ -127,8 +126,7 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx<impl HirDatabase>) | |||
127 | edit.insert(last_match_pat.syntax().text_range().end(), buf); | 126 | edit.insert(last_match_pat.syntax().text_range().end(), buf); |
128 | edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1)); | 127 | edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1)); |
129 | }, | 128 | }, |
130 | ); | 129 | ) |
131 | ctx.build() | ||
132 | } | 130 | } |
133 | 131 | ||
134 | #[cfg(test)] | 132 | #[cfg(test)] |
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index 2df48a838..86bbaeeef 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs | |||
@@ -22,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
22 | // r#"Hello, World!"#; | 22 | // r#"Hello, World!"#; |
23 | // } | 23 | // } |
24 | // ``` | 24 | // ``` |
25 | pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 25 | pub(crate) fn make_raw_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
26 | let token = ctx.find_token_at_offset(STRING)?; | 26 | let token = ctx.find_token_at_offset(STRING)?; |
27 | let text = token.text().as_str(); | 27 | let text = token.text().as_str(); |
28 | let usual_string_range = find_usual_string_range(text)?; | 28 | let usual_string_range = find_usual_string_range(text)?; |
@@ -41,7 +41,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
41 | if error.is_err() { | 41 | if error.is_err() { |
42 | return None; | 42 | return None; |
43 | } | 43 | } |
44 | ctx.add_action(AssistId("make_raw_string"), "make raw string", |edit| { | 44 | ctx.add_assist(AssistId("make_raw_string"), "make raw string", |edit| { |
45 | edit.target(token.text_range()); | 45 | edit.target(token.text_range()); |
46 | let max_hash_streak = count_hashes(&unescaped); | 46 | let max_hash_streak = count_hashes(&unescaped); |
47 | let mut hashes = String::with_capacity(max_hash_streak + 1); | 47 | let mut hashes = String::with_capacity(max_hash_streak + 1); |
@@ -49,8 +49,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
49 | hashes.push('#'); | 49 | hashes.push('#'); |
50 | } | 50 | } |
51 | edit.replace(token.text_range(), format!("r{}\"{}\"{}", hashes, unescaped, hashes)); | 51 | edit.replace(token.text_range(), format!("r{}\"{}\"{}", hashes, unescaped, hashes)); |
52 | }); | 52 | }) |
53 | ctx.build() | ||
54 | } | 53 | } |
55 | 54 | ||
56 | // Assist: make_usual_string | 55 | // Assist: make_usual_string |
@@ -68,11 +67,11 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
68 | // "Hello, \"World!\""; | 67 | // "Hello, \"World!\""; |
69 | // } | 68 | // } |
70 | // ``` | 69 | // ``` |
71 | pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 70 | pub(crate) fn make_usual_string(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
72 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 71 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
73 | let text = token.text().as_str(); | 72 | let text = token.text().as_str(); |
74 | let usual_string_range = find_usual_string_range(text)?; | 73 | let usual_string_range = find_usual_string_range(text)?; |
75 | ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| { | 74 | ctx.add_assist(AssistId("make_usual_string"), "make usual string", |edit| { |
76 | edit.target(token.text_range()); | 75 | edit.target(token.text_range()); |
77 | // parse inside string to escape `"` | 76 | // parse inside string to escape `"` |
78 | let start_of_inside = usual_string_range.start().to_usize() + 1; | 77 | let start_of_inside = usual_string_range.start().to_usize() + 1; |
@@ -80,8 +79,7 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
80 | let inside_str = &text[start_of_inside..end_of_inside]; | 79 | let inside_str = &text[start_of_inside..end_of_inside]; |
81 | let escaped = inside_str.escape_default().to_string(); | 80 | let escaped = inside_str.escape_default().to_string(); |
82 | edit.replace(token.text_range(), format!("\"{}\"", escaped)); | 81 | edit.replace(token.text_range(), format!("\"{}\"", escaped)); |
83 | }); | 82 | }) |
84 | ctx.build() | ||
85 | } | 83 | } |
86 | 84 | ||
87 | // Assist: add_hash | 85 | // Assist: add_hash |
@@ -99,14 +97,13 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option< | |||
99 | // r##"Hello, World!"##; | 97 | // r##"Hello, World!"##; |
100 | // } | 98 | // } |
101 | // ``` | 99 | // ``` |
102 | pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 100 | pub(crate) fn add_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
103 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 101 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
104 | ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| { | 102 | ctx.add_assist(AssistId("add_hash"), "add hash to raw string", |edit| { |
105 | edit.target(token.text_range()); | 103 | edit.target(token.text_range()); |
106 | edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#"); | 104 | edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#"); |
107 | edit.insert(token.text_range().end(), "#"); | 105 | edit.insert(token.text_range().end(), "#"); |
108 | }); | 106 | }) |
109 | ctx.build() | ||
110 | } | 107 | } |
111 | 108 | ||
112 | // Assist: remove_hash | 109 | // Assist: remove_hash |
@@ -124,14 +121,14 @@ pub(crate) fn add_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | |||
124 | // r"Hello, World!"; | 121 | // r"Hello, World!"; |
125 | // } | 122 | // } |
126 | // ``` | 123 | // ``` |
127 | pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 124 | pub(crate) fn remove_hash(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
128 | let token = ctx.find_token_at_offset(RAW_STRING)?; | 125 | let token = ctx.find_token_at_offset(RAW_STRING)?; |
129 | let text = token.text().as_str(); | 126 | let text = token.text().as_str(); |
130 | if text.starts_with("r\"") { | 127 | if text.starts_with("r\"") { |
131 | // no hash to remove | 128 | // no hash to remove |
132 | return None; | 129 | return None; |
133 | } | 130 | } |
134 | ctx.add_action(AssistId("remove_hash"), "remove hash from raw string", |edit| { | 131 | ctx.add_assist(AssistId("remove_hash"), "remove hash from raw string", |edit| { |
135 | edit.target(token.text_range()); | 132 | edit.target(token.text_range()); |
136 | let result = &text[2..text.len() - 1]; | 133 | let result = &text[2..text.len() - 1]; |
137 | let result = if result.starts_with("\"") { | 134 | let result = if result.starts_with("\"") { |
@@ -142,8 +139,7 @@ pub(crate) fn remove_hash(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist | |||
142 | result.to_owned() | 139 | result.to_owned() |
143 | }; | 140 | }; |
144 | edit.replace(token.text_range(), format!("r{}", result)); | 141 | edit.replace(token.text_range(), format!("r{}", result)); |
145 | }); | 142 | }) |
146 | ctx.build() | ||
147 | } | 143 | } |
148 | 144 | ||
149 | fn count_hashes(s: &str) -> usize { | 145 | fn count_hashes(s: &str) -> usize { |
diff --git a/crates/ra_assists/src/assists/remove_dbg.rs b/crates/ra_assists/src/assists/remove_dbg.rs index 44b8de814..aedf8747f 100644 --- a/crates/ra_assists/src/assists/remove_dbg.rs +++ b/crates/ra_assists/src/assists/remove_dbg.rs | |||
@@ -21,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
21 | // 92; | 21 | // 92; |
22 | // } | 22 | // } |
23 | // ``` | 23 | // ``` |
24 | pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 24 | pub(crate) fn remove_dbg(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
25 | let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; | 25 | let macro_call = ctx.find_node_at_offset::<ast::MacroCall>()?; |
26 | 26 | ||
27 | if !is_valid_macrocall(¯o_call, "dbg")? { | 27 | if !is_valid_macrocall(¯o_call, "dbg")? { |
@@ -58,13 +58,11 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> | |||
58 | text.slice(without_parens).to_string() | 58 | text.slice(without_parens).to_string() |
59 | }; | 59 | }; |
60 | 60 | ||
61 | ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| { | 61 | ctx.add_assist(AssistId("remove_dbg"), "remove dbg!()", |edit| { |
62 | edit.target(macro_call.syntax().text_range()); | 62 | edit.target(macro_call.syntax().text_range()); |
63 | edit.replace(macro_range, macro_content); | 63 | edit.replace(macro_range, macro_content); |
64 | edit.set_cursor(cursor_pos); | 64 | edit.set_cursor(cursor_pos); |
65 | }); | 65 | }) |
66 | |||
67 | ctx.build() | ||
68 | } | 66 | } |
69 | 67 | ||
70 | /// Verifies that the given macro_call actually matches the given name | 68 | /// Verifies that the given macro_call actually matches the given name |
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 58ef2ff20..dff84d865 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 | |||
@@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
31 | // } | 31 | // } |
32 | // } | 32 | // } |
33 | // ``` | 33 | // ``` |
34 | pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 34 | pub(crate) fn replace_if_let_with_match(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
35 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | 35 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; |
36 | let cond = if_expr.condition()?; | 36 | let cond = if_expr.condition()?; |
37 | let pat = cond.pat()?; | 37 | let pat = cond.pat()?; |
@@ -42,14 +42,12 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx<impl HirDatabase>) -> | |||
42 | ast::ElseBranch::IfExpr(_) => return None, | 42 | ast::ElseBranch::IfExpr(_) => return None, |
43 | }; | 43 | }; |
44 | 44 | ||
45 | ctx.add_action(AssistId("replace_if_let_with_match"), "replace with match", |edit| { | 45 | ctx.add_assist(AssistId("replace_if_let_with_match"), "replace with match", |edit| { |
46 | let match_expr = build_match_expr(expr, pat, then_block, else_block); | 46 | let match_expr = build_match_expr(expr, pat, then_block, else_block); |
47 | edit.target(if_expr.syntax().text_range()); | 47 | edit.target(if_expr.syntax().text_range()); |
48 | edit.replace_node_and_indent(if_expr.syntax(), match_expr); | 48 | edit.replace_node_and_indent(if_expr.syntax(), match_expr); |
49 | edit.set_cursor(if_expr.syntax().text_range().start()) | 49 | edit.set_cursor(if_expr.syntax().text_range().start()) |
50 | }); | 50 | }) |
51 | |||
52 | ctx.build() | ||
53 | } | 51 | } |
54 | 52 | ||
55 | fn build_match_expr( | 53 | fn build_match_expr( |
diff --git a/crates/ra_assists/src/assists/split_import.rs b/crates/ra_assists/src/assists/split_import.rs index 8d8a28987..5f8d6b0be 100644 --- a/crates/ra_assists/src/assists/split_import.rs +++ b/crates/ra_assists/src/assists/split_import.rs | |||
@@ -16,7 +16,7 @@ use crate::{Assist, AssistCtx, AssistId}; | |||
16 | // ``` | 16 | // ``` |
17 | // use std::{collections::HashMap}; | 17 | // use std::{collections::HashMap}; |
18 | // ``` | 18 | // ``` |
19 | pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 19 | pub(crate) fn split_import(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
20 | let colon_colon = ctx.find_token_at_offset(T![::])?; | 20 | let colon_colon = ctx.find_token_at_offset(T![::])?; |
21 | let path = ast::Path::cast(colon_colon.parent())?; | 21 | let path = ast::Path::cast(colon_colon.parent())?; |
22 | let top_path = successors(Some(path), |it| it.parent_path()).last()?; | 22 | let top_path = successors(Some(path), |it| it.parent_path()).last()?; |
@@ -32,14 +32,12 @@ pub(crate) fn split_import(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assis | |||
32 | None => top_path.syntax().text_range().end(), | 32 | None => top_path.syntax().text_range().end(), |
33 | }; | 33 | }; |
34 | 34 | ||
35 | ctx.add_action(AssistId("split_import"), "split import", |edit| { | 35 | ctx.add_assist(AssistId("split_import"), "split import", |edit| { |
36 | edit.target(colon_colon.text_range()); | 36 | edit.target(colon_colon.text_range()); |
37 | edit.insert(l_curly, "{"); | 37 | edit.insert(l_curly, "{"); |
38 | edit.insert(r_curly, "}"); | 38 | edit.insert(r_curly, "}"); |
39 | edit.set_cursor(l_curly + TextUnit::of_str("{")); | 39 | edit.set_cursor(l_curly + TextUnit::of_str("{")); |
40 | }); | 40 | }) |
41 | |||
42 | ctx.build() | ||
43 | } | 41 | } |
44 | 42 | ||
45 | #[cfg(test)] | 43 | #[cfg(test)] |