From 7b9aefc29db762789645c4114cae093d8f3dfa4f Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Sat, 9 Feb 2019 13:41:03 +0200 Subject: Fix introduce var duplicating newlines 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. --- crates/ra_assists/src/introduce_variable.rs | 81 ++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) (limited to 'crates/ra_assists') 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) -> Option u32 { ", " fn foo() -> u32 { + let <|>var_name = 2 + 2; + return var_name; +} +", + ); + } + + #[test] + fn test_introduce_var_does_not_add_extra_whitespace() { + check_assist( + introduce_variable, + " +fn foo() -> u32 { + + + r<|>eturn 2 + 2; +} +", + " +fn foo() -> u32 { + + + let <|>var_name = 2 + 2; + return var_name; +} +", + ); + + check_assist( + introduce_variable, + " +fn foo() -> u32 { + + r<|>eturn 2 + 2; +} +", + " +fn foo() -> u32 { + + let <|>var_name = 2 + 2; + return var_name; +} +", + ); + + check_assist( + introduce_variable, + " +fn foo() -> u32 { + let foo = 1; + + // bar + + + r<|>eturn 2 + 2; +} +", + " +fn foo() -> u32 { + let foo = 1; + + // bar + + let <|>var_name = 2 + 2; return var_name; } -- cgit v1.2.3