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, 20 insertions, 4 deletions
diff --git a/crates/ra_editor/src/extend_selection.rs b/crates/ra_editor/src/extend_selection.rs index bf0727dde..7a423852b 100644 --- a/crates/ra_editor/src/extend_selection.rs +++ b/crates/ra_editor/src/extend_selection.rs | |||
@@ -6,6 +6,7 @@ use ra_syntax::{ | |||
6 | }; | 6 | }; |
7 | 7 | ||
8 | pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { | 8 | pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { |
9 | let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING]; | ||
9 | if range.is_empty() { | 10 | if range.is_empty() { |
10 | let offset = range.start(); | 11 | let offset = range.start(); |
11 | let mut leaves = find_leaf_at_offset(root, offset); | 12 | let mut leaves = find_leaf_at_offset(root, offset); |
@@ -15,8 +16,8 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan | |||
15 | let leaf_range = match leaves { | 16 | let leaf_range = match leaves { |
16 | LeafAtOffset::None => return None, | 17 | LeafAtOffset::None => return None, |
17 | LeafAtOffset::Single(l) => { | 18 | LeafAtOffset::Single(l) => { |
18 | if l.kind() == COMMENT { | 19 | if string_kinds.contains(&l.kind()) { |
19 | extend_single_word_in_comment(l, offset).unwrap_or_else(|| l.range()) | 20 | extend_single_word_in_comment_or_string(l, offset).unwrap_or_else(|| l.range()) |
20 | } else { | 21 | } else { |
21 | l.range() | 22 | l.range() |
22 | } | 23 | } |
@@ -26,7 +27,7 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan | |||
26 | return Some(leaf_range); | 27 | return Some(leaf_range); |
27 | }; | 28 | }; |
28 | let node = find_covering_node(root, range); | 29 | let node = find_covering_node(root, range); |
29 | if node.kind() == COMMENT && range == node.range() { | 30 | if string_kinds.contains(&node.kind()) && range == node.range() { |
30 | if let Some(range) = extend_comments(node) { | 31 | if let Some(range) = extend_comments(node) { |
31 | return Some(range); | 32 | return Some(range); |
32 | } | 33 | } |
@@ -38,7 +39,10 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan | |||
38 | } | 39 | } |
39 | } | 40 | } |
40 | 41 | ||
41 | fn extend_single_word_in_comment(leaf: SyntaxNodeRef, offset: TextUnit) -> Option<TextRange> { | 42 | fn extend_single_word_in_comment_or_string( |
43 | leaf: SyntaxNodeRef, | ||
44 | offset: TextUnit, | ||
45 | ) -> Option<TextRange> { | ||
42 | let text: &str = leaf.leaf_text()?; | 46 | let text: &str = leaf.leaf_text()?; |
43 | let cursor_position: u32 = (offset - leaf.range().start()).into(); | 47 | let cursor_position: u32 = (offset - leaf.range().start()).into(); |
44 | 48 | ||
@@ -262,4 +266,16 @@ impl S { | |||
262 | &["hello", "// hello world"], | 266 | &["hello", "// hello world"], |
263 | ); | 267 | ); |
264 | } | 268 | } |
269 | |||
270 | #[test] | ||
271 | fn test_extend_selection_string() { | ||
272 | do_check( | ||
273 | r#" | ||
274 | fn bar(){} | ||
275 | |||
276 | " fn f<|>oo() {" | ||
277 | "#, | ||
278 | &["foo", "\" fn foo() {\""], | ||
279 | ); | ||
280 | } | ||
265 | } | 281 | } |