fix(ide-assists): unwrap block when it parent is let stmt
This commit is contained in:
parent
8fa69f9f7d
commit
90b1222b0b
@ -2,6 +2,7 @@
|
|||||||
ast::{
|
ast::{
|
||||||
self,
|
self,
|
||||||
edit::{AstNodeEdit, IndentLevel},
|
edit::{AstNodeEdit, IndentLevel},
|
||||||
|
make,
|
||||||
},
|
},
|
||||||
AstNode, SyntaxKind, TextRange, T,
|
AstNode, SyntaxKind, TextRange, T,
|
||||||
};
|
};
|
||||||
@ -37,15 +38,42 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
|
|||||||
parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
|
parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
|
||||||
}
|
}
|
||||||
|
|
||||||
if matches!(parent.kind(), SyntaxKind::STMT_LIST | SyntaxKind::EXPR_STMT | SyntaxKind::LET_STMT)
|
let kind = parent.kind();
|
||||||
{
|
if matches!(kind, SyntaxKind::STMT_LIST | SyntaxKind::EXPR_STMT) {
|
||||||
return acc.add(assist_id, assist_label, target, |builder| {
|
acc.add(assist_id, assist_label, target, |builder| {
|
||||||
builder.replace(block.syntax().text_range(), update_expr_string(block.to_string()));
|
builder.replace(block.syntax().text_range(), update_expr_string(block.to_string()));
|
||||||
});
|
})
|
||||||
|
} else if matches!(kind, SyntaxKind::LET_STMT) {
|
||||||
|
let parent = ast::LetStmt::cast(parent)?;
|
||||||
|
let pattern = ast::Pat::cast(parent.syntax().first_child()?)?;
|
||||||
|
let ty = parent.ty();
|
||||||
|
let list = block.stmt_list()?;
|
||||||
|
let replaced = match list.syntax().last_child() {
|
||||||
|
Some(last) => {
|
||||||
|
let stmts: Vec<ast::Stmt> = list.statements().collect();
|
||||||
|
let initializer = ast::Expr::cast(last.clone())?;
|
||||||
|
let let_stmt = make::let_stmt(pattern, ty, Some(initializer));
|
||||||
|
if stmts.len() > 0 {
|
||||||
|
let block = make::block_expr(stmts, None);
|
||||||
|
format!(
|
||||||
|
"{}\n {}",
|
||||||
|
update_expr_string(block.to_string()),
|
||||||
|
let_stmt.to_string()
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
let_stmt.to_string()
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let empty_tuple = make::expr_tuple([]);
|
||||||
|
make::let_stmt(pattern, ty, Some(empty_tuple)).to_string()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
acc.add(assist_id, assist_label, target, |builder| {
|
||||||
|
builder.replace(parent.syntax().text_range(), replaced);
|
||||||
|
})
|
||||||
|
} else {
|
||||||
let parent = ast::Expr::cast(parent)?;
|
let parent = ast::Expr::cast(parent)?;
|
||||||
|
|
||||||
match parent.clone() {
|
match parent.clone() {
|
||||||
ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::LoopExpr(_) => (),
|
ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::LoopExpr(_) => (),
|
||||||
ast::Expr::MatchExpr(_) => block = block.dedent(IndentLevel(1)),
|
ast::Expr::MatchExpr(_) => block = block.dedent(IndentLevel(1)),
|
||||||
@ -93,6 +121,7 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
|
|||||||
builder.replace(parent.syntax().text_range(), update_expr_string(block.to_string()));
|
builder.replace(parent.syntax().text_range(), update_expr_string(block.to_string()));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn update_expr_string(expr_string: String) -> String {
|
fn update_expr_string(expr_string: String) -> String {
|
||||||
update_expr_string_with_pat(expr_string, &[' ', '\n'])
|
update_expr_string_with_pat(expr_string, &[' ', '\n'])
|
||||||
@ -724,6 +753,19 @@ fn unwrap_block_in_let_initializers() {
|
|||||||
check_assist(
|
check_assist(
|
||||||
unwrap_block,
|
unwrap_block,
|
||||||
r#"
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let x = {$0};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let x = ();
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
check_assist(
|
||||||
|
unwrap_block,
|
||||||
|
r#"
|
||||||
fn main() {
|
fn main() {
|
||||||
let x = {$0
|
let x = {$0
|
||||||
bar
|
bar
|
||||||
@ -734,6 +776,34 @@ fn main() {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let x = bar;
|
let x = bar;
|
||||||
}
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
check_assist(
|
||||||
|
unwrap_block,
|
||||||
|
r#"
|
||||||
|
fn main() -> i32 {
|
||||||
|
let _ = {$01; 2};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() -> i32 {
|
||||||
|
1;
|
||||||
|
let _ = 2;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
check_assist(
|
||||||
|
unwrap_block,
|
||||||
|
r#"
|
||||||
|
fn main() -> i32 {
|
||||||
|
let mut a = {$01; 2};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() -> i32 {
|
||||||
|
1;
|
||||||
|
let mut a = 2;
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user