diff options
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 26 |
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..24a1e1d91 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 | ||
524 | pub trait AstNodeEdit: AstNode + Sized { | 516 | pub 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 dedent(&self, indent: IndentLevel) -> Self { | ||
559 | Self::cast(indent.decrease_indent(self.syntax().clone())).unwrap() | ||
560 | } | ||
561 | } | 561 | } |
562 | 562 | ||
563 | impl<N: AstNode> AstNodeEdit for N {} | 563 | impl<N: AstNode + Clone> AstNodeEdit for N {} |
564 | 564 | ||
565 | fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> { | 565 | fn 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 | "{ |