From 4896ffb65c09ae7a2e961e6c942287296826ba46 Mon Sep 17 00:00:00 2001 From: Jeroen Vannevel Date: Sat, 22 Jan 2022 10:54:37 +0000 Subject: [PATCH] removing unwraps --- Cargo.toml | 2 +- .../src/handlers/extract_function.rs | 53 +++++++++---------- crates/syntax/src/ast/make.rs | 9 ++-- 3 files changed, 30 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b322acdc6..3f83041bec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ exclude = ["crates/proc_macro_test/imp"] [profile.dev] # Disabling debug info speeds up builds a bunch, # and we don't rely on it for debugging that much. -debug = 2 +debug = 0 [profile.dev.package] # These speed up local tests. diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs index 41aafecd00..cfa4343f7c 100644 --- a/crates/ide_assists/src/handlers/extract_function.rs +++ b/crates/ide_assists/src/handlers/extract_function.rs @@ -484,9 +484,6 @@ impl FunctionBody { } } - // selection: 36..67 - // resulting: 57..67 - fn from_range(parent: ast::StmtList, selected: TextRange) -> FunctionBody { let full_body = parent.syntax().children_with_tokens(); for st in parent.syntax().children_with_tokens() { @@ -1468,33 +1465,32 @@ fn make_body( .map(|it| match it { syntax::NodeOrToken::Node(n) => { println!("Found node: {:?}", n); - return syntax::NodeOrToken::try_from(rewrite_body_segment( - ctx, - &fun.params, - &handler, - &n, - )) - .unwrap() + let node_rewritten = rewrite_body_segment(ctx, &fun.params, &handler, &n); + + syntax::NodeOrToken::Node(node_rewritten) } syntax::NodeOrToken::Token(t) => { println!("Found token: {:?}", t); - return syntax::NodeOrToken::try_from(t).unwrap() + syntax::NodeOrToken::Token(t) } }) .collect(); let mut tail_expr = match elements.pop() { - Some(node) if node.as_node().is_some() => { - ast::Expr::cast(node.as_node().unwrap().clone()).or_else(|| { - elements.push(node); + Some(element) => match element { + syntax::NodeOrToken::Node(node) => { + ast::Expr::cast(node.clone()).or_else(|| { + elements.push(syntax::NodeOrToken::Node(node)); + None + }) + } + syntax::NodeOrToken::Token(token) if token.kind() == COMMENT => { + elements.push(syntax::NodeOrToken::Token(token)); None - }) + } + _ => None, }, - Some(token) if token.as_token().is_some() && token.as_token().unwrap().kind() == COMMENT => { - elements.push(token); - None - }, - _ => None, + None => None, }; if tail_expr.is_none() { @@ -1510,19 +1506,20 @@ fn make_body( } } } - + let body_indent = IndentLevel(1); - let elements: Vec = elements.into_iter().map(|stmt| { - match stmt { + let elements: Vec = elements + .into_iter() + .map(|stmt| match stmt { syntax::NodeOrToken::Node(n) => { let ast_element = ast::Stmt::cast(n).unwrap(); let indented = ast_element.dedent(old_indent).indent(body_indent); let ast_node = indented.syntax().clone_subtree(); syntax::NodeOrToken::try_from(ast_node).unwrap() - }, - syntax::NodeOrToken::Token(t) => syntax::NodeOrToken::try_from(t).unwrap() - } - }).collect::>(); + } + syntax::NodeOrToken::Token(t) => syntax::NodeOrToken::try_from(t).unwrap(), + }) + .collect::>(); let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent)); for element in &elements { @@ -1531,8 +1528,6 @@ fn make_body( make::block_expr_full(elements, tail_expr) - - // make::block_expr(parent.statements().into_iter(), tail_expr) } }; diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 3b492a4257..2d1e9ac05d 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs @@ -335,14 +335,15 @@ pub fn block_expr_full( ) -> ast::BlockExpr { let mut buf = "{\n".to_string(); for stmt in stmts.into_iter() { - match stmt { rowan::NodeOrToken::Node(n) => { println!("Node: {:?}", n.text()); format_to!(buf, " {}\n", n) - }, - rowan::NodeOrToken::Token(t) if t.kind() == SyntaxKind::COMMENT => format_to!(buf, " {}\n", t), - _ => () + } + rowan::NodeOrToken::Token(t) if t.kind() == SyntaxKind::COMMENT => { + format_to!(buf, " {}\n", t) + } + _ => (), } } if let Some(tail_expr) = tail_expr {