diff options
author | Aleksey Kladov <[email protected]> | 2018-10-02 16:14:33 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-10-02 16:14:33 +0100 |
commit | 1a2a8dec14ec04ea8eeccae99fd885e7a280e45b (patch) | |
tree | f94bb3a8bd47e3612ef29e815532c07d7871fa0f /crates/ra_syntax | |
parent | d323c81d5cc6a198239285abcede2166181d8f39 (diff) |
Make siblings an inherent method
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r-- | crates/ra_syntax/src/algo/mod.rs | 16 | ||||
-rw-r--r-- | crates/ra_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_syntax/src/yellow/mod.rs | 13 |
3 files changed, 14 insertions, 17 deletions
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 | |||
94 | common_ancestor(left, right) | 94 | common_ancestor(left, right) |
95 | } | 95 | } |
96 | 96 | ||
97 | #[derive(Debug)] | ||
98 | pub enum Direction { | ||
99 | Forward, | ||
100 | Backward, | ||
101 | } | ||
102 | |||
103 | pub fn siblings<'a>( | ||
104 | node: SyntaxNodeRef<'a>, | ||
105 | direction: Direction | ||
106 | ) -> impl Iterator<Item=SyntaxNodeRef<'a>> { | ||
107 | generate(Some(node), move |&node| match direction { | ||
108 | Direction::Forward => node.next_sibling(), | ||
109 | Direction::Backward => node.prev_sibling(), | ||
110 | }) | ||
111 | } | ||
112 | |||
113 | fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { | 97 | fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { |
114 | for p in n1.ancestors() { | 98 | for p in n1.ancestors() { |
115 | if n2.ancestors().any(|a| a == p) { | 99 | 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 { | |||
51 | ast::AstNode, | 51 | ast::AstNode, |
52 | lexer::{tokenize, Token}, | 52 | lexer::{tokenize, Token}, |
53 | syntax_kinds::SyntaxKind, | 53 | syntax_kinds::SyntaxKind, |
54 | yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError}, | 54 | yellow::{SyntaxNode, SyntaxNodeRef, OwnedRoot, RefRoot, TreeRoot, SyntaxError, Direction}, |
55 | reparsing::AtomEdit, | 55 | reparsing::AtomEdit, |
56 | }; | 56 | }; |
57 | 57 | ||
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 { | |||
58 | SyntaxNode(::rowan::SyntaxNode::new(green, errors)) | 58 | SyntaxNode(::rowan::SyntaxNode::new(green, errors)) |
59 | } | 59 | } |
60 | } | 60 | } |
61 | |||
62 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
63 | pub enum Direction { | ||
64 | Next, | ||
65 | Prev, | ||
66 | } | ||
67 | |||
61 | impl<'a> SyntaxNodeRef<'a> { | 68 | impl<'a> SyntaxNodeRef<'a> { |
62 | pub fn leaf_text(self) -> Option<&'a SmolStr> { | 69 | pub fn leaf_text(self) -> Option<&'a SmolStr> { |
63 | self.0.leaf_text() | 70 | self.0.leaf_text() |
@@ -71,6 +78,12 @@ impl<'a> SyntaxNodeRef<'a> { | |||
71 | ::algo::walk::WalkEvent::Exit(_) => None, | 78 | ::algo::walk::WalkEvent::Exit(_) => None, |
72 | }) | 79 | }) |
73 | } | 80 | } |
81 | pub fn siblings(self, direction: Direction) -> impl Iterator<Item=SyntaxNodeRef<'a>> { | ||
82 | ::algo::generate(Some(self), move |&node| match direction { | ||
83 | Direction::Next => node.next_sibling(), | ||
84 | Direction::Prev => node.prev_sibling(), | ||
85 | }) | ||
86 | } | ||
74 | } | 87 | } |
75 | 88 | ||
76 | impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { | 89 | impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { |