aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/ast
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-09-28 17:50:16 +0100
committerAleksey Kladov <[email protected]>2019-09-28 17:50:16 +0100
commit5dbbfda34ae423229487595fd0ae9e727ae42906 (patch)
tree647be92788a5d338bcfbb088ffb4e56582ad3367 /crates/ra_syntax/src/ast
parentdbdf0e24d51ce425c0066a76a0efc723e41e5071 (diff)
simplify strip attrs
Diffstat (limited to 'crates/ra_syntax/src/ast')
-rw-r--r--crates/ra_syntax/src/ast/edit.rs21
-rw-r--r--crates/ra_syntax/src/ast/extensions.rs12
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.
4use std::{iter, ops::RangeInclusive};
4 5
5use arrayvec::ArrayVec; 6use arrayvec::ArrayVec;
6use std::ops::RangeInclusive;
7 7
8use crate::{ 8use 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
14impl ast::FnDef { 16impl ast::FnDef {
@@ -31,6 +33,23 @@ impl ast::FnDef {
31 } 33 }
32} 34}
33 35
36pub 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
40fn 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]
35fn insert_children<N: AstNode>( 54fn 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 @@
4use itertools::Itertools; 4use itertools::Itertools;
5 5
6use crate::{ 6use 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
206impl 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)]
207pub enum StructKind { 217pub enum StructKind {
208 Tuple(ast::TupleFieldDefList), 218 Tuple(ast::TupleFieldDefList),