aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/assists/introduce_variable.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/assists/introduce_variable.rs')
-rw-r--r--crates/ra_assists/src/assists/introduce_variable.rs26
1 files changed, 19 insertions, 7 deletions
diff --git a/crates/ra_assists/src/assists/introduce_variable.rs b/crates/ra_assists/src/assists/introduce_variable.rs
index 43378c4b0..0623d4475 100644
--- a/crates/ra_assists/src/assists/introduce_variable.rs
+++ b/crates/ra_assists/src/assists/introduce_variable.rs
@@ -1,5 +1,3 @@
1//! FIXME: write short doc here
2
3use format_buf::format; 1use format_buf::format;
4use hir::db::HirDatabase; 2use hir::db::HirDatabase;
5use ra_syntax::{ 3use ra_syntax::{
@@ -14,7 +12,23 @@ use test_utils::tested_by;
14 12
15use crate::{Assist, AssistCtx, AssistId}; 13use crate::{Assist, AssistCtx, AssistId};
16 14
17pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { 15// Assist: introduce_variable
16//
17// Extracts subexpression into a variable.
18//
19// ```
20// fn main() {
21// <|>(1 + 2)<|> * 4;
22// }
23// ```
24// ->
25// ```
26// fn main() {
27// let var_name = (1 + 2);
28// var_name * 4;
29// }
30// ```
31pub(crate) fn introduce_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
18 if ctx.frange.range.is_empty() { 32 if ctx.frange.range.is_empty() {
19 return None; 33 return None;
20 } 34 }
@@ -29,7 +43,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
29 if indent.kind() != WHITESPACE { 43 if indent.kind() != WHITESPACE {
30 return None; 44 return None;
31 } 45 }
32 ctx.add_action(AssistId("introduce_variable"), "introduce variable", move |edit| { 46 ctx.add_assist(AssistId("introduce_variable"), "introduce variable", move |edit| {
33 let mut buf = String::new(); 47 let mut buf = String::new();
34 48
35 let cursor_offset = if wrap_in_block { 49 let cursor_offset = if wrap_in_block {
@@ -74,9 +88,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx<impl HirDatabase>) -> Option
74 } 88 }
75 } 89 }
76 edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); 90 edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset);
77 }); 91 })
78
79 ctx.build()
80} 92}
81 93
82/// 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.