Make siblings an inherent method

This commit is contained in:
Aleksey Kladov
2018-10-02 18:14:33 +03:00
parent d323c81d5c
commit 1a2a8dec14
6 changed files with 30 additions and 34 deletions

View File

@@ -94,22 +94,6 @@ pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRe
common_ancestor(left, right)
}
#[derive(Debug)]
pub enum Direction {
Forward,
Backward,
}
pub fn siblings<'a>(
node: SyntaxNodeRef<'a>,
direction: Direction
) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
generate(Some(node), move |&node| match direction {
Direction::Forward => node.next_sibling(),
Direction::Backward => node.prev_sibling(),
})
}
fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> {
for p in n1.ancestors() {
if n2.ancestors().any(|a| a == p) {

View File

@@ -51,7 +51,7 @@ pub use {
ast::AstNode,
lexer::{tokenize, Token},
syntax_kinds::SyntaxKind,
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError},
yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError, Direction},
reparsing::AtomEdit,
};

View File

@@ -58,6 +58,13 @@ impl SyntaxNode {
SyntaxNode(::rowan::SyntaxNode::new(green, errors))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Direction {
Next,
Prev,
}
impl<'a> SyntaxNodeRef<'a> {
pub fn leaf_text(self) -> Option<&'a SmolStr> {
self.0.leaf_text()
@@ -71,6 +78,12 @@ impl<'a> SyntaxNodeRef<'a> {
::algo::walk::WalkEvent::Exit(_) => None,
})
}
pub fn siblings(self, direction: Direction) -> impl Iterator<Item=SyntaxNodeRef<'a>> {
::algo::generate(Some(self), move |&node| match direction {
Direction::Next => node.next_sibling(),
Direction::Prev => node.prev_sibling(),
})
}
}
impl<R: TreeRoot<RaTypes>> SyntaxNode<R> {