aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_editor/src/assists/flip_comma.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-03 16:08:52 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-03 16:08:52 +0000
commit33924c0939bb64cdb6fc4644582411a7be1f135d (patch)
treeea555719a2ec8574f5399de896b9c4e2586c7270 /crates/ra_editor/src/assists/flip_comma.rs
parent5443205fdd9f4886cc88ad15c3a6061ffa90ca19 (diff)
parent6be39ba758eade2a20ab6384127a2b1ca48ca9ce (diff)
Merge #416
416: assist-builder r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_editor/src/assists/flip_comma.rs')
-rw-r--r--crates/ra_editor/src/assists/flip_comma.rs36
1 files changed, 11 insertions, 25 deletions
diff --git a/crates/ra_editor/src/assists/flip_comma.rs b/crates/ra_editor/src/assists/flip_comma.rs
index d8727db0d..a343413cc 100644
--- a/crates/ra_editor/src/assists/flip_comma.rs
+++ b/crates/ra_editor/src/assists/flip_comma.rs
@@ -1,45 +1,31 @@
1use ra_text_edit::TextEditBuilder;
2use ra_syntax::{ 1use ra_syntax::{
3 algo::find_leaf_at_offset, 2 Direction,
4 Direction, SourceFileNode,
5 SyntaxKind::COMMA, 3 SyntaxKind::COMMA,
6 TextUnit,
7}; 4};
8 5
9use crate::assists::{LocalEdit, non_trivia_sibling}; 6use crate::assists::{non_trivia_sibling, AssistCtx, Assist};
10 7
11pub fn flip_comma<'a>( 8pub fn flip_comma(ctx: AssistCtx) -> Option<Assist> {
12 file: &'a SourceFileNode, 9 let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?;
13 offset: TextUnit,
14) -> Option<impl FnOnce() -> LocalEdit + 'a> {
15 let syntax = file.syntax();
16
17 let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?;
18 let prev = non_trivia_sibling(comma, Direction::Prev)?; 10 let prev = non_trivia_sibling(comma, Direction::Prev)?;
19 let next = non_trivia_sibling(comma, Direction::Next)?; 11 let next = non_trivia_sibling(comma, Direction::Next)?;
20 Some(move || { 12 ctx.build("flip comma", |edit| {
21 let mut edit = TextEditBuilder::new(); 13 edit.replace(prev.range(), next.text());
22 edit.replace(prev.range(), next.text().to_string()); 14 edit.replace(next.range(), prev.text());
23 edit.replace(next.range(), prev.text().to_string());
24 LocalEdit {
25 label: "flip comma".to_string(),
26 edit: edit.finish(),
27 cursor_position: None,
28 }
29 }) 15 })
30} 16}
31 17
32#[cfg(test)] 18#[cfg(test)]
33mod tests { 19mod tests {
34 use super::*; 20 use super::*;
35 use crate::test_utils::check_action; 21 use crate::assists::check_assist;
36 22
37 #[test] 23 #[test]
38 fn test_swap_comma() { 24 fn flip_comma_works_for_function_parameters() {
39 check_action( 25 check_assist(
26 flip_comma,
40 "fn foo(x: i32,<|> y: Result<(), ()>) {}", 27 "fn foo(x: i32,<|> y: Result<(), ()>) {}",
41 "fn foo(y: Result<(), ()>,<|> x: i32) {}", 28 "fn foo(y: Result<(), ()>,<|> x: i32) {}",
42 |file, off| flip_comma(file, off).map(|f| f()),
43 ) 29 )
44 } 30 }
45} 31}