From a4635a199bc446bd103aa5821e57dc19b8a15751 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 3 Jan 2019 18:59:17 +0300 Subject: more enterprisey assists API --- crates/ra_editor/src/assists/flip_comma.rs | 36 +++++++++--------------------- 1 file changed, 11 insertions(+), 25 deletions(-) (limited to 'crates/ra_editor/src/assists/flip_comma.rs') 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 @@ -use ra_text_edit::TextEditBuilder; use ra_syntax::{ - algo::find_leaf_at_offset, - Direction, SourceFileNode, + Direction, SyntaxKind::COMMA, - TextUnit, }; -use crate::assists::{LocalEdit, non_trivia_sibling}; +use crate::assists::{non_trivia_sibling, AssistCtx, Assist}; -pub fn flip_comma<'a>( - file: &'a SourceFileNode, - offset: TextUnit, -) -> Option LocalEdit + 'a> { - let syntax = file.syntax(); - - let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; +pub fn flip_comma(ctx: AssistCtx) -> Option { + let comma = ctx.leaf_at_offset().find(|leaf| leaf.kind() == COMMA)?; let prev = non_trivia_sibling(comma, Direction::Prev)?; let next = non_trivia_sibling(comma, Direction::Next)?; - Some(move || { - let mut edit = TextEditBuilder::new(); - edit.replace(prev.range(), next.text().to_string()); - edit.replace(next.range(), prev.text().to_string()); - LocalEdit { - label: "flip comma".to_string(), - edit: edit.finish(), - cursor_position: None, - } + ctx.build("flip comma", |edit| { + edit.replace(prev.range(), next.text()); + edit.replace(next.range(), prev.text()); }) } #[cfg(test)] mod tests { use super::*; - use crate::test_utils::check_action; + use crate::assists::check_assist; #[test] - fn test_swap_comma() { - check_action( + fn flip_comma_works_for_function_parameters() { + check_assist( + flip_comma, "fn foo(x: i32,<|> y: Result<(), ()>) {}", "fn foo(y: Result<(), ()>,<|> x: i32) {}", - |file, off| flip_comma(file, off).map(|f| f()), ) } } -- cgit v1.2.3