2019-01-26 02:20:30 -06:00
|
|
|
use ra_db::SourceDatabase;
|
2019-03-20 15:52:55 -05:00
|
|
|
use ra_syntax::{
|
2019-03-30 05:25:53 -05:00
|
|
|
algo::{find_covering_element, find_token_at_offset, TokenAtOffset},
|
2019-04-02 02:23:18 -05:00
|
|
|
ast::{self, AstNode, AstToken},
|
2019-07-04 15:05:17 -05:00
|
|
|
Direction, SyntaxElement,
|
|
|
|
SyntaxKind::*,
|
|
|
|
SyntaxNode, SyntaxToken, TextRange, TextUnit, T,
|
2019-01-08 13:33:36 -06:00
|
|
|
};
|
|
|
|
|
2019-07-04 15:05:17 -05:00
|
|
|
use crate::{db::RootDatabase, FileRange};
|
2019-03-20 15:52:55 -05:00
|
|
|
|
2019-03-16 11:40:41 -05:00
|
|
|
// FIXME: restore macro support
|
2019-01-08 13:33:36 -06:00
|
|
|
pub(crate) fn extend_selection(db: &RootDatabase, frange: FileRange) -> TextRange {
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = db.parse(frange.file_id);
|
|
|
|
try_extend_selection(parse.tree().syntax(), frange.range).unwrap_or(frange.range)
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option<TextRange> {
|
|
|
|
let string_kinds = [COMMENT, STRING, RAW_STRING, BYTE_STRING, RAW_BYTE_STRING];
|
|
|
|
let list_kinds = [
|
|
|
|
FIELD_PAT_LIST,
|
|
|
|
MATCH_ARM_LIST,
|
|
|
|
NAMED_FIELD_DEF_LIST,
|
|
|
|
POS_FIELD_DEF_LIST,
|
|
|
|
NAMED_FIELD_LIST,
|
|
|
|
ENUM_VARIANT_LIST,
|
|
|
|
USE_TREE_LIST,
|
|
|
|
TYPE_PARAM_LIST,
|
|
|
|
TYPE_ARG_LIST,
|
|
|
|
PARAM_LIST,
|
|
|
|
ARG_LIST,
|
|
|
|
ARRAY_EXPR,
|
|
|
|
];
|
|
|
|
|
|
|
|
if range.is_empty() {
|
|
|
|
let offset = range.start();
|
2019-03-30 05:25:53 -05:00
|
|
|
let mut leaves = find_token_at_offset(root, offset);
|
2019-03-20 15:52:55 -05:00
|
|
|
if leaves.clone().all(|it| it.kind() == WHITESPACE) {
|
|
|
|
return Some(extend_ws(root, leaves.next()?, offset));
|
|
|
|
}
|
|
|
|
let leaf_range = match leaves {
|
2019-03-30 05:25:53 -05:00
|
|
|
TokenAtOffset::None => return None,
|
|
|
|
TokenAtOffset::Single(l) => {
|
2019-03-20 15:52:55 -05:00
|
|
|
if string_kinds.contains(&l.kind()) {
|
2019-07-19 04:56:47 -05:00
|
|
|
extend_single_word_in_comment_or_string(&l, offset).unwrap_or_else(|| l.range())
|
2019-03-20 15:52:55 -05:00
|
|
|
} else {
|
|
|
|
l.range()
|
|
|
|
}
|
|
|
|
}
|
2019-03-30 05:25:53 -05:00
|
|
|
TokenAtOffset::Between(l, r) => pick_best(l, r).range(),
|
2019-03-20 15:52:55 -05:00
|
|
|
};
|
|
|
|
return Some(leaf_range);
|
|
|
|
};
|
2019-03-30 05:25:53 -05:00
|
|
|
let node = match find_covering_element(root, range) {
|
|
|
|
SyntaxElement::Token(token) => {
|
|
|
|
if token.range() != range {
|
|
|
|
return Some(token.range());
|
|
|
|
}
|
2019-07-19 04:56:47 -05:00
|
|
|
if let Some(comment) = ast::Comment::cast(token.clone()) {
|
2019-03-30 05:25:53 -05:00
|
|
|
if let Some(range) = extend_comments(comment) {
|
|
|
|
return Some(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
token.parent()
|
|
|
|
}
|
|
|
|
SyntaxElement::Node(node) => node,
|
|
|
|
};
|
|
|
|
if node.range() != range {
|
|
|
|
return Some(node.range());
|
|
|
|
}
|
2019-03-20 15:52:55 -05:00
|
|
|
|
|
|
|
// Using shallowest node with same range allows us to traverse siblings.
|
|
|
|
let node = node.ancestors().take_while(|n| n.range() == node.range()).last().unwrap();
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
if node.parent().map(|n| list_kinds.contains(&n.kind())) == Some(true) {
|
2019-07-19 04:56:47 -05:00
|
|
|
if let Some(range) = extend_list_item(&node) {
|
2019-03-30 05:25:53 -05:00
|
|
|
return Some(range);
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
node.parent().map(|it| it.range())
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extend_single_word_in_comment_or_string(
|
2019-07-19 04:56:47 -05:00
|
|
|
leaf: &SyntaxToken,
|
2019-03-20 15:52:55 -05:00
|
|
|
offset: TextUnit,
|
|
|
|
) -> Option<TextRange> {
|
2019-03-30 05:25:53 -05:00
|
|
|
let text: &str = leaf.text();
|
2019-03-20 15:52:55 -05:00
|
|
|
let cursor_position: u32 = (offset - leaf.range().start()).into();
|
|
|
|
|
|
|
|
let (before, after) = text.split_at(cursor_position as usize);
|
|
|
|
|
|
|
|
fn non_word_char(c: char) -> bool {
|
|
|
|
!(c.is_alphanumeric() || c == '_')
|
|
|
|
}
|
|
|
|
|
|
|
|
let start_idx = before.rfind(non_word_char)? as u32;
|
2019-06-03 09:27:51 -05:00
|
|
|
let end_idx = after.find(non_word_char).unwrap_or_else(|| after.len()) as u32;
|
2019-03-20 15:52:55 -05:00
|
|
|
|
|
|
|
let from: TextUnit = (start_idx + 1).into();
|
|
|
|
let to: TextUnit = (cursor_position + end_idx).into();
|
|
|
|
|
|
|
|
let range = TextRange::from_to(from, to);
|
|
|
|
if range.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(range + leaf.range().start())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
fn extend_ws(root: &SyntaxNode, ws: SyntaxToken, offset: TextUnit) -> TextRange {
|
|
|
|
let ws_text = ws.text();
|
2019-03-20 15:52:55 -05:00
|
|
|
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') {
|
2019-03-30 05:25:53 -05:00
|
|
|
if let Some(node) = ws.next_sibling_or_token() {
|
2019-03-20 15:52:55 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
fn pick_best<'a>(l: SyntaxToken, r: SyntaxToken) -> SyntaxToken {
|
|
|
|
return if priority(&r) > priority(&l) { r } else { l };
|
|
|
|
fn priority(n: &SyntaxToken) -> usize {
|
2019-03-20 15:52:55 -05:00
|
|
|
match n.kind() {
|
|
|
|
WHITESPACE => 0,
|
2019-05-08 10:35:32 -05:00
|
|
|
IDENT | T![self] | T![super] | T![crate] | LIFETIME => 2,
|
2019-03-20 15:52:55 -05:00
|
|
|
_ => 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extend list item selection to include nearby comma and whitespace.
|
|
|
|
fn extend_list_item(node: &SyntaxNode) -> Option<TextRange> {
|
2019-03-30 05:25:53 -05:00
|
|
|
fn is_single_line_ws(node: &SyntaxToken) -> bool {
|
|
|
|
node.kind() == WHITESPACE && !node.text().contains('\n')
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
fn nearby_comma(node: &SyntaxNode, dir: Direction) -> Option<SyntaxToken> {
|
|
|
|
node.siblings_with_tokens(dir)
|
2019-03-20 15:52:55 -05:00
|
|
|
.skip(1)
|
2019-03-30 05:25:53 -05:00
|
|
|
.skip_while(|node| match node {
|
|
|
|
SyntaxElement::Node(_) => false,
|
|
|
|
SyntaxElement::Token(it) => is_single_line_ws(it),
|
|
|
|
})
|
2019-03-20 15:52:55 -05:00
|
|
|
.next()
|
2019-07-19 11:05:34 -05:00
|
|
|
.and_then(|it| it.into_token())
|
2019-05-15 07:35:47 -05:00
|
|
|
.filter(|node| node.kind() == T![,])
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(comma_node) = nearby_comma(node, Direction::Prev) {
|
|
|
|
return Some(TextRange::from_to(comma_node.range().start(), node.range().end()));
|
|
|
|
}
|
|
|
|
if let Some(comma_node) = nearby_comma(node, Direction::Next) {
|
|
|
|
// Include any following whitespace when comma if after list item.
|
|
|
|
let final_node = comma_node
|
2019-03-30 05:25:53 -05:00
|
|
|
.next_sibling_or_token()
|
2019-07-19 11:05:34 -05:00
|
|
|
.and_then(|it| it.into_token())
|
2019-03-20 15:52:55 -05:00
|
|
|
.filter(|node| is_single_line_ws(node))
|
|
|
|
.unwrap_or(comma_node);
|
|
|
|
|
|
|
|
return Some(TextRange::from_to(node.range().start(), final_node.range().end()));
|
|
|
|
}
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
None
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
|
2019-04-02 02:23:18 -05:00
|
|
|
fn extend_comments(comment: ast::Comment) -> Option<TextRange> {
|
2019-07-19 04:56:47 -05:00
|
|
|
let prev = adj_comments(&comment, Direction::Prev);
|
|
|
|
let next = adj_comments(&comment, Direction::Next);
|
2019-03-20 15:52:55 -05:00
|
|
|
if prev != next {
|
2019-03-30 05:25:53 -05:00
|
|
|
Some(TextRange::from_to(prev.syntax().range().start(), next.syntax().range().end()))
|
2019-03-20 15:52:55 -05:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 04:56:47 -05:00
|
|
|
fn adj_comments(comment: &ast::Comment, dir: Direction) -> ast::Comment {
|
|
|
|
let mut res = comment.clone();
|
2019-03-30 05:25:53 -05:00
|
|
|
for element in comment.syntax().siblings_with_tokens(dir) {
|
|
|
|
let token = match element.as_token() {
|
|
|
|
None => break,
|
|
|
|
Some(token) => token,
|
|
|
|
};
|
2019-07-19 04:56:47 -05:00
|
|
|
if let Some(c) = ast::Comment::cast(token.clone()) {
|
2019-03-30 05:25:53 -05:00
|
|
|
res = c
|
|
|
|
} else if token.kind() != WHITESPACE || token.text().contains("\n\n") {
|
|
|
|
break;
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-07-04 15:05:17 -05:00
|
|
|
use ra_syntax::{AstNode, SourceFile};
|
2019-03-20 15:52:55 -05:00
|
|
|
use test_utils::extract_offset;
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
fn do_check(before: &str, afters: &[&str]) {
|
|
|
|
let (cursor, before) = extract_offset(before);
|
2019-07-12 11:41:13 -05:00
|
|
|
let parse = SourceFile::parse(&before);
|
2019-03-20 15:52:55 -05:00
|
|
|
let mut range = TextRange::offset_len(cursor, 0.into());
|
|
|
|
for &after in afters {
|
2019-07-12 11:41:13 -05:00
|
|
|
range = try_extend_selection(parse.tree().syntax(), range).unwrap();
|
2019-03-20 15:52:55 -05:00
|
|
|
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_list() {
|
|
|
|
do_check(r#"fn foo(<|>x: i32) {}"#, &["x", "x: i32"]);
|
|
|
|
do_check(r#"fn foo(<|>x: i32, y: i32) {}"#, &["x", "x: i32", "x: i32, "]);
|
|
|
|
do_check(r#"fn foo(<|>x: i32,y: i32) {}"#, &["x", "x: i32", "x: i32,"]);
|
|
|
|
do_check(r#"fn foo(x: i32, <|>y: i32) {}"#, &["y", "y: i32", ", y: i32"]);
|
|
|
|
do_check(r#"fn foo(x: i32, <|>y: i32, ) {}"#, &["y", "y: i32", ", y: i32"]);
|
|
|
|
do_check(r#"fn foo(x: i32,<|>y: i32) {}"#, &["y", "y: i32", ",y: i32"]);
|
|
|
|
|
|
|
|
do_check(r#"const FOO: [usize; 2] = [ 22<|> , 33];"#, &["22", "22 , "]);
|
|
|
|
do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|>];"#, &["33", ", 33"]);
|
|
|
|
do_check(r#"const FOO: [usize; 2] = [ 22 , 33<|> ,];"#, &["33", ", 33"]);
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
const FOO: [usize; 2] = [
|
|
|
|
22,
|
|
|
|
<|>33,
|
|
|
|
]"#,
|
|
|
|
&["33", "33,"],
|
|
|
|
);
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
const FOO: [usize; 2] = [
|
|
|
|
22
|
|
|
|
, 33<|>,
|
|
|
|
]"#,
|
|
|
|
&["33", ", 33"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_start_of_the_line() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
impl S {
|
|
|
|
<|> fn foo() {
|
|
|
|
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
&[" fn foo() {\n\n }\n"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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}"],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_comments() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn bar(){}
|
|
|
|
|
|
|
|
// fn foo() {
|
|
|
|
// 1 + <|>1
|
|
|
|
// }
|
|
|
|
|
|
|
|
// fn foo(){}
|
|
|
|
"#,
|
|
|
|
&["1", "// 1 + 1", "// fn foo() {\n// 1 + 1\n// }"],
|
|
|
|
);
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
// pub enum Direction {
|
|
|
|
// <|> Next,
|
|
|
|
// Prev
|
|
|
|
// }
|
|
|
|
"#,
|
|
|
|
&[
|
|
|
|
"// Next,",
|
|
|
|
"// #[derive(Debug, Clone, Copy, PartialEq, Eq)]\n// pub enum Direction {\n// Next,\n// Prev\n// }",
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
/*
|
|
|
|
foo
|
|
|
|
_bar1<|>*/
|
2019-03-30 05:25:53 -05:00
|
|
|
"#,
|
2019-03-20 15:52:55 -05:00
|
|
|
&["_bar1", "/*\nfoo\n_bar1*/"],
|
|
|
|
);
|
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
do_check(r#"//!<|>foo_2 bar"#, &["foo_2", "//!foo_2 bar"]);
|
2019-03-20 15:52:55 -05:00
|
|
|
|
2019-03-30 05:25:53 -05:00
|
|
|
do_check(r#"/<|>/foo bar"#, &["//foo bar"]);
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_prefer_idents() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn main() { foo<|>+bar;}
|
2019-03-30 05:25:53 -05:00
|
|
|
"#,
|
2019-03-20 15:52:55 -05:00
|
|
|
&["foo", "foo+bar"],
|
|
|
|
);
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn main() { foo+<|>bar;}
|
2019-03-30 05:25:53 -05:00
|
|
|
"#,
|
2019-03-20 15:52:55 -05:00
|
|
|
&["bar", "foo+bar"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_prefer_lifetimes() {
|
|
|
|
do_check(r#"fn foo<<|>'a>() {}"#, &["'a", "<'a>"]);
|
|
|
|
do_check(r#"fn foo<'a<|>>() {}"#, &["'a", "<'a>"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_select_first_word() {
|
|
|
|
do_check(r#"// foo bar b<|>az quxx"#, &["baz", "// foo bar baz quxx"]);
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
impl S {
|
2019-03-30 05:25:53 -05:00
|
|
|
fn foo() {
|
|
|
|
// hel<|>lo world
|
2019-03-20 15:52:55 -05:00
|
|
|
}
|
2019-03-30 05:25:53 -05:00
|
|
|
}
|
|
|
|
"#,
|
2019-03-20 15:52:55 -05:00
|
|
|
&["hello", "// hello world"],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_extend_selection_string() {
|
|
|
|
do_check(
|
|
|
|
r#"
|
|
|
|
fn bar(){}
|
|
|
|
|
|
|
|
" fn f<|>oo() {"
|
2019-03-30 05:25:53 -05:00
|
|
|
"#,
|
2019-03-20 15:52:55 -05:00
|
|
|
&["foo", "\" fn foo() {\""],
|
|
|
|
);
|
|
|
|
}
|
2019-01-08 13:33:36 -06:00
|
|
|
}
|