2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-06-09 04:52:45 -05:00
|
|
|
ast::{
|
|
|
|
self,
|
|
|
|
edit::{AstNodeEdit, IndentLevel},
|
2023-01-23 09:53:30 -06:00
|
|
|
make,
|
2020-06-09 04:52:45 -05:00
|
|
|
},
|
2020-11-17 07:08:31 -06:00
|
|
|
AstNode, SyntaxKind, TextRange, T,
|
2020-06-09 04:52:45 -05:00
|
|
|
};
|
2020-05-20 06:41:58 -05:00
|
|
|
|
2021-12-18 16:19:53 -06:00
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
2020-04-29 06:52:55 -05:00
|
|
|
|
|
|
|
// Assist: unwrap_block
|
|
|
|
//
|
2020-04-29 07:53:47 -05:00
|
|
|
// This assist removes if...else, for, while and loop control statements to just keep the body.
|
2020-04-29 06:52:55 -05:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
// if true {$0
|
2020-04-29 06:52:55 -05:00
|
|
|
// println!("foo");
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn foo() {
|
2020-04-29 07:53:47 -05:00
|
|
|
// println!("foo");
|
2020-04-29 06:52:55 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2020-07-02 16:48:35 -05:00
|
|
|
let assist_id = AssistId("unwrap_block", AssistKind::RefactorRewrite);
|
2020-05-10 08:31:51 -05:00
|
|
|
let assist_label = "Unwrap block";
|
2020-06-09 04:52:45 -05:00
|
|
|
|
2020-11-06 15:21:56 -06:00
|
|
|
let l_curly_token = ctx.find_token_syntax_at_offset(T!['{'])?;
|
2022-06-10 09:30:09 -05:00
|
|
|
let mut block = ast::BlockExpr::cast(l_curly_token.parent_ancestors().nth(1)?)?;
|
2020-11-17 07:07:13 -06:00
|
|
|
let target = block.syntax().text_range();
|
2020-06-09 04:52:45 -05:00
|
|
|
let mut parent = block.syntax().parent()?;
|
|
|
|
if ast::MatchArm::can_cast(parent.kind()) {
|
|
|
|
parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
|
|
|
|
}
|
|
|
|
|
2023-01-23 09:53:30 -06:00
|
|
|
let kind = parent.kind();
|
|
|
|
if matches!(kind, SyntaxKind::STMT_LIST | SyntaxKind::EXPR_STMT) {
|
|
|
|
acc.add(assist_id, assist_label, target, |builder| {
|
2021-07-14 14:00:48 -05:00
|
|
|
builder.replace(block.syntax().text_range(), update_expr_string(block.to_string()));
|
2023-01-23 09:53:30 -06:00
|
|
|
})
|
|
|
|
} 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();
|
2023-03-28 09:22:12 -05:00
|
|
|
let initializer = ast::Expr::cast(last)?;
|
2023-01-23 09:53:30 -06:00
|
|
|
let let_stmt = make::let_stmt(pattern, ty, Some(initializer));
|
|
|
|
if stmts.len() > 0 {
|
|
|
|
let block = make::block_expr(stmts, None);
|
2023-03-28 09:22:12 -05:00
|
|
|
format!("{}\n {}", update_expr_string(block.to_string()), let_stmt)
|
2023-01-23 09:53:30 -06:00
|
|
|
} 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)?;
|
|
|
|
match parent.clone() {
|
|
|
|
ast::Expr::ForExpr(_) | ast::Expr::WhileExpr(_) | ast::Expr::LoopExpr(_) => (),
|
|
|
|
ast::Expr::MatchExpr(_) => block = block.dedent(IndentLevel(1)),
|
|
|
|
ast::Expr::IfExpr(if_expr) => {
|
|
|
|
let then_branch = if_expr.then_branch()?;
|
|
|
|
if then_branch == block {
|
|
|
|
if let Some(ancestor) = if_expr.syntax().parent().and_then(ast::IfExpr::cast) {
|
|
|
|
// For `else if` blocks
|
|
|
|
let ancestor_then_branch = ancestor.then_branch()?;
|
|
|
|
|
|
|
|
return acc.add(assist_id, assist_label, target, |edit| {
|
|
|
|
let range_to_del_else_if = TextRange::new(
|
|
|
|
ancestor_then_branch.syntax().text_range().end(),
|
|
|
|
l_curly_token.text_range().start(),
|
|
|
|
);
|
|
|
|
let range_to_del_rest = TextRange::new(
|
|
|
|
then_branch.syntax().text_range().end(),
|
|
|
|
if_expr.syntax().text_range().end(),
|
|
|
|
);
|
|
|
|
|
|
|
|
edit.delete(range_to_del_rest);
|
|
|
|
edit.delete(range_to_del_else_if);
|
|
|
|
edit.replace(
|
|
|
|
target,
|
|
|
|
update_expr_string_without_newline(then_branch.to_string()),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-02 16:48:35 -05:00
|
|
|
return acc.add(assist_id, assist_label, target, |edit| {
|
2023-01-23 09:53:30 -06:00
|
|
|
let range_to_del = TextRange::new(
|
2020-07-02 16:48:35 -05:00
|
|
|
then_branch.syntax().text_range().end(),
|
2023-01-23 09:53:30 -06:00
|
|
|
l_curly_token.text_range().start(),
|
2020-07-02 16:48:35 -05:00
|
|
|
);
|
2020-06-09 04:33:28 -05:00
|
|
|
|
2023-01-23 09:53:30 -06:00
|
|
|
edit.delete(range_to_del);
|
|
|
|
edit.replace(target, update_expr_string_without_newline(block.to_string()));
|
2020-07-02 16:48:35 -05:00
|
|
|
});
|
2020-05-10 08:31:51 -05:00
|
|
|
}
|
2020-06-09 04:33:28 -05:00
|
|
|
}
|
2023-01-23 09:53:30 -06:00
|
|
|
_ => return None,
|
|
|
|
};
|
2020-04-29 06:52:55 -05:00
|
|
|
|
2023-01-23 09:53:30 -06:00
|
|
|
acc.add(assist_id, assist_label, target, |builder| {
|
|
|
|
builder.replace(parent.syntax().text_range(), update_expr_string(block.to_string()));
|
|
|
|
})
|
|
|
|
}
|
2020-04-29 06:52:55 -05:00
|
|
|
}
|
|
|
|
|
2021-07-14 14:00:48 -05:00
|
|
|
fn update_expr_string(expr_string: String) -> String {
|
|
|
|
update_expr_string_with_pat(expr_string, &[' ', '\n'])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_expr_string_without_newline(expr_string: String) -> String {
|
|
|
|
update_expr_string_with_pat(expr_string, &[' '])
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_expr_string_with_pat(expr_str: String, whitespace_pat: &[char]) -> String {
|
|
|
|
// Remove leading whitespace, index [1..] to remove the leading '{',
|
|
|
|
// then continue to remove leading whitespace.
|
|
|
|
let expr_str =
|
|
|
|
expr_str.trim_start_matches(whitespace_pat)[1..].trim_start_matches(whitespace_pat);
|
2020-05-10 08:31:51 -05:00
|
|
|
|
2021-07-14 14:00:48 -05:00
|
|
|
// Remove trailing whitespace, index [..expr_str.len() - 1] to remove the trailing '}',
|
|
|
|
// then continue to remove trailing whitespace.
|
|
|
|
let expr_str = expr_str.trim_end_matches(whitespace_pat);
|
|
|
|
let expr_str = expr_str[..expr_str.len() - 1].trim_end_matches(whitespace_pat);
|
|
|
|
|
|
|
|
expr_str
|
|
|
|
.lines()
|
2020-05-10 08:31:51 -05:00
|
|
|
.map(|line| line.replacen(" ", "", 1)) // Delete indentation
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join("\n")
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:52:55 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-04-29 06:52:55 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2020-11-17 07:08:31 -06:00
|
|
|
#[test]
|
|
|
|
fn unwrap_tail_expr_block() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0{
|
2020-11-17 07:08:31 -06:00
|
|
|
92
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
92
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwrap_stmt_expr_block() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0{
|
2020-11-17 07:08:31 -06:00
|
|
|
92;
|
|
|
|
}
|
|
|
|
()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
92;
|
|
|
|
()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
// Pedantically, we should add an `;` here...
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0{
|
2020-11-17 07:08:31 -06:00
|
|
|
92
|
|
|
|
}
|
|
|
|
()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
92
|
|
|
|
()
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:52:55 -05:00
|
|
|
#[test]
|
|
|
|
fn simple_if() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
bar();
|
2021-01-06 14:15:48 -06:00
|
|
|
if true {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
bar();
|
|
|
|
foo();
|
2020-04-29 06:52:55 -05:00
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_else() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
bar();
|
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
2021-01-06 14:15:48 -06:00
|
|
|
} else {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
bar();
|
|
|
|
if true {
|
|
|
|
foo();
|
2020-05-10 08:31:51 -05:00
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
}
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_else_if() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2021-01-06 14:15:48 -06:00
|
|
|
} else if false {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
println!("bar");
|
|
|
|
} else {
|
|
|
|
println!("foo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
2020-05-10 08:31:51 -05:00
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
}
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-10 08:31:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_else_if_nested() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
} else if false {
|
|
|
|
println!("bar");
|
2021-01-06 14:15:48 -06:00
|
|
|
} else if true {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
println!("foo");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-10 08:31:51 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
} else if false {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
println!("foo");
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-10 08:31:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_else_if_nested_else() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
} else if false {
|
|
|
|
println!("bar");
|
|
|
|
} else if true {
|
|
|
|
println!("foo");
|
2021-01-06 14:15:48 -06:00
|
|
|
} else {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
println!("else");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-10 08:31:51 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
} else if false {
|
|
|
|
println!("bar");
|
|
|
|
} else if true {
|
|
|
|
println!("foo");
|
|
|
|
}
|
|
|
|
println!("else");
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-10 08:31:51 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_else_if_nested_middle() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
} else if false {
|
|
|
|
println!("bar");
|
2021-01-06 14:15:48 -06:00
|
|
|
} else if true {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
println!("foo");
|
|
|
|
} else {
|
|
|
|
println!("else");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-10 08:31:51 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-03-24 06:22:03 -05:00
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
println!("true");
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
|
|
|
// bar();
|
2020-11-17 07:43:23 -06:00
|
|
|
} else if false {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
println!("foo");
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_bad_cursor_position() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar();$0
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_for() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
for i in 0..5 {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_in_for() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
for i in 0..5 {
|
2021-01-06 14:15:48 -06:00
|
|
|
if true {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
for i in 0..5 {
|
|
|
|
foo();
|
2020-04-29 06:52:55 -05:00
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_loop() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
loop {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_while() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
while true {$0
|
2020-11-17 07:43:23 -06:00
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
if true {
|
|
|
|
foo();
|
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-09 04:52:45 -05:00
|
|
|
#[test]
|
|
|
|
fn unwrap_match_arm() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match rel_path {
|
2021-01-06 14:15:48 -06:00
|
|
|
Ok(rel_path) => {$0
|
2020-06-09 04:52:45 -05:00
|
|
|
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
|
|
|
|
Some((*id, rel_path))
|
|
|
|
}
|
|
|
|
Err(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
|
|
|
|
Some((*id, rel_path))
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-29 06:52:55 -05:00
|
|
|
#[test]
|
|
|
|
fn simple_if_in_while_bad_cursor_position() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2020-11-17 07:43:23 -06:00
|
|
|
fn main() {
|
|
|
|
while true {
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo();$0
|
2020-11-17 07:43:23 -06:00
|
|
|
|
2021-03-24 06:22:03 -05:00
|
|
|
// comment
|
2020-11-17 07:43:23 -06:00
|
|
|
bar();
|
|
|
|
} else {
|
|
|
|
println!("bar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-14 14:00:48 -05:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_single_line() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
{$0 0 }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_nested_block() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
$0{
|
|
|
|
{
|
|
|
|
3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
{
|
|
|
|
3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nested_single_line() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
{$0 { println!("foo"); } }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
{ println!("foo"); }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
{$0 { 0 } }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
{ 0 }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_if_single_line() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
if true {$0 /* foo */ foo() } else { bar() /* bar */}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
/* foo */ foo()
|
|
|
|
}
|
2021-12-18 16:19:53 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn if_single_statement() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
if true {$0
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiple_statements() {
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() -> i32 {
|
|
|
|
if 2 > 1 {$0
|
|
|
|
let a = 5;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
5
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() -> i32 {
|
|
|
|
let a = 5;
|
|
|
|
return 3;
|
|
|
|
5
|
|
|
|
}
|
2022-12-04 21:07:31 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwrap_block_in_let_initializers() {
|
|
|
|
// https://github.com/rust-lang/rust-analyzer/issues/13679
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2023-01-23 09:53:30 -06:00
|
|
|
fn main() {
|
|
|
|
let x = {$0};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let x = ();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
2022-12-04 21:07:31 -06:00
|
|
|
fn main() {
|
|
|
|
let x = {$0
|
|
|
|
bar
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let x = bar;
|
|
|
|
}
|
2023-01-23 09:53:30 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
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;
|
|
|
|
}
|
2022-12-04 21:07:31 -06:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unwrap_if_in_let_initializers() {
|
|
|
|
// https://github.com/rust-lang/rust-analyzer/issues/13679
|
|
|
|
check_assist(
|
|
|
|
unwrap_block,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let a = 1;
|
|
|
|
let x = if a - 1 == 0 {$0
|
|
|
|
foo
|
|
|
|
} else {
|
|
|
|
bar
|
|
|
|
};
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let a = 1;
|
|
|
|
let x = foo;
|
|
|
|
}
|
2020-11-17 07:43:23 -06:00
|
|
|
"#,
|
2020-04-29 06:52:55 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|