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

@ -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<impl FnOnce()
let syntax = file.syntax();
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
let left = non_trivia_sibling(comma, Direction::Backward)?;
let right = non_trivia_sibling(comma, Direction::Forward)?;
let prev = non_trivia_sibling(comma, Direction::Prev)?;
let next = non_trivia_sibling(comma, Direction::Next)?;
Some(move || {
let mut edit = EditBuilder::new();
edit.replace(left.range(), right.text().to_string());
edit.replace(right.range(), left.text().to_string());
edit.replace(prev.range(), next.text().to_string());
edit.replace(next.range(), prev.text().to_string());
LocalEdit {
edit: edit.finish(),
cursor_position: None,
@ -129,7 +128,7 @@ pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl F
}
fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
siblings(node, direction)
node.siblings(direction)
.skip(1)
.find(|node| !node.kind().is_trivia())
}

View file

@ -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<TextRange> {
@ -71,12 +71,12 @@ fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a
}
fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
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<TextRange> {
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") => (),

View file

@ -3,7 +3,7 @@ use std::collections::HashSet;
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") => (),