From 9e638c9f3ee68784cd93ec0458b0c92c18776f06 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 17:26:51 +0300 Subject: simplify --- crates/ra_assists/src/assists/add_import.rs | 43 +++++++++-------------------- 1 file changed, 13 insertions(+), 30 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index 149d1403f..7aa66b1fe 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs @@ -52,36 +52,18 @@ pub(crate) fn add_import(mut ctx: AssistCtx) -> Option return None; } - if let Some(module) = path.syntax().ancestors().find_map(ast::Module::cast) { - if let (Some(item_list), Some(name)) = (module.item_list(), module.name()) { - ctx.add_action( - AssistId("add_import"), - format!("import {} in mod {}", fmt_segments(&segments), name.text()), - |edit| { - apply_auto_import( - item_list.syntax(), - &path, - &segments, - edit.text_edit_builder(), - ); - }, - ); + let module = path.syntax().ancestors().find_map(ast::Module::cast); + let anchor = match module.and_then(|it| it.item_list()) { + Some(item_list) => item_list.syntax().clone(), + None => { + let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; + current_file.syntax().clone() } - } else { - let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; - ctx.add_action( - AssistId("add_import"), - format!("import {} in the current file", fmt_segments(&segments)), - |edit| { - apply_auto_import( - current_file.syntax(), - &path, - &segments, - edit.text_edit_builder(), - ); - }, - ); - } + }; + + ctx.add_action(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { + apply_auto_import(&anchor, &path, &segments, edit.text_edit_builder()); + }); ctx.build() } @@ -595,9 +577,10 @@ fn collect_hir_path_segments(path: &hir::Path) -> Option> { #[cfg(test)] mod tests { - use super::*; use crate::helpers::{check_assist, check_assist_not_applicable}; + use super::*; + #[test] fn test_auto_import_add_use_no_anchor() { check_assist( -- cgit v1.2.3 From cda6355de23825c201d02e6062cb2dd414e98bf9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 27 Oct 2019 17:35:37 +0300 Subject: simplify AssistCtx API We never actually use ability to create multiple actions out of a single context --- crates/ra_assists/src/assist_ctx.rs | 10 ++-- crates/ra_assists/src/assists/add_derive.rs | 8 ++- crates/ra_assists/src/assists/add_explicit_type.rs | 7 ++- crates/ra_assists/src/assists/add_impl.rs | 8 ++- crates/ra_assists/src/assists/add_import.rs | 12 ++--- .../src/assists/add_missing_impl_members.rs | 8 ++- crates/ra_assists/src/assists/apply_demorgan.rs | 7 ++- crates/ra_assists/src/assists/change_visibility.rs | 18 +++---- crates/ra_assists/src/assists/early_return.rs | 7 ++- crates/ra_assists/src/assists/fill_match_arms.rs | 8 ++- crates/ra_assists/src/assists/flip_binexpr.rs | 8 ++- crates/ra_assists/src/assists/flip_comma.rs | 8 ++- crates/ra_assists/src/assists/flip_trait_bound.rs | 8 ++- .../src/assists/inline_local_variable.rs | 8 ++- .../ra_assists/src/assists/introduce_variable.rs | 8 ++- crates/ra_assists/src/assists/merge_match_arms.rs | 8 ++- crates/ra_assists/src/assists/move_bounds.rs | 58 ++++++++++------------ crates/ra_assists/src/assists/move_guard.rs | 14 +++--- crates/ra_assists/src/assists/raw_string.rs | 28 +++++------ crates/ra_assists/src/assists/remove_dbg.rs | 8 ++- .../src/assists/replace_if_let_with_match.rs | 8 ++- crates/ra_assists/src/assists/split_import.rs | 8 ++- 22 files changed, 105 insertions(+), 160 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index c52736679..c5e9056af 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -82,12 +82,12 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { f(ctx) } - pub(crate) fn add_action( - &mut self, + pub(crate) fn add_assist( + mut self, id: AssistId, label: impl Into, f: impl FnOnce(&mut AssistBuilder), - ) -> &mut Self { + ) -> Option { let label = AssistLabel { label: label.into(), id }; match &mut self.assist { Assist::Unresolved(labels) => labels.push(label), @@ -100,10 +100,6 @@ impl<'a, DB: HirDatabase> AssistCtx<'a, DB> { labels_actions.push((label, action)); } } - self - } - - pub(crate) fn build(self) -> Option { Some(self.assist) } diff --git a/crates/ra_assists/src/assists/add_derive.rs b/crates/ra_assists/src/assists/add_derive.rs index d1e925b71..764b17bd8 100644 --- a/crates/ra_assists/src/assists/add_derive.rs +++ b/crates/ra_assists/src/assists/add_derive.rs @@ -25,10 +25,10 @@ use crate::{Assist, AssistCtx, AssistId}; // y: u32, // } // ``` -pub(crate) fn add_derive(mut ctx: AssistCtx) -> Option { +pub(crate) fn add_derive(ctx: AssistCtx) -> Option { let nominal = ctx.find_node_at_offset::()?; let node_start = derive_insertion_offset(&nominal)?; - ctx.add_action(AssistId("add_derive"), "add `#[derive]`", |edit| { + ctx.add_assist(AssistId("add_derive"), "add `#[derive]`", |edit| { let derive_attr = nominal .attrs() .filter_map(|x| x.as_simple_call()) @@ -44,9 +44,7 @@ pub(crate) fn add_derive(mut ctx: AssistCtx) -> Option }; edit.target(nominal.syntax().text_range()); edit.set_cursor(offset) - }); - - ctx.build() + }) } // Insert `derive` after doc comments. diff --git a/crates/ra_assists/src/assists/add_explicit_type.rs b/crates/ra_assists/src/assists/add_explicit_type.rs index ffbdc0b62..ddda1a0f2 100644 --- a/crates/ra_assists/src/assists/add_explicit_type.rs +++ b/crates/ra_assists/src/assists/add_explicit_type.rs @@ -21,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; // let x: i32 = 92; // } // ``` -pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option { +pub(crate) fn add_explicit_type(ctx: AssistCtx) -> Option { let stmt = ctx.find_node_at_offset::()?; let expr = stmt.initializer()?; let pat = stmt.pat()?; @@ -47,11 +47,10 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< return None; } - ctx.add_action(AssistId("add_explicit_type"), "add explicit type", |edit| { + ctx.add_assist(AssistId("add_explicit_type"), "add explicit type", |edit| { edit.target(pat_range); edit.insert(name_range.end(), format!(": {}", ty.display(db))); - }); - ctx.build() + }) } /// Returns true if any type parameter is unknown diff --git a/crates/ra_assists/src/assists/add_impl.rs b/crates/ra_assists/src/assists/add_impl.rs index fd3588d24..7da0cfd0d 100644 --- a/crates/ra_assists/src/assists/add_impl.rs +++ b/crates/ra_assists/src/assists/add_impl.rs @@ -27,10 +27,10 @@ use crate::{Assist, AssistCtx, AssistId}; // // } // ``` -pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { +pub(crate) fn add_impl(ctx: AssistCtx) -> Option { let nominal = ctx.find_node_at_offset::()?; let name = nominal.name()?; - ctx.add_action(AssistId("add_impl"), "add impl", |edit| { + ctx.add_assist(AssistId("add_impl"), "add impl", |edit| { edit.target(nominal.syntax().text_range()); let type_params = nominal.type_param_list(); let start_offset = nominal.syntax().text_range().end(); @@ -54,9 +54,7 @@ pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { edit.set_cursor(start_offset + TextUnit::of_str(&buf)); buf.push_str("\n}"); edit.insert(start_offset, buf); - }); - - ctx.build() + }) } #[cfg(test)] diff --git a/crates/ra_assists/src/assists/add_import.rs b/crates/ra_assists/src/assists/add_import.rs index 7aa66b1fe..c522d6a5a 100644 --- a/crates/ra_assists/src/assists/add_import.rs +++ b/crates/ra_assists/src/assists/add_import.rs @@ -39,7 +39,7 @@ pub fn auto_import_text_edit( } } -pub(crate) fn add_import(mut ctx: AssistCtx) -> Option { +pub(crate) fn add_import(ctx: AssistCtx) -> Option { let path: ast::Path = ctx.find_node_at_offset()?; // We don't want to mess with use statements if path.syntax().ancestors().find_map(ast::UseItem::cast).is_some() { @@ -53,7 +53,7 @@ pub(crate) fn add_import(mut ctx: AssistCtx) -> Option } let module = path.syntax().ancestors().find_map(ast::Module::cast); - let anchor = match module.and_then(|it| it.item_list()) { + let position = match module.and_then(|it| it.item_list()) { Some(item_list) => item_list.syntax().clone(), None => { let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?; @@ -61,11 +61,9 @@ pub(crate) fn add_import(mut ctx: AssistCtx) -> Option } }; - ctx.add_action(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { - apply_auto_import(&anchor, &path, &segments, edit.text_edit_builder()); - }); - - ctx.build() + ctx.add_assist(AssistId("add_import"), format!("import {}", fmt_segments(&segments)), |edit| { + apply_auto_import(&position, &path, &segments, edit.text_edit_builder()); + }) } fn collect_path_segments_raw( diff --git a/crates/ra_assists/src/assists/add_missing_impl_members.rs b/crates/ra_assists/src/assists/add_missing_impl_members.rs index 2585f3045..41de23921 100644 --- a/crates/ra_assists/src/assists/add_missing_impl_members.rs +++ b/crates/ra_assists/src/assists/add_missing_impl_members.rs @@ -91,7 +91,7 @@ pub(crate) fn add_missing_default_members(ctx: AssistCtx) -> O } fn add_missing_impl_members_inner( - mut ctx: AssistCtx, + ctx: AssistCtx, mode: AddMissingImplMembersMode, assist_id: &'static str, label: &'static str, @@ -133,7 +133,7 @@ fn add_missing_impl_members_inner( return None; } - ctx.add_action(AssistId(assist_id), label, |edit| { + ctx.add_assist(AssistId(assist_id), label, |edit| { let n_existing_items = impl_item_list.impl_items().count(); let items = missing_items .into_iter() @@ -150,9 +150,7 @@ fn add_missing_impl_members_inner( edit.replace_ast(impl_item_list, new_impl_item_list); edit.set_cursor(cursor_position); - }); - - ctx.build() + }) } fn add_body(fn_def: ast::FnDef) -> ast::FnDef { diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/assists/apply_demorgan.rs index 8d5984a58..068da1774 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs @@ -23,7 +23,7 @@ use crate::{Assist, AssistCtx, AssistId}; // if !(x == 4 && y) {} // } // ``` -pub(crate) fn apply_demorgan(mut ctx: AssistCtx) -> Option { +pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option { let expr = ctx.find_node_at_offset::()?; let op = expr.op_kind()?; let op_range = expr.op_token()?.text_range(); @@ -39,13 +39,12 @@ pub(crate) fn apply_demorgan(mut ctx: AssistCtx) -> Option) -> Option) -> Option { +fn add_vis(ctx: AssistCtx) -> Option { let item_keyword = ctx.token_at_offset().find(|leaf| match leaf.kind() { T![fn] | T![mod] | T![struct] | T![enum] | T![trait] => true, _ => false, @@ -57,13 +57,11 @@ fn add_vis(mut ctx: AssistCtx) -> Option { (vis_offset(field.syntax()), ident.text_range()) }; - ctx.add_action(AssistId("change_visibility"), "make pub(crate)", |edit| { + ctx.add_assist(AssistId("change_visibility"), "make pub(crate)", |edit| { edit.target(target); edit.insert(offset, "pub(crate) "); edit.set_cursor(offset); - }); - - ctx.build() + }) } fn vis_offset(node: &SyntaxNode) -> TextUnit { @@ -77,24 +75,20 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { .unwrap_or_else(|| node.text_range().start()) } -fn change_vis(mut ctx: AssistCtx, vis: ast::Visibility) -> Option { +fn change_vis(ctx: AssistCtx, vis: ast::Visibility) -> Option { if vis.syntax().text() == "pub" { - ctx.add_action(AssistId("change_visibility"), "change to pub(crate)", |edit| { + return ctx.add_assist(AssistId("change_visibility"), "change to pub(crate)", |edit| { edit.target(vis.syntax().text_range()); edit.replace(vis.syntax().text_range(), "pub(crate)"); edit.set_cursor(vis.syntax().text_range().start()) }); - - return ctx.build(); } if vis.syntax().text() == "pub(crate)" { - ctx.add_action(AssistId("change_visibility"), "change to pub", |edit| { + return ctx.add_assist(AssistId("change_visibility"), "change to pub", |edit| { edit.target(vis.syntax().text_range()); edit.replace(vis.syntax().text_range(), "pub"); edit.set_cursor(vis.syntax().text_range().start()); }); - - return ctx.build(); } None } diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index 75822dc65..e839d831e 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs @@ -35,7 +35,7 @@ use crate::{ // bar(); // } // ``` -pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> Option { +pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option { let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; let expr = if_expr.condition()?.expr()?; let then_block = if_expr.then_branch()?.block()?; @@ -75,7 +75,7 @@ pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> then_block.syntax().last_child_or_token().filter(|t| t.kind() == R_CURLY)?; let cursor_position = ctx.frange.range.start(); - ctx.add_action(AssistId("convert_to_guarded_return"), "convert to guarded return", |edit| { + ctx.add_assist(AssistId("convert_to_guarded_return"), "convert to guarded return", |edit| { let if_indent_level = IndentLevel::from_node(&if_expr.syntax()); let new_if_expr = if_indent_level.increase_indent(make::if_expression(&expr, early_expression)); @@ -105,8 +105,7 @@ pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> edit.target(if_expr.syntax().text_range()); edit.replace_ast(parent_block, ast::Block::cast(new_block).unwrap()); edit.set_cursor(cursor_position); - }); - ctx.build() + }) } #[cfg(test)] diff --git a/crates/ra_assists/src/assists/fill_match_arms.rs b/crates/ra_assists/src/assists/fill_match_arms.rs index c62c0efbe..2b74f355c 100644 --- a/crates/ra_assists/src/assists/fill_match_arms.rs +++ b/crates/ra_assists/src/assists/fill_match_arms.rs @@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // } // ``` -pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option { +pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option { let match_expr = ctx.find_node_at_offset::()?; let match_arm_list = match_expr.match_arm_list()?; @@ -53,7 +53,7 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option) -> Option bool { diff --git a/crates/ra_assists/src/assists/flip_binexpr.rs b/crates/ra_assists/src/assists/flip_binexpr.rs index 9765d5ddd..386045eb0 100644 --- a/crates/ra_assists/src/assists/flip_binexpr.rs +++ b/crates/ra_assists/src/assists/flip_binexpr.rs @@ -18,7 +18,7 @@ use crate::{Assist, AssistCtx, AssistId}; // let _ = 2 + 90; // } // ``` -pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option { +pub(crate) fn flip_binexpr(ctx: AssistCtx) -> Option { let expr = ctx.find_node_at_offset::()?; let lhs = expr.lhs()?.syntax().clone(); let rhs = expr.rhs()?.syntax().clone(); @@ -34,16 +34,14 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option) -> Option { +pub(crate) fn flip_comma(ctx: AssistCtx) -> Option { let comma = ctx.find_token_at_offset(T![,])?; let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; @@ -29,13 +29,11 @@ pub(crate) fn flip_comma(mut ctx: AssistCtx) -> Option return None; } - ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { + ctx.add_assist(AssistId("flip_comma"), "flip comma", |edit| { edit.target(comma.text_range()); edit.replace(prev.text_range(), next.to_string()); edit.replace(next.text_range(), prev.to_string()); - }); - - ctx.build() + }) } #[cfg(test)] diff --git a/crates/ra_assists/src/assists/flip_trait_bound.rs b/crates/ra_assists/src/assists/flip_trait_bound.rs index 1625b241f..6017b39dd 100644 --- a/crates/ra_assists/src/assists/flip_trait_bound.rs +++ b/crates/ra_assists/src/assists/flip_trait_bound.rs @@ -18,7 +18,7 @@ use crate::{Assist, AssistCtx, AssistId}; // ``` // fn foo() { } // ``` -pub(crate) fn flip_trait_bound(mut ctx: AssistCtx) -> Option { +pub(crate) fn flip_trait_bound(ctx: AssistCtx) -> Option { // We want to replicate the behavior of `flip_binexpr` by only suggesting // the assist when the cursor is on a `+` let plus = ctx.find_token_at_offset(T![+])?; @@ -33,13 +33,11 @@ pub(crate) fn flip_trait_bound(mut ctx: AssistCtx) -> Option) -> Option { +pub(crate) fn inline_local_varialbe(ctx: AssistCtx) -> Option { let let_stmt = ctx.find_node_at_offset::()?; let bind_pat = match let_stmt.pat()? { ast::Pat::BindPat(pat) => pat, @@ -93,7 +93,7 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt let init_str = initializer_expr.syntax().text().to_string(); let init_in_paren = format!("({})", &init_str); - ctx.add_action( + ctx.add_assist( AssistId("inline_local_variable"), "inline local variable", move |edit: &mut AssistBuilder| { @@ -107,9 +107,7 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt } edit.set_cursor(delete_range.start()) }, - ); - - ctx.build() + ) } #[cfg(test)] diff --git a/crates/ra_assists/src/assists/introduce_variable.rs b/crates/ra_assists/src/assists/introduce_variable.rs index 8245dc99f..0623d4475 100644 --- a/crates/ra_assists/src/assists/introduce_variable.rs +++ b/crates/ra_assists/src/assists/introduce_variable.rs @@ -28,7 +28,7 @@ use crate::{Assist, AssistCtx, AssistId}; // var_name * 4; // } // ``` -pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option { +pub(crate) fn introduce_variable(ctx: AssistCtx) -> Option { if ctx.frange.range.is_empty() { return None; } @@ -43,7 +43,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option if indent.kind() != WHITESPACE { return None; } - ctx.add_action(AssistId("introduce_variable"), "introduce variable", move |edit| { + ctx.add_assist(AssistId("introduce_variable"), "introduce variable", move |edit| { let mut buf = String::new(); let cursor_offset = if wrap_in_block { @@ -88,9 +88,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option } } edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); - }); - - ctx.build() + }) } /// Check whether the node is a valid expression which can be extracted to a variable. diff --git a/crates/ra_assists/src/assists/merge_match_arms.rs b/crates/ra_assists/src/assists/merge_match_arms.rs index b0c4ee78b..e9f2cae91 100644 --- a/crates/ra_assists/src/assists/merge_match_arms.rs +++ b/crates/ra_assists/src/assists/merge_match_arms.rs @@ -26,7 +26,7 @@ use ra_syntax::ast::{AstNode, MatchArm}; // } // } // ``` -pub(crate) fn merge_match_arms(mut ctx: AssistCtx) -> Option { +pub(crate) fn merge_match_arms(ctx: AssistCtx) -> Option { let current_arm = ctx.find_node_at_offset::()?; // We check if the following match arm matches this one. We could, but don't, @@ -52,7 +52,7 @@ pub(crate) fn merge_match_arms(mut ctx: AssistCtx) -> Option bool { a.pats().any(|x| match x { ra_syntax::ast::Pat::PlaceholderPat(..) => true, @@ -80,9 +80,7 @@ pub(crate) fn merge_match_arms(mut ctx: AssistCtx) -> Option) -> Option { +pub(crate) fn move_bounds_to_where_clause(ctx: AssistCtx) -> Option { let type_param_list = ctx.find_node_at_offset::()?; let mut type_params = type_param_list.type_params(); @@ -46,38 +46,30 @@ pub(crate) fn move_bounds_to_where_clause(mut ctx: AssistCtx) _ => return None, }; - ctx.add_action( - AssistId("move_bounds_to_where_clause"), - "move_bounds_to_where_clause", - |edit| { - let new_params = type_param_list - .type_params() - .filter(|it| it.type_bound_list().is_some()) - .map(|type_param| { - let without_bounds = type_param.remove_bounds(); - (type_param, without_bounds) - }); - - let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); - edit.replace_ast(type_param_list.clone(), new_type_param_list); - - let where_clause = { - let predicates = type_param_list.type_params().filter_map(build_predicate); - make::where_clause(predicates) - }; - - let to_insert = match anchor.prev_sibling_or_token() { - Some(ref elem) if elem.kind() == WHITESPACE => { - format!("{} ", where_clause.syntax()) - } - _ => format!(" {}", where_clause.syntax()), - }; - edit.insert(anchor.text_range().start(), to_insert); - edit.target(type_param_list.syntax().text_range()); - }, - ); - - ctx.build() + ctx.add_assist(AssistId("move_bounds_to_where_clause"), "move_bounds_to_where_clause", |edit| { + let new_params = type_param_list + .type_params() + .filter(|it| it.type_bound_list().is_some()) + .map(|type_param| { + let without_bounds = type_param.remove_bounds(); + (type_param, without_bounds) + }); + + let new_type_param_list = edit::replace_descendants(&type_param_list, new_params); + edit.replace_ast(type_param_list.clone(), new_type_param_list); + + let where_clause = { + let predicates = type_param_list.type_params().filter_map(build_predicate); + make::where_clause(predicates) + }; + + let to_insert = match anchor.prev_sibling_or_token() { + Some(ref elem) if elem.kind() == WHITESPACE => format!("{} ", where_clause.syntax()), + _ => format!(" {}", where_clause.syntax()), + }; + edit.insert(anchor.text_range().start(), to_insert); + edit.target(type_param_list.syntax().text_range()); + }) } fn build_predicate(param: ast::TypeParam) -> Option { diff --git a/crates/ra_assists/src/assists/move_guard.rs b/crates/ra_assists/src/assists/move_guard.rs index e820a73c8..b49ec6172 100644 --- a/crates/ra_assists/src/assists/move_guard.rs +++ b/crates/ra_assists/src/assists/move_guard.rs @@ -32,7 +32,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // } // ``` -pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Option { +pub(crate) fn move_guard_to_arm_body(ctx: AssistCtx) -> Option { let match_arm = ctx.find_node_at_offset::()?; let guard = match_arm.guard()?; let space_before_guard = guard.syntax().prev_sibling_or_token(); @@ -41,7 +41,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op let arm_expr = match_arm.expr()?; let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); - ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { + ctx.add_assist(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { edit.target(guard.syntax().text_range()); let offseting_amount = match space_before_guard.and_then(|it| it.into_token()) { Some(tok) => { @@ -61,8 +61,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op edit.set_cursor( arm_expr.syntax().text_range().start() + TextUnit::from(3) - offseting_amount, ); - }); - ctx.build() + }) } // Assist: move_arm_cond_to_match_guard @@ -90,7 +89,7 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op // } // } // ``` -pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx) -> Option { +pub(crate) fn move_arm_cond_to_match_guard(ctx: AssistCtx) -> Option { let match_arm: MatchArm = ctx.find_node_at_offset::()?; let last_match_pat = match_arm.pats().last()?; @@ -110,7 +109,7 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx) let buf = format!(" if {}", cond.syntax().text()); - ctx.add_action( + ctx.add_assist( AssistId("move_arm_cond_to_match_guard"), "move condition to match guard", |edit| { @@ -127,8 +126,7 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx) edit.insert(last_match_pat.syntax().text_range().end(), buf); edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1)); }, - ); - ctx.build() + ) } #[cfg(test)] diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index 2df48a838..86bbaeeef 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs @@ -22,7 +22,7 @@ use crate::{Assist, AssistCtx, AssistId}; // r#"Hello, World!"#; // } // ``` -pub(crate) fn make_raw_string(mut ctx: AssistCtx) -> Option { +pub(crate) fn make_raw_string(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(STRING)?; let text = token.text().as_str(); let usual_string_range = find_usual_string_range(text)?; @@ -41,7 +41,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx) -> Option) -> Option) -> Option) -> Option { +pub(crate) fn make_usual_string(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; let text = token.text().as_str(); let usual_string_range = find_usual_string_range(text)?; - ctx.add_action(AssistId("make_usual_string"), "make usual string", |edit| { + ctx.add_assist(AssistId("make_usual_string"), "make usual string", |edit| { edit.target(token.text_range()); // parse inside string to escape `"` let start_of_inside = usual_string_range.start().to_usize() + 1; @@ -80,8 +79,7 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx) -> Option< let inside_str = &text[start_of_inside..end_of_inside]; let escaped = inside_str.escape_default().to_string(); edit.replace(token.text_range(), format!("\"{}\"", escaped)); - }); - ctx.build() + }) } // Assist: add_hash @@ -99,14 +97,13 @@ pub(crate) fn make_usual_string(mut ctx: AssistCtx) -> Option< // r##"Hello, World!"##; // } // ``` -pub(crate) fn add_hash(mut ctx: AssistCtx) -> Option { +pub(crate) fn add_hash(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; - ctx.add_action(AssistId("add_hash"), "add hash to raw string", |edit| { + ctx.add_assist(AssistId("add_hash"), "add hash to raw string", |edit| { edit.target(token.text_range()); edit.insert(token.text_range().start() + TextUnit::of_char('r'), "#"); edit.insert(token.text_range().end(), "#"); - }); - ctx.build() + }) } // Assist: remove_hash @@ -124,14 +121,14 @@ pub(crate) fn add_hash(mut ctx: AssistCtx) -> Option { // r"Hello, World!"; // } // ``` -pub(crate) fn remove_hash(mut ctx: AssistCtx) -> Option { +pub(crate) fn remove_hash(ctx: AssistCtx) -> Option { let token = ctx.find_token_at_offset(RAW_STRING)?; let text = token.text().as_str(); if text.starts_with("r\"") { // no hash to remove return None; } - ctx.add_action(AssistId("remove_hash"), "remove hash from raw string", |edit| { + ctx.add_assist(AssistId("remove_hash"), "remove hash from raw string", |edit| { edit.target(token.text_range()); let result = &text[2..text.len() - 1]; let result = if result.starts_with("\"") { @@ -142,8 +139,7 @@ pub(crate) fn remove_hash(mut ctx: AssistCtx) -> Option usize { diff --git a/crates/ra_assists/src/assists/remove_dbg.rs b/crates/ra_assists/src/assists/remove_dbg.rs index 44b8de814..aedf8747f 100644 --- a/crates/ra_assists/src/assists/remove_dbg.rs +++ b/crates/ra_assists/src/assists/remove_dbg.rs @@ -21,7 +21,7 @@ use crate::{Assist, AssistCtx, AssistId}; // 92; // } // ``` -pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option { +pub(crate) fn remove_dbg(ctx: AssistCtx) -> Option { let macro_call = ctx.find_node_at_offset::()?; if !is_valid_macrocall(¯o_call, "dbg")? { @@ -58,13 +58,11 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option text.slice(without_parens).to_string() }; - ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| { + ctx.add_assist(AssistId("remove_dbg"), "remove dbg!()", |edit| { edit.target(macro_call.syntax().text_range()); edit.replace(macro_range, macro_content); edit.set_cursor(cursor_pos); - }); - - ctx.build() + }) } /// Verifies that the given macro_call actually matches the given name diff --git a/crates/ra_assists/src/assists/replace_if_let_with_match.rs b/crates/ra_assists/src/assists/replace_if_let_with_match.rs index 58ef2ff20..dff84d865 100644 --- a/crates/ra_assists/src/assists/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/assists/replace_if_let_with_match.rs @@ -31,7 +31,7 @@ use crate::{Assist, AssistCtx, AssistId}; // } // } // ``` -pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx) -> Option { +pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option { let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; let cond = if_expr.condition()?; let pat = cond.pat()?; @@ -42,14 +42,12 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx) -> ast::ElseBranch::IfExpr(_) => return None, }; - ctx.add_action(AssistId("replace_if_let_with_match"), "replace with match", |edit| { + ctx.add_assist(AssistId("replace_if_let_with_match"), "replace with match", |edit| { let match_expr = build_match_expr(expr, pat, then_block, else_block); edit.target(if_expr.syntax().text_range()); edit.replace_node_and_indent(if_expr.syntax(), match_expr); edit.set_cursor(if_expr.syntax().text_range().start()) - }); - - ctx.build() + }) } fn build_match_expr( diff --git a/crates/ra_assists/src/assists/split_import.rs b/crates/ra_assists/src/assists/split_import.rs index 8d8a28987..5f8d6b0be 100644 --- a/crates/ra_assists/src/assists/split_import.rs +++ b/crates/ra_assists/src/assists/split_import.rs @@ -16,7 +16,7 @@ use crate::{Assist, AssistCtx, AssistId}; // ``` // use std::{collections::HashMap}; // ``` -pub(crate) fn split_import(mut ctx: AssistCtx) -> Option { +pub(crate) fn split_import(ctx: AssistCtx) -> Option { let colon_colon = ctx.find_token_at_offset(T![::])?; let path = ast::Path::cast(colon_colon.parent())?; let top_path = successors(Some(path), |it| it.parent_path()).last()?; @@ -32,14 +32,12 @@ pub(crate) fn split_import(mut ctx: AssistCtx) -> Option top_path.syntax().text_range().end(), }; - ctx.add_action(AssistId("split_import"), "split import", |edit| { + ctx.add_assist(AssistId("split_import"), "split import", |edit| { edit.target(colon_colon.text_range()); edit.insert(l_curly, "{"); edit.insert(r_curly, "}"); edit.set_cursor(l_curly + TextUnit::of_str("{")); - }); - - ctx.build() + }) } #[cfg(test)] -- cgit v1.2.3