2021-04-13 13:32:45 -05:00
|
|
|
use std::{iter::once, mem};
|
2021-03-16 07:37:00 -05:00
|
|
|
|
|
|
|
use hir::Semantics;
|
2021-06-22 10:28:07 -05:00
|
|
|
use ide_db::{base_db::FileRange, helpers::pick_best_token, RootDatabase};
|
2021-03-19 03:28:09 -05:00
|
|
|
use itertools::Itertools;
|
2021-06-22 10:28:07 -05:00
|
|
|
use syntax::{algo, ast, match_ast, AstNode, SyntaxElement, SyntaxKind, SyntaxNode, TextRange};
|
2021-03-16 07:37:00 -05:00
|
|
|
use text_edit::{TextEdit, TextEditBuilder};
|
|
|
|
|
2021-03-30 05:44:09 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2021-03-16 07:37:00 -05:00
|
|
|
pub enum Direction {
|
|
|
|
Up,
|
|
|
|
Down,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Feature: Move Item
|
|
|
|
//
|
|
|
|
// Move item under cursor or selection up and down.
|
|
|
|
//
|
|
|
|
// |===
|
|
|
|
// | Editor | Action Name
|
|
|
|
//
|
2022-08-01 06:47:09 -05:00
|
|
|
// | VS Code | **rust-analyzer: Move item up**
|
|
|
|
// | VS Code | **rust-analyzer: Move item down**
|
2021-03-16 07:37:00 -05:00
|
|
|
// |===
|
2021-03-30 18:08:10 -05:00
|
|
|
//
|
|
|
|
// image::https://user-images.githubusercontent.com/48062697/113065576-04298180-91b1-11eb-91ce-4505e99ed598.gif[]
|
2021-03-16 07:37:00 -05:00
|
|
|
pub(crate) fn move_item(
|
|
|
|
db: &RootDatabase,
|
|
|
|
range: FileRange,
|
|
|
|
direction: Direction,
|
|
|
|
) -> Option<TextEdit> {
|
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let file = sema.parse(range.file_id);
|
|
|
|
|
2021-03-30 05:44:09 -05:00
|
|
|
let item = if range.range.is_empty() {
|
2021-06-22 10:28:07 -05:00
|
|
|
SyntaxElement::Token(pick_best_token(
|
|
|
|
file.syntax().token_at_offset(range.range.start()),
|
|
|
|
|kind| match kind {
|
|
|
|
SyntaxKind::IDENT | SyntaxKind::LIFETIME_IDENT => 2,
|
|
|
|
kind if kind.is_trivia() => 0,
|
|
|
|
_ => 1,
|
|
|
|
},
|
|
|
|
)?)
|
2021-03-30 05:44:09 -05:00
|
|
|
} else {
|
|
|
|
file.syntax().covering_element(range.range)
|
|
|
|
};
|
|
|
|
|
2021-03-19 03:28:09 -05:00
|
|
|
find_ancestors(item, direction, range.range)
|
2021-03-16 07:37:00 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 03:28:09 -05:00
|
|
|
fn find_ancestors(item: SyntaxElement, direction: Direction, range: TextRange) -> Option<TextEdit> {
|
2021-03-16 07:37:00 -05:00
|
|
|
let root = match item {
|
2021-03-30 05:44:09 -05:00
|
|
|
SyntaxElement::Node(node) => node,
|
|
|
|
SyntaxElement::Token(token) => token.parent()?,
|
2021-03-16 07:37:00 -05:00
|
|
|
};
|
|
|
|
|
2021-03-17 10:38:40 -05:00
|
|
|
let movable = [
|
2021-03-19 03:28:09 -05:00
|
|
|
SyntaxKind::ARG_LIST,
|
2021-03-19 09:43:47 -05:00
|
|
|
SyntaxKind::GENERIC_PARAM_LIST,
|
|
|
|
SyntaxKind::GENERIC_ARG_LIST,
|
|
|
|
SyntaxKind::VARIANT_LIST,
|
|
|
|
SyntaxKind::TYPE_BOUND_LIST,
|
2021-03-17 10:38:40 -05:00
|
|
|
SyntaxKind::MATCH_ARM,
|
|
|
|
SyntaxKind::PARAM,
|
|
|
|
SyntaxKind::LET_STMT,
|
|
|
|
SyntaxKind::EXPR_STMT,
|
2021-03-30 05:44:09 -05:00
|
|
|
SyntaxKind::IF_EXPR,
|
|
|
|
SyntaxKind::FOR_EXPR,
|
|
|
|
SyntaxKind::LOOP_EXPR,
|
|
|
|
SyntaxKind::WHILE_EXPR,
|
|
|
|
SyntaxKind::RETURN_EXPR,
|
2021-03-17 10:38:40 -05:00
|
|
|
SyntaxKind::MATCH_EXPR,
|
|
|
|
SyntaxKind::MACRO_CALL,
|
|
|
|
SyntaxKind::TYPE_ALIAS,
|
|
|
|
SyntaxKind::TRAIT,
|
|
|
|
SyntaxKind::IMPL,
|
|
|
|
SyntaxKind::MACRO_DEF,
|
|
|
|
SyntaxKind::STRUCT,
|
|
|
|
SyntaxKind::UNION,
|
|
|
|
SyntaxKind::ENUM,
|
|
|
|
SyntaxKind::FN,
|
|
|
|
SyntaxKind::MODULE,
|
|
|
|
SyntaxKind::USE,
|
|
|
|
SyntaxKind::STATIC,
|
|
|
|
SyntaxKind::CONST,
|
|
|
|
SyntaxKind::MACRO_RULES,
|
2021-03-27 00:48:15 -05:00
|
|
|
SyntaxKind::MACRO_DEF,
|
2021-03-17 10:38:40 -05:00
|
|
|
];
|
|
|
|
|
2021-03-16 07:37:00 -05:00
|
|
|
let ancestor = once(root.clone())
|
|
|
|
.chain(root.ancestors())
|
2021-03-17 10:38:40 -05:00
|
|
|
.find(|ancestor| movable.contains(&ancestor.kind()))?;
|
2021-03-16 07:37:00 -05:00
|
|
|
|
2021-03-19 03:28:09 -05:00
|
|
|
move_in_direction(&ancestor, direction, range)
|
2021-03-16 07:37:00 -05:00
|
|
|
}
|
|
|
|
|
2021-03-19 03:28:09 -05:00
|
|
|
fn move_in_direction(
|
|
|
|
node: &SyntaxNode,
|
|
|
|
direction: Direction,
|
|
|
|
range: TextRange,
|
|
|
|
) -> Option<TextEdit> {
|
|
|
|
match_ast! {
|
|
|
|
match node {
|
2021-03-30 05:44:09 -05:00
|
|
|
ast::ArgList(it) => swap_sibling_in_list(node, it.args(), range, direction),
|
|
|
|
ast::GenericParamList(it) => swap_sibling_in_list(node, it.generic_params(), range, direction),
|
|
|
|
ast::GenericArgList(it) => swap_sibling_in_list(node, it.generic_args(), range, direction),
|
|
|
|
ast::VariantList(it) => swap_sibling_in_list(node, it.variants(), range, direction),
|
|
|
|
ast::TypeBoundList(it) => swap_sibling_in_list(node, it.bounds(), range, direction),
|
2021-04-13 13:32:45 -05:00
|
|
|
_ => Some(replace_nodes(range, node, &match direction {
|
2021-03-19 03:28:09 -05:00
|
|
|
Direction::Up => node.prev_sibling(),
|
|
|
|
Direction::Down => node.next_sibling(),
|
|
|
|
}?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
|
2021-03-19 10:01:39 -05:00
|
|
|
fn swap_sibling_in_list<A: AstNode + Clone, I: Iterator<Item = A>>(
|
2021-03-30 05:44:09 -05:00
|
|
|
node: &SyntaxNode,
|
2021-03-19 03:28:09 -05:00
|
|
|
list: I,
|
|
|
|
range: TextRange,
|
|
|
|
direction: Direction,
|
|
|
|
) -> Option<TextEdit> {
|
2021-10-16 06:32:55 -05:00
|
|
|
let list_lookup = list.tuple_windows().find(|(l, r)| match direction {
|
|
|
|
Direction::Up => r.syntax().text_range().contains_range(range),
|
|
|
|
Direction::Down => l.syntax().text_range().contains_range(range),
|
|
|
|
});
|
2021-03-30 05:44:09 -05:00
|
|
|
|
|
|
|
if let Some((l, r)) = list_lookup {
|
2021-04-13 13:32:45 -05:00
|
|
|
Some(replace_nodes(range, l.syntax(), r.syntax()))
|
2021-03-30 05:44:09 -05:00
|
|
|
} else {
|
|
|
|
// Cursor is beyond any movable list item (for example, on curly brace in enum).
|
|
|
|
// It's not necessary, that parent of list is movable (arg list's parent is not, for example),
|
|
|
|
// and we have to continue tree traversal to find suitable node.
|
|
|
|
find_ancestors(SyntaxElement::Node(node.parent()?), direction, range)
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
}
|
|
|
|
|
2021-04-13 13:32:45 -05:00
|
|
|
fn replace_nodes<'a>(
|
|
|
|
range: TextRange,
|
|
|
|
mut first: &'a SyntaxNode,
|
|
|
|
mut second: &'a SyntaxNode,
|
|
|
|
) -> TextEdit {
|
|
|
|
let cursor_offset = if range.is_empty() {
|
|
|
|
// FIXME: `applySnippetTextEdits` does not support non-empty selection ranges
|
|
|
|
if first.text_range().contains_range(range) {
|
|
|
|
Some(range.start() - first.text_range().start())
|
|
|
|
} else if second.text_range().contains_range(range) {
|
|
|
|
mem::swap(&mut first, &mut second);
|
|
|
|
Some(range.start() - first.text_range().start())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let first_with_cursor = match cursor_offset {
|
|
|
|
Some(offset) => {
|
|
|
|
let mut item_text = first.text().to_string();
|
|
|
|
item_text.insert_str(offset.into(), "$0");
|
|
|
|
item_text
|
|
|
|
}
|
|
|
|
None => first.text().to_string(),
|
|
|
|
};
|
|
|
|
|
2021-03-16 07:37:00 -05:00
|
|
|
let mut edit = TextEditBuilder::default();
|
|
|
|
|
|
|
|
algo::diff(first, second).into_text_edit(&mut edit);
|
2021-04-13 13:32:45 -05:00
|
|
|
edit.replace(second.text_range(), first_with_cursor);
|
2021-03-16 07:37:00 -05:00
|
|
|
|
|
|
|
edit.finish()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::fixture;
|
|
|
|
use expect_test::{expect, Expect};
|
|
|
|
|
|
|
|
use crate::Direction;
|
|
|
|
|
|
|
|
fn check(ra_fixture: &str, expect: Expect, direction: Direction) {
|
|
|
|
let (analysis, range) = fixture::range(ra_fixture);
|
|
|
|
let edit = analysis.move_item(range, direction).unwrap().unwrap_or_default();
|
|
|
|
let mut file = analysis.file_text(range.file_id).unwrap().to_string();
|
|
|
|
edit.apply(&mut file);
|
|
|
|
expect.assert_eq(&file);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_match_arm_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match true {
|
|
|
|
true => {
|
|
|
|
println!("Hello, world");
|
|
|
|
},
|
|
|
|
false =>$0$0 {
|
|
|
|
println!("Test");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-29 15:32:24 -05:00
|
|
|
fn main() {
|
|
|
|
match true {
|
|
|
|
false =>$0 {
|
|
|
|
println!("Test");
|
|
|
|
}
|
|
|
|
true => {
|
|
|
|
println!("Hello, world");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_match_arm_down() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match true {
|
|
|
|
true =>$0$0 {
|
|
|
|
println!("Hello, world");
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
println!("Test");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-29 15:32:24 -05:00
|
|
|
fn main() {
|
|
|
|
match true {
|
|
|
|
false => {
|
|
|
|
println!("Test");
|
|
|
|
}
|
|
|
|
true =>$0 {
|
|
|
|
println!("Hello, world");
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Down,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nowhere_to_move() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
match true {
|
|
|
|
true =>$0$0 {
|
|
|
|
println!("Hello, world");
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
println!("Test");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
match true {
|
|
|
|
true => {
|
|
|
|
println!("Hello, world");
|
|
|
|
},
|
|
|
|
false => {
|
|
|
|
println!("Test");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_let_stmt_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let test = 123;
|
|
|
|
let test2$0$0 = 456;
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
let test2$0 = 456;
|
|
|
|
let test = 123;
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_expr_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world");
|
|
|
|
println!("All I want to say is...");$0$0
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
println!("All I want to say is...");$0
|
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world");
|
|
|
|
|
|
|
|
if true {
|
|
|
|
println!("Test");
|
|
|
|
}$0$0
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
if true {
|
|
|
|
println!("Test");
|
|
|
|
}$0
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world");
|
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
println!("Test");
|
|
|
|
}$0$0
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
for i in 0..10 {
|
|
|
|
println!("Test");
|
|
|
|
}$0
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world");
|
|
|
|
|
|
|
|
loop {
|
|
|
|
println!("Test");
|
|
|
|
}$0$0
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
loop {
|
|
|
|
println!("Test");
|
|
|
|
}$0
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world");
|
|
|
|
|
|
|
|
while true {
|
|
|
|
println!("Test");
|
|
|
|
}$0$0
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
while true {
|
|
|
|
println!("Test");
|
|
|
|
}$0
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("Hello, world");
|
|
|
|
|
|
|
|
return 123;$0$0
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
return 123;$0
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nowhere_to_move_stmt() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
println!("All I want to say is...");$0$0
|
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
println!("All I want to say is...");
|
|
|
|
println!("Hello, world");
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_move_item() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
fn foo() {}$0$0
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn foo() {}$0
|
2021-03-16 07:37:00 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_move_impl_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Yay;
|
|
|
|
|
|
|
|
trait Wow {}
|
|
|
|
|
2021-03-19 09:43:47 -05:00
|
|
|
impl Wow for Yay $0$0{}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
struct Yay;
|
2021-03-16 07:37:00 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
impl Wow for Yay $0{}
|
2021-03-16 07:37:00 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
trait Wow {}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_move_use_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
use std::vec::Vec;
|
|
|
|
use std::collections::HashMap$0$0;
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
use std::collections::HashMap$0;
|
|
|
|
use std::vec::Vec;
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-03-19 03:28:09 -05:00
|
|
|
fn test_moves_match_expr_up() {
|
2021-03-16 07:37:00 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
let test = 123;
|
|
|
|
|
|
|
|
$0match test {
|
|
|
|
456 => {},
|
|
|
|
_ => {}
|
2021-03-17 10:38:40 -05:00
|
|
|
};$0
|
2021-03-16 07:37:00 -05:00
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-16 07:37:00 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
match test {
|
|
|
|
456 => {},
|
|
|
|
_ => {}
|
|
|
|
};
|
2021-03-16 07:37:00 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
let test = 123;
|
|
|
|
}
|
2021-03-16 07:37:00 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:38:40 -05:00
|
|
|
#[test]
|
2021-04-13 13:32:45 -05:00
|
|
|
fn test_moves_param() {
|
2021-03-17 10:38:40 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn test(one: i32, two$0$0: u32) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
test(123, 456);
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-17 10:38:40 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn test(two$0: u32, one: i32) {}
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
test(123, 456);
|
|
|
|
}
|
2021-03-19 03:28:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
2021-04-13 13:32:45 -05:00
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn f($0$0arg: u8, arg2: u16) {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-04-13 13:32:45 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn f(arg2: u16, $0arg: u8) {}
|
2021-04-13 13:32:45 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Down,
|
|
|
|
);
|
2021-03-19 03:28:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_arg_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn test(one: i32, two: u32) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
test(123, 456$0$0);
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 03:28:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn test(one: i32, two: u32) {}
|
2021-03-19 03:28:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
test(456$0, 123);
|
|
|
|
}
|
2021-03-19 03:28:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_arg_down() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn test(one: i32, two: u32) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
test(123$0$0, 456);
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 03:28:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn test(one: i32, two: u32) {}
|
2021-03-19 03:28:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
test(456, 123$0);
|
|
|
|
}
|
2021-03-19 03:28:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Down,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nowhere_to_move_arg() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn test(one: i32, two: u32) {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
test(123$0$0, 456);
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 03:28:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn test(one: i32, two: u32) {}
|
2021-03-19 03:28:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
test(123, 456);
|
|
|
|
}
|
2021-03-17 10:38:40 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:43:47 -05:00
|
|
|
#[test]
|
|
|
|
fn test_moves_generic_param_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Test<A, B$0$0>(A, B);
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 09:43:47 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
struct Test<B$0, A>(A, B);
|
2021-03-19 09:43:47 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-19 09:43:47 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_generic_arg_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Test<A, B>(A, B);
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let t = Test::<i32, &str$0$0>(123, "yay");
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 09:43:47 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
struct Test<A, B>(A, B);
|
2021-03-19 09:43:47 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {
|
|
|
|
let t = Test::<&str$0, i32>(123, "yay");
|
|
|
|
}
|
2021-03-19 09:43:47 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_variant_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum Hello {
|
|
|
|
One,
|
|
|
|
Two$0$0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 09:43:47 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
enum Hello {
|
|
|
|
Two$0,
|
|
|
|
One
|
|
|
|
}
|
2021-03-19 09:43:47 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-19 09:43:47 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_moves_type_bound_up() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
trait One {}
|
|
|
|
|
|
|
|
trait Two {}
|
|
|
|
|
|
|
|
fn test<T: One + Two$0$0>(t: T) {}
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-19 09:43:47 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
trait One {}
|
2021-03-19 09:43:47 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
trait Two {}
|
2021-03-19 09:43:47 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn test<T: Two$0 + One>(t: T) {}
|
2021-03-19 09:43:47 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-19 09:43:47 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-17 10:38:40 -05:00
|
|
|
#[test]
|
|
|
|
fn test_prioritizes_trait_items() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Test;
|
|
|
|
|
|
|
|
trait Yay {
|
|
|
|
type One;
|
|
|
|
|
|
|
|
type Two;
|
|
|
|
|
|
|
|
fn inner();
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Yay for Test {
|
|
|
|
type One = i32;
|
|
|
|
|
|
|
|
type Two = u32;
|
|
|
|
|
|
|
|
fn inner() {$0$0
|
|
|
|
println!("Mmmm");
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-17 10:38:40 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
struct Test;
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
trait Yay {
|
|
|
|
type One;
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
type Two;
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn inner();
|
|
|
|
}
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
impl Yay for Test {
|
|
|
|
type One = i32;
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn inner() {$0
|
|
|
|
println!("Mmmm");
|
|
|
|
}
|
2021-03-17 10:38:40 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
type Two = u32;
|
|
|
|
}
|
2021-03-17 10:38:40 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_weird_nesting() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
fn test() {
|
|
|
|
mod hello {
|
|
|
|
fn inner() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod hi {$0$0
|
|
|
|
fn inner() {}
|
|
|
|
}
|
|
|
|
}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-17 10:38:40 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn test() {
|
|
|
|
mod hi {$0
|
|
|
|
fn inner() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
mod hello {
|
|
|
|
fn inner() {}
|
|
|
|
}
|
|
|
|
}
|
2021-03-17 10:38:40 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-30 05:44:09 -05:00
|
|
|
#[test]
|
|
|
|
fn test_cursor_at_item_start() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
$0$0#[derive(Debug)]
|
|
|
|
enum FooBar {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
|
|
|
expect![[r##"
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
$0#[derive(Debug)]
|
|
|
|
enum FooBar {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
"##]],
|
2021-03-30 05:44:09 -05:00
|
|
|
Direction::Down,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
$0$0enum FooBar {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
$0enum FooBar {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Down,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Test;
|
|
|
|
|
|
|
|
trait SomeTrait {}
|
|
|
|
|
|
|
|
$0$0impl SomeTrait for Test {}
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
struct Test;
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
$0impl SomeTrait for Test {}
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
trait SomeTrait {}
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cursor_at_item_end() {
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
enum FooBar {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}$0$0
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
enum FooBar {
|
|
|
|
Foo,
|
|
|
|
Bar,
|
|
|
|
}$0
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Down,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r#"
|
|
|
|
struct Test;
|
|
|
|
|
|
|
|
trait SomeTrait {}
|
|
|
|
|
|
|
|
impl SomeTrait for Test {}$0$0
|
|
|
|
|
|
|
|
fn main() {}
|
2021-07-30 06:03:28 -05:00
|
|
|
"#,
|
2021-03-30 05:44:09 -05:00
|
|
|
expect![[r#"
|
2021-07-30 06:03:28 -05:00
|
|
|
struct Test;
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
impl SomeTrait for Test {}$0
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
trait SomeTrait {}
|
2021-03-30 05:44:09 -05:00
|
|
|
|
2021-07-30 06:03:28 -05:00
|
|
|
fn main() {}
|
2021-03-30 05:44:09 -05:00
|
|
|
"#]],
|
|
|
|
Direction::Up,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-16 07:37:00 -05:00
|
|
|
#[test]
|
|
|
|
fn handles_empty_file() {
|
|
|
|
check(r#"$0$0"#, expect![[r#""#]], Direction::Up);
|
|
|
|
}
|
|
|
|
}
|