diff options
Diffstat (limited to 'crates/ra_editor/src/extend_selection.rs')
-rw-r--r-- | crates/ra_editor/src/extend_selection.rs | 24 |
1 files changed, 12 insertions, 12 deletions
diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index 7a423852b..08cae5a51 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs | |||
@@ -1,11 +1,10 @@ | |||
1 | use ra_syntax::{ | 1 | use ra_syntax::{ |
2 | Direction, SyntaxNode, TextRange, TextUnit, | ||
2 | algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, | 3 | algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, |
3 | Direction, | ||
4 | SyntaxKind::*, | 4 | SyntaxKind::*, |
5 | SyntaxNodeRef, TextRange, TextUnit, | ||
6 | }; | 5 | }; |
7 | 6 | ||
8 | pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { | 7 | pub fn extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> { |
9 | let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING]; | 8 | let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING]; |
10 | if range.is_empty() { | 9 | if range.is_empty() { |
11 | let offset = range.start(); | 10 | let offset = range.start(); |
@@ -40,7 +39,7 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan | |||
40 | } | 39 | } |
41 | 40 | ||
42 | fn extend_single_word_in_comment_or_string( | 41 | fn extend_single_word_in_comment_or_string( |
43 | leaf: SyntaxNodeRef, | 42 | leaf: &SyntaxNode, |
44 | offset: TextUnit, | 43 | offset: TextUnit, |
45 | ) -> Option<TextRange> { | 44 | ) -> Option<TextRange> { |
46 | let text: &str = leaf.leaf_text()?; | 45 | let text: &str = leaf.leaf_text()?; |
@@ -66,7 +65,7 @@ fn extend_single_word_in_comment_or_string( | |||
66 | } | 65 | } |
67 | } | 66 | } |
68 | 67 | ||
69 | fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRange { | 68 | fn extend_ws(root: &SyntaxNode, ws: &SyntaxNode, offset: TextUnit) -> TextRange { |
70 | let ws_text = ws.leaf_text().unwrap(); | 69 | let ws_text = ws.leaf_text().unwrap(); |
71 | let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start(); | 70 | let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start(); |
72 | let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start(); | 71 | let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start(); |
@@ -89,9 +88,9 @@ fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRa | |||
89 | ws.range() | 88 | ws.range() |
90 | } | 89 | } |
91 | 90 | ||
92 | fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> { | 91 | fn pick_best<'a>(l: &'a SyntaxNode, r: &'a SyntaxNode) -> &'a SyntaxNode { |
93 | return if priority(r) > priority(l) { r } else { l }; | 92 | return if priority(r) > priority(l) { r } else { l }; |
94 | fn priority(n: SyntaxNodeRef) -> usize { | 93 | fn priority(n: &SyntaxNode) -> usize { |
95 | match n.kind() { | 94 | match n.kind() { |
96 | WHITESPACE => 0, | 95 | WHITESPACE => 0, |
97 | IDENT | SELF_KW | SUPER_KW | CRATE_KW | LIFETIME => 2, | 96 | IDENT | SELF_KW | SUPER_KW | CRATE_KW | LIFETIME => 2, |
@@ -100,7 +99,7 @@ fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a | |||
100 | } | 99 | } |
101 | } | 100 | } |
102 | 101 | ||
103 | fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> { | 102 | fn extend_comments(node: &SyntaxNode) -> Option<TextRange> { |
104 | let prev = adj_comments(node, Direction::Prev); | 103 | let prev = adj_comments(node, Direction::Prev); |
105 | let next = adj_comments(node, Direction::Next); | 104 | let next = adj_comments(node, Direction::Next); |
106 | if prev != next { | 105 | if prev != next { |
@@ -110,7 +109,7 @@ fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> { | |||
110 | } | 109 | } |
111 | } | 110 | } |
112 | 111 | ||
113 | fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { | 112 | fn adj_comments(node: &SyntaxNode, dir: Direction) -> &SyntaxNode { |
114 | let mut res = node; | 113 | let mut res = node; |
115 | for node in node.siblings(dir) { | 114 | for node in node.siblings(dir) { |
116 | match node.kind() { | 115 | match node.kind() { |
@@ -124,13 +123,14 @@ fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef { | |||
124 | 123 | ||
125 | #[cfg(test)] | 124 | #[cfg(test)] |
126 | mod tests { | 125 | mod tests { |
127 | use super::*; | 126 | use ra_syntax::{SourceFile, AstNode}; |
128 | use ra_syntax::SourceFileNode; | ||
129 | use test_utils::extract_offset; | 127 | use test_utils::extract_offset; |
130 | 128 | ||
129 | use super::*; | ||
130 | |||
131 | fn do_check(before: &str, afters: &[&str]) { | 131 | fn do_check(before: &str, afters: &[&str]) { |
132 | let (cursor, before) = extract_offset(before); | 132 | let (cursor, before) = extract_offset(before); |
133 | let file = SourceFileNode::parse(&before); | 133 | let file = SourceFile::parse(&before); |
134 | let mut range = TextRange::offset_len(cursor, 0.into()); | 134 | let mut range = TextRange::offset_len(cursor, 0.into()); |
135 | for &after in afters { | 135 | for &after in afters { |
136 | range = extend_selection(file.syntax(), range).unwrap(); | 136 | range = extend_selection(file.syntax(), range).unwrap(); |