From 4f2134cc33f07c09fe166cec42971828843bc0ef Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 2 May 2020 01:18:19 +0200 Subject: Introduce EffectExpr --- crates/ra_syntax/src/ast/edit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 26e4576ff..c507dc683 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -28,7 +28,7 @@ impl ast::BinExpr { impl ast::FnDef { #[must_use] - pub fn with_body(&self, body: ast::Block) -> ast::FnDef { + pub fn with_body(&self, body: ast::BlockExpr) -> ast::FnDef { let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); let old_body_or_semi: SyntaxElement = if let Some(old_body) = self.body() { old_body.syntax().clone().into() -- cgit v1.2.3 From 92665358cd98913e3fef8294e1889cc0bb919e3f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 May 2020 23:56:10 +0800 Subject: Rename ImplItem to AssocItem --- crates/ra_syntax/src/ast/edit.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index c507dc683..3e6dd6061 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -79,7 +79,7 @@ where impl ast::ItemList { #[must_use] - pub fn append_items(&self, items: impl IntoIterator) -> ast::ItemList { + pub fn append_items(&self, items: impl IntoIterator) -> ast::ItemList { let mut res = self.clone(); if !self.syntax().text().contains_char('\n') { res = make_multiline(res); @@ -89,8 +89,8 @@ impl ast::ItemList { } #[must_use] - pub fn append_item(&self, item: ast::ImplItem) -> ast::ItemList { - let (indent, position) = match self.impl_items().last() { + pub fn append_item(&self, item: ast::AssocItem) -> ast::ItemList { + let (indent, position) = match self.assoc_items().last() { Some(it) => ( leading_indent(it.syntax()).unwrap_or_default().to_string(), InsertPosition::After(it.syntax().clone().into()), -- cgit v1.2.3 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/ast/edit.rs') 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 From 5c04d8544c647e1f9bbf3c5a2f1e86409d4080f5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 9 May 2020 14:48:43 +0200 Subject: unindent -> dedent --- crates/ra_syntax/src/ast/edit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 94dfb1a13..24a1e1d91 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -555,7 +555,7 @@ pub trait AstNodeEdit: AstNode + Clone + Sized { Self::cast(indent.increase_indent(self.syntax().clone())).unwrap() } #[must_use] - fn unindent(&self, indent: IndentLevel) -> Self { + fn dedent(&self, indent: IndentLevel) -> Self { Self::cast(indent.decrease_indent(self.syntax().clone())).unwrap() } } -- cgit v1.2.3 From e6fc0bdffb213f6e94c5bb4081e6d175ccbd518f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 19 May 2020 23:12:01 +0200 Subject: Moderate cleanup of add_function --- crates/ra_syntax/src/ast/edit.rs | 44 ++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'crates/ra_syntax/src/ast/edit.rs') diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index 24a1e1d91..29eb3fcb9 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs @@ -1,7 +1,10 @@ //! This module contains functions for editing syntax trees. As the trees are //! immutable, all function here return a fresh copy of the tree, instead of //! doing an in-place modification. -use std::{iter, ops::RangeInclusive}; +use std::{ + fmt, iter, + ops::{self, RangeInclusive}, +}; use arrayvec::ArrayVec; @@ -437,6 +440,28 @@ impl From for IndentLevel { } } +impl fmt::Display for IndentLevel { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let spaces = " "; + let buf; + let len = self.0 as usize * 4; + let indent = if len <= spaces.len() { + &spaces[..len] + } else { + buf = iter::repeat(' ').take(len).collect::(); + &buf + }; + fmt::Display::fmt(indent, f) + } +} + +impl ops::Add for IndentLevel { + type Output = IndentLevel; + fn add(self, rhs: u8) -> IndentLevel { + IndentLevel(self.0 + rhs) + } +} + impl IndentLevel { pub fn from_node(node: &SyntaxNode) -> IndentLevel { let first_token = match node.first_token() { @@ -453,6 +478,14 @@ impl IndentLevel { IndentLevel(0) } + /// XXX: this intentionally doesn't change the indent of the very first token. + /// Ie, in something like + /// ``` + /// fn foo() { + /// 92 + /// } + /// ``` + /// if you indent the block, the `{` token would stay put. fn increase_indent(self, node: SyntaxNode) -> SyntaxNode { let mut rewriter = SyntaxRewriter::default(); node.descendants_with_tokens() @@ -463,12 +496,7 @@ impl IndentLevel { text.contains('\n') }) .for_each(|ws| { - let new_ws = make::tokens::whitespace(&format!( - "{}{:width$}", - ws.syntax().text(), - "", - width = self.0 as usize * 4 - )); + let new_ws = make::tokens::whitespace(&format!("{}{}", ws.syntax(), self,)); rewriter.replace(ws.syntax(), &new_ws) }); rewriter.rewrite(&node) @@ -485,7 +513,7 @@ impl IndentLevel { }) .for_each(|ws| { let new_ws = make::tokens::whitespace( - &ws.syntax().text().replace(&format!("\n{:1$}", "", self.0 as usize * 4), "\n"), + &ws.syntax().text().replace(&format!("\n{}", self), "\n"), ); rewriter.replace(ws.syntax(), &new_ws) }); -- cgit v1.2.3