diff options
author | Aleksey Kladov <[email protected]> | 2019-09-28 17:50:16 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-09-28 17:50:16 +0100 |
commit | 5dbbfda34ae423229487595fd0ae9e727ae42906 (patch) | |
tree | 647be92788a5d338bcfbb088ffb4e56582ad3367 /crates/ra_syntax/src/ast | |
parent | dbdf0e24d51ce425c0066a76a0efc723e41e5071 (diff) |
simplify strip attrs
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r-- | crates/ra_syntax/src/ast/edit.rs | 21 | ||||
-rw-r--r-- | crates/ra_syntax/src/ast/extensions.rs | 12 |
2 files changed, 31 insertions, 2 deletions
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), |