diff options
author | Ekaterina Babshukova <[email protected]> | 2019-07-03 14:17:18 +0100 |
---|---|---|
committer | Ekaterina Babshukova <[email protected]> | 2019-07-03 14:17:18 +0100 |
commit | e0e42095db8fb6a7c47c23f2a9bbcebbd8971191 (patch) | |
tree | 409432325d11fb35fb684d3c22da419a9f22b693 /crates/ra_assists/src/ast_editor.rs | |
parent | f15f0d1ec006c6c628fb70414d311bd85c2a0469 (diff) |
extend add_impl_members to constants and types
Diffstat (limited to 'crates/ra_assists/src/ast_editor.rs')
-rw-r--r-- | crates/ra_assists/src/ast_editor.rs | 41 |
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 | ||
190 | impl 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 | |||
195 | impl AstEditor<ast::FnDef> { | 207 | impl 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 | ||
230 | pub struct AstBuilder<N: AstNode> { | 227 | pub struct AstBuilder<N: AstNode> { |