simplify strip attrs

This commit is contained in:
Aleksey Kladov 2019-09-28 19:50:16 +03:00
parent dbdf0e24d5
commit 5dbbfda34a
5 changed files with 35 additions and 29 deletions

View file

@ -1,14 +1,16 @@
//! This module contains functions for editing syntax trees. As the trees are
//! immutable, all function here return a fresh copy of the tree, instead of
//! doing an in-place modification.
use std::{iter, ops::RangeInclusive};
use arrayvec::ArrayVec;
use std::ops::RangeInclusive;
use crate::{
algo,
ast::{self, make, AstNode},
InsertPosition, SyntaxElement,
SyntaxKind::{ATTR, COMMENT, WHITESPACE},
SyntaxNode,
};
impl ast::FnDef {
@ -31,6 +33,23 @@ impl ast::FnDef {
}
}
pub fn strip_attrs_and_docs<N: ast::AttrsOwner>(node: N) -> N {
N::cast(strip_attrs_and_docs_inner(node.syntax().clone())).unwrap()
}
fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
while let Some(start) =
node.children_with_tokens().find(|it| it.kind() == ATTR || it.kind() == COMMENT)
{
let end = match &start.next_sibling_or_token() {
Some(el) if el.kind() == WHITESPACE => el.clone(),
Some(_) | None => start.clone(),
};
node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty());
}
node
}
#[must_use]
fn insert_children<N: AstNode>(
parent: &N,