aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/unwrap_block.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/unwrap_block.rs')
-rw-r--r--crates/ra_assists/src/handlers/unwrap_block.rs34
1 files changed, 16 insertions, 18 deletions
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs
index e52ec557e..8440c7d0f 100644
--- a/crates/ra_assists/src/handlers/unwrap_block.rs
+++ b/crates/ra_assists/src/handlers/unwrap_block.rs
@@ -1,8 +1,10 @@
1use crate::{AssistContext, AssistId, Assists};
2
3use ast::{ElseBranch, Expr, LoopBodyOwner};
4use ra_fmt::unwrap_trivial_block; 1use ra_fmt::unwrap_trivial_block;
5use ra_syntax::{ast, match_ast, AstNode, TextRange, T}; 2use ra_syntax::{
3 ast::{self, ElseBranch, Expr, LoopBodyOwner},
4 match_ast, AstNode, TextRange, T,
5};
6
7use crate::{AssistContext, AssistId, Assists};
6 8
7// Assist: unwrap_block 9// Assist: unwrap_block
8// 10//
@@ -60,7 +62,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
60 let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); 62 let range_to_del_else_if = TextRange::new(ancestor_then_branch.syntax().text_range().end(), l_curly_token.text_range().start());
61 let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end()); 63 let range_to_del_rest = TextRange::new(then_branch.syntax().text_range().end(), if_expr.syntax().text_range().end());
62 64
63 edit.set_cursor(ancestor_then_branch.syntax().text_range().end());
64 edit.delete(range_to_del_rest); 65 edit.delete(range_to_del_rest);
65 edit.delete(range_to_del_else_if); 66 edit.delete(range_to_del_else_if);
66 edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{'])); 67 edit.replace(target, update_expr_string(then_branch.to_string(), &[' ', '{']));
@@ -77,7 +78,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
77 return acc.add(assist_id, assist_label, target, |edit| { 78 return acc.add(assist_id, assist_label, target, |edit| {
78 let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start()); 79 let range_to_del = TextRange::new(then_branch.syntax().text_range().end(), l_curly_token.text_range().start());
79 80
80 edit.set_cursor(then_branch.syntax().text_range().end());
81 edit.delete(range_to_del); 81 edit.delete(range_to_del);
82 edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{'])); 82 edit.replace(target, update_expr_string(else_block.to_string(), &[' ', '{']));
83 }); 83 });
@@ -95,8 +95,6 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
95 95
96 let target = expr_to_unwrap.syntax().text_range(); 96 let target = expr_to_unwrap.syntax().text_range();
97 acc.add(assist_id, assist_label, target, |edit| { 97 acc.add(assist_id, assist_label, target, |edit| {
98 edit.set_cursor(expr.syntax().text_range().start());
99
100 edit.replace( 98 edit.replace(
101 expr.syntax().text_range(), 99 expr.syntax().text_range(),
102 update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']), 100 update_expr_string(expr_to_unwrap.to_string(), &[' ', '{', '\n']),
@@ -152,7 +150,7 @@ mod tests {
152 r#" 150 r#"
153 fn main() { 151 fn main() {
154 bar(); 152 bar();
155 <|>foo(); 153 foo();
156 154
157 //comment 155 //comment
158 bar(); 156 bar();
@@ -186,7 +184,7 @@ mod tests {
186 184
187 //comment 185 //comment
188 bar(); 186 bar();
189 }<|> 187 }
190 println!("bar"); 188 println!("bar");
191 } 189 }
192 "#, 190 "#,
@@ -220,7 +218,7 @@ mod tests {
220 218
221 //comment 219 //comment
222 //bar(); 220 //bar();
223 }<|> 221 }
224 println!("bar"); 222 println!("bar");
225 } 223 }
226 "#, 224 "#,
@@ -256,7 +254,7 @@ mod tests {
256 //bar(); 254 //bar();
257 } else if false { 255 } else if false {
258 println!("bar"); 256 println!("bar");
259 }<|> 257 }
260 println!("foo"); 258 println!("foo");
261 } 259 }
262 "#, 260 "#,
@@ -296,7 +294,7 @@ mod tests {
296 println!("bar"); 294 println!("bar");
297 } else if true { 295 } else if true {
298 println!("foo"); 296 println!("foo");
299 }<|> 297 }
300 println!("else"); 298 println!("else");
301 } 299 }
302 "#, 300 "#,
@@ -334,7 +332,7 @@ mod tests {
334 //bar(); 332 //bar();
335 } else if false { 333 } else if false {
336 println!("bar"); 334 println!("bar");
337 }<|> 335 }
338 println!("foo"); 336 println!("foo");
339 } 337 }
340 "#, 338 "#,
@@ -381,7 +379,7 @@ mod tests {
381 "#, 379 "#,
382 r#" 380 r#"
383 fn main() { 381 fn main() {
384 <|>if true { 382 if true {
385 foo(); 383 foo();
386 384
387 //comment 385 //comment
@@ -415,7 +413,7 @@ mod tests {
415 r#" 413 r#"
416 fn main() { 414 fn main() {
417 for i in 0..5 { 415 for i in 0..5 {
418 <|>foo(); 416 foo();
419 417
420 //comment 418 //comment
421 bar(); 419 bar();
@@ -445,7 +443,7 @@ mod tests {
445 "#, 443 "#,
446 r#" 444 r#"
447 fn main() { 445 fn main() {
448 <|>if true { 446 if true {
449 foo(); 447 foo();
450 448
451 //comment 449 //comment
@@ -478,7 +476,7 @@ mod tests {
478 "#, 476 "#,
479 r#" 477 r#"
480 fn main() { 478 fn main() {
481 <|>if true { 479 if true {
482 foo(); 480 foo();
483 481
484 //comment 482 //comment