removing unwraps

This commit is contained in:
Jeroen Vannevel 2022-01-22 10:54:37 +00:00
parent 50c913c733
commit 4896ffb65c
3 changed files with 30 additions and 34 deletions

View file

@ -5,7 +5,7 @@ exclude = ["crates/proc_macro_test/imp"]
[profile.dev] [profile.dev]
# Disabling debug info speeds up builds a bunch, # Disabling debug info speeds up builds a bunch,
# and we don't rely on it for debugging that much. # and we don't rely on it for debugging that much.
debug = 2 debug = 0
[profile.dev.package] [profile.dev.package]
# These speed up local tests. # These speed up local tests.

View file

@ -484,9 +484,6 @@ impl FunctionBody {
} }
} }
// selection: 36..67
// resulting: 57..67
fn from_range(parent: ast::StmtList, selected: TextRange) -> FunctionBody { fn from_range(parent: ast::StmtList, selected: TextRange) -> FunctionBody {
let full_body = parent.syntax().children_with_tokens(); let full_body = parent.syntax().children_with_tokens();
for st in parent.syntax().children_with_tokens() { for st in parent.syntax().children_with_tokens() {
@ -1468,33 +1465,32 @@ fn make_body(
.map(|it| match it { .map(|it| match it {
syntax::NodeOrToken::Node(n) => { syntax::NodeOrToken::Node(n) => {
println!("Found node: {:?}", n); println!("Found node: {:?}", n);
return syntax::NodeOrToken::try_from(rewrite_body_segment( let node_rewritten = rewrite_body_segment(ctx, &fun.params, &handler, &n);
ctx,
&fun.params, syntax::NodeOrToken::Node(node_rewritten)
&handler,
&n,
))
.unwrap()
} }
syntax::NodeOrToken::Token(t) => { syntax::NodeOrToken::Token(t) => {
println!("Found token: {:?}", t); println!("Found token: {:?}", t);
return syntax::NodeOrToken::try_from(t).unwrap() syntax::NodeOrToken::Token(t)
} }
}) })
.collect(); .collect();
let mut tail_expr = match elements.pop() { let mut tail_expr = match elements.pop() {
Some(node) if node.as_node().is_some() => { Some(element) => match element {
ast::Expr::cast(node.as_node().unwrap().clone()).or_else(|| { syntax::NodeOrToken::Node(node) => {
elements.push(node); ast::Expr::cast(node.clone()).or_else(|| {
elements.push(syntax::NodeOrToken::Node(node));
None None
}) })
}, }
Some(token) if token.as_token().is_some() && token.as_token().unwrap().kind() == COMMENT => { syntax::NodeOrToken::Token(token) if token.kind() == COMMENT => {
elements.push(token); elements.push(syntax::NodeOrToken::Token(token));
None None
}, }
_ => None, _ => None,
},
None => None,
}; };
if tail_expr.is_none() { if tail_expr.is_none() {
@ -1512,17 +1508,18 @@ fn make_body(
} }
let body_indent = IndentLevel(1); let body_indent = IndentLevel(1);
let elements: Vec<SyntaxElement> = elements.into_iter().map(|stmt| { let elements: Vec<SyntaxElement> = elements
match stmt { .into_iter()
.map(|stmt| match stmt {
syntax::NodeOrToken::Node(n) => { syntax::NodeOrToken::Node(n) => {
let ast_element = ast::Stmt::cast(n).unwrap(); let ast_element = ast::Stmt::cast(n).unwrap();
let indented = ast_element.dedent(old_indent).indent(body_indent); let indented = ast_element.dedent(old_indent).indent(body_indent);
let ast_node = indented.syntax().clone_subtree(); let ast_node = indented.syntax().clone_subtree();
syntax::NodeOrToken::try_from(ast_node).unwrap() syntax::NodeOrToken::try_from(ast_node).unwrap()
},
syntax::NodeOrToken::Token(t) => syntax::NodeOrToken::try_from(t).unwrap()
} }
}).collect::<Vec<SyntaxElement>>(); syntax::NodeOrToken::Token(t) => syntax::NodeOrToken::try_from(t).unwrap(),
})
.collect::<Vec<SyntaxElement>>();
let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent)); let tail_expr = tail_expr.map(|expr| expr.dedent(old_indent).indent(body_indent));
for element in &elements { for element in &elements {
@ -1531,8 +1528,6 @@ fn make_body(
make::block_expr_full(elements, tail_expr) make::block_expr_full(elements, tail_expr)
// make::block_expr(parent.statements().into_iter(), tail_expr) // make::block_expr(parent.statements().into_iter(), tail_expr)
} }
}; };

View file

@ -335,14 +335,15 @@ pub fn block_expr_full(
) -> ast::BlockExpr { ) -> ast::BlockExpr {
let mut buf = "{\n".to_string(); let mut buf = "{\n".to_string();
for stmt in stmts.into_iter() { for stmt in stmts.into_iter() {
match stmt { match stmt {
rowan::NodeOrToken::Node(n) => { rowan::NodeOrToken::Node(n) => {
println!("Node: {:?}", n.text()); println!("Node: {:?}", n.text());
format_to!(buf, " {}\n", n) 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 { if let Some(tail_expr) = tail_expr {