@@ -122,7 +122,7 @@ pub fn introduce_variable<'a>(
122
122
let node = find_covering_node ( file. syntax ( ) , range) ;
123
123
let expr = node. ancestors ( ) . filter_map ( ast:: Expr :: cast) . next ( ) ?;
124
124
125
- let anchor_stmt = ahchor_stmt ( expr) ?;
125
+ let anchor_stmt = anchor_stmt ( expr) ?;
126
126
let indent = anchor_stmt. prev_sibling ( ) ?;
127
127
if indent. kind ( ) != WHITESPACE {
128
128
return None ;
@@ -133,7 +133,12 @@ pub fn introduce_variable<'a>(
133
133
134
134
buf. push_str ( "let var_name = " ) ;
135
135
expr. syntax ( ) . text ( ) . push_to ( & mut buf) ;
136
- if expr. syntax ( ) . range ( ) . start ( ) == anchor_stmt. range ( ) . start ( ) {
136
+ let is_full_stmt = if let Some ( expr_stmt) = ast:: ExprStmt :: cast ( anchor_stmt) {
137
+ Some ( expr. syntax ( ) ) == expr_stmt. expr ( ) . map ( |e| e. syntax ( ) )
138
+ } else {
139
+ false
140
+ } ;
141
+ if is_full_stmt {
137
142
edit. replace ( expr. syntax ( ) . range ( ) , buf) ;
138
143
} else {
139
144
buf. push_str ( ";" ) ;
@@ -150,7 +155,7 @@ pub fn introduce_variable<'a>(
150
155
151
156
/// Statement or last in the block expression, which will follow
152
157
/// the freshly introduced var.
153
- fn ahchor_stmt ( expr : ast:: Expr ) -> Option < SyntaxNodeRef > {
158
+ fn anchor_stmt ( expr : ast:: Expr ) -> Option < SyntaxNodeRef > {
154
159
expr. syntax ( ) . ancestors ( ) . find ( |& node| {
155
160
if ast:: Stmt :: cast ( node) . is_some ( ) {
156
161
return true ;
@@ -250,7 +255,7 @@ struct Foo { a: i32, }
250
255
}
251
256
252
257
#[ test]
253
- fn test_intrdoduce_var_simple ( ) {
258
+ fn test_introduce_var_simple ( ) {
254
259
check_action_range (
255
260
"
256
261
fn foo() {
@@ -266,7 +271,7 @@ fn foo() {
266
271
}
267
272
268
273
#[ test]
269
- fn test_intrdoduce_var_expr_stmt ( ) {
274
+ fn test_introduce_var_expr_stmt ( ) {
270
275
check_action_range (
271
276
"
272
277
fn foo() {
@@ -281,7 +286,23 @@ fn foo() {
281
286
}
282
287
283
288
#[ test]
284
- fn test_intrdoduce_var_last_expr ( ) {
289
+ fn test_introduce_var_part_of_expr_stmt ( ) {
290
+ check_action_range (
291
+ "
292
+ fn foo() {
293
+ <|>1<|> + 1;
294
+ }" ,
295
+ "
296
+ fn foo() {
297
+ let <|>var_name = 1;
298
+ var_name + 1;
299
+ }" ,
300
+ |file, range| introduce_variable ( file, range) . map ( |f| f ( ) ) ,
301
+ ) ;
302
+ }
303
+
304
+ #[ test]
305
+ fn test_introduce_var_last_expr ( ) {
285
306
check_action_range (
286
307
"
287
308
fn foo() {
@@ -296,4 +317,20 @@ fn foo() {
296
317
) ;
297
318
}
298
319
320
+ #[ test]
321
+ fn test_introduce_var_last_full_expr ( ) {
322
+ check_action_range (
323
+ "
324
+ fn foo() {
325
+ <|>bar(1 + 1)<|>
326
+ }" ,
327
+ "
328
+ fn foo() {
329
+ let <|>var_name = bar(1 + 1);
330
+ var_name
331
+ }" ,
332
+ |file, range| introduce_variable ( file, range) . map ( |f| f ( ) ) ,
333
+ ) ;
334
+ }
335
+
299
336
}
0 commit comments