From a1c187eef3ba08076aedb5154929f7eda8d1b424 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 18:26:51 +0200 Subject: Rename ra_syntax -> syntax --- crates/ra_assists/src/handlers/unwrap_block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_assists/src/handlers/unwrap_block.rs') diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 8b38695a9..2879090b8 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -1,5 +1,5 @@ use ra_fmt::unwrap_trivial_block; -use ra_syntax::{ +use syntax::{ ast::{ self, edit::{AstNodeEdit, IndentLevel}, -- cgit v1.2.3 From f0a9128761e8dd42f0dd40610a1d8734a52f3d9c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 11:41:20 +0200 Subject: Minor --- crates/ra_assists/src/handlers/unwrap_block.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_assists/src/handlers/unwrap_block.rs') diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 2879090b8..3851aeb3e 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -1,4 +1,3 @@ -use ra_fmt::unwrap_trivial_block; use syntax::{ ast::{ self, @@ -7,7 +6,7 @@ use syntax::{ AstNode, TextRange, T, }; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists}; // Assist: unwrap_block // -- cgit v1.2.3 From fc34403018079ea053f26d0a31b7517053c7dd8c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 17:33:38 +0200 Subject: Rename ra_assists -> assists --- crates/ra_assists/src/handlers/unwrap_block.rs | 517 ------------------------- 1 file changed, 517 deletions(-) delete mode 100644 crates/ra_assists/src/handlers/unwrap_block.rs (limited to 'crates/ra_assists/src/handlers/unwrap_block.rs') diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs deleted file mode 100644 index 3851aeb3e..000000000 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ /dev/null @@ -1,517 +0,0 @@ -use syntax::{ - ast::{ - self, - edit::{AstNodeEdit, IndentLevel}, - }, - AstNode, TextRange, T, -}; - -use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists}; - -// Assist: unwrap_block -// -// This assist removes if...else, for, while and loop control statements to just keep the body. -// -// ``` -// fn foo() { -// if true {<|> -// println!("foo"); -// } -// } -// ``` -// -> -// ``` -// fn foo() { -// println!("foo"); -// } -// ``` -pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - let assist_id = AssistId("unwrap_block", AssistKind::RefactorRewrite); - let assist_label = "Unwrap block"; - - let l_curly_token = ctx.find_token_at_offset(T!['{'])?; - let mut block = ast::BlockExpr::cast(l_curly_token.parent())?; - let mut parent = block.syntax().parent()?; - if ast::MatchArm::can_cast(parent.kind()) { - parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))? - } - - 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()?; - - let target = then_branch.syntax().text_range(); - 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(then_branch.to_string(), &[' ', '{']), - ); - }); - } - } else { - let target = block.syntax().text_range(); - return acc.add(assist_id, assist_label, target, |edit| { - let range_to_del = TextRange::new( - then_branch.syntax().text_range().end(), - l_curly_token.text_range().start(), - ); - - edit.delete(range_to_del); - edit.replace(target, update_expr_string(block.to_string(), &[' ', '{'])); - }); - } - } - _ => return None, - }; - - let unwrapped = unwrap_trivial_block(block); - let target = unwrapped.syntax().text_range(); - acc.add(assist_id, assist_label, target, |builder| { - builder.replace( - parent.syntax().text_range(), - update_expr_string(unwrapped.to_string(), &[' ', '{', '\n']), - ); - }) -} - -fn update_expr_string(expr_str: String, trim_start_pat: &[char]) -> String { - let expr_string = expr_str.trim_start_matches(trim_start_pat); - let mut expr_string_lines: Vec<&str> = expr_string.lines().collect(); - expr_string_lines.pop(); // Delete last line - - expr_string_lines - .into_iter() - .map(|line| line.replacen(" ", "", 1)) // Delete indentation - .collect::>() - .join("\n") -} - -#[cfg(test)] -mod tests { - use crate::tests::{check_assist, check_assist_not_applicable}; - - use super::*; - - #[test] - fn simple_if() { - check_assist( - unwrap_block, - r#" - fn main() { - bar(); - if true {<|> - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - "#, - r#" - fn main() { - bar(); - foo(); - - //comment - bar(); - } - "#, - ); - } - - #[test] - fn simple_if_else() { - check_assist( - unwrap_block, - r#" - fn main() { - bar(); - if true { - foo(); - - //comment - bar(); - } else {<|> - println!("bar"); - } - } - "#, - r#" - fn main() { - bar(); - if true { - foo(); - - //comment - bar(); - } - println!("bar"); - } - "#, - ); - } - - #[test] - fn simple_if_else_if() { - check_assist( - unwrap_block, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false {<|> - println!("bar"); - } else { - println!("foo"); - } - } - "#, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } - println!("bar"); - } - "#, - ); - } - - #[test] - fn simple_if_else_if_nested() { - check_assist( - unwrap_block, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false { - println!("bar"); - } else if true {<|> - println!("foo"); - } - } - "#, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false { - println!("bar"); - } - println!("foo"); - } - "#, - ); - } - - #[test] - fn simple_if_else_if_nested_else() { - check_assist( - unwrap_block, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false { - println!("bar"); - } else if true { - println!("foo"); - } else {<|> - println!("else"); - } - } - "#, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false { - println!("bar"); - } else if true { - println!("foo"); - } - println!("else"); - } - "#, - ); - } - - #[test] - fn simple_if_else_if_nested_middle() { - check_assist( - unwrap_block, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false { - println!("bar"); - } else if true {<|> - println!("foo"); - } else { - println!("else"); - } - } - "#, - r#" - fn main() { - //bar(); - if true { - println!("true"); - - //comment - //bar(); - } else if false { - println!("bar"); - } - println!("foo"); - } - "#, - ); - } - - #[test] - fn simple_if_bad_cursor_position() { - check_assist_not_applicable( - unwrap_block, - r#" - fn main() { - bar();<|> - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - "#, - ); - } - - #[test] - fn simple_for() { - check_assist( - unwrap_block, - r#" - fn main() { - for i in 0..5 {<|> - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - } - "#, - r#" - fn main() { - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - "#, - ); - } - - #[test] - fn simple_if_in_for() { - check_assist( - unwrap_block, - r#" - fn main() { - for i in 0..5 { - if true {<|> - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - } - "#, - r#" - fn main() { - for i in 0..5 { - foo(); - - //comment - bar(); - } - } - "#, - ); - } - - #[test] - fn simple_loop() { - check_assist( - unwrap_block, - r#" - fn main() { - loop {<|> - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - } - "#, - r#" - fn main() { - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - "#, - ); - } - - #[test] - fn simple_while() { - check_assist( - unwrap_block, - r#" - fn main() { - while true {<|> - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - } - "#, - r#" - fn main() { - if true { - foo(); - - //comment - bar(); - } else { - println!("bar"); - } - } - "#, - ); - } - - #[test] - fn unwrap_match_arm() { - check_assist( - unwrap_block, - r#" -fn main() { - match rel_path { - Ok(rel_path) => {<|> - 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)) -} -"#, - ); - } - - #[test] - fn simple_if_in_while_bad_cursor_position() { - check_assist_not_applicable( - unwrap_block, - r#" - fn main() { - while true { - if true { - foo();<|> - - //comment - bar(); - } else { - println!("bar"); - } - } - } - "#, - ); - } -} -- cgit v1.2.3