aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_function.rs
diff options
context:
space:
mode:
authorvsrs <[email protected]>2020-05-08 17:34:34 +0100
committervsrs <[email protected]>2020-05-08 17:34:34 +0100
commit0ef17ef1ee9fb0ce7149176d12f4d225f6d01401 (patch)
treefa2f168120f36f8dbef5dc1e85fea0c0071639c2 /crates/ra_assists/src/handlers/add_function.rs
parent1be6320ea6cf7830195f80681fa0f43cc340da7e (diff)
parentd3eb9d8eafbebca7da95fa8a4813b92eb5080500 (diff)
Merge remote-tracking branch 'upstream/master' into uniformed_debug_lens
# Conflicts: # editors/code/src/commands/runnables.ts
Diffstat (limited to 'crates/ra_assists/src/handlers/add_function.rs')
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs31
1 files changed, 15 insertions, 16 deletions
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs
index 1d9d4e638..6b5616aa9 100644
--- a/crates/ra_assists/src/handlers/add_function.rs
+++ b/crates/ra_assists/src/handlers/add_function.rs
@@ -1,13 +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, AssistFile, AssistId};
7use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
8use hir::HirDisplay;
9use rustc_hash::{FxHashMap, FxHashSet}; 7use rustc_hash::{FxHashMap, FxHashSet};
10 8
9use crate::{AssistContext, AssistId, Assists};
10
11// Assist: add_function 11// Assist: add_function
12// 12//
13// 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.
@@ -33,7 +33,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
33// } 33// }
34// 34//
35// ``` 35// ```
36pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> { 36pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
37 let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; 37 let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
38 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?; 38 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
39 let path = path_expr.path()?; 39 let path = path_expr.path()?;
@@ -58,7 +58,7 @@ pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; 58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
59 59
60 let target = call.syntax().text_range(); 60 let target = call.syntax().text_range();
61 ctx.add_assist(AssistId("add_function"), "Add function", target, |edit| { 61 acc.add(AssistId("add_function"), "Add function", target, |edit| {
62 let function_template = function_builder.render(); 62 let function_template = function_builder.render();
63 edit.set_file(function_template.file); 63 edit.set_file(function_template.file);
64 edit.set_cursor(function_template.cursor_offset); 64 edit.set_cursor(function_template.cursor_offset);
@@ -70,7 +70,7 @@ struct FunctionTemplate {
70 insert_offset: TextSize, 70 insert_offset: TextSize,
71 cursor_offset: TextSize, 71 cursor_offset: TextSize,
72 fn_def: ast::SourceFile, 72 fn_def: ast::SourceFile,
73 file: AssistFile, 73 file: FileId,
74} 74}
75 75
76struct FunctionBuilder { 76struct FunctionBuilder {
@@ -78,7 +78,7 @@ struct FunctionBuilder {
78 fn_name: ast::Name, 78 fn_name: ast::Name,
79 type_params: Option<ast::TypeParamList>, 79 type_params: Option<ast::TypeParamList>,
80 params: ast::ParamList, 80 params: ast::ParamList,
81 file: AssistFile, 81 file: FileId,
82 needs_pub: bool, 82 needs_pub: bool,
83} 83}
84 84
@@ -86,13 +86,13 @@ impl FunctionBuilder {
86 /// Prepares a generated function that matches `call` in `generate_in` 86 /// Prepares a generated function that matches `call` in `generate_in`
87 /// (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`)
88 fn from_call( 88 fn from_call(
89 ctx: &AssistCtx, 89 ctx: &AssistContext,
90 call: &ast::CallExpr, 90 call: &ast::CallExpr,
91 path: &ast::Path, 91 path: &ast::Path,
92 target_module: Option<hir::InFile<hir::ModuleSource>>, 92 target_module: Option<hir::InFile<hir::ModuleSource>>,
93 ) -> Option<Self> { 93 ) -> Option<Self> {
94 let needs_pub = target_module.is_some(); 94 let needs_pub = target_module.is_some();
95 let mut file = AssistFile::default(); 95 let mut file = ctx.frange.file_id;
96 let target = if let Some(target_module) = target_module { 96 let target = if let Some(target_module) = target_module {
97 let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?; 97 let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
98 file = in_file; 98 file = in_file;
@@ -151,7 +151,7 @@ fn fn_name(call: &ast::Path) -> Option<ast::Name> {
151 151
152/// Computes the type variables and arguments required for the generated function 152/// Computes the type variables and arguments required for the generated function
153fn fn_args( 153fn fn_args(
154 ctx: &AssistCtx, 154 ctx: &AssistContext,
155 call: &ast::CallExpr, 155 call: &ast::CallExpr,
156) -> Option<(Option<ast::TypeParamList>, ast::ParamList)> { 156) -> Option<(Option<ast::TypeParamList>, ast::ParamList)> {
157 let mut arg_names = Vec::new(); 157 let mut arg_names = Vec::new();
@@ -218,7 +218,7 @@ fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
218 } 218 }
219} 219}
220 220
221fn fn_arg_type(ctx: &AssistCtx, fn_arg: &ast::Expr) -> Option<String> { 221fn fn_arg_type(ctx: &AssistContext, fn_arg: &ast::Expr) -> Option<String> {
222 let ty = ctx.sema.type_of_expr(fn_arg)?; 222 let ty = ctx.sema.type_of_expr(fn_arg)?;
223 if ty.is_unknown() { 223 if ty.is_unknown() {
224 return None; 224 return None;
@@ -253,9 +253,8 @@ fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFu
253fn next_space_for_fn_in_module( 253fn next_space_for_fn_in_module(
254 db: &dyn hir::db::AstDatabase, 254 db: &dyn hir::db::AstDatabase,
255 module: hir::InFile<hir::ModuleSource>, 255 module: hir::InFile<hir::ModuleSource>,
256) -> Option<(AssistFile, GeneratedFunctionTarget)> { 256) -> Option<(FileId, GeneratedFunctionTarget)> {
257 let file = module.file_id.original_file(db); 257 let file = module.file_id.original_file(db);
258 let assist_file = AssistFile::TargetFile(file);
259 let assist_item = match module.value { 258 let assist_item = match module.value {
260 hir::ModuleSource::SourceFile(it) => { 259 hir::ModuleSource::SourceFile(it) => {
261 if let Some(last_item) = it.items().last() { 260 if let Some(last_item) = it.items().last() {
@@ -272,7 +271,7 @@ fn next_space_for_fn_in_module(
272 } 271 }
273 } 272 }
274 }; 273 };
275 Some((assist_file, assist_item)) 274 Some((file, assist_item))
276} 275}
277 276
278#[cfg(test)] 277#[cfg(test)]