aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-05-08 17:51:09 +0100
committerGitHub <[email protected]>2021-05-08 17:51:09 +0100
commit18d026a2a638a8c835e321fc8c29149d13a828ec (patch)
treef41918cdce6ac8c64225d0cf368328c92f3c647d /crates
parent526040eea8886a748dfd0a5449526f37a8bcf6af (diff)
parent7ab4fd762884a10c432dbe13e690ded5bcc716c0 (diff)
Merge #8768
8768: internal: remove one more usage of SyntaxRewriter r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/extract_function.rs102
1 files changed, 46 insertions, 56 deletions
diff --git a/crates/ide_assists/src/handlers/extract_function.rs b/crates/ide_assists/src/handlers/extract_function.rs
index 93b28370c..4116985ae 100644
--- a/crates/ide_assists/src/handlers/extract_function.rs
+++ b/crates/ide_assists/src/handlers/extract_function.rs
@@ -10,7 +10,6 @@ use ide_db::{
10use itertools::Itertools; 10use itertools::Itertools;
11use stdx::format_to; 11use stdx::format_to;
12use syntax::{ 12use syntax::{
13 algo::SyntaxRewriter,
14 ast::{ 13 ast::{
15 self, 14 self,
16 edit::{AstNodeEdit, IndentLevel}, 15 edit::{AstNodeEdit, IndentLevel},
@@ -1362,7 +1361,8 @@ fn rewrite_body_segment(
1362 syntax: &SyntaxNode, 1361 syntax: &SyntaxNode,
1363) -> SyntaxNode { 1362) -> SyntaxNode {
1364 let syntax = fix_param_usages(ctx, params, syntax); 1363 let syntax = fix_param_usages(ctx, params, syntax);
1365 update_external_control_flow(handler, &syntax) 1364 update_external_control_flow(handler, &syntax);
1365 syntax
1366} 1366}
1367 1367
1368/// change all usages to account for added `&`/`&mut` for some params 1368/// change all usages to account for added `&`/`&mut` for some params
@@ -1415,75 +1415,65 @@ fn fix_param_usages(ctx: &AssistContext, params: &[Param], syntax: &SyntaxNode)
1415 res 1415 res
1416} 1416}
1417 1417
1418fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) -> SyntaxNode { 1418fn update_external_control_flow(handler: &FlowHandler, syntax: &SyntaxNode) {
1419 let mut rewriter = SyntaxRewriter::default();
1420
1421 let mut nested_loop = None; 1419 let mut nested_loop = None;
1422 let mut nested_scope = None; 1420 let mut nested_scope = None;
1423 for event in syntax.preorder() { 1421 for event in syntax.preorder() {
1424 let node = match event { 1422 match event {
1425 WalkEvent::Enter(e) => { 1423 WalkEvent::Enter(e) => match e.kind() {
1426 match e.kind() { 1424 SyntaxKind::LOOP_EXPR | SyntaxKind::WHILE_EXPR | SyntaxKind::FOR_EXPR => {
1427 SyntaxKind::LOOP_EXPR | SyntaxKind::WHILE_EXPR | SyntaxKind::FOR_EXPR => { 1425 if nested_loop.is_none() {
1428 if nested_loop.is_none() { 1426 nested_loop = Some(e.clone());
1429 nested_loop = Some(e.clone());
1430 }
1431 } 1427 }
1432 SyntaxKind::FN 1428 }
1433 | SyntaxKind::CONST 1429 SyntaxKind::FN
1434 | SyntaxKind::STATIC 1430 | SyntaxKind::CONST
1435 | SyntaxKind::IMPL 1431 | SyntaxKind::STATIC
1436 | SyntaxKind::MODULE => { 1432 | SyntaxKind::IMPL
1437 if nested_scope.is_none() { 1433 | SyntaxKind::MODULE => {
1438 nested_scope = Some(e.clone()); 1434 if nested_scope.is_none() {
1439 } 1435 nested_scope = Some(e.clone());
1440 } 1436 }
1441 _ => {}
1442 } 1437 }
1443 e 1438 _ => {}
1444 } 1439 },
1445 WalkEvent::Leave(e) => { 1440 WalkEvent::Leave(e) => {
1441 if nested_scope.is_none() {
1442 if let Some(expr) = ast::Expr::cast(e.clone()) {
1443 match expr {
1444 ast::Expr::ReturnExpr(return_expr) if nested_scope.is_none() => {
1445 let expr = return_expr.expr();
1446 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1447 ted::replace(return_expr.syntax(), replacement.syntax())
1448 }
1449 }
1450 ast::Expr::BreakExpr(break_expr) if nested_loop.is_none() => {
1451 let expr = break_expr.expr();
1452 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1453 ted::replace(break_expr.syntax(), replacement.syntax())
1454 }
1455 }
1456 ast::Expr::ContinueExpr(continue_expr) if nested_loop.is_none() => {
1457 if let Some(replacement) = make_rewritten_flow(handler, None) {
1458 ted::replace(continue_expr.syntax(), replacement.syntax())
1459 }
1460 }
1461 _ => {
1462 // do nothing
1463 }
1464 }
1465 }
1466 }
1467
1446 if nested_loop.as_ref() == Some(&e) { 1468 if nested_loop.as_ref() == Some(&e) {
1447 nested_loop = None; 1469 nested_loop = None;
1448 } 1470 }
1449 if nested_scope.as_ref() == Some(&e) { 1471 if nested_scope.as_ref() == Some(&e) {
1450 nested_scope = None; 1472 nested_scope = None;
1451 } 1473 }
1452 continue;
1453 } 1474 }
1454 }; 1475 };
1455 if nested_scope.is_some() {
1456 continue;
1457 }
1458 let expr = match ast::Expr::cast(node) {
1459 Some(e) => e,
1460 None => continue,
1461 };
1462 match expr {
1463 ast::Expr::ReturnExpr(return_expr) if nested_scope.is_none() => {
1464 let expr = return_expr.expr();
1465 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1466 rewriter.replace_ast(&return_expr.into(), &replacement);
1467 }
1468 }
1469 ast::Expr::BreakExpr(break_expr) if nested_loop.is_none() => {
1470 let expr = break_expr.expr();
1471 if let Some(replacement) = make_rewritten_flow(handler, expr) {
1472 rewriter.replace_ast(&break_expr.into(), &replacement);
1473 }
1474 }
1475 ast::Expr::ContinueExpr(continue_expr) if nested_loop.is_none() => {
1476 if let Some(replacement) = make_rewritten_flow(handler, None) {
1477 rewriter.replace_ast(&continue_expr.into(), &replacement);
1478 }
1479 }
1480 _ => {
1481 // do nothing
1482 }
1483 }
1484 } 1476 }
1485
1486 rewriter.rewrite(syntax)
1487} 1477}
1488 1478
1489fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> { 1479fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Option<ast::Expr> {
@@ -1502,7 +1492,7 @@ fn make_rewritten_flow(handler: &FlowHandler, arg_expr: Option<ast::Expr>) -> Op
1502 make::expr_call(make::expr_path(make_path_from_text("Err")), args) 1492 make::expr_call(make::expr_path(make_path_from_text("Err")), args)
1503 } 1493 }
1504 }; 1494 };
1505 Some(make::expr_return(Some(value))) 1495 Some(make::expr_return(Some(value)).clone_for_update())
1506} 1496}
1507 1497
1508#[cfg(test)] 1498#[cfg(test)]