aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/syntax/src/ast/edit.rs60
1 files changed, 31 insertions, 29 deletions
diff --git a/crates/syntax/src/ast/edit.rs b/crates/syntax/src/ast/edit.rs
index cbc75f922..78c0dc102 100644
--- a/crates/syntax/src/ast/edit.rs
+++ b/crates/syntax/src/ast/edit.rs
@@ -15,7 +15,7 @@ use crate::{
15 make::{self, tokens}, 15 make::{self, tokens},
16 AstNode, GenericParamsOwner, NameOwner, TypeBoundsOwner, 16 AstNode, GenericParamsOwner, NameOwner, TypeBoundsOwner,
17 }, 17 },
18 AstToken, Direction, InsertPosition, SmolStr, SyntaxElement, SyntaxKind, 18 ted, AstToken, Direction, InsertPosition, NodeOrToken, SmolStr, SyntaxElement, SyntaxKind,
19 SyntaxKind::{ATTR, COMMENT, WHITESPACE}, 19 SyntaxKind::{ATTR, COMMENT, WHITESPACE},
20 SyntaxNode, SyntaxToken, T, 20 SyntaxNode, SyntaxToken, T,
21}; 21};
@@ -588,37 +588,39 @@ impl IndentLevel {
588 /// ``` 588 /// ```
589 /// if you indent the block, the `{` token would stay put. 589 /// if you indent the block, the `{` token would stay put.
590 fn increase_indent(self, node: SyntaxNode) -> SyntaxNode { 590 fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
591 let mut rewriter = SyntaxRewriter::default(); 591 let res = node.clone_subtree().clone_for_update();
592 node.descendants_with_tokens() 592 let tokens = res.preorder_with_tokens().filter_map(|event| match event {
593 .filter_map(|el| el.into_token()) 593 rowan::WalkEvent::Leave(NodeOrToken::Token(it)) => Some(it),
594 .filter_map(ast::Whitespace::cast) 594 _ => None,
595 .filter(|ws| { 595 });
596 let text = ws.syntax().text(); 596 for token in tokens {
597 text.contains('\n') 597 if let Some(ws) = ast::Whitespace::cast(token) {
598 }) 598 if ws.text().contains('\n') {
599 .for_each(|ws| { 599 let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self));
600 let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,)); 600 ted::replace(ws.syntax(), &new_ws)
601 rewriter.replace(ws.syntax(), &new_ws) 601 }
602 }); 602 }
603 rewriter.rewrite(&node) 603 }
604 res.clone_subtree()
604 } 605 }
605 606
606 fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode { 607 fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode {
607 let mut rewriter = SyntaxRewriter::default(); 608 let res = node.clone_subtree().clone_for_update();
608 node.descendants_with_tokens() 609 let tokens = res.preorder_with_tokens().filter_map(|event| match event {
609 .filter_map(|el| el.into_token()) 610 rowan::WalkEvent::Leave(NodeOrToken::Token(it)) => Some(it),
610 .filter_map(ast::Whitespace::cast) 611 _ => None,
611 .filter(|ws| { 612 });
612 let text = ws.syntax().text(); 613 for token in tokens {
613 text.contains('\n') 614 if let Some(ws) = ast::Whitespace::cast(token) {
614 }) 615 if ws.text().contains('\n') {
615 .for_each(|ws| { 616 let new_ws = make::tokens::whitespace(
616 let new_ws = make::tokens::whitespace( 617 &ws.syntax().text().replace(&format!("\n{}", self), "\n"),
617 &ws.syntax().text().replace(&format!("\n{}", self), "\n"), 618 );
618 ); 619 ted::replace(ws.syntax(), &new_ws)
619 rewriter.replace(ws.syntax(), &new_ws) 620 }
620 }); 621 }
621 rewriter.rewrite(&node) 622 }
623 res.clone_subtree()
622 } 624 }
623} 625}
624 626