From 4867968d22899395e6551f22641b3617e995140c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 6 May 2020 18:45:35 +0200 Subject: Refactor assists API to be more convenient for adding new assists It now duplicates completion API in its shape. --- crates/ra_assists/src/handlers/add_function.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'crates/ra_assists/src/handlers/add_function.rs') diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index 278079665..6b5616aa9 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -1,14 +1,13 @@ +use hir::HirDisplay; +use ra_db::FileId; use ra_syntax::{ - ast::{self, AstNode}, + ast::{self, edit::IndentLevel, ArgListOwner, AstNode, ModuleItemOwner}, SyntaxKind, SyntaxNode, TextSize, }; - -use crate::{Assist, AssistCtx, AssistId}; -use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner}; -use hir::HirDisplay; -use ra_db::FileId; use rustc_hash::{FxHashMap, FxHashSet}; +use crate::{AssistContext, AssistId, Assists}; + // Assist: add_function // // Adds a stub function with a signature matching the function under the cursor. @@ -34,7 +33,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; // } // // ``` -pub(crate) fn add_function(ctx: AssistCtx) -> Option { +pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?; let path = path_expr.path()?; @@ -59,7 +58,7 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option { let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; let target = call.syntax().text_range(); - ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| { + acc.add(AssistId("add_function"), "Add function", target, |edit| { let function_template = function_builder.render(); edit.set_file(function_template.file); edit.set_cursor(function_template.cursor_offset); @@ -87,7 +86,7 @@ impl FunctionBuilder { /// Prepares a generated function that matches `call` in `generate_in` /// (or as close to `call` as possible, if `generate_in` is `None`) fn from_call( - ctx: &AssistCtx, + ctx: &AssistContext, call: &ast::CallExpr, path: &ast::Path, target_module: Option>, @@ -152,7 +151,7 @@ fn fn_name(call: &ast::Path) -> Option { /// Computes the type variables and arguments required for the generated function fn fn_args( - ctx: &AssistCtx, + ctx: &AssistContext, call: &ast::CallExpr, ) -> Option<(Option, ast::ParamList)> { let mut arg_names = Vec::new(); @@ -219,7 +218,7 @@ fn fn_arg_name(fn_arg: &ast::Expr) -> Option { } } -fn fn_arg_type(ctx: &AssistCtx, fn_arg: &ast::Expr) -> Option { +fn fn_arg_type(ctx: &AssistContext, fn_arg: &ast::Expr) -> Option { let ty = ctx.sema.type_of_expr(fn_arg)?; if ty.is_unknown() { return None; -- cgit v1.2.3