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 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) (limited to 'crates/ra_editor/src') 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() {\""], + ); + } } -- cgit v1.2.3