aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_editor/src/code_actions.rs13
-rw-r--r--crates/ra_editor/src/extend_selection.rs16
-rw-r--r--crates/ra_editor/src/folding_ranges.rs4
-rw-r--r--crates/ra_syntax/src/algo/mod.rs16
-rw-r--r--crates/ra_syntax/src/lib.rs2
-rw-r--r--crates/ra_syntax/src/yellow/mod.rs13
6 files changed, 30 insertions, 34 deletions
diff --git a/crates/ra_editor/src/code_actions.rs b/crates/ra_editor/src/code_actions.rs
index b0f0d8f1d..216d592ff 100644
--- a/crates/ra_editor/src/code_actions.rs
+++ b/crates/ra_editor/src/code_actions.rs
@@ -1,12 +1,11 @@
1use join_to_string::join; 1use join_to_string::join;
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 File, TextUnit, TextRange, 4 File, TextUnit, TextRange, Direction,
5 ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner}, 5 ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner},
6 SyntaxKind::{COMMA, WHITESPACE}, 6 SyntaxKind::{COMMA, WHITESPACE},
7 SyntaxNodeRef, 7 SyntaxNodeRef,
8 algo::{ 8 algo::{
9 Direction, siblings,
10 find_leaf_at_offset, 9 find_leaf_at_offset,
11 find_covering_node, 10 find_covering_node,
12 }, 11 },
@@ -24,12 +23,12 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce()
24 let syntax = file.syntax(); 23 let syntax = file.syntax();
25 24
26 let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; 25 let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
27 let left = non_trivia_sibling(comma, Direction::Backward)?; 26 let prev = non_trivia_sibling(comma, Direction::Prev)?;
28 let right = non_trivia_sibling(comma, Direction::Forward)?; 27 let next = non_trivia_sibling(comma, Direction::Next)?;
29 Some(move || { 28 Some(move || {
30 let mut edit = EditBuilder::new(); 29 let mut edit = EditBuilder::new();
31 edit.replace(left.range(), right.text().to_string()); 30 edit.replace(prev.range(), next.text().to_string());
32 edit.replace(right.range(), left.text().to_string()); 31 edit.replace(next.range(), prev.text().to_string());
33 LocalEdit { 32 LocalEdit {
34 edit: edit.finish(), 33 edit: edit.finish(),
35 cursor_position: None, 34 cursor_position: None,
@@ -129,7 +128,7 @@ pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl F
129} 128}
130 129
131fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> { 130fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
132 siblings(node, direction) 131 node.siblings(direction)
133 .skip(1) 132 .skip(1)
134 .find(|node| !node.kind().is_trivia()) 133 .find(|node| !node.kind().is_trivia())
135} 134}
diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs
index 2f8a04b2b..b00a457b9 100644
--- a/crates/ra_editor/src/extend_selection.rs
+++ b/crates/ra_editor/src/extend_selection.rs
@@ -1,7 +1,7 @@
1use ra_syntax::{ 1use ra_syntax::{
2 File, TextRange, SyntaxNodeRef, TextUnit, 2 File, TextRange, SyntaxNodeRef, TextUnit, Direction,
3 SyntaxKind::*, 3 SyntaxKind::*,
4 algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node, Direction, siblings}, 4 algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node},
5}; 5};
6 6
7pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> { 7pub 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
71} 71}
72 72
73fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> { 73fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
74 let left = adj_comments(node, Direction::Backward); 74 let prev = adj_comments(node, Direction::Prev);
75 let right = adj_comments(node, Direction::Forward); 75 let next = adj_comments(node, Direction::Next);
76 if left != right { 76 if prev != next {
77 Some(TextRange::from_to( 77 Some(TextRange::from_to(
78 left.range().start(), 78 prev.range().start(),
79 right.range().end(), 79 next.range().end(),
80 )) 80 ))
81 } else { 81 } else {
82 None 82 None
@@ -85,7 +85,7 @@ fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
85 85
86fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { 86fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef {
87 let mut res = node; 87 let mut res = node;
88 for node in siblings(node, dir) { 88 for node in node.siblings(dir) {
89 match node.kind() { 89 match node.kind() {
90 COMMENT => res = node, 90 COMMENT => res = node,
91 WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (), 91 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 892aaf97b..733512368 100644
--- a/crates/ra_editor/src/folding_ranges.rs
+++ b/crates/ra_editor/src/folding_ranges.rs
@@ -3,7 +3,7 @@ use std::collections::HashSet;
3use ra_syntax::{ 3use ra_syntax::{
4 File, TextRange, SyntaxNodeRef, 4 File, TextRange, SyntaxNodeRef,
5 SyntaxKind, 5 SyntaxKind,
6 algo::{Direction, siblings}, 6 Direction,
7}; 7};
8 8
9#[derive(Debug, PartialEq, Eq)] 9#[derive(Debug, PartialEq, Eq)]
@@ -62,7 +62,7 @@ fn contiguous_range_for<'a>(
62 62
63 let left = node; 63 let left = node;
64 let mut right = node; 64 let mut right = node;
65 for node in siblings(node, Direction::Forward) { 65 for node in node.siblings(Direction::Next) {
66 visited.insert(node); 66 visited.insert(node);
67 match node.kind() { 67 match node.kind() {
68 SyntaxKind::WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (), 68 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 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)]
98pub enum Direction {
99 Forward,
100 Backward,
101}
102
103pub 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
113fn common_ancestor<'a>(n1: SyntaxNodeRef<'a>, n2: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { 97fn 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)]
63pub enum Direction {
64 Next,
65 Prev,
66}
67
61impl<'a> SyntaxNodeRef<'a> { 68impl<'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
76impl<R: TreeRoot<RaTypes>> SyntaxNode<R> { 89impl<R: TreeRoot<RaTypes>> SyntaxNode<R> {