aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-05-09 13:40:45 +0100
committerGitHub <[email protected]>2020-05-09 13:40:45 +0100
commit5af66e54866b2b9942b2a5298a8874ce6a44a1d5 (patch)
tree9097335ae61afaf895c61ec1532fbf52aeb13aa5 /crates/ra_syntax
parent25e37e2c93ba91a8956fdff8bbe9701c623d386f (diff)
parent231fddab5420ffe5edf7b93609ea21155653254a (diff)
Merge #4387
4387: More fluent indent API r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs26
1 files changed, 13 insertions, 13 deletions
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs
index 3e6dd6061..94dfb1a13 100644
--- a/crates/ra_syntax/src/ast/edit.rs
+++ b/crates/ra_syntax/src/ast/edit.rs
@@ -453,11 +453,7 @@ impl IndentLevel {
453 IndentLevel(0) 453 IndentLevel(0)
454 } 454 }
455 455
456 pub fn increase_indent<N: AstNode>(self, node: N) -> N { 456 fn increase_indent(self, node: SyntaxNode) -> SyntaxNode {
457 N::cast(self._increase_indent(node.syntax().clone())).unwrap()
458 }
459
460 fn _increase_indent(self, node: SyntaxNode) -> SyntaxNode {
461 let mut rewriter = SyntaxRewriter::default(); 457 let mut rewriter = SyntaxRewriter::default();
462 node.descendants_with_tokens() 458 node.descendants_with_tokens()
463 .filter_map(|el| el.into_token()) 459 .filter_map(|el| el.into_token())
@@ -478,11 +474,7 @@ impl IndentLevel {
478 rewriter.rewrite(&node) 474 rewriter.rewrite(&node)
479 } 475 }
480 476
481 pub fn decrease_indent<N: AstNode>(self, node: N) -> N { 477 fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode {
482 N::cast(self._decrease_indent(node.syntax().clone())).unwrap()
483 }
484
485 fn _decrease_indent(self, node: SyntaxNode) -> SyntaxNode {
486 let mut rewriter = SyntaxRewriter::default(); 478 let mut rewriter = SyntaxRewriter::default();
487 node.descendants_with_tokens() 479 node.descendants_with_tokens()
488 .filter_map(|el| el.into_token()) 480 .filter_map(|el| el.into_token())
@@ -521,7 +513,7 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
521 iter::successors(Some(token), |token| token.prev_token()) 513 iter::successors(Some(token), |token| token.prev_token())
522} 514}
523 515
524pub trait AstNodeEdit: AstNode + Sized { 516pub trait AstNodeEdit: AstNode + Clone + Sized {
525 #[must_use] 517 #[must_use]
526 fn insert_children( 518 fn insert_children(
527 &self, 519 &self,
@@ -558,9 +550,17 @@ pub trait AstNodeEdit: AstNode + Sized {
558 } 550 }
559 rewriter.rewrite_ast(self) 551 rewriter.rewrite_ast(self)
560 } 552 }
553 #[must_use]
554 fn indent(&self, indent: IndentLevel) -> Self {
555 Self::cast(indent.increase_indent(self.syntax().clone())).unwrap()
556 }
557 #[must_use]
558 fn unindent(&self, indent: IndentLevel) -> Self {
559 Self::cast(indent.decrease_indent(self.syntax().clone())).unwrap()
560 }
561} 561}
562 562
563impl<N: AstNode> AstNodeEdit for N {} 563impl<N: AstNode + Clone> AstNodeEdit for N {}
564 564
565fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> { 565fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> {
566 let element = element.into(); 566 let element = element.into();
@@ -580,7 +580,7 @@ fn test_increase_indent() {
580 _ => (), 580 _ => (),
581}" 581}"
582 ); 582 );
583 let indented = IndentLevel(2).increase_indent(arm_list); 583 let indented = arm_list.indent(IndentLevel(2));
584 assert_eq!( 584 assert_eq!(
585 indented.syntax().to_string(), 585 indented.syntax().to_string(),
586 "{ 586 "{