diff options
-rw-r--r-- | crates/ra_assists/src/assists/add_missing_impl_members.rs | 12 | ||||
-rw-r--r-- | crates/ra_assists/src/ast_editor.rs | 17 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 21 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 12 |
5 files changed, 35 insertions, 29 deletions
diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 682455bce..3fce4a5b7 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use hir::{db::HirDatabase, HasSource}; | 1 | use hir::{db::HirDatabase, HasSource}; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, make, AstNode, NameOwner}, | 3 | ast::{self, edit, make, AstNode, NameOwner}, |
4 | SmolStr, | 4 | SmolStr, |
5 | }; | 5 | }; |
6 | 6 | ||
@@ -76,8 +76,8 @@ fn add_missing_impl_members_inner( | |||
76 | ctx.add_action(AssistId(assist_id), label, |edit| { | 76 | ctx.add_action(AssistId(assist_id), label, |edit| { |
77 | let n_existing_items = impl_item_list.impl_items().count(); | 77 | let n_existing_items = impl_item_list.impl_items().count(); |
78 | let items = missing_items.into_iter().map(|it| match it { | 78 | let items = missing_items.into_iter().map(|it| match it { |
79 | ast::ImplItem::FnDef(def) => strip_docstring(add_body(def).into()), | 79 | ast::ImplItem::FnDef(def) => edit::strip_attrs_and_docs(add_body(def).into()), |
80 | _ => strip_docstring(it), | 80 | _ => edit::strip_attrs_and_docs(it), |
81 | }); | 81 | }); |
82 | let mut ast_editor = AstEditor::new(impl_item_list); | 82 | let mut ast_editor = AstEditor::new(impl_item_list); |
83 | 83 | ||
@@ -93,12 +93,6 @@ fn add_missing_impl_members_inner( | |||
93 | ctx.build() | 93 | ctx.build() |
94 | } | 94 | } |
95 | 95 | ||
96 | fn strip_docstring(item: ast::ImplItem) -> ast::ImplItem { | ||
97 | let mut ast_editor = AstEditor::new(item); | ||
98 | ast_editor.strip_attrs_and_docs(); | ||
99 | ast_editor.ast().to_owned() | ||
100 | } | ||
101 | |||
102 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { | 96 | fn add_body(fn_def: ast::FnDef) -> ast::FnDef { |
103 | if fn_def.body().is_none() { | 97 | if fn_def.body().is_none() { |
104 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) | 98 | fn_def.with_body(make::block_from_expr(make::expr_unimplemented())) |
diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index 72c8c478a..60b8923e1 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs | |||
@@ -212,23 +212,6 @@ impl AstEditor<ast::ItemList> { | |||
212 | } | 212 | } |
213 | } | 213 | } |
214 | 214 | ||
215 | impl AstEditor<ast::ImplItem> { | ||
216 | pub fn strip_attrs_and_docs(&mut self) { | ||
217 | while let Some(start) = self | ||
218 | .ast() | ||
219 | .syntax() | ||
220 | .children_with_tokens() | ||
221 | .find(|it| it.kind() == ATTR || it.kind() == COMMENT) | ||
222 | { | ||
223 | let end = match &start.next_sibling_or_token() { | ||
224 | Some(el) if el.kind() == WHITESPACE => el.clone(), | ||
225 | Some(_) | None => start.clone(), | ||
226 | }; | ||
227 | self.ast = self.replace_children(RangeInclusive::new(start, end), iter::empty()); | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | |||
232 | impl AstEditor<ast::TypeParam> { | 215 | impl AstEditor<ast::TypeParam> { |
233 | pub fn remove_bounds(&mut self) -> &mut Self { | 216 | pub fn remove_bounds(&mut self) -> &mut Self { |
234 | let colon = match self.ast.colon_token() { | 217 | let colon = match self.ast.colon_token() { |
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs index fdffd8cb1..1b2ce921a 100644 --- a/crates/ra_syntax/src/ast.rs +++ b/crates/ra_syntax/src/ast.rs | |||
@@ -5,7 +5,7 @@ mod traits; | |||
5 | mod tokens; | 5 | mod tokens; |
6 | mod extensions; | 6 | mod extensions; |
7 | mod expr_extensions; | 7 | mod expr_extensions; |
8 | mod edit; | 8 | pub mod edit; |
9 | pub mod make; | 9 | pub mod make; |
10 | 10 | ||
11 | use std::marker::PhantomData; | 11 | use std::marker::PhantomData; |
diff --git a/crates/ra_syntax/src/ast/edit.rs b/crates/ra_syntax/src/ast/edit.rs index c65899812..7013cc9b5 100644 --- a/crates/ra_syntax/src/ast/edit.rs +++ b/crates/ra_syntax/src/ast/edit.rs | |||
@@ -1,14 +1,16 @@ | |||
1 | //! This module contains functions for editing syntax trees. As the trees are | 1 | //! This module contains functions for editing syntax trees. As the trees are |
2 | //! immutable, all function here return a fresh copy of the tree, instead of | 2 | //! immutable, all function here return a fresh copy of the tree, instead of |
3 | //! doing an in-place modification. | 3 | //! doing an in-place modification. |
4 | use std::{iter, ops::RangeInclusive}; | ||
4 | 5 | ||
5 | use arrayvec::ArrayVec; | 6 | use arrayvec::ArrayVec; |
6 | use std::ops::RangeInclusive; | ||
7 | 7 | ||
8 | use crate::{ | 8 | use crate::{ |
9 | algo, | 9 | algo, |
10 | ast::{self, make, AstNode}, | 10 | ast::{self, make, AstNode}, |
11 | InsertPosition, SyntaxElement, | 11 | InsertPosition, SyntaxElement, |
12 | SyntaxKind::{ATTR, COMMENT, WHITESPACE}, | ||
13 | SyntaxNode, | ||
12 | }; | 14 | }; |
13 | 15 | ||
14 | impl ast::FnDef { | 16 | impl ast::FnDef { |
@@ -31,6 +33,23 @@ impl ast::FnDef { | |||
31 | } | 33 | } |
32 | } | 34 | } |
33 | 35 | ||
36 | pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: N) -> N { | ||
37 | N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap() | ||
38 | } | ||
39 | |||
40 | fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode { | ||
41 | while let Some(start) = | ||
42 | node.children_with_tokens().find(|it| it.kind() == ATTR || it.kind() == COMMENT) | ||
43 | { | ||
44 | let end = match &start.next_sibling_or_token() { | ||
45 | Some(el) if el.kind() == WHITESPACE => el.clone(), | ||
46 | Some(_) | None => start.clone(), | ||
47 | }; | ||
48 | node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty()); | ||
49 | } | ||
50 | node | ||
51 | } | ||
52 | |||
34 | #[must_use] | 53 | #[must_use] |
35 | fn insert_children<N: AstNode>( | 54 | fn insert_children<N: AstNode>( |
36 | parent: &N, | 55 | parent: &N, |
diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index 0433edb84..8c5ece65d 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs | |||
@@ -4,7 +4,7 @@ | |||
4 | use itertools::Itertools; | 4 | use itertools::Itertools; |
5 | 5 | ||
6 | use crate::{ | 6 | use crate::{ |
7 | ast::{self, child_opt, children, AstNode, SyntaxNode}, | 7 | ast::{self, child_opt, children, AstChildren, AstNode, SyntaxNode}, |
8 | SmolStr, SyntaxElement, | 8 | SmolStr, SyntaxElement, |
9 | SyntaxKind::*, | 9 | SyntaxKind::*, |
10 | SyntaxToken, T, | 10 | SyntaxToken, T, |
@@ -203,6 +203,16 @@ impl ast::ImplBlock { | |||
203 | } | 203 | } |
204 | } | 204 | } |
205 | 205 | ||
206 | impl ast::AttrsOwner for ast::ImplItem { | ||
207 | fn attrs(&self) -> AstChildren<ast::Attr> { | ||
208 | match self { | ||
209 | ast::ImplItem::FnDef(it) => it.attrs(), | ||
210 | ast::ImplItem::TypeAliasDef(it) => it.attrs(), | ||
211 | ast::ImplItem::ConstDef(it) => it.attrs(), | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | |||
206 | #[derive(Debug, Clone, PartialEq, Eq)] | 216 | #[derive(Debug, Clone, PartialEq, Eq)] |
207 | pub enum StructKind { | 217 | pub enum StructKind { |
208 | Tuple(ast::TupleFieldDefList), | 218 | Tuple(ast::TupleFieldDefList), |