405: extend selection inside a string literal should select a word first r=matklad a=gfreezy

 fixed #402

Co-authored-by: gfreezy <gfreezy@gmail.com>
This commit is contained in:
bors[bot] 2019-01-02 15:49:25 +00:00
commit e6aadf6ef2
2 changed files with 21 additions and 5 deletions

View file

@ -6,6 +6,7 @@ use ra_syntax::{
}; };
pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> { 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() { if range.is_empty() {
let offset = range.start(); let offset = range.start();
let mut leaves = find_leaf_at_offset(root, offset); 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 { let leaf_range = match leaves {
LeafAtOffset::None => return None, LeafAtOffset::None => return None,
LeafAtOffset::Single(l) => { LeafAtOffset::Single(l) => {
if l.kind() == COMMENT { if string_kinds.contains(&l.kind()) {
extend_single_word_in_comment(l, offset).unwrap_or_else(|| l.range()) extend_single_word_in_comment_or_string(l, offset).unwrap_or_else(|| l.range())
} else { } else {
l.range() l.range()
} }
@ -26,7 +27,7 @@ pub fn extend_selection(root: SyntaxNodeRef, range: TextRange) -> Option<TextRan
return Some(leaf_range); return Some(leaf_range);
}; };
let node = find_covering_node(root, 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) { if let Some(range) = extend_comments(node) {
return Some(range); 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 text: &str = leaf.leaf_text()?;
let cursor_position: u32 = (offset - leaf.range().start()).into(); let cursor_position: u32 = (offset - leaf.range().start()).into();
@ -262,4 +266,16 @@ impl S {
&["hello", "// hello world"], &["hello", "// hello world"],
); );
} }
#[test]
fn test_extend_selection_string() {
do_check(
r#"
fn bar(){}
" fn f<|>oo() {"
"#,
&["foo", "\" fn foo() {\""],
);
}
} }

View file

@ -11,7 +11,7 @@
//! to support custom watcher events (related to https://github.com/rust-analyzer/rust-analyzer/issues/131) //! 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 //! 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. //! Cargo package.
mod arena; mod arena;
mod io; mod io;