aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/inline_local_variable.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/inline_local_variable.rs')
-rw-r--r--crates/ra_assists/src/handlers/inline_local_variable.rs31
1 files changed, 14 insertions, 17 deletions
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs
index e5765c845..5b26814d3 100644
--- a/crates/ra_assists/src/handlers/inline_local_variable.rs
+++ b/crates/ra_assists/src/handlers/inline_local_variable.rs
@@ -5,7 +5,10 @@ use ra_syntax::{
5}; 5};
6use test_utils::tested_by; 6use test_utils::tested_by;
7 7
8use crate::{assist_ctx::ActionBuilder, Assist, AssistCtx, AssistId}; 8use crate::{
9 assist_context::{AssistContext, Assists},
10 AssistId,
11};
9 12
10// Assist: inline_local_variable 13// Assist: inline_local_variable
11// 14//
@@ -23,7 +26,7 @@ use crate::{assist_ctx::ActionBuilder, Assist, AssistCtx, AssistId};
23// (1 + 2) * 4; 26// (1 + 2) * 4;
24// } 27// }
25// ``` 28// ```
26pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { 29pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
27 let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?; 30 let let_stmt = ctx.find_node_at_offset::<ast::LetStmt>()?;
28 let bind_pat = match let_stmt.pat()? { 31 let bind_pat = match let_stmt.pat()? {
29 ast::Pat::BindPat(pat) => pat, 32 ast::Pat::BindPat(pat) => pat,
@@ -33,7 +36,7 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
33 tested_by!(test_not_inline_mut_variable); 36 tested_by!(test_not_inline_mut_variable);
34 return None; 37 return None;
35 } 38 }
36 if !bind_pat.syntax().text_range().contains_inclusive(ctx.frange.range.start()) { 39 if !bind_pat.syntax().text_range().contains_inclusive(ctx.offset()) {
37 tested_by!(not_applicable_outside_of_bind_pat); 40 tested_by!(not_applicable_outside_of_bind_pat);
38 return None; 41 return None;
39 } 42 }
@@ -107,20 +110,14 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> {
107 let init_in_paren = format!("({})", &init_str); 110 let init_in_paren = format!("({})", &init_str);
108 111
109 let target = bind_pat.syntax().text_range(); 112 let target = bind_pat.syntax().text_range();
110 ctx.add_assist( 113 acc.add(AssistId("inline_local_variable"), "Inline variable", target, move |builder| {
111 AssistId("inline_local_variable"), 114 builder.delete(delete_range);
112 "Inline variable", 115 for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) {
113 target, 116 let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() };
114 move |edit: &mut ActionBuilder| { 117 builder.replace(desc.file_range.range, replacement)
115 edit.delete(delete_range); 118 }
116 for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { 119 builder.set_cursor(delete_range.start())
117 let replacement = 120 })
118 if should_wrap { init_in_paren.clone() } else { init_str.clone() };
119 edit.replace(desc.file_range.range, replacement)
120 }
121 edit.set_cursor(delete_range.start())
122 },
123 )
124} 121}
125 122
126#[cfg(test)] 123#[cfg(test)]