diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-02 15:49:25 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-02 15:49:25 +0000 |
commit | e6aadf6ef21a25a6dae55f39bc774b5a0ef2ad6c (patch) | |
tree | 3ba089370f0a838df54b82181ccf2fb636f8d780 /crates | |
parent | 29d8bfb9c909847cb37ff6e564ea0e61744277ad (diff) | |
parent | 9672ae001e6be8f4ad3a2fd247999ebb205736ab (diff) |
Merge #405
405: extend selection inside a string literal should select a word first r=matklad a=gfreezy
fixed #402
Co-authored-by: gfreezy <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_editor/src/extend_selection.rs | 24 | ||||
-rw-r--r-- | crates/ra_vfs/src/lib.rs | 2 |
2 files changed, 21 insertions, 5 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 | } |
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs index 757eac95b..5bbc3e993 100644 --- a/crates/ra_vfs/src/lib.rs +++ b/crates/ra_vfs/src/lib.rs | |||
@@ -11,7 +11,7 @@ | |||
11 | //! to support custom watcher events (related to https://github.com/rust-analyzer/rust-analyzer/issues/131) | 11 | //! to support custom watcher events (related to https://github.com/rust-analyzer/rust-analyzer/issues/131) |
12 | //! | 12 | //! |
13 | //! VFS is based on a concept of roots: a set of directories on the file system | 13 | //! VFS is based on a concept of roots: a set of directories on the file system |
14 | //! whihc are watched for changes. Typically, there will be a root for each | 14 | //! which are watched for changes. Typically, there will be a root for each |
15 | //! Cargo package. | 15 | //! Cargo package. |
16 | mod arena; | 16 | mod arena; |
17 | mod io; | 17 | mod io; |