aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs')
-rw-r--r--crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs26
1 files changed, 6 insertions, 20 deletions
diff --git a/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs b/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
index 30c371480..d64c34d54 100644
--- a/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
+++ b/crates/ra_ide_api_light/src/assists/replace_if_let_with_match.rs
@@ -1,9 +1,9 @@
1use ra_syntax::{ 1use ra_syntax::{AstNode, ast};
2 AstNode, SyntaxKind::{L_CURLY, R_CURLY, WHITESPACE},
3 ast,
4};
5 2
6use crate::assists::{AssistCtx, Assist}; 3use crate::{
4 assists::{AssistCtx, Assist},
5 formatting::extract_trivial_expression,
6};
7 7
8pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { 8pub fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> {
9 let if_expr: &ast::IfExpr = ctx.node_at_offset()?; 9 let if_expr: &ast::IfExpr = ctx.node_at_offset()?;
@@ -39,26 +39,12 @@ fn build_match_expr(
39} 39}
40 40
41fn format_arm(block: &ast::Block) -> String { 41fn format_arm(block: &ast::Block) -> String {
42 match extract_expression(block) { 42 match extract_trivial_expression(block) {
43 None => block.syntax().text().to_string(), 43 None => block.syntax().text().to_string(),
44 Some(e) => format!("{},", e.syntax().text()), 44 Some(e) => format!("{},", e.syntax().text()),
45 } 45 }
46} 46}
47 47
48fn extract_expression(block: &ast::Block) -> Option<&ast::Expr> {
49 let expr = block.expr()?;
50 let non_trivial_children = block.syntax().children().filter(|it| {
51 !(it == &expr.syntax()
52 || it.kind() == L_CURLY
53 || it.kind() == R_CURLY
54 || it.kind() == WHITESPACE)
55 });
56 if non_trivial_children.count() > 0 {
57 return None;
58 }
59 Some(expr)
60}
61
62#[cfg(test)] 48#[cfg(test)]
63mod tests { 49mod tests {
64 use super::*; 50 use super::*;