aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/ast_editor.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/ast_editor.rs')
-rw-r--r--crates/ra_assists/src/ast_editor.rs41
1 files changed, 19 insertions, 22 deletions
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs
index de0529b32..5f8ba3df6 100644
--- a/crates/ra_assists/src/ast_editor.rs
+++ b/crates/ra_assists/src/ast_editor.rs
@@ -163,11 +163,7 @@ impl AstEditor<ast::ItemList> {
163 self.do_make_multiline() 163 self.do_make_multiline()
164 } 164 }
165 165
166 pub fn append_functions<'a>(&mut self, fns: impl Iterator<Item = &'a ast::FnDef>) { 166 pub fn append_item(&mut self, item: &ast::ImplItem) {
167 fns.for_each(|it| self.append_function(it))
168 }
169
170 pub fn append_function(&mut self, fn_def: &ast::FnDef) {
171 let (indent, position) = match self.ast().impl_items().last() { 167 let (indent, position) = match self.ast().impl_items().last() {
172 Some(it) => ( 168 Some(it) => (
173 leading_indent(it.syntax()).unwrap_or("").to_string(), 169 leading_indent(it.syntax()).unwrap_or("").to_string(),
@@ -182,8 +178,7 @@ impl AstEditor<ast::ItemList> {
182 }, 178 },
183 }; 179 };
184 let ws = tokens::WsBuilder::new(&format!("\n{}", indent)); 180 let ws = tokens::WsBuilder::new(&format!("\n{}", indent));
185 let to_insert: ArrayVec<[SyntaxElement; 2]> = 181 let to_insert: ArrayVec<[SyntaxElement; 2]> = [ws.ws().into(), item.syntax().into()].into();
186 [ws.ws().into(), fn_def.syntax().into()].into();
187 self.ast = self.insert_children(position, to_insert.into_iter()); 182 self.ast = self.insert_children(position, to_insert.into_iter());
188 } 183 }
189 184
@@ -192,6 +187,23 @@ impl AstEditor<ast::ItemList> {
192 } 187 }
193} 188}
194 189
190impl AstEditor<ast::ImplItem> {
191 pub fn strip_attrs_and_docs(&mut self) {
192 while let Some(start) = self
193 .ast()
194 .syntax()
195 .children_with_tokens()
196 .find(|it| it.kind() == ATTR || it.kind() == COMMENT)
197 {
198 let end = match start.next_sibling_or_token() {
199 Some(el) if el.kind() == WHITESPACE => el,
200 Some(_) | None => start,
201 };
202 self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty());
203 }
204 }
205}
206
195impl AstEditor<ast::FnDef> { 207impl AstEditor<ast::FnDef> {
196 pub fn set_body(&mut self, body: &ast::Block) { 208 pub fn set_body(&mut self, body: &ast::Block) {
197 let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new(); 209 let mut to_insert: ArrayVec<[SyntaxElement; 2]> = ArrayVec::new();
@@ -210,21 +222,6 @@ impl AstEditor<ast::FnDef> {
210 let replace_range = RangeInclusive::new(old_body_or_semi, old_body_or_semi); 222 let replace_range = RangeInclusive::new(old_body_or_semi, old_body_or_semi);
211 self.ast = self.replace_children(replace_range, to_insert.into_iter()) 223 self.ast = self.replace_children(replace_range, to_insert.into_iter())
212 } 224 }
213
214 pub fn strip_attrs_and_docs(&mut self) {
215 while let Some(start) = self
216 .ast()
217 .syntax()
218 .children_with_tokens()
219 .find(|it| it.kind() == ATTR || it.kind() == COMMENT)
220 {
221 let end = match start.next_sibling_or_token() {
222 Some(el) if el.kind() == WHITESPACE => el,
223 Some(_) | None => start,
224 };
225 self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty());
226 }
227 }
228} 225}
229 226
230pub struct AstBuilder<N: AstNode> { 227pub struct AstBuilder<N: AstNode> {