2018-08-28 22:37:49 +03:00
|
|
|
use join_to_string::join;
|
|
|
|
|
2018-08-12 18:50:16 +03:00
|
|
|
use libsyntax2::{
|
2018-09-06 00:59:07 +03:00
|
|
|
File, TextUnit, TextRange,
|
2018-08-25 11:40:17 +03:00
|
|
|
ast::{self, AstNode, AttrsOwner, TypeParamsOwner, NameOwner},
|
2018-09-06 00:59:07 +03:00
|
|
|
SyntaxKind::{COMMA, WHITESPACE},
|
2018-08-17 22:00:13 +03:00
|
|
|
SyntaxNodeRef,
|
2018-08-12 18:50:16 +03:00
|
|
|
algo::{
|
|
|
|
Direction, siblings,
|
2018-08-26 12:09:28 +03:00
|
|
|
find_leaf_at_offset,
|
2018-09-06 00:59:07 +03:00
|
|
|
find_covering_node,
|
|
|
|
ancestors,
|
2018-08-12 18:50:16 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-08-26 09:12:18 +03:00
|
|
|
use {EditBuilder, Edit, find_node_at_offset};
|
2018-08-22 19:02:37 +03:00
|
|
|
|
2018-08-23 20:55:23 +03:00
|
|
|
#[derive(Debug)]
|
2018-08-29 18:03:14 +03:00
|
|
|
pub struct LocalEdit {
|
2018-08-15 23:24:20 +03:00
|
|
|
pub edit: Edit,
|
2018-08-22 12:58:34 +03:00
|
|
|
pub cursor_position: Option<TextUnit>,
|
2018-08-15 23:24:20 +03:00
|
|
|
}
|
|
|
|
|
2018-08-29 18:03:14 +03:00
|
|
|
pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-08-12 18:50:16 +03:00
|
|
|
let syntax = file.syntax();
|
|
|
|
|
|
|
|
let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
|
|
|
|
let left = non_trivia_sibling(comma, Direction::Backward)?;
|
|
|
|
let right = non_trivia_sibling(comma, Direction::Forward)?;
|
|
|
|
Some(move || {
|
|
|
|
let mut edit = EditBuilder::new();
|
2018-08-28 14:06:30 +03:00
|
|
|
edit.replace(left.range(), right.text().to_string());
|
|
|
|
edit.replace(right.range(), left.text().to_string());
|
2018-08-29 18:03:14 +03:00
|
|
|
LocalEdit {
|
2018-08-15 23:24:20 +03:00
|
|
|
edit: edit.finish(),
|
2018-08-22 12:58:34 +03:00
|
|
|
cursor_position: None,
|
2018-08-15 23:24:20 +03:00
|
|
|
}
|
2018-08-12 18:50:16 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:03:14 +03:00
|
|
|
pub fn add_derive<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-08-26 09:12:18 +03:00
|
|
|
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
|
2018-08-14 13:33:44 +03:00
|
|
|
Some(move || {
|
2018-08-16 13:11:20 +03:00
|
|
|
let derive_attr = nominal
|
|
|
|
.attrs()
|
|
|
|
.filter_map(|x| x.as_call())
|
|
|
|
.filter(|(name, _arg)| name == "derive")
|
|
|
|
.map(|(_name, arg)| arg)
|
|
|
|
.next();
|
2018-08-14 13:33:44 +03:00
|
|
|
let mut edit = EditBuilder::new();
|
2018-08-16 13:11:20 +03:00
|
|
|
let offset = match derive_attr {
|
|
|
|
None => {
|
|
|
|
let node_start = nominal.syntax().range().start();
|
|
|
|
edit.insert(node_start, "#[derive()]\n".to_string());
|
|
|
|
node_start + TextUnit::of_str("#[derive(")
|
|
|
|
}
|
|
|
|
Some(tt) => {
|
|
|
|
tt.syntax().range().end() - TextUnit::of_char(')')
|
|
|
|
}
|
|
|
|
};
|
2018-08-29 18:03:14 +03:00
|
|
|
LocalEdit {
|
2018-08-15 23:24:20 +03:00
|
|
|
edit: edit.finish(),
|
2018-08-22 12:58:34 +03:00
|
|
|
cursor_position: Some(offset),
|
2018-08-15 23:24:20 +03:00
|
|
|
}
|
2018-08-14 13:33:44 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-29 18:03:14 +03:00
|
|
|
pub fn add_impl<'a>(file: &'a File, offset: TextUnit) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
2018-08-26 09:12:18 +03:00
|
|
|
let nominal = find_node_at_offset::<ast::NominalDef>(file.syntax(), offset)?;
|
2018-08-22 18:05:43 +03:00
|
|
|
let name = nominal.name()?;
|
|
|
|
|
|
|
|
Some(move || {
|
2018-08-22 19:02:37 +03:00
|
|
|
let type_params = nominal.type_param_list();
|
2018-08-22 18:05:43 +03:00
|
|
|
let mut edit = EditBuilder::new();
|
|
|
|
let start_offset = nominal.syntax().range().end();
|
2018-08-22 19:02:37 +03:00
|
|
|
let mut buf = String::new();
|
|
|
|
buf.push_str("\n\nimpl");
|
|
|
|
if let Some(type_params) = type_params {
|
2018-08-28 22:58:02 +03:00
|
|
|
type_params.syntax().text()
|
|
|
|
.push_to(&mut buf);
|
2018-08-22 19:02:37 +03:00
|
|
|
}
|
|
|
|
buf.push_str(" ");
|
|
|
|
buf.push_str(name.text().as_str());
|
|
|
|
if let Some(type_params) = type_params {
|
2018-08-28 23:59:57 +03: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());
|
|
|
|
join(lifetime_params.chain(type_params))
|
2018-08-28 22:58:02 +03:00
|
|
|
.surround_with("<", ">")
|
|
|
|
.to_buf(&mut buf);
|
2018-08-22 19:02:37 +03: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 18:03:14 +03:00
|
|
|
LocalEdit {
|
2018-08-22 18:05:43 +03:00
|
|
|
edit: edit.finish(),
|
2018-08-22 19:02:37 +03:00
|
|
|
cursor_position: Some(offset),
|
2018-08-22 18:05:43 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-06 00:59:07 +03:00
|
|
|
pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl FnOnce() -> LocalEdit + 'a> {
|
|
|
|
let node = find_covering_node(file.syntax(), range);
|
|
|
|
let expr = ancestors(node).filter_map(ast::Expr::cast).next()?;
|
|
|
|
let anchor_stmt = ancestors(expr.syntax()).filter_map(ast::Stmt::cast).next()?;
|
|
|
|
let indent = anchor_stmt.syntax().prev_sibling()?;
|
|
|
|
if indent.kind() != WHITESPACE {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(move || {
|
|
|
|
let mut buf = String::new();
|
2018-09-06 01:19:24 +03:00
|
|
|
let mut edit = EditBuilder::new();
|
|
|
|
|
2018-09-06 00:59:07 +03:00
|
|
|
buf.push_str("let var_name = ");
|
|
|
|
expr.syntax().text().push_to(&mut buf);
|
2018-09-06 01:19:24 +03:00
|
|
|
if expr.syntax().range().start() == anchor_stmt.syntax().range().start() {
|
|
|
|
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());
|
|
|
|
edit.insert(anchor_stmt.syntax().range().start(), buf);
|
|
|
|
}
|
|
|
|
let cursor_position = anchor_stmt.syntax().range().start() + TextUnit::of_str("let ");
|
2018-09-06 00:59:07 +03:00
|
|
|
LocalEdit {
|
|
|
|
edit: edit.finish(),
|
2018-09-06 01:19:24 +03:00
|
|
|
cursor_position: Some(cursor_position),
|
2018-09-06 00:59:07 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-08-12 18:50:16 +03:00
|
|
|
fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option<SyntaxNodeRef> {
|
|
|
|
siblings(node, direction)
|
|
|
|
.skip(1)
|
|
|
|
.find(|node| !node.kind().is_trivia())
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:47:12 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2018-09-06 00:59:07 +03:00
|
|
|
use test_utils::{check_action, check_action_range};
|
2018-08-28 14:47:12 +03: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]
|
|
|
|
fn test_add_derive() {
|
|
|
|
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()),
|
|
|
|
);
|
|
|
|
check_action(
|
|
|
|
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
|
|
|
|
"#[derive(Clone<|>)]\nstruct Foo { a: i32, }",
|
|
|
|
|file, off| add_derive(file, off).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 23:59:57 +03: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 14:47:12 +03:00
|
|
|
}
|
|
|
|
|
2018-09-06 00:59:07 +03:00
|
|
|
#[test]
|
2018-09-06 01:19:24 +03:00
|
|
|
fn test_intrdoduce_var_simple() {
|
2018-09-06 00:59:07 +03:00
|
|
|
check_action_range(
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
foo(<|>1 + 1<|>);
|
|
|
|
}", "
|
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = 1 + 1;
|
|
|
|
foo(var_name);
|
2018-09-06 01:19:24 +03:00
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn test_intrdoduce_var_expr_stmt() {
|
|
|
|
check_action_range(
|
|
|
|
"
|
|
|
|
fn foo() {
|
|
|
|
<|>1 + 1<|>;
|
|
|
|
}", "
|
|
|
|
fn foo() {
|
|
|
|
let <|>var_name = 1 + 1;
|
2018-09-06 00:59:07 +03:00
|
|
|
}",
|
|
|
|
|file, range| introduce_variable(file, range).map(|f| f()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-28 14:47:12 +03:00
|
|
|
}
|