From 1a2a8dec14ec04ea8eeccae99fd885e7a280e45b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 2 Oct 2018 18:14:33 +0300 Subject: Make siblings an inherent method --- crates/ra_syntax/src/algo/mod.rs | 16 ---------------- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/yellow/mod.rs | 13 +++++++++++++ 3 files changed, 14 insertions(+), 17 deletions(-) (limited to 'crates/ra_syntax') diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs index 3716a6000..a6678093d 100644 --- a/crates/ra_syntax/src/algo/mod.rs +++ b/crates/ra_syntax/src/algo/mod.rs @@ -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> { - 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) { diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index c7eda4563..738664afd 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -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, }; diff --git a/crates/ra_syntax/src/yellow/mod.rs b/crates/ra_syntax/src/yellow/mod.rs index 95d277a2f..710320f47 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs @@ -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> { + ::algo::generate(Some(self), move |&node| match direction { + Direction::Next => node.next_sibling(), + Direction::Prev => node.prev_sibling(), + }) + } } impl> SyntaxNode { -- cgit v1.2.3