2018-09-16 12:54:24 +03:00
|
|
|
use ra_syntax::{
|
2018-10-02 18:14:33 +03:00
|
|
|
File, TextRange, SyntaxNodeRef, TextUnit, Direction,
|
2018-08-26 10:43:03 +03:00
|
|
|
SyntaxKind::*,
|
2018-10-02 18:14:33 +03:00
|
|
|
algo::{find_leaf_at_offset, LeafAtOffset, find_covering_node},
|
2018-08-07 18:28:30 +03:00
|
|
|
};
|
|
|
|
|
2018-08-25 11:44:58 +03:00
|
|
|
pub fn extend_selection(file: &File, range: TextRange) -> Option<TextRange> {
|
2018-08-11 12:28:59 +03:00
|
|
|
let syntax = file.syntax();
|
2018-08-17 22:03:55 +03:00
|
|
|
extend(syntax.borrowed(), range)
|
2018-08-11 12:28:59 +03:00
|
|
|
}
|
2018-08-07 18:28:30 +03:00
|
|
|
|
2018-08-11 12:28:59 +03:00
|
|
|
pub(crate) fn extend(root: SyntaxNodeRef, range: TextRange) -> Option<TextRange> {
|
2018-08-07 18:28:30 +03:00
|
|
|
if range.is_empty() {
|
|
|
|
let offset = range.start();
|
|
|
|
let mut leaves = find_leaf_at_offset(root, offset);
|
2018-09-04 12:48:39 +03:00
|
|
|
if leaves.clone().all(|it| it.kind() == WHITESPACE) {
|
|
|
|
return Some(extend_ws(root, leaves.next()?, offset));
|
2018-08-07 18:28:30 +03:00
|
|
|
}
|
2018-10-03 17:04:00 -04:00
|
|
|
let leaf_range = match leaves {
|
2018-09-04 12:48:39 +03:00
|
|
|
LeafAtOffset::None => return None,
|
2018-10-03 17:04:00 -04:00
|
|
|
LeafAtOffset::Single(l) => {
|
|
|
|
if l.kind() == COMMENT {
|
2018-10-04 09:35:55 -04:00
|
|
|
extend_single_word_in_comment(l, offset).unwrap_or_else(||l.range())
|
2018-10-03 17:04:00 -04:00
|
|
|
} else {
|
|
|
|
l.range()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
LeafAtOffset::Between(l, r) => pick_best(l, r).range(),
|
2018-09-04 12:48:39 +03:00
|
|
|
};
|
2018-10-03 17:04:00 -04:00
|
|
|
return Some(leaf_range);
|
2018-08-07 18:28:30 +03:00
|
|
|
};
|
|
|
|
let node = find_covering_node(root, range);
|
2018-08-26 10:43:03 +03:00
|
|
|
if node.kind() == COMMENT && range == node.range() {
|
|
|
|
if let Some(range) = extend_comments(node) {
|
|
|
|
return Some(range);
|
|
|
|
}
|
|
|
|
}
|
2018-08-07 18:28:30 +03:00
|
|
|
|
2018-10-02 18:02:57 +03:00
|
|
|
match node.ancestors().skip_while(|n| n.range() == range).next() {
|
2018-08-07 18:28:30 +03:00
|
|
|
None => None,
|
|
|
|
Some(parent) => Some(parent.range()),
|
|
|
|
}
|
|
|
|
}
|
2018-08-26 10:43:03 +03:00
|
|
|
|
2018-10-04 09:35:55 -04:00
|
|
|
fn extend_single_word_in_comment(leaf: SyntaxNodeRef, offset: TextUnit) -> Option<TextRange> {
|
2018-10-09 16:16:51 +03:00
|
|
|
let text: &str = leaf.leaf_text()?;
|
2018-10-04 09:35:55 -04:00
|
|
|
let cursor_position: u32 = (offset - leaf.range().start()).into();
|
2018-10-03 17:04:00 -04:00
|
|
|
|
|
|
|
let (before, after) = text.split_at(cursor_position as usize);
|
2018-10-04 09:39:02 -04:00
|
|
|
let start_idx = before.rfind(char::is_whitespace)? as u32;
|
|
|
|
let end_idx = after.find(char::is_whitespace)? as u32;
|
2018-10-03 17:04:00 -04:00
|
|
|
|
2018-10-09 16:16:51 +03:00
|
|
|
let from: TextUnit = (start_idx + 1).into();
|
|
|
|
let to: TextUnit = (cursor_position + end_idx).into();
|
2018-10-04 09:35:55 -04:00
|
|
|
|
2018-10-09 16:16:51 +03:00
|
|
|
Some(TextRange::from_to(from, to) + leaf.range().start())
|
2018-10-03 17:04:00 -04:00
|
|
|
}
|
|
|
|
|
2018-09-04 12:48:39 +03:00
|
|
|
fn extend_ws(root: SyntaxNodeRef, ws: SyntaxNodeRef, offset: TextUnit) -> TextRange {
|
|
|
|
let ws_text = ws.leaf_text().unwrap();
|
|
|
|
let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start();
|
|
|
|
let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start();
|
|
|
|
let ws_suffix = &ws_text.as_str()[suffix];
|
|
|
|
let ws_prefix = &ws_text.as_str()[prefix];
|
|
|
|
if ws_text.contains("\n") && !ws_suffix.contains("\n") {
|
|
|
|
if let Some(node) = ws.next_sibling() {
|
|
|
|
let start = match ws_prefix.rfind('\n') {
|
|
|
|
Some(idx) => ws.range().start() + TextUnit::from((idx + 1) as u32),
|
|
|
|
None => node.range().start()
|
|
|
|
};
|
|
|
|
let end = if root.text().char_at(node.range().end()) == Some('\n') {
|
|
|
|
node.range().end() + TextUnit::of_char('\n')
|
|
|
|
} else {
|
|
|
|
node.range().end()
|
|
|
|
};
|
|
|
|
return TextRange::from_to(start, end);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.range()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pick_best<'a>(l: SyntaxNodeRef<'a>, r: SyntaxNodeRef<'a>) -> SyntaxNodeRef<'a> {
|
|
|
|
return if priority(r) > priority(l) { r } else { l };
|
|
|
|
fn priority(n: SyntaxNodeRef) -> usize {
|
|
|
|
match n.kind() {
|
|
|
|
WHITESPACE => 0,
|
2018-09-19 13:55:47 +03:00
|
|
|
IDENT | SELF_KW | SUPER_KW | CRATE_KW | LIFETIME => 2,
|
2018-09-04 12:48:39 +03:00
|
|
|
_ => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-26 10:43:03 +03:00
|
|
|
fn extend_comments(node: SyntaxNodeRef) -> Option<TextRange> {
|
2018-10-02 18:14:33 +03:00
|
|
|
let prev = adj_comments(node, Direction::Prev);
|
|
|
|
let next = adj_comments(node, Direction::Next);
|
|
|
|
if prev != next {
|
2018-08-26 10:43:03 +03:00
|
|
|
Some(TextRange::from_to(
|
2018-10-02 18:14:33 +03:00
|
|
|
prev.range().start(),
|
|
|
|
next.range().end(),
|
2018-08-26 10:43:03 +03:00
|
|
|
))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn adj_comments(node: SyntaxNodeRef, dir: Direction) -> SyntaxNodeRef {
|
|
|
|
let mut res = node;
|
2018-10-02 18:14:33 +03:00
|
|
|
for node in node.siblings(dir) {
|
2018-08-26 10:43:03 +03:00
|
|
|
match node.kind() {
|
|
|
|
COMMENT => res = node,
|
|
|
|
WHITESPACE if !node.leaf_text().unwrap().as_str().contains("\n\n") => (),
|
|
|
|
_ => break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
2018-08-28 14:47:12 +03:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use test_utils::extract_offset;
|
|
|
|
|
|
|
|
fn do_check(before: &str, afters: &[&str]) {
|
|
|
|
let (cursor, before) = extract_offset(before);
|
|
|
|
let file = File::parse(&before);
|
|
|
|
let mut range = TextRange::offset_len(cursor, 0.into());
|
|
|
|
for &after in afters {
|
|
|
|
range = extend_selection(&file, range)
|
|
|
|
.unwrap();
|
|
|
|
let actual = &before[range];
|
|
|
|
assert_eq!(after, actual);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_arith() {
|
|
|
|
do_check(
|
|
|
|
r#"fn foo() { <|>1 + 1 }"#,
|
|
|
|
&["1", "1 + 1", "{ 1 + 1 }"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_start_of_the_lind() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
impl S {
|
|
|
|
<|> fn foo() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}"#,
|
2018-08-31 14:52:29 +03:00
|
|
|
&[" fn foo() {\n\n }\n"]
|
2018-08-28 14:47:12 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-10-08 17:33:13 +03:00
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_doc_comments() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
struct A;
|
|
|
|
|
|
|
|
/// bla
|
|
|
|
/// bla
|
|
|
|
struct B {
|
|
|
|
<|>
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
&["\n \n", "{\n \n}", "/// bla\n/// bla\nstruct B {\n \n}"]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:47:12 +03:00
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_comments() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn bar(){}
|
|
|
|
|
|
|
|
// fn foo() {
|
|
|
|
// 1 + <|>1
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn foo(){}
|
|
|
|
"#,
|
|
|
|
&["// 1 + 1", "// fn foo() {\n// 1 + 1\n// }"]
|
|
|
|
);
|
|
|
|
}
|
2018-09-04 12:48:39 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_prefer_idents() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn main() { foo<|>+bar;}
|
|
|
|
"#,
|
|
|
|
&["foo", "foo+bar"]
|
|
|
|
);
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn main() { foo+<|>bar;}
|
|
|
|
"#,
|
|
|
|
&["bar", "foo+bar"]
|
|
|
|
);
|
|
|
|
}
|
2018-09-19 13:55:47 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_prefer_lifetimes() {
|
|
|
|
do_check(
|
|
|
|
r#"fn foo<<|>'a>() {}"#,
|
|
|
|
&["'a", "<'a>"]
|
|
|
|
);
|
|
|
|
do_check(
|
|
|
|
r#"fn foo<'a<|>>() {}"#,
|
|
|
|
&["'a", "<'a>"]
|
|
|
|
);
|
|
|
|
}
|
2018-10-03 17:04:00 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_select_first_word() {
|
|
|
|
do_check(
|
|
|
|
r#"// foo bar b<|>az quxx"#,
|
|
|
|
&["baz", "// foo bar baz quxx"]
|
|
|
|
);
|
2018-10-09 16:16:51 +03:00
|
|
|
do_check(r#"
|
|
|
|
impl S {
|
|
|
|
fn foo() {
|
|
|
|
// hel<|>lo world
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
&["hello", "// hello world"]
|
|
|
|
);
|
2018-10-03 17:04:00 -04:00
|
|
|
}
|
2018-08-28 14:47:12 +03:00
|
|
|
}
|