diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-10 08:16:08 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-02-10 08:16:08 +0000 |
commit | 166c72042564bc94586b071d627164a06400576b (patch) | |
tree | 42e398a2b474c712c2e638060ad704b85a8fa2e3 /crates/ra_assists/src | |
parent | a8a4f8012e525c816aedf5b0bc51e3ad4c13a0ab (diff) | |
parent | 7b9aefc29db762789645c4114cae093d8f3dfa4f (diff) |
Merge #770
770: Fix introduce var duplicating newlines r=matklad a=vipentti
This fixes #713.
If the block before the statement we want to use introduce var on, had empty
lines these empty lines would also be added between the let-statement and
the current line where the new variable is used.
This fixes that by trimming excess newlines from the start of the indent chunk
and simply adding a single newline (when the chunk had newlines) between the
let-statement and the current statement. If there were no newlines this
matches the previous behaviour.
Co-authored-by: Ville Penttinen <[email protected]>
Diffstat (limited to 'crates/ra_assists/src')
-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, |