From 9672ae001e6be8f4ad3a2fd247999ebb205736ab Mon Sep 17 00:00:00 2001 From: gfreezy Date: Wed, 2 Jan 2019 23:42:38 +0800 Subject: extend selection inside a string literal should select a word first --- crates/ra_editor/src/extend_selection.rs | 24 ++++++++++++++++++++---- 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::{ }; pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option { + 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 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 Option Option { +fn extend_single_word_in_comment_or_string( + leaf: SyntaxNodeRef, + offset: TextUnit, +) -> Option { let text: &str = leaf.leaf_text()?; let cursor_position: u32 = (offset - leaf.range().start()).into(); @@ -262,4 +266,16 @@ impl S { &["hello", "// hello world"], ); } + + #[test] + fn test_extend_selection_string() { + do_check( + r#" +fn bar(){} + +" fn f<|>oo() {" + "#, + &["foo", "\" fn foo() {\""], + ); + } } 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 @@ //! 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; -- cgit v1.2.3