aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_function.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/add_function.rs')
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs21
1 files changed, 10 insertions, 11 deletions
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 @@
1use hir::HirDisplay;
2use ra_db::FileId;
1use ra_syntax::{ 3use ra_syntax::{
2 ast::{self, AstNode}, 4 ast::{self, edit::IndentLevel, ArgListOwner, AstNode, ModuleItemOwner},
3 SyntaxKind, SyntaxNode, TextSize, 5 SyntaxKind, SyntaxNode, TextSize,
4}; 6};
5
6use crate::{Assist, AssistCtx, AssistId};
7use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
8use hir::HirDisplay;
9use ra_db::FileId;
10use rustc_hash::{FxHashMap, FxHashSet}; 7use rustc_hash::{FxHashMap, FxHashSet};
11 8
9use crate::{AssistContext, AssistId, Assists};
10
12// Assist: add_function 11// Assist: add_function
13// 12//
14// Adds a stub function with a signature matching the function under the cursor. 13// Adds a stub function with a signature matching the function under the cursor.
@@ -34,7 +33,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
34// } 33// }
35// 34//
36// ``` 35// ```
37pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> { 36pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
38 let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; 37 let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
39 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?; 38 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
40 let path = path_expr.path()?; 39 let path = path_expr.path()?;
@@ -59,7 +58,7 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
59 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; 58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
60 59
61 let target = call.syntax().text_range(); 60 let target = call.syntax().text_range();
62 ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| { 61 acc.add(AssistId("add_function"), "Add function", target, |edit| {
63 let function_template = function_builder.render(); 62 let function_template = function_builder.render();
64 edit.set_file(function_template.file); 63 edit.set_file(function_template.file);
65 edit.set_cursor(function_template.cursor_offset); 64 edit.set_cursor(function_template.cursor_offset);
@@ -87,7 +86,7 @@ impl FunctionBuilder {
87 /// Prepares a generated function that matches `call` in `generate_in` 86 /// Prepares a generated function that matches `call` in `generate_in`
88 /// (or as close to `call` as possible, if `generate_in` is `None`) 87 /// (or as close to `call` as possible, if `generate_in` is `None`)
89 fn from_call( 88 fn from_call(
90 ctx: &AssistCtx, 89 ctx: &AssistContext,
91 call: &ast::CallExpr, 90 call: &ast::CallExpr,
92 path: &ast::Path, 91 path: &ast::Path,
93 target_module: Option<hir::InFile<hir::ModuleSource>>, 92 target_module: Option<hir::InFile<hir::ModuleSource>>,
@@ -152,7 +151,7 @@ fn fn_name(call: &ast::Path) -> Option<ast::Name> {
152 151
153/// Computes the type variables and arguments required for the generated function 152/// Computes the type variables and arguments required for the generated function
154fn fn_args( 153fn fn_args(
155 ctx: &AssistCtx, 154 ctx: &AssistContext,
156 call: &ast::CallExpr, 155 call: &ast::CallExpr,
157) -> Option<(Option<ast::TypeParamList>, ast::ParamList)> { 156) -> Option<(Option<ast::TypeParamList>, ast::ParamList)> {
158 let mut arg_names = Vec::new(); 157 let mut arg_names = Vec::new();
@@ -219,7 +218,7 @@ fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
219 } 218 }
220} 219}
221 220
222fn fn_arg_type(ctx: &AssistCtx, fn_arg: &ast::Expr) -> Option<String> { 221fn fn_arg_type(ctx: &AssistContext, fn_arg: &ast::Expr) -> Option<String> {
223 let ty = ctx.sema.type_of_expr(fn_arg)?; 222 let ty = ctx.sema.type_of_expr(fn_arg)?;
224 if ty.is_unknown() { 223 if ty.is_unknown() {
225 return None; 224 return None;