2018-08-28 21:37:49 +02:00
|
|
|
use join_to_string::join;
|
|
|
|
|
2018-09-16 11:54:24 +02:00
|
|
|
use ra_syntax::{
|
2018-10-15 23:44:23 +02:00
|
|
|
algo::{find_covering_node, find_leaf_at_offset},
|
|
|
|
ast::{self, AstNode, AttrsOwner, NameOwner, TypeParamsOwner},
|
2018-11-07 16:32:33 +01:00
|
|
|
Direction, SourceFileNode,
|
2018-12-02 10:19:22 +01:00
|
|
|
SyntaxKind::{COMMA, WHITESPACE, COMMENT},
|
2018-10-15 23:44:23 +02:00
|
|
|
SyntaxNodeRef, TextRange, TextUnit,
|
2018-08-12 17:50:16 +02:00
|
|
|
};
|
|
|
|
|
2018-12-11 19:07:17 +01:00
|
|
|
use crate::{find_node_at_offset, TextEdit, TextEditBuilder};
|
2018-08-22 18:02:37 +02:00
|
|
|
|
2018-08-23 19:55:23 +02:00
|
|
|
#[derive(Debug)]
|
2018-08-29 17:03:14 +02:00
|
|
|
pub struct LocalEdit {
|
2018-12-11 19:07:17 +01:00
|
|
|
pub edit: TextEdit,
|
2018-08-22 11:58:34 +02:00
|
|
|
pub cursor_position: Option<TextUnit>,
|
2018-08-15 22:24:20 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 16:32:33 +01:00
|
|
|
pub fn flip_comma<'a>(
|
|
|
|
file: &'a SourceFileNode,
|
|
|
|
offset: TextUnit,
|
|
|
|
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-08-12 17:50:16 +02:00
|
|
|
let syntax = file.syntax();
|
|
|
|
|
|
|
|
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
|
2018-10-02 17:14:33 +02:00
|
|
|
let prev = non_trivia_sibling(comma, Direction::Prev)?;
|
|
|
|
let next = non_trivia_sibling(comma, Direction::Next)?;
|
2018-08-12 17:50:16 +02:00
|
|
|
Some(move || {
|
2018-12-11 19:07:17 +01:00
|
|
|
let mut edit = TextEditBuilder::new();
|
2018-10-02 17:14:33 +02:00
|
|
|
edit.replace(prev.range(), next.text().to_string());
|
|
|
|
edit.replace(next.range(), prev.text().to_string());
|
2018-08-29 17:03:14 +02:00
|
|
|
LocalEdit {
|
2018-08-15 22:24:20 +02:00
|
|
|
edit: edit.finish(),
|
2018-08-22 11:58:34 +02:00
|
|
|
cursor_position: None,
|
2018-08-15 22:24:20 +02:00
|
|
|
}
|
2018-08-12 17:50:16 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-11-07 16:32:33 +01:00
|
|
|
pub fn add_derive<'a>(
|
|
|
|
file: &'a SourceFileNode,
|
|
|
|
offset: TextUnit,
|
|
|
|
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-08-26 08:12:18 +02:00
|
|
|
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
|
2018-12-02 10:19:22 +01:00
|
|
|
let node_start = derive_insertion_offset(nominal)?;
|
|
|
|
return Some(move || {
|
2018-08-16 12:11:20 +02:00
|
|
|
let derive_attr = nominal
|
|
|
|
.attrs()
|
|
|
|
.filter_map(|x| x.as_call())
|
|
|
|
.filter(|(name, _arg)| name == "derive")
|
|
|
|
.map(|(_name, arg)| arg)
|
|
|
|
.next();
|
2018-12-11 19:07:17 +01:00
|
|
|
let mut edit = TextEditBuilder::new();
|
2018-08-16 12:11:20 +02:00
|
|
|
let offset = match derive_attr {
|
|
|
|
None => {
|
|
|
|
edit.insert(node_start, "#[derive()]\n".to_string());
|
|
|
|
node_start + TextUnit::of_str("#[derive(")
|
|
|
|
}
|
2018-10-15 23:44:23 +02:00
|
|
|
Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'),
|
2018-08-16 12:11:20 +02:00
|
|
|
};
|
2018-08-29 17:03:14 +02:00
|
|
|
LocalEdit {
|
2018-08-15 22:24:20 +02:00
|
|
|
edit: edit.finish(),
|
2018-08-22 11:58:34 +02:00
|
|
|
cursor_position: Some(offset),
|
2018-08-15 22:24:20 +02:00
|
|
|
}
|
2018-12-02 10:19:22 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Insert `derive` after doc comments.
|
|
|
|
fn derive_insertion_offset(nominal: ast::NominalDef) -> Option<TextUnit> {
|
|
|
|
let non_ws_child = nominal
|
|
|
|
.syntax()
|
|
|
|
.children()
|
|
|
|
.find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?;
|
|
|
|
Some(non_ws_child.range().start())
|
|
|
|
}
|
2018-08-14 12:33:44 +02:00
|
|
|
}
|
|
|
|
|
2018-11-07 16:32:33 +01:00
|
|
|
pub fn add_impl<'a>(
|
|
|
|
file: &'a SourceFileNode,
|
|
|
|
offset: TextUnit,
|
|
|
|
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-08-26 08:12:18 +02:00
|
|
|
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
|
2018-08-22 17:05:43 +02:00
|
|
|
let name = nominal.name()?;
|
|
|
|
|
|
|
|
Some(move || {
|
2018-08-22 18:02:37 +02:00
|
|
|
let type_params = nominal.type_param_list();
|
2018-12-11 19:07:17 +01:00
|
|
|
let mut edit = TextEditBuilder::new();
|
2018-08-22 17:05:43 +02:00
|
|
|
let start_offset = nominal.syntax().range().end();
|
2018-08-22 18:02:37 +02:00
|
|
|
let mut buf = String::new();
|
|
|
|
buf.push_str("\n\nimpl");
|
|
|
|
if let Some(type_params) = type_params {
|
2018-10-15 23:44:23 +02:00
|
|
|
type_params.syntax().text().push_to(&mut buf);
|
2018-08-22 18:02:37 +02:00
|
|
|
}
|
|
|
|
buf.push_str(" ");
|
|
|
|
buf.push_str(name.text().as_str());
|
|
|
|
if let Some(type_params) = type_params {
|
2018-10-15 23:44:23 +02:00
|
|
|
let lifetime_params = type_params
|
|
|
|
.lifetime_params()
|
|
|
|
.filter_map(|it| it.lifetime())
|
|
|
|
.map(|it| it.text());
|
|
|
|
let type_params = type_params
|
|
|
|
.type_params()
|
|
|
|
.filter_map(|it| it.name())
|
|
|
|
.map(|it| it.text());
|
2018-08-28 22:59:57 +02:00
|
|
|
join(lifetime_params.chain(type_params))
|
2018-08-28 21:58:02 +02:00
|
|
|
.surround_with("<", ">")
|
|
|
|
.to_buf(&mut buf);
|
2018-08-22 18:02:37 +02:00
|
|
|
}
|
|
|
|
buf.push_str(" {\n");
|
|
|
|
let offset = start_offset + TextUnit::of_str(&buf);
|
|
|
|
buf.push_str("\n}");
|
|
|
|
edit.insert(start_offset, buf);
|
2018-08-29 17:03:14 +02:00
|
|
|
LocalEdit {
|
2018-08-22 17:05:43 +02:00
|
|
|
edit: edit.finish(),
|
2018-08-22 18:02:37 +02:00
|
|
|
cursor_position: Some(offset),
|
2018-08-22 17:05:43 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-15 23:44:23 +02:00
|
|
|
pub fn introduce_variable<'a>(
|
2018-11-07 16:32:33 +01:00
|
|
|
file: &'a SourceFileNode,
|
2018-10-15 23:44:23 +02:00
|
|
|
range: TextRange,
|
|
|
|
) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-09-05 23:59:07 +02:00
|
|
|
let node = find_covering_node(file.syntax(), range);
|
2018-10-02 17:02:57 +02:00
|
|
|
let expr = node.ancestors().filter_map(ast::Expr::cast).next()?;
|
2018-11-05 13:44:34 +01:00
|
|
|
|
2018-12-02 14:00:46 +01:00
|
|
|
let anchor_stmt = anchor_stmt(expr)?;
|
2018-11-05 13:44:34 +01:00
|
|
|
let indent = anchor_stmt.prev_sibling()?;
|
2018-09-05 23:59:07 +02:00
|
|
|
if indent.kind() != WHITESPACE {
|
|
|
|
return None;
|
|
|
|
}
|
2018-11-05 13:44:34 +01:00
|
|
|
return Some(move || {
|
2018-09-05 23:59:07 +02:00
|
|
|
let mut buf = String::new();
|
2018-12-11 19:07:17 +01:00
|
|
|
let mut edit = TextEditBuilder::new();
|
2018-09-06 00:19:24 +02:00
|
|
|
|
2018-09-05 23:59:07 +02:00
|
|
|
buf.push_str("let var_name = ");
|
|
|
|
expr.syntax().text().push_to(&mut buf);
|
2018-12-02 14:00:46 +01:00
|
|
|
let is_full_stmt = if let Some(expr_stmt) = ast::ExprStmt::cast(anchor_stmt) {
|
|
|
|
Some(expr.syntax()) == expr_stmt.expr().map(|e| e.syntax())
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
|
|
|
if is_full_stmt {
|
2018-09-06 00:19:24 +02:00
|
|
|
edit.replace(expr.syntax().range(), buf);
|
|
|
|
} else {
|
|
|
|
buf.push_str(";");
|
|
|
|
indent.text().push_to(&mut buf);
|
|
|
|
edit.replace(expr.syntax().range(), "var_name".to_string());
|
2018-11-05 13:44:34 +01:00
|
|
|
edit.insert(anchor_stmt.range().start(), buf);
|
2018-09-06 00:19:24 +02:00
|
|
|
}
|
2018-11-05 13:44:34 +01:00
|
|
|
let cursor_position = anchor_stmt.range().start() + TextUnit::of_str("let ");
|
2018-09-05 23:59:07 +02:00
|
|
|
LocalEdit {
|
|
|
|
edit: edit.finish(),
|
2018-09-06 00:19:24 +02:00
|
|
|
cursor_position: Some(cursor_position),
|
2018-09-05 23:59:07 +02:00
|
|
|
}
|
2018-11-05 13:44:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/// Statement or last in the block expression, which will follow
|
|
|
|
/// the freshly introduced var.
|
2018-12-02 14:00:46 +01:00
|
|
|
fn anchor_stmt(expr: ast::Expr) -> Option<SyntaxNodeRef> {
|
2018-11-05 13:44:34 +01:00
|
|
|
expr.syntax().ancestors().find(|&node| {
|
|
|
|
if ast::Stmt::cast(node).is_some() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if let Some(expr) = node
|
|
|
|
.parent()
|
|
|
|
.and_then(ast::Block::cast)
|
|
|
|
.and_then(|it| it.expr())
|
|
|
|
{
|
|
|
|
if expr.syntax() == node {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
})
|
|
|
|
}
|
2018-09-05 23:59:07 +02:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:50:16 +02:00
|
|
|
fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
|
2018-10-02 17:14:33 +02:00
|
|
|
node.siblings(direction)
|
2018-08-12 17:50:16 +02:00
|
|
|
.skip(1)
|
|
|
|
.find(|node| !node.kind().is_trivia())
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:47:12 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-10-15 19:05:26 +02:00
|
|
|
use crate::test_utils::{check_action, check_action_range};
|
2018-08-28 13:47:12 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap_comma() {
|
|
|
|
check_action(
|
|
|
|
"fn foo(x: i32,<|> y: Result<(), ()>) {}",
|
|
|
|
"fn foo(y: Result<(), ()>,<|> x: i32) {}",
|
|
|
|
|file, off| flip_comma(file, off).map(|f| f()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2018-12-02 10:19:22 +01:00
|
|
|
fn add_derive_new() {
|
2018-08-28 13:47:12 +02:00
|
|
|
check_action(
|
|
|
|
"struct Foo { a: i32, <|>}",
|
|
|
|
"#[derive(<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
|
|
|
check_action(
|
|
|
|
"struct Foo { <|> a: i32, }",
|
|
|
|
"#[derive(<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
2018-12-02 10:19:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_derive_existing() {
|
2018-08-28 13:47:12 +02:00
|
|
|
check_action(
|
|
|
|
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
|
|
|
|
"#[derive(Clone<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-02 10:19:22 +01:00
|
|
|
#[test]
|
|
|
|
fn add_derive_new_with_doc_comment() {
|
|
|
|
check_action(
|
|
|
|
"
|
|
|
|
/// `Foo` is a pretty important struct.
|
|
|
|
/// It does stuff.
|
|
|
|
struct Foo { a: i32<|>, }
|
|
|
|
",
|
|
|
|
"
|
|
|
|
/// `Foo` is a pretty important struct.
|
|
|
|
/// It does stuff.
|
|
|
|
#[derive(<|>)]
|
|
|
|
struct Foo { a: i32, }
|
|
|
|
",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:47:12 +02:00
|
|
|
#[test]
|
|
|
|
fn test_add_impl() {
|
|
|
|
check_action(
|
|
|
|
"struct Foo {<|>}\n",
|
|
|
|
"struct Foo {}\n\nimpl Foo {\n<|>\n}\n",
|
|
|
|
|file, off| add_impl(file, off).map(|f| f()),
|
|
|
|
);
|
|
|
|
check_action(
|
|
|
|
"struct Foo<T: Clone> {<|>}",
|
|
|
|
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}",
|
|
|
|
|file, off| add_impl(file, off).map(|f| f()),
|
|
|
|
);
|
2018-08-28 22:59:57 +02:00
|
|
|
check_action(
|
|
|
|
"struct Foo<'a, T: Foo<'a>> {<|>}",
|
|
|
|
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}",
|
|
|
|
|file, off| add_impl(file, off).map(|f| f()),
|
|
|
|
);
|
2018-08-28 13:47:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-05 23:59:07 +02:00
|
|
|
#[test]
|
2018-12-02 14:00:46 +01:00
|
|
|
fn test_introduce_var_simple() {
|
2018-09-05 23:59:07 +02:00
|
|
|
check_action_range(
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
foo(<|>1 + 1<|>);
|
2018-10-15 23:44:23 +02:00
|
|
|
}",
|
|
|
|
"
|
2018-09-05 23:59:07 +02:00
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = 1 + 1;
|
|
|
|
foo(var_name);
|
2018-09-06 00:19:24 +02:00
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
2018-11-05 13:44:34 +01:00
|
|
|
|
2018-09-06 00:19:24 +02:00
|
|
|
#[test]
|
2018-12-02 14:00:46 +01:00
|
|
|
fn test_introduce_var_expr_stmt() {
|
2018-10-15 23:44:23 +02:00
|
|
|
check_action_range(
|
2018-09-06 00:19:24 +02:00
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
<|>1 + 1<|>;
|
2018-10-15 23:44:23 +02:00
|
|
|
}",
|
|
|
|
"
|
2018-09-06 00:19:24 +02:00
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = 1 + 1;
|
2018-09-05 23:59:07 +02:00
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-11-05 13:44:34 +01:00
|
|
|
#[test]
|
2018-12-02 14:00:46 +01:00
|
|
|
fn test_introduce_var_part_of_expr_stmt() {
|
|
|
|
check_action_range(
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
<|>1<|> + 1;
|
|
|
|
}",
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = 1;
|
|
|
|
var_name + 1;
|
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_introduce_var_last_expr() {
|
2018-11-05 13:44:34 +01:00
|
|
|
check_action_range(
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
bar(<|>1 + 1<|>)
|
|
|
|
}",
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = 1 + 1;
|
|
|
|
bar(var_name)
|
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-02 14:00:46 +01:00
|
|
|
#[test]
|
|
|
|
fn test_introduce_var_last_full_expr() {
|
|
|
|
check_action_range(
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(1 + 1)<|>
|
|
|
|
}",
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = bar(1 + 1);
|
|
|
|
var_name
|
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:47:12 +02:00
|
|
|
}
|