aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r--crates/ra_assists/src/handlers/unwrap_block.rs44
1 files changed, 40 insertions, 4 deletions
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs
index c48ecaae8..1fb13f481 100644
--- a/crates/ra_assists/src/handlers/unwrap_block.rs
+++ b/crates/ra_assists/src/handlers/unwrap_block.rs
@@ -1,5 +1,11 @@
1use ra_fmt::unwrap_trivial_block; 1use ra_fmt::unwrap_trivial_block;
2use ra_syntax::{ast, AstNode, TextRange, T}; 2use ra_syntax::{
3 ast::{
4 self,
5 edit::{AstNodeEdit, IndentLevel},
6 },
7 AstNode, TextRange, T,
8};
3 9
4use crate::{AssistContext, AssistId, Assists}; 10use crate::{AssistContext, AssistId, Assists};
5 11
@@ -21,15 +27,21 @@ use crate::{AssistContext, AssistId, Assists};
21// } 27// }
22// ``` 28// ```
23pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 29pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
24 let l_curly_token = ctx.find_token_at_offset(T!['{'])?;
25 let block = ast::BlockExpr::cast(l_curly_token.parent())?;
26 let parent = block.syntax().parent()?;
27 let assist_id = AssistId("unwrap_block"); 30 let assist_id = AssistId("unwrap_block");
28 let assist_label = "Unwrap block"; 31 let assist_label = "Unwrap block";
32
33 let l_curly_token = ctx.find_token_at_offset(T!['{'])?;
34 let mut block = ast::BlockExpr::cast(l_curly_token.parent())?;
35 let mut parent = block.syntax().parent()?;
36 if ast::MatchArm::can_cast(parent.kind()) {
37 parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
38 }
39
29 let parent = ast::Expr::cast(parent)?; 40 let parent = ast::Expr::cast(parent)?;
30 41
31 match parent.clone() { 42 match parent.clone() {
32 ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::LoopExpr(_) => (), 43 ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::LoopExpr(_) => (),
44 ast::Expr::MatchExpr(_) => block = block.dedent(IndentLevel(1)),
33 ast::Expr::IfExpr(if_expr) => { 45 ast::Expr::IfExpr(if_expr) => {
34 let then_branch = if_expr.then_branch()?; 46 let then_branch = if_expr.then_branch()?;
35 if then_branch == block { 47 if then_branch == block {
@@ -460,6 +472,30 @@ mod tests {
460 } 472 }
461 473
462 #[test] 474 #[test]
475 fn unwrap_match_arm() {
476 check_assist(
477 unwrap_block,
478 r#"
479fn main() {
480 match rel_path {
481 Ok(rel_path) => {<|>
482 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
483 Some((*id, rel_path))
484 }
485 Err(_) => None,
486 }
487}
488"#,
489 r#"
490fn main() {
491 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
492 Some((*id, rel_path))
493}
494"#,
495 );
496 }
497
498 #[test]
463 fn simple_if_in_while_bad_cursor_position() { 499 fn simple_if_in_while_bad_cursor_position() {
464 check_assist_not_applicable( 500 check_assist_not_applicable(
465 unwrap_block, 501 unwrap_block,