diff options
-rw-r--r-- | crates/ra_assists/src/introduce_variable.rs | 81 |
1 files changed, 80 insertions, 1 deletions
diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index 934d1d6b3..954b97b05 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs | |||
@@ -44,7 +44,22 @@ pub(crate) fn introduce_variable(ctx: AssistCtx<impl HirDatabase>) -> Option<Ass | |||
44 | edit.replace(expr.syntax().range(), buf); | 44 | edit.replace(expr.syntax().range(), buf); |
45 | } else { | 45 | } else { |
46 | buf.push_str(";"); | 46 | buf.push_str(";"); |
47 | indent.text().push_to(&mut buf); | 47 | |
48 | // We want to maintain the indent level, | ||
49 | // but we do not want to duplicate possible | ||
50 | // extra newlines in the indent block | ||
51 | for chunk in indent.text().chunks() { | ||
52 | if chunk.starts_with("\r\n") { | ||
53 | buf.push_str("\r\n"); | ||
54 | buf.push_str(chunk.trim_start_matches("\r\n")); | ||
55 | } else if chunk.starts_with("\n") { | ||
56 | buf.push_str("\n"); | ||
57 | buf.push_str(chunk.trim_start_matches("\n")); | ||
58 | } else { | ||
59 | buf.push_str(chunk); | ||
60 | } | ||
61 | } | ||
62 | |||
48 | edit.target(expr.syntax().range()); | 63 | edit.target(expr.syntax().range()); |
49 | edit.replace(expr.syntax().range(), "var_name".to_string()); | 64 | edit.replace(expr.syntax().range(), "var_name".to_string()); |
50 | edit.insert(anchor_stmt.range().start(), buf); | 65 | edit.insert(anchor_stmt.range().start(), buf); |
@@ -345,6 +360,70 @@ fn foo() -> u32 { | |||
345 | } | 360 | } |
346 | 361 | ||
347 | #[test] | 362 | #[test] |
363 | fn test_introduce_var_does_not_add_extra_whitespace() { | ||
364 | check_assist( | ||
365 | introduce_variable, | ||
366 | " | ||
367 | fn foo() -> u32 { | ||
368 | |||
369 | |||
370 | r<|>eturn 2 + 2; | ||
371 | } | ||
372 | ", | ||
373 | " | ||
374 | fn foo() -> u32 { | ||
375 | |||
376 | |||
377 | let <|>var_name = 2 + 2; | ||
378 | return var_name; | ||
379 | } | ||
380 | ", | ||
381 | ); | ||
382 | |||
383 | check_assist( | ||
384 | introduce_variable, | ||
385 | " | ||
386 | fn foo() -> u32 { | ||
387 | |||
388 | r<|>eturn 2 + 2; | ||
389 | } | ||
390 | ", | ||
391 | " | ||
392 | fn foo() -> u32 { | ||
393 | |||
394 | let <|>var_name = 2 + 2; | ||
395 | return var_name; | ||
396 | } | ||
397 | ", | ||
398 | ); | ||
399 | |||
400 | check_assist( | ||
401 | introduce_variable, | ||
402 | " | ||
403 | fn foo() -> u32 { | ||
404 | let foo = 1; | ||
405 | |||
406 | // bar | ||
407 | |||
408 | |||
409 | r<|>eturn 2 + 2; | ||
410 | } | ||
411 | ", | ||
412 | " | ||
413 | fn foo() -> u32 { | ||
414 | let foo = 1; | ||
415 | |||
416 | // bar | ||
417 | |||
418 | |||
419 | let <|>var_name = 2 + 2; | ||
420 | return var_name; | ||
421 | } | ||
422 | ", | ||
423 | ); | ||
424 | } | ||
425 | |||
426 | #[test] | ||
348 | fn test_introduce_var_break() { | 427 | fn test_introduce_var_break() { |
349 | check_assist( | 428 | check_assist( |
350 | introduce_variable, | 429 | introduce_variable, |