diff --git a/crates/ra_editor/src/code_actions.rs b/crates/ra_editor/src/code_actions.rs index b0f0d8f1d0f..216d592ff45 100644 --- a/crates/ra_editor/src/code_actions.rs +++ b/crates/ra_editor/src/code_actions.rs @@ -1,12 +1,11 @@ use join_to_string::join; use ra_syntax::{ - File, TextUnit, TextRange, + File, TextUnit, TextRange, Direction, ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, SyntaxKind::{COMMA, WHITESPACE}, SyntaxNodeRef, algo::{ - Direction, siblings, find_leaf_at_offset, find_covering_node, }, @@ -24,12 +23,12 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option(file: &'a File, range: TextRange) -> Option Option { - siblings(node, direction) + node.siblings(direction) .skip(1) .find(|node| !node.kind().is_trivia()) } diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index 2f8a04b2b32..b00a457b901 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs @@ -1,7 +1,7 @@ use ra_syntax::{ - File, TextRange, SyntaxNodeRef, TextUnit, + File, TextRange, SyntaxNodeRef, TextUnit, Direction, SyntaxKind::*, - algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node, Direction, siblings}, + algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node}, }; pub fn extend_selection(file: &File, range: TextRange) -> Option { @@ -71,12 +71,12 @@ fn priority(n: SyntaxNodeRef) -> usize { } fn extend_comments(node: SyntaxNodeRef) -> Option { - let left = adj_comments(node, Direction::Backward); - let right = adj_comments(node, Direction::Forward); - if left != right { + let prev = adj_comments(node, Direction::Prev); + let next = adj_comments(node, Direction::Next); + if prev != next { Some(TextRange::from_to( - left.range().start(), - right.range().end(), + prev.range().start(), + next.range().end(), )) } else { None @@ -85,7 +85,7 @@ fn extend_comments(node: SyntaxNodeRef) -> Option { fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { let mut res = node; - for node in siblings(node, dir) { + for node in node.siblings(dir) { match node.kind() { COMMENT => res = node, WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (), diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 892aaf97be6..733512368ef 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs @@ -3,7 +3,7 @@ use ra_syntax::{ File, TextRange, SyntaxNodeRef, SyntaxKind, - algo::{Direction, siblings}, + Direction, }; #[derive(Debug, PartialEq, Eq)] @@ -62,7 +62,7 @@ fn contiguous_range_for<'a>( let left = node; let mut right = node; - for node in siblings(node, Direction::Forward) { + for node in node.siblings(Direction::Next) { visited.insert(node); match node.kind() { SyntaxKind::WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (), diff --git a/crates/ra_syntax/src/algo/mod.rs b/crates/ra_syntax/src/algo/mod.rs index 3716a6000f4..a6678093d9e 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 c7eda45636e..738664afd0a 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -51,7 +51,7 @@ 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 95d277a2fc7..710320f47ca 100644 --- a/crates/ra_syntax/src/yellow/mod.rs +++ b/crates/ra_syntax/src/yellow/mod.rs @@ -58,6 +58,13 @@ pub(crate) fn new(green: GreenNode, errors: Vec) -> 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 @@ pub fn descendants(self) -> impl Iterator> { ::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 {