aboutsummaryrefslogtreecommitdiff
path: root/crates/libeditor
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libeditor')
-rw-r--r--crates/libeditor/src/code_actions.rs34
1 files changed, 26 insertions, 8 deletions
diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs
index ebe70681b..4a07d1bc9 100644
--- a/crates/libeditor/src/code_actions.rs
+++ b/crates/libeditor/src/code_actions.rs
@@ -109,17 +109,22 @@ pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl F
109 } 109 }
110 Some(move || { 110 Some(move || {
111 let mut buf = String::new(); 111 let mut buf = String::new();
112 let mut edit = EditBuilder::new();
113
112 buf.push_str("let var_name = "); 114 buf.push_str("let var_name = ");
113 expr.syntax().text().push_to(&mut buf); 115 expr.syntax().text().push_to(&mut buf);
114 buf.push_str(";"); 116 if expr.syntax().range().start() == anchor_stmt.syntax().range().start() {
115 indent.text().push_to(&mut buf); 117 edit.replace(expr.syntax().range(), buf);
116 118 } else {
117 let mut edit = EditBuilder::new(); 119 buf.push_str(";");
118 edit.replace(expr.syntax().range(), "var_name".to_string()); 120 indent.text().push_to(&mut buf);
119 edit.insert(anchor_stmt.syntax().range().start(), buf); 121 edit.replace(expr.syntax().range(), "var_name".to_string());
122 edit.insert(anchor_stmt.syntax().range().start(), buf);
123 }
124 let cursor_position = anchor_stmt.syntax().range().start() + TextUnit::of_str("let ");
120 LocalEdit { 125 LocalEdit {
121 edit: edit.finish(), 126 edit: edit.finish(),
122 cursor_position: Some(anchor_stmt.syntax().range().start() + TextUnit::of_str("let ")), 127 cursor_position: Some(cursor_position),
123 } 128 }
124 }) 129 })
125} 130}
@@ -183,7 +188,7 @@ mod tests {
183 } 188 }
184 189
185 #[test] 190 #[test]
186 fn test_intrdoduce_var() { 191 fn test_intrdoduce_var_simple() {
187 check_action_range( 192 check_action_range(
188 " 193 "
189fn foo() { 194fn foo() {
@@ -196,5 +201,18 @@ fn foo() {
196 |file, range| introduce_variable(file, range).map(|f| f()), 201 |file, range| introduce_variable(file, range).map(|f| f()),
197 ); 202 );
198 } 203 }
204 #[test]
205 fn test_intrdoduce_var_expr_stmt() {
206check_action_range(
207 "
208fn foo() {
209 <|>1 + 1<|>;
210}", "
211fn foo() {
212 let <|>var_name = 1 + 1;
213}",
214 |file, range| introduce_variable(file, range).map(|f| f()),
215 );
216 }
199 217
200} 218}