]> git.lizzy.rs Git - rust.git/commitdiff
extend selection inside a string literal should select a word first
authorgfreezy <gfreezy@gmail.com>
Wed, 2 Jan 2019 15:42:38 +0000 (23:42 +0800)
committergfreezy <gfreezy@gmail.com>
Wed, 2 Jan 2019 15:42:38 +0000 (23:42 +0800)
crates/ra_editor/src/extend_selection.rs
crates/ra_vfs/src/lib.rs

index bf0727dde29271774940436e2040281cf6cd714d..7a423852bff11e36fd3c7655db53a528ad5b04f1 100644 (file)
@@ -6,6 +6,7 @@
 };
 
 pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> {
+    let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING];
     if range.is_empty() {
         let offset = range.start();
         let mut leaves = find_leaf_at_offset(root, offset);
@@ -15,8 +16,8 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan
         let leaf_range = match leaves {
             LeafAtOffset::None => return None,
             LeafAtOffset::Single(l) => {
-                if l.kind() == COMMENT {
-                    extend_single_word_in_comment(l, offset).unwrap_or_else(|| l.range())
+                if string_kinds.contains(&l.kind()) {
+                    extend_single_word_in_comment_or_string(l, offset).unwrap_or_else(|| l.range())
                 } else {
                     l.range()
                 }
@@ -26,7 +27,7 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan
         return Some(leaf_range);
     };
     let node = find_covering_node(root, range);
-    if node.kind() == COMMENT && range == node.range() {
+    if string_kinds.contains(&node.kind()) && range == node.range() {
         if let Some(range) = extend_comments(node) {
             return Some(range);
         }
@@ -38,7 +39,10 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan
     }
 }
 
-fn extend_single_word_in_comment(leaf: SyntaxNodeRef, offset: TextUnit) -> Option<TextRange> {
+fn extend_single_word_in_comment_or_string(
+    leaf: SyntaxNodeRef,
+    offset: TextUnit,
+) -> Option<TextRange> {
     let text: &str = leaf.leaf_text()?;
     let cursor_position: u32 = (offset - leaf.range().start()).into();
 
@@ -262,4 +266,16 @@ fn foo() {
             &["hello", "// hello world"],
         );
     }
+
+    #[test]
+    fn test_extend_selection_string() {
+        do_check(
+            r#"
+fn bar(){}
+
+" fn f<|>oo() {"
+    "#,
+            &["foo", "\" fn foo() {\""],
+        );
+    }
 }
index 757eac95bb232ce993ea064a79c5ad1062955768..5bbc3e99353371e424270b551bdad958d6e73c9c 100644 (file)
@@ -11,7 +11,7 @@
 //! to support custom watcher events (related to https://github.com/rust-analyzer/rust-analyzer/issues/131)
 //!
 //! VFS is based on a concept of roots: a set of directories on the file system
-//! whihc are watched for changes. Typically, there will be a root for each
+//! which are watched for changes. Typically, there will be a root for each
 //! Cargo package.
 mod arena;
 mod io;