From 231fddab5420ffe5edf7b93609ea21155653254a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 14:40:11 +0200 Subject: More fluent indent API --- crates/ra_syntax/src/ast/edit.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'crates/ra_syntax/src') 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 { IndentLevel(0) } - pub fn increase_indent(self, node: N) -> N { - N::cast(self._increase_indent(node.syntax().clone())).unwrap() - } - - fn _increase_indent(self, node: SyntaxNode) -> SyntaxNode { + fn increase_indent(self, node: SyntaxNode) -> SyntaxNode { let mut rewriter = SyntaxRewriter::default(); node.descendants_with_tokens() .filter_map(|el| el.into_token()) @@ -478,11 +474,7 @@ impl IndentLevel { rewriter.rewrite(&node) } - pub fn decrease_indent(self, node: N) -> N { - N::cast(self._decrease_indent(node.syntax().clone())).unwrap() - } - - fn _decrease_indent(self, node: SyntaxNode) -> SyntaxNode { + fn decrease_indent(self, node: SyntaxNode) -> SyntaxNode { let mut rewriter = SyntaxRewriter::default(); node.descendants_with_tokens() .filter_map(|el| el.into_token()) @@ -521,7 +513,7 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator { iter::successors(Some(token), |token| token.prev_token()) } -pub trait AstNodeEdit: AstNode + Sized { +pub trait AstNodeEdit: AstNode + Clone + Sized { #[must_use] fn insert_children( &self, @@ -558,9 +550,17 @@ pub trait AstNodeEdit: AstNode + Sized { } rewriter.rewrite_ast(self) } + #[must_use] + fn indent(&self, indent: IndentLevel) -> Self { + Self::cast(indent.increase_indent(self.syntax().clone())).unwrap() + } + #[must_use] + fn unindent(&self, indent: IndentLevel) -> Self { + Self::cast(indent.decrease_indent(self.syntax().clone())).unwrap() + } } -impl AstNodeEdit for N {} +impl AstNodeEdit for N {} fn single_node(element: impl Into) -> RangeInclusive { let element = element.into(); @@ -580,7 +580,7 @@ fn test_increase_indent() { _ => (), }" ); - let indented = IndentLevel(2).increase_indent(arm_list); + let indented = arm_list.indent(IndentLevel(2)); assert_eq!( indented.syntax().to_string(), "{ -- cgit v1.2.3