2021-09-29 07:04:32 -05:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
|
|
|
|
2021-11-10 15:02:50 -06:00
|
|
|
use hir::{HasSource, HirDisplay, Module, Semantics, TypeInfo};
|
2021-09-21 11:15:57 -05:00
|
|
|
use ide_db::{
|
|
|
|
base_db::FileId,
|
|
|
|
defs::{Definition, NameRefClass},
|
2022-03-06 12:01:30 -06:00
|
|
|
famous_defs::FamousDefs,
|
|
|
|
RootDatabase, SnippetCap,
|
2021-09-21 11:15:57 -05:00
|
|
|
};
|
2021-03-06 11:53:21 -06:00
|
|
|
use stdx::to_lower_snake_case;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-05-09 07:40:11 -05:00
|
|
|
ast::{
|
|
|
|
self,
|
|
|
|
edit::{AstNodeEdit, IndentLevel},
|
2021-09-27 05:54:24 -05:00
|
|
|
make, AstNode, CallExpr, HasArgList, HasModuleItem,
|
2020-05-09 07:40:11 -05:00
|
|
|
},
|
2021-08-21 03:59:15 -05:00
|
|
|
SyntaxKind, SyntaxNode, TextRange, TextSize,
|
2020-03-26 14:59:35 -05:00
|
|
|
};
|
|
|
|
|
2020-05-19 18:53:21 -05:00
|
|
|
use crate::{
|
2021-09-29 07:04:32 -05:00
|
|
|
utils::convert_reference_type,
|
2021-08-07 01:59:49 -05:00
|
|
|
utils::{find_struct_impl, render_snippet, Cursor},
|
2020-06-28 17:36:05 -05:00
|
|
|
AssistContext, AssistId, AssistKind, Assists,
|
2020-05-19 18:53:21 -05:00
|
|
|
};
|
2020-05-06 11:45:35 -05:00
|
|
|
|
2020-07-03 11:15:03 -05:00
|
|
|
// Assist: generate_function
|
2020-03-26 14:59:35 -05:00
|
|
|
//
|
|
|
|
// Adds a stub function with a signature matching the function under the cursor.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// struct Baz;
|
|
|
|
// fn baz() -> Baz { Baz }
|
|
|
|
// fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
// bar$0("", baz());
|
2020-03-26 14:59:35 -05:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// struct Baz;
|
|
|
|
// fn baz() -> Baz { Baz }
|
|
|
|
// fn foo() {
|
2020-04-16 16:53:25 -05:00
|
|
|
// bar("", baz());
|
2020-03-26 14:59:35 -05:00
|
|
|
// }
|
|
|
|
//
|
2021-09-04 03:43:36 -05:00
|
|
|
// fn bar(arg: &str, baz: Baz) ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
// todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ```
|
2020-07-03 11:15:03 -05:00
|
|
|
pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-08-09 11:07:07 -05:00
|
|
|
gen_fn(acc, ctx).or_else(|| gen_method(acc, ctx))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2020-03-26 14:59:35 -05:00
|
|
|
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
|
|
|
|
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
|
2021-07-09 08:48:22 -05:00
|
|
|
let path = path_expr.path()?;
|
2021-09-16 11:03:37 -05:00
|
|
|
let name_ref = path.segment()?.name_ref()?;
|
2020-03-26 14:59:35 -05:00
|
|
|
if ctx.sema.resolve_path(&path).is_some() {
|
|
|
|
// The function call already resolves, no need to add a function
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-09-16 11:03:37 -05:00
|
|
|
let fn_name = &*name_ref.text();
|
2021-08-23 02:30:10 -05:00
|
|
|
let target_module;
|
|
|
|
let mut adt_name = None;
|
|
|
|
|
|
|
|
let (target, file, insert_offset) = match path.qualifier() {
|
2020-04-25 09:58:28 -05:00
|
|
|
Some(qualifier) => match ctx.sema.resolve_path(&qualifier) {
|
2021-08-23 02:30:10 -05:00
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) => {
|
|
|
|
target_module = Some(module);
|
|
|
|
get_fn_target(ctx, &target_module, call.clone())?
|
|
|
|
}
|
|
|
|
Some(hir::PathResolution::Def(hir::ModuleDef::Adt(adt))) => {
|
|
|
|
let current_module = current_module(call.syntax(), ctx)?;
|
|
|
|
let module = adt.module(ctx.sema.db);
|
|
|
|
target_module = if current_module == module { None } else { Some(module) };
|
|
|
|
if current_module.krate() != module.krate() {
|
|
|
|
return None;
|
|
|
|
}
|
2021-09-16 11:03:37 -05:00
|
|
|
let (impl_, file) = get_adt_source(ctx, &adt, fn_name)?;
|
2021-08-23 02:30:10 -05:00
|
|
|
let (target, insert_offset) = get_method_target(ctx, &module, &impl_)?;
|
|
|
|
adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
|
|
|
|
(target, file, insert_offset)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
2020-04-25 09:58:28 -05:00
|
|
|
},
|
2021-08-23 02:30:10 -05:00
|
|
|
_ => {
|
|
|
|
target_module = None;
|
|
|
|
get_fn_target(ctx, &target_module, call.clone())?
|
|
|
|
}
|
2020-04-16 16:53:25 -05:00
|
|
|
};
|
2021-09-16 11:03:37 -05:00
|
|
|
let function_builder = FunctionBuilder::from_call(ctx, &call, fn_name, target_module, target)?;
|
2021-08-23 02:30:10 -05:00
|
|
|
let text_range = call.syntax().text_range();
|
2021-09-13 11:50:19 -05:00
|
|
|
let label = format!("Generate {} function", function_builder.fn_name);
|
2021-08-23 02:30:10 -05:00
|
|
|
add_func_to_accumulator(
|
|
|
|
acc,
|
|
|
|
ctx,
|
|
|
|
text_range,
|
|
|
|
function_builder,
|
|
|
|
insert_offset,
|
|
|
|
file,
|
|
|
|
adt_name,
|
|
|
|
label,
|
|
|
|
)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
2021-08-09 11:07:07 -05:00
|
|
|
fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2021-08-05 04:30:08 -05:00
|
|
|
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
|
2022-02-02 04:37:24 -06:00
|
|
|
if ctx.sema.resolve_method_call(&call).is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2021-08-14 10:42:06 -05:00
|
|
|
let fn_name = call.name_ref()?;
|
|
|
|
let adt = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
|
2021-08-07 01:59:49 -05:00
|
|
|
|
2021-08-21 02:22:07 -05:00
|
|
|
let current_module = current_module(call.syntax(), ctx)?;
|
2021-08-14 10:42:06 -05:00
|
|
|
let target_module = adt.module(ctx.sema.db);
|
2021-08-07 04:29:33 -05:00
|
|
|
|
|
|
|
if current_module.krate() != target_module.krate() {
|
|
|
|
return None;
|
|
|
|
}
|
2021-08-23 02:30:10 -05:00
|
|
|
let (impl_, file) = get_adt_source(ctx, &adt, fn_name.text().as_str())?;
|
2021-08-21 09:28:29 -05:00
|
|
|
let (target, insert_offset) = get_method_target(ctx, &target_module, &impl_)?;
|
|
|
|
let function_builder =
|
|
|
|
FunctionBuilder::from_method_call(ctx, &call, &fn_name, target_module, target)?;
|
|
|
|
let text_range = call.syntax().text_range();
|
|
|
|
let adt_name = if impl_.is_none() { Some(adt.name(ctx.sema.db)) } else { None };
|
2021-09-13 11:50:19 -05:00
|
|
|
let label = format!("Generate {} method", function_builder.fn_name);
|
2021-08-21 09:28:29 -05:00
|
|
|
add_func_to_accumulator(
|
|
|
|
acc,
|
2021-08-07 01:59:49 -05:00
|
|
|
ctx,
|
2021-08-21 09:28:29 -05:00
|
|
|
text_range,
|
|
|
|
function_builder,
|
|
|
|
insert_offset,
|
2021-08-23 02:30:10 -05:00
|
|
|
file,
|
2021-08-21 09:28:29 -05:00
|
|
|
adt_name,
|
2021-08-22 03:01:16 -05:00
|
|
|
label,
|
2021-08-21 09:28:29 -05:00
|
|
|
)
|
2021-08-21 03:59:15 -05:00
|
|
|
}
|
2021-08-05 04:30:08 -05:00
|
|
|
|
2021-08-21 03:59:15 -05:00
|
|
|
fn add_func_to_accumulator(
|
|
|
|
acc: &mut Assists,
|
|
|
|
ctx: &AssistContext,
|
2021-08-21 09:28:29 -05:00
|
|
|
text_range: TextRange,
|
2021-08-21 03:59:15 -05:00
|
|
|
function_builder: FunctionBuilder,
|
2021-08-21 08:01:37 -05:00
|
|
|
insert_offset: TextSize,
|
|
|
|
file: FileId,
|
2021-08-21 03:59:15 -05:00
|
|
|
adt_name: Option<hir::Name>,
|
2021-08-22 03:01:16 -05:00
|
|
|
label: String,
|
2021-08-21 03:59:15 -05:00
|
|
|
) -> Option<()> {
|
2021-08-22 03:01:16 -05:00
|
|
|
acc.add(AssistId("generate_function", AssistKind::Generate), label, text_range, |builder| {
|
|
|
|
let function_template = function_builder.render();
|
|
|
|
let mut func = function_template.to_string(ctx.config.snippet_cap);
|
|
|
|
if let Some(name) = adt_name {
|
|
|
|
func = format!("\nimpl {} {{\n{}\n}}", name, func);
|
|
|
|
}
|
|
|
|
builder.edit_file(file);
|
|
|
|
match ctx.config.snippet_cap {
|
|
|
|
Some(cap) => builder.insert_snippet(cap, insert_offset, func),
|
|
|
|
None => builder.insert(insert_offset, func),
|
|
|
|
}
|
|
|
|
})
|
2021-08-05 04:30:08 -05:00
|
|
|
}
|
|
|
|
|
2021-08-21 02:22:07 -05:00
|
|
|
fn current_module(current_node: &SyntaxNode, ctx: &AssistContext) -> Option<Module> {
|
|
|
|
ctx.sema.scope(current_node).module()
|
|
|
|
}
|
|
|
|
|
2021-08-23 02:30:10 -05:00
|
|
|
fn get_adt_source(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
adt: &hir::Adt,
|
|
|
|
fn_name: &str,
|
|
|
|
) -> Option<(Option<ast::Impl>, FileId)> {
|
|
|
|
let range = adt.source(ctx.sema.db)?.syntax().original_file_range(ctx.sema.db);
|
|
|
|
let file = ctx.sema.parse(range.file_id);
|
|
|
|
let adt_source =
|
|
|
|
ctx.sema.find_node_at_offset_with_macros(file.syntax(), range.range.start())?;
|
|
|
|
find_struct_impl(ctx, &adt_source, fn_name).map(|impl_| (impl_, range.file_id))
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
struct FunctionTemplate {
|
2020-05-19 16:12:01 -05:00
|
|
|
leading_ws: String,
|
2020-07-30 07:51:08 -05:00
|
|
|
fn_def: ast::Fn,
|
2021-08-12 09:05:32 -05:00
|
|
|
ret_type: Option<ast::RetType>,
|
2021-08-13 04:44:29 -05:00
|
|
|
should_focus_return_type: bool,
|
2020-05-19 16:12:01 -05:00
|
|
|
trailing_ws: String,
|
2021-08-12 07:56:39 -05:00
|
|
|
tail_expr: ast::Expr,
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:12:01 -05:00
|
|
|
impl FunctionTemplate {
|
|
|
|
fn to_string(&self, cap: Option<SnippetCap>) -> String {
|
2021-08-12 07:56:39 -05:00
|
|
|
let f = match cap {
|
|
|
|
Some(cap) => {
|
2021-08-13 04:44:29 -05:00
|
|
|
let cursor = if self.should_focus_return_type {
|
|
|
|
// Focus the return type if there is one
|
2021-10-03 07:53:01 -05:00
|
|
|
match self.ret_type {
|
|
|
|
Some(ref ret_type) => ret_type.syntax(),
|
|
|
|
None => self.tail_expr.syntax(),
|
2021-08-12 09:05:32 -05:00
|
|
|
}
|
2021-08-12 07:56:39 -05:00
|
|
|
} else {
|
|
|
|
self.tail_expr.syntax()
|
|
|
|
};
|
|
|
|
render_snippet(cap, self.fn_def.syntax(), Cursor::Replace(cursor))
|
2020-10-04 14:21:30 -05:00
|
|
|
}
|
2021-08-12 07:56:39 -05:00
|
|
|
None => self.fn_def.to_string(),
|
2020-05-19 16:12:01 -05:00
|
|
|
};
|
2021-08-12 07:56:39 -05:00
|
|
|
|
2020-05-19 16:12:01 -05:00
|
|
|
format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
struct FunctionBuilder {
|
2020-04-16 16:53:25 -05:00
|
|
|
target: GeneratedFunctionTarget,
|
2020-03-31 16:02:48 -05:00
|
|
|
fn_name: ast::Name,
|
2020-07-30 08:36:21 -05:00
|
|
|
type_params: Option<ast::GenericParamList>,
|
2020-03-31 16:02:48 -05:00
|
|
|
params: ast::ParamList,
|
2021-08-12 09:05:32 -05:00
|
|
|
ret_type: Option<ast::RetType>,
|
2021-08-13 04:44:29 -05:00
|
|
|
should_focus_return_type: bool,
|
2020-04-20 11:36:12 -05:00
|
|
|
needs_pub: bool,
|
2021-07-09 08:48:22 -05:00
|
|
|
is_async: bool,
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionBuilder {
|
2020-04-25 09:58:28 -05:00
|
|
|
/// Prepares a generated function that matches `call`.
|
|
|
|
/// The function is generated in `target_module` or next to `call`
|
2020-04-16 16:53:25 -05:00
|
|
|
fn from_call(
|
2020-05-06 11:45:35 -05:00
|
|
|
ctx: &AssistContext,
|
2020-04-16 16:53:25 -05:00
|
|
|
call: &ast::CallExpr,
|
2021-09-16 11:03:37 -05:00
|
|
|
fn_name: &str,
|
2020-04-25 09:58:28 -05:00
|
|
|
target_module: Option<hir::Module>,
|
2021-08-21 09:28:29 -05:00
|
|
|
target: GeneratedFunctionTarget,
|
|
|
|
) -> Option<Self> {
|
2020-04-25 09:58:28 -05:00
|
|
|
let needs_pub = target_module.is_some();
|
2021-08-21 02:22:07 -05:00
|
|
|
let target_module = target_module.or_else(|| current_module(target.syntax(), ctx))?;
|
2021-09-16 11:03:37 -05:00
|
|
|
let fn_name = make::name(fn_name);
|
2021-10-03 05:42:00 -05:00
|
|
|
let (type_params, params) =
|
|
|
|
fn_args(ctx, target_module, ast::CallableExpr::Call(call.clone()))?;
|
2020-04-25 09:58:28 -05:00
|
|
|
|
2021-07-09 08:48:22 -05:00
|
|
|
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
|
|
|
|
let is_async = await_expr.is_some();
|
|
|
|
|
2021-08-13 04:44:29 -05:00
|
|
|
let (ret_type, should_focus_return_type) =
|
2021-08-12 09:05:32 -05:00
|
|
|
make_return_type(ctx, &ast::Expr::CallExpr(call.clone()), target_module);
|
2021-03-06 16:29:45 -06:00
|
|
|
|
2021-08-21 09:28:29 -05:00
|
|
|
Some(Self {
|
|
|
|
target,
|
|
|
|
fn_name,
|
|
|
|
type_params,
|
|
|
|
params,
|
|
|
|
ret_type,
|
|
|
|
should_focus_return_type,
|
|
|
|
needs_pub,
|
|
|
|
is_async,
|
|
|
|
})
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
2020-04-20 11:36:12 -05:00
|
|
|
|
2021-08-05 04:30:08 -05:00
|
|
|
fn from_method_call(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
call: &ast::MethodCallExpr,
|
|
|
|
name: &ast::NameRef,
|
2021-08-07 01:59:49 -05:00
|
|
|
target_module: Module,
|
2021-08-21 09:28:29 -05:00
|
|
|
target: GeneratedFunctionTarget,
|
|
|
|
) -> Option<Self> {
|
|
|
|
let needs_pub =
|
|
|
|
!module_is_descendant(¤t_module(call.syntax(), ctx)?, &target_module, ctx);
|
2021-08-05 04:30:08 -05:00
|
|
|
let fn_name = make::name(&name.text());
|
2021-10-03 05:42:00 -05:00
|
|
|
let (type_params, params) =
|
|
|
|
fn_args(ctx, target_module, ast::CallableExpr::MethodCall(call.clone()))?;
|
2021-08-05 04:30:08 -05:00
|
|
|
|
|
|
|
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
|
|
|
|
let is_async = await_expr.is_some();
|
|
|
|
|
2021-08-13 04:44:29 -05:00
|
|
|
let (ret_type, should_focus_return_type) =
|
2021-08-12 09:05:32 -05:00
|
|
|
make_return_type(ctx, &ast::Expr::MethodCallExpr(call.clone()), target_module);
|
2021-08-05 04:30:08 -05:00
|
|
|
|
2021-08-21 09:28:29 -05:00
|
|
|
Some(Self {
|
|
|
|
target,
|
|
|
|
fn_name,
|
|
|
|
type_params,
|
|
|
|
params,
|
|
|
|
ret_type,
|
|
|
|
should_focus_return_type,
|
|
|
|
needs_pub,
|
|
|
|
is_async,
|
|
|
|
})
|
2021-08-05 04:30:08 -05:00
|
|
|
}
|
|
|
|
|
2021-08-21 08:01:37 -05:00
|
|
|
fn render(self) -> FunctionTemplate {
|
2021-05-09 11:51:06 -05:00
|
|
|
let placeholder_expr = make::ext::expr_todo();
|
2020-05-19 16:12:01 -05:00
|
|
|
let fn_body = make::block_expr(vec![], Some(placeholder_expr));
|
|
|
|
let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
|
2020-10-04 14:21:30 -05:00
|
|
|
let mut fn_def = make::fn_(
|
|
|
|
visibility,
|
|
|
|
self.fn_name,
|
|
|
|
self.type_params,
|
|
|
|
self.params,
|
|
|
|
fn_body,
|
2021-08-12 09:05:32 -05:00
|
|
|
self.ret_type,
|
2021-07-09 08:48:22 -05:00
|
|
|
self.is_async,
|
2020-10-04 14:21:30 -05:00
|
|
|
);
|
2020-05-19 16:12:01 -05:00
|
|
|
let leading_ws;
|
|
|
|
let trailing_ws;
|
|
|
|
|
2021-08-21 08:01:37 -05:00
|
|
|
match self.target {
|
2020-04-16 16:53:25 -05:00
|
|
|
GeneratedFunctionTarget::BehindItem(it) => {
|
2020-05-19 16:12:01 -05:00
|
|
|
let indent = IndentLevel::from_node(&it);
|
|
|
|
leading_ws = format!("\n\n{}", indent);
|
|
|
|
fn_def = fn_def.indent(indent);
|
|
|
|
trailing_ws = String::new();
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
2021-01-20 13:05:48 -06:00
|
|
|
let indent = IndentLevel::from_node(&it);
|
2020-05-19 16:12:01 -05:00
|
|
|
leading_ws = format!("\n{}", indent + 1);
|
|
|
|
fn_def = fn_def.indent(indent + 1);
|
|
|
|
trailing_ws = format!("\n{}", indent);
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-21 08:01:37 -05:00
|
|
|
FunctionTemplate {
|
|
|
|
leading_ws,
|
|
|
|
ret_type: fn_def.ret_type(),
|
|
|
|
// PANIC: we guarantee we always create a function body with a tail expr
|
|
|
|
tail_expr: fn_def.body().unwrap().tail_expr().unwrap(),
|
|
|
|
should_focus_return_type: self.should_focus_return_type,
|
|
|
|
fn_def,
|
|
|
|
trailing_ws,
|
|
|
|
}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-13 04:44:29 -05:00
|
|
|
/// Makes an optional return type along with whether the return type should be focused by the cursor.
|
2021-09-04 03:43:36 -05:00
|
|
|
/// If we cannot infer what the return type should be, we create a placeholder type.
|
2021-08-13 04:44:29 -05:00
|
|
|
///
|
|
|
|
/// The rule for whether we focus a return type or not (and thus focus the function body),
|
|
|
|
/// is rather simple:
|
|
|
|
/// * If we could *not* infer what the return type should be, focus it (so the user can fill-in
|
|
|
|
/// the correct return type).
|
|
|
|
/// * If we could infer the return type, don't focus it (and thus focus the function body) so the
|
|
|
|
/// user can change the `todo!` function body.
|
2021-08-12 09:05:32 -05:00
|
|
|
fn make_return_type(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
call: &ast::Expr,
|
|
|
|
target_module: Module,
|
|
|
|
) -> (Option<ast::RetType>, bool) {
|
2021-08-13 04:44:29 -05:00
|
|
|
let (ret_ty, should_focus_return_type) = {
|
2021-08-12 09:05:32 -05:00
|
|
|
match ctx.sema.type_of_expr(call).map(TypeInfo::original) {
|
2021-09-04 03:43:36 -05:00
|
|
|
Some(ty) if ty.is_unknown() => (Some(make::ty_placeholder()), true),
|
|
|
|
None => (Some(make::ty_placeholder()), true),
|
2021-08-13 04:44:29 -05:00
|
|
|
Some(ty) if ty.is_unit() => (None, false),
|
2021-08-12 09:05:32 -05:00
|
|
|
Some(ty) => {
|
|
|
|
let rendered = ty.display_source_code(ctx.db(), target_module.into());
|
|
|
|
match rendered {
|
|
|
|
Ok(rendered) => (Some(make::ty(&rendered)), false),
|
2021-09-04 03:43:36 -05:00
|
|
|
Err(_) => (Some(make::ty_placeholder()), true),
|
2021-08-12 09:05:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-09-13 11:50:19 -05:00
|
|
|
let ret_type = ret_ty.map(make::ret_type);
|
2021-08-13 04:44:29 -05:00
|
|
|
(ret_type, should_focus_return_type)
|
2021-08-12 09:05:32 -05:00
|
|
|
}
|
|
|
|
|
2021-08-21 09:28:29 -05:00
|
|
|
fn get_fn_target(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
target_module: &Option<Module>,
|
|
|
|
call: CallExpr,
|
|
|
|
) -> Option<(GeneratedFunctionTarget, FileId, TextSize)> {
|
2021-10-13 07:39:37 -05:00
|
|
|
let mut file = ctx.file_id();
|
2021-08-21 09:28:29 -05:00
|
|
|
let target = match target_module {
|
|
|
|
Some(target_module) => {
|
|
|
|
let module_source = target_module.definition_source(ctx.db());
|
|
|
|
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, &module_source)?;
|
|
|
|
file = in_file;
|
|
|
|
target
|
|
|
|
}
|
2021-10-03 05:42:00 -05:00
|
|
|
None => next_space_for_fn_after_call_site(ast::CallableExpr::Call(call))?,
|
2021-08-21 09:28:29 -05:00
|
|
|
};
|
|
|
|
Some((target.clone(), file, get_insert_offset(&target)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_method_target(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
target_module: &Module,
|
|
|
|
impl_: &Option<ast::Impl>,
|
|
|
|
) -> Option<(GeneratedFunctionTarget, TextSize)> {
|
|
|
|
let target = match impl_ {
|
2021-09-13 11:50:19 -05:00
|
|
|
Some(impl_) => next_space_for_fn_in_impl(impl_)?,
|
2021-08-21 09:28:29 -05:00
|
|
|
None => {
|
|
|
|
next_space_for_fn_in_module(ctx.sema.db, &target_module.definition_source(ctx.sema.db))?
|
|
|
|
.1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some((target.clone(), get_insert_offset(&target)))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_insert_offset(target: &GeneratedFunctionTarget) -> TextSize {
|
|
|
|
match &target {
|
|
|
|
GeneratedFunctionTarget::BehindItem(it) => it.text_range().end(),
|
|
|
|
GeneratedFunctionTarget::InEmptyItemList(it) => it.text_range().start() + TextSize::of('{'),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2020-04-16 16:53:25 -05:00
|
|
|
enum GeneratedFunctionTarget {
|
|
|
|
BehindItem(SyntaxNode),
|
2021-01-20 13:05:48 -06:00
|
|
|
InEmptyItemList(SyntaxNode),
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
|
2020-04-25 09:58:28 -05:00
|
|
|
impl GeneratedFunctionTarget {
|
|
|
|
fn syntax(&self) -> &SyntaxNode {
|
|
|
|
match self {
|
|
|
|
GeneratedFunctionTarget::BehindItem(it) => it,
|
2021-01-20 13:05:48 -06:00
|
|
|
GeneratedFunctionTarget::InEmptyItemList(it) => it,
|
2020-04-25 09:58:28 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:02:48 -05:00
|
|
|
/// Computes the type variables and arguments required for the generated function
|
|
|
|
fn fn_args(
|
2020-05-06 11:45:35 -05:00
|
|
|
ctx: &AssistContext,
|
2020-04-25 09:58:28 -05:00
|
|
|
target_module: hir::Module,
|
2021-10-03 05:42:00 -05:00
|
|
|
call: ast::CallableExpr,
|
2020-07-30 08:36:21 -05:00
|
|
|
) -> Option<(Option<ast::GenericParamList>, ast::ParamList)> {
|
2020-03-26 14:59:35 -05:00
|
|
|
let mut arg_names = Vec::new();
|
|
|
|
let mut arg_types = Vec::new();
|
|
|
|
for arg in call.arg_list()?.args() {
|
2021-09-21 17:32:23 -05:00
|
|
|
arg_names.push(fn_arg_name(&ctx.sema, &arg));
|
2021-09-29 07:04:32 -05:00
|
|
|
arg_types.push(fn_arg_type(ctx, target_module, &arg));
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
deduplicate_arg_names(&mut arg_names);
|
2021-07-02 18:33:34 -05:00
|
|
|
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| {
|
|
|
|
make::param(make::ext::simple_ident_pat(make::name(&name)).into(), make::ty(&ty))
|
|
|
|
});
|
2020-03-26 14:59:35 -05:00
|
|
|
|
2021-08-05 07:07:00 -05:00
|
|
|
Some((
|
|
|
|
None,
|
|
|
|
make::param_list(
|
|
|
|
match call {
|
2021-10-03 05:42:00 -05:00
|
|
|
ast::CallableExpr::Call(_) => None,
|
|
|
|
ast::CallableExpr::MethodCall(_) => Some(make::self_param()),
|
2021-08-05 07:07:00 -05:00
|
|
|
},
|
|
|
|
params,
|
|
|
|
),
|
|
|
|
))
|
2021-08-05 04:30:08 -05:00
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
/// Makes duplicate argument names unique by appending incrementing numbers.
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let mut names: Vec<String> =
|
|
|
|
/// vec!["foo".into(), "foo".into(), "bar".into(), "baz".into(), "bar".into()];
|
|
|
|
/// deduplicate_arg_names(&mut names);
|
|
|
|
/// let expected: Vec<String> =
|
|
|
|
/// vec!["foo_1".into(), "foo_2".into(), "bar_1".into(), "baz".into(), "bar_2".into()];
|
|
|
|
/// assert_eq!(names, expected);
|
|
|
|
/// ```
|
|
|
|
fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
|
2022-01-02 04:03:34 -06:00
|
|
|
let mut arg_name_counts = FxHashMap::default();
|
|
|
|
for name in arg_names.iter() {
|
|
|
|
*arg_name_counts.entry(name).or_insert(0) += 1;
|
|
|
|
}
|
2020-03-26 14:59:35 -05:00
|
|
|
let duplicate_arg_names: FxHashSet<String> = arg_name_counts
|
|
|
|
.into_iter()
|
|
|
|
.filter(|(_, count)| *count >= 2)
|
|
|
|
.map(|(name, _)| name.clone())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let mut counter_per_name = FxHashMap::default();
|
|
|
|
for arg_name in arg_names.iter_mut() {
|
|
|
|
if duplicate_arg_names.contains(arg_name) {
|
|
|
|
let counter = counter_per_name.entry(arg_name.clone()).or_insert(1);
|
|
|
|
arg_name.push('_');
|
|
|
|
arg_name.push_str(&counter.to_string());
|
|
|
|
*counter += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-21 17:32:23 -05:00
|
|
|
fn fn_arg_name(sema: &Semantics<RootDatabase>, arg_expr: &ast::Expr) -> String {
|
2021-09-16 13:45:41 -05:00
|
|
|
let name = (|| match arg_expr {
|
2021-09-21 17:32:23 -05:00
|
|
|
ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(sema, &cast_expr.expr()?)),
|
2021-09-16 13:45:41 -05:00
|
|
|
expr => {
|
2022-02-02 04:37:24 -06:00
|
|
|
let name_ref = expr
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.filter_map(ast::NameRef::cast)
|
|
|
|
.filter(|name| name.ident_token().is_some())
|
|
|
|
.last()?;
|
2021-11-10 15:02:50 -06:00
|
|
|
if let Some(NameRefClass::Definition(Definition::Const(_) | Definition::Static(_))) =
|
|
|
|
NameRefClass::classify(sema, &name_ref)
|
2021-09-21 11:15:57 -05:00
|
|
|
{
|
2021-09-21 17:32:23 -05:00
|
|
|
return Some(name_ref.to_string().to_lowercase());
|
2021-09-21 11:15:57 -05:00
|
|
|
};
|
|
|
|
Some(to_lower_snake_case(&name_ref.to_string()))
|
2021-03-06 11:53:21 -06:00
|
|
|
}
|
2021-09-16 13:45:41 -05:00
|
|
|
})();
|
|
|
|
match name {
|
|
|
|
Some(mut name) if name.starts_with(|c: char| c.is_ascii_digit()) => {
|
|
|
|
name.insert_str(0, "arg");
|
|
|
|
name
|
|
|
|
}
|
|
|
|
Some(name) => name,
|
|
|
|
None => "arg".to_string(),
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-29 07:04:32 -05:00
|
|
|
fn fn_arg_type(ctx: &AssistContext, target_module: hir::Module, fn_arg: &ast::Expr) -> String {
|
|
|
|
fn maybe_displayed_type(
|
|
|
|
ctx: &AssistContext,
|
|
|
|
target_module: hir::Module,
|
|
|
|
fn_arg: &ast::Expr,
|
|
|
|
) -> Option<String> {
|
|
|
|
let ty = ctx.sema.type_of_expr(fn_arg)?.adjusted();
|
|
|
|
if ty.is_unknown() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ty.is_reference() || ty.is_mutable_reference() {
|
2021-10-20 14:35:35 -05:00
|
|
|
let famous_defs = &FamousDefs(&ctx.sema, ctx.sema.scope(fn_arg.syntax()).krate());
|
|
|
|
convert_reference_type(ty.strip_references(), ctx.db(), famous_defs)
|
|
|
|
.map(|conversion| conversion.convert_type(ctx.db()))
|
|
|
|
.or_else(|| ty.display_source_code(ctx.db(), target_module.into()).ok())
|
2021-09-29 07:04:32 -05:00
|
|
|
} else {
|
|
|
|
ty.display_source_code(ctx.db(), target_module.into()).ok()
|
|
|
|
}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
2020-04-25 09:58:28 -05:00
|
|
|
|
2021-09-29 07:04:32 -05:00
|
|
|
maybe_displayed_type(ctx, target_module, fn_arg).unwrap_or_else(|| String::from("_"))
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the position inside the current mod or file
|
|
|
|
/// directly after the current block
|
|
|
|
/// We want to write the generated function directly after
|
|
|
|
/// fns, impls or macro calls, but inside mods
|
2021-10-03 05:42:00 -05:00
|
|
|
fn next_space_for_fn_after_call_site(expr: ast::CallableExpr) -> Option<GeneratedFunctionTarget> {
|
2021-08-05 04:30:08 -05:00
|
|
|
let mut ancestors = expr.syntax().ancestors().peekable();
|
|
|
|
let mut last_ancestor: Option<SyntaxNode> = None;
|
|
|
|
while let Some(next_ancestor) = ancestors.next() {
|
|
|
|
match next_ancestor.kind() {
|
|
|
|
SyntaxKind::SOURCE_FILE => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
SyntaxKind::ITEM_LIST => {
|
|
|
|
if ancestors.peek().map(|a| a.kind()) == Some(SyntaxKind::MODULE) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
last_ancestor = Some(next_ancestor);
|
|
|
|
}
|
|
|
|
last_ancestor.map(GeneratedFunctionTarget::BehindItem)
|
|
|
|
}
|
|
|
|
|
2020-04-18 09:12:21 -05:00
|
|
|
fn next_space_for_fn_in_module(
|
|
|
|
db: &dyn hir::db::AstDatabase,
|
2020-04-25 09:58:28 -05:00
|
|
|
module_source: &hir::InFile<hir::ModuleSource>,
|
2020-05-06 09:39:11 -05:00
|
|
|
) -> Option<(FileId, GeneratedFunctionTarget)> {
|
2020-04-25 09:58:28 -05:00
|
|
|
let file = module_source.file_id.original_file(db);
|
|
|
|
let assist_item = match &module_source.value {
|
2021-10-03 07:53:01 -05:00
|
|
|
hir::ModuleSource::SourceFile(it) => match it.items().last() {
|
|
|
|
Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
|
|
|
|
None => GeneratedFunctionTarget::BehindItem(it.syntax().clone()),
|
|
|
|
},
|
|
|
|
hir::ModuleSource::Module(it) => match it.item_list().and_then(|it| it.items().last()) {
|
|
|
|
Some(last_item) => GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()),
|
|
|
|
None => GeneratedFunctionTarget::InEmptyItemList(it.item_list()?.syntax().clone()),
|
|
|
|
},
|
2021-01-20 13:05:48 -06:00
|
|
|
hir::ModuleSource::BlockExpr(it) => {
|
|
|
|
if let Some(last_item) =
|
|
|
|
it.statements().take_while(|stmt| matches!(stmt, ast::Stmt::Item(_))).last()
|
|
|
|
{
|
|
|
|
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
|
|
|
|
} else {
|
|
|
|
GeneratedFunctionTarget::InEmptyItemList(it.syntax().clone())
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 09:12:21 -05:00
|
|
|
};
|
2020-05-06 09:39:11 -05:00
|
|
|
Some((file, assist_item))
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
2021-08-07 01:59:49 -05:00
|
|
|
fn next_space_for_fn_in_impl(impl_: &ast::Impl) -> Option<GeneratedFunctionTarget> {
|
|
|
|
if let Some(last_item) = impl_.assoc_item_list().and_then(|it| it.assoc_items().last()) {
|
|
|
|
Some(GeneratedFunctionTarget::BehindItem(last_item.syntax().clone()))
|
|
|
|
} else {
|
|
|
|
Some(GeneratedFunctionTarget::InEmptyItemList(impl_.assoc_item_list()?.syntax().clone()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-07 04:29:33 -05:00
|
|
|
fn module_is_descendant(module: &hir::Module, ans: &hir::Module, ctx: &AssistContext) -> bool {
|
|
|
|
if module == ans {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for c in ans.children(ctx.sema.db) {
|
|
|
|
if module_is_descendant(module, &c, ctx) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2020-03-26 14:59:35 -05:00
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_no_args() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0();
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_from_method() {
|
|
|
|
// This ensures that the function is correctly generated
|
|
|
|
// in the next outer mod or file
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0();
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_directly_after_current_block() {
|
|
|
|
// The new fn should not be created at the end of the file or module
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo1() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0();
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn foo2() {}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo1() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn foo2() {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_no_args_in_same_module() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
mod baz {
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0();
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod baz {
|
|
|
|
fn foo() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:53:21 -06:00
|
|
|
#[test]
|
|
|
|
fn add_function_with_upper_camel_case_arg() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
struct BazBaz;
|
|
|
|
fn foo() {
|
|
|
|
bar$0(BazBaz);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct BazBaz;
|
|
|
|
fn foo() {
|
|
|
|
bar(BazBaz);
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(baz_baz: BazBaz) ${0:-> _} {
|
2021-03-06 11:53:21 -06:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_upper_camel_case_arg_as_cast() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
struct BazBaz;
|
|
|
|
fn foo() {
|
|
|
|
bar$0(&BazBaz as *const BazBaz);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct BazBaz;
|
|
|
|
fn foo() {
|
|
|
|
bar(&BazBaz as *const BazBaz);
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(baz_baz: *const BazBaz) ${0:-> _} {
|
2021-03-06 11:53:21 -06:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_with_function_call_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
struct Baz;
|
2020-04-13 15:17:06 -05:00
|
|
|
fn baz() -> Baz { todo!() }
|
2020-03-26 14:59:35 -05:00
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0(baz());
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
2020-04-13 15:17:06 -05:00
|
|
|
fn baz() -> Baz { todo!() }
|
2020-03-26 14:59:35 -05:00
|
|
|
fn foo() {
|
|
|
|
bar(baz());
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(baz: Baz) ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_method_call_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn foo(&self) -> Baz {
|
2021-01-06 14:15:48 -06:00
|
|
|
ba$0r(self.baz())
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
fn baz(&self) -> Baz {
|
|
|
|
Baz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn foo(&self) -> Baz {
|
|
|
|
bar(self.baz())
|
|
|
|
}
|
|
|
|
fn baz(&self) -> Baz {
|
|
|
|
Baz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 16:29:45 -06:00
|
|
|
fn bar(baz: Baz) -> Baz {
|
2021-08-12 07:56:39 -05:00
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_string_literal_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar("bar")
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
bar("bar")
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(arg: &str) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_char_literal_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar('x')
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
bar('x')
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(arg: char) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_int_literal_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(42)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(42)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(arg: i32) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_cast_int_literal_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(42 as u8)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(42 as u8)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(arg: u8) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn name_of_cast_variable_is_used() {
|
|
|
|
// Ensures that the name of the cast type isn't used
|
|
|
|
// in the generated function signature.
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x = 42;
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0(x as u8)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x = 42;
|
|
|
|
bar(x as u8)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(x: u8) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_variable_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let worble = ();
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(worble)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let worble = ();
|
|
|
|
bar(worble)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(worble: ()) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_impl_trait_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2021-06-15 17:23:04 -05:00
|
|
|
r#"
|
2021-08-12 09:31:00 -05:00
|
|
|
//- minicore: sized
|
2020-03-26 14:59:35 -05:00
|
|
|
trait Foo {}
|
|
|
|
fn foo() -> impl Foo {
|
2020-04-13 15:17:06 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
fn baz() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(foo())
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
2021-06-15 17:23:04 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2020-03-26 14:59:35 -05:00
|
|
|
trait Foo {}
|
|
|
|
fn foo() -> impl Foo {
|
2020-04-13 15:17:06 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
fn baz() {
|
|
|
|
bar(foo())
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(foo: impl Foo) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
2021-06-15 17:23:04 -05:00
|
|
|
"#,
|
2020-03-26 14:59:35 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-04-25 09:58:28 -05:00
|
|
|
fn borrowed_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-04-25 09:58:28 -05:00
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { todo!() }
|
|
|
|
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0(&baz())
|
2020-04-25 09:58:28 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { todo!() }
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar(&baz())
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(baz: &Baz) {
|
|
|
|
${0:todo!()}
|
2020-04-25 09:58:28 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-26 14:59:35 -05:00
|
|
|
fn add_function_with_qualified_path_arg() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
mod Baz {
|
|
|
|
pub struct Bof;
|
|
|
|
pub fn baz() -> Bof { Bof }
|
|
|
|
}
|
2020-04-25 09:58:28 -05:00
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(Baz::baz())
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod Baz {
|
|
|
|
pub struct Bof;
|
|
|
|
pub fn baz() -> Bof { Bof }
|
|
|
|
}
|
2020-04-25 09:58:28 -05:00
|
|
|
fn foo() {
|
|
|
|
bar(Baz::baz())
|
|
|
|
}
|
2020-03-26 14:59:35 -05:00
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(baz: Baz::Bof) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_generic_arg() {
|
2021-06-15 08:37:58 -05:00
|
|
|
// FIXME: This is wrong, generated `bar` should include generic parameter.
|
2020-03-26 14:59:35 -05:00
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo<T>(t: T) {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(t)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo<T>(t: T) {
|
|
|
|
bar(t)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(t: T) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_fn_arg() {
|
2021-06-15 08:37:58 -05:00
|
|
|
// FIXME: The argument in `bar` is wrong.
|
2020-03-26 14:59:35 -05:00
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn new() -> Self { Baz }
|
|
|
|
}
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(Baz::new);
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn new() -> Self { Baz }
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
bar(Baz::new);
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(new: fn) ${0:-> _} {
|
2020-10-04 14:21:30 -05:00
|
|
|
todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_closure_arg() {
|
2021-06-15 08:37:58 -05:00
|
|
|
// FIXME: The argument in `bar` is wrong.
|
2020-03-26 14:59:35 -05:00
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let closure = |x: i64| x - 1;
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(closure)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let closure = |x: i64| x - 1;
|
|
|
|
bar(closure)
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(closure: _) {
|
2021-08-12 09:05:32 -05:00
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-09-04 03:43:36 -05:00
|
|
|
fn unresolveable_types_default_to_placeholder() {
|
2020-03-26 14:59:35 -05:00
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(baz)
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(baz)
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(baz: _) {
|
2021-08-12 09:05:32 -05:00
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_names_dont_overlap() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(baz(), baz())
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
|
|
|
bar(baz(), baz())
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(baz_1: Baz, baz_2: Baz) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_name_counters_start_at_1_per_name() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r#"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0bar(baz(), baz(), "foo", "bar")
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
|
|
|
bar(baz(), baz(), "foo", "bar")
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
|
|
|
|
${0:todo!()}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:53:25 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_in_module() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-04-16 16:53:25 -05:00
|
|
|
r"
|
|
|
|
mod bar {}
|
|
|
|
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar::my_fn$0()
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod bar {
|
2021-08-12 09:05:32 -05:00
|
|
|
pub(crate) fn my_fn() {
|
|
|
|
${0:todo!()}
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::my_fn()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-25 09:58:28 -05:00
|
|
|
#[test]
|
|
|
|
fn qualified_path_uses_correct_scope() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2021-06-15 08:37:58 -05:00
|
|
|
r#"
|
2020-04-25 09:58:28 -05:00
|
|
|
mod foo {
|
|
|
|
pub struct Foo;
|
|
|
|
}
|
|
|
|
fn bar() {
|
|
|
|
use foo::Foo;
|
|
|
|
let foo = Foo;
|
2021-01-06 14:15:48 -06:00
|
|
|
baz$0(foo)
|
2020-04-25 09:58:28 -05:00
|
|
|
}
|
2021-06-15 08:37:58 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
2020-04-25 09:58:28 -05:00
|
|
|
mod foo {
|
|
|
|
pub struct Foo;
|
|
|
|
}
|
|
|
|
fn bar() {
|
|
|
|
use foo::Foo;
|
|
|
|
let foo = Foo;
|
|
|
|
baz(foo)
|
|
|
|
}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
fn baz(foo: foo::Foo) {
|
|
|
|
${0:todo!()}
|
2020-04-25 09:58:28 -05:00
|
|
|
}
|
2021-06-15 08:37:58 -05:00
|
|
|
"#,
|
2020-04-25 09:58:28 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:53:25 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_in_module_containing_other_items() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-04-16 16:53:25 -05:00
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
fn something_else() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar::my_fn$0()
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
fn something_else() {}
|
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
pub(crate) fn my_fn() {
|
|
|
|
${0:todo!()}
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::my_fn()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_in_nested_module() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-04-16 16:53:25 -05:00
|
|
|
r"
|
|
|
|
mod bar {
|
2020-04-20 11:36:12 -05:00
|
|
|
mod baz {}
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar::baz::my_fn$0()
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
mod baz {
|
2021-08-12 09:05:32 -05:00
|
|
|
pub(crate) fn my_fn() {
|
|
|
|
${0:todo!()}
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::baz::my_fn()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-18 09:12:21 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_in_another_file() {
|
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-04-18 09:12:21 -05:00
|
|
|
r"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
fn main() {
|
2021-01-06 14:15:48 -06:00
|
|
|
foo::bar$0()
|
2020-04-18 09:12:21 -05:00
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
|
2020-06-23 17:30:34 -05:00
|
|
|
|
2021-08-12 09:05:32 -05:00
|
|
|
pub(crate) fn bar() {
|
|
|
|
${0:todo!()}
|
2020-04-20 11:02:36 -05:00
|
|
|
}",
|
2020-04-18 09:12:21 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-06 16:03:55 -06:00
|
|
|
#[test]
|
|
|
|
fn add_function_with_return_type() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
fn main() {
|
|
|
|
let x: u32 = foo$0();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn main() {
|
|
|
|
let x: u32 = foo();
|
|
|
|
}
|
|
|
|
|
2021-03-06 16:29:45 -06:00
|
|
|
fn foo() -> u32 {
|
2021-08-12 07:56:39 -05:00
|
|
|
${0:todo!()}
|
2021-03-06 16:03:55 -06:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_not_applicable_if_function_already_exists() {
|
|
|
|
check_assist_not_applicable(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2020-03-26 14:59:35 -05:00
|
|
|
r"
|
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar$0();
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_not_applicable_if_unresolved_variable_in_call_is_selected() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
// bar is resolved, but baz isn't.
|
|
|
|
// The assist is only active if the cursor is on an unresolved path,
|
|
|
|
// but the assist should only be offered if the path is a function call.
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2021-06-15 08:37:58 -05:00
|
|
|
r#"
|
2020-03-26 14:59:35 -05:00
|
|
|
fn foo() {
|
2021-01-06 14:15:48 -06:00
|
|
|
bar(b$0az);
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz: ()) {}
|
2021-06-15 08:37:58 -05:00
|
|
|
"#,
|
2020-03-26 14:59:35 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_method_with_no_args() {
|
2021-08-09 11:07:07 -05:00
|
|
|
check_assist(
|
2020-07-03 11:15:03 -05:00
|
|
|
generate_function,
|
2021-06-15 08:37:58 -05:00
|
|
|
r#"
|
2020-03-26 14:59:35 -05:00
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
2021-01-06 14:15:48 -06:00
|
|
|
self.bar()$0;
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
2021-08-09 11:07:07 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self.bar();
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(&self) ${0:-> _} {
|
2021-08-09 11:07:07 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
2020-03-26 14:59:35 -05:00
|
|
|
)
|
|
|
|
}
|
2021-07-09 08:48:22 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_function_with_async() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
$0bar(42).await();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(42).await();
|
|
|
|
}
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
async fn bar(arg: i32) ${0:-> _} {
|
2021-07-09 08:48:22 -05:00
|
|
|
todo!()
|
|
|
|
}
|
2021-08-09 09:25:10 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_method() {
|
|
|
|
check_assist(
|
2021-08-09 11:07:07 -05:00
|
|
|
generate_function,
|
2021-08-09 09:25:10 -05:00
|
|
|
r"
|
|
|
|
struct S;
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {S.bar$0();}
|
2021-08-09 09:25:10 -05:00
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {S.bar();}
|
2021-08-09 09:25:10 -05:00
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(&self) ${0:-> _} {
|
2021-08-09 09:25:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_method_within_an_impl() {
|
|
|
|
check_assist(
|
2021-08-09 11:07:07 -05:00
|
|
|
generate_function,
|
2021-08-09 09:25:10 -05:00
|
|
|
r"
|
|
|
|
struct S;
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {S.bar$0();}
|
2021-08-09 09:25:10 -05:00
|
|
|
impl S {}
|
|
|
|
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {S.bar();}
|
2021-08-09 09:25:10 -05:00
|
|
|
impl S {
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(&self) ${0:-> _} {
|
2021-08-09 09:25:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_method_from_different_module() {
|
|
|
|
check_assist(
|
2021-08-09 11:07:07 -05:00
|
|
|
generate_function,
|
2021-08-09 09:25:10 -05:00
|
|
|
r"
|
|
|
|
mod s {
|
|
|
|
pub struct S;
|
|
|
|
}
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {s::S.bar$0();}
|
2021-08-09 09:25:10 -05:00
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod s {
|
|
|
|
pub struct S;
|
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
pub(crate) fn bar(&self) ${0:-> _} {
|
2021-08-09 09:25:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {s::S.bar();}
|
2021-08-09 09:25:10 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_method_from_descendant_module() {
|
|
|
|
check_assist(
|
2021-08-09 11:07:07 -05:00
|
|
|
generate_function,
|
2021-08-09 09:25:10 -05:00
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
mod s {
|
|
|
|
fn foo() {
|
|
|
|
super::S.bar$0();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
mod s {
|
|
|
|
fn foo() {
|
|
|
|
super::S.bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(&self) ${0:-> _} {
|
2021-08-09 09:51:15 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_method_with_cursor_anywhere_on_call_expresion() {
|
|
|
|
check_assist(
|
2021-08-09 11:07:07 -05:00
|
|
|
generate_function,
|
2021-08-09 09:51:15 -05:00
|
|
|
r"
|
|
|
|
struct S;
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {$0S.bar();}
|
2021-08-09 09:51:15 -05:00
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
2021-08-09 11:07:07 -05:00
|
|
|
fn foo() {S.bar();}
|
2021-08-09 09:51:15 -05:00
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar(&self) ${0:-> _} {
|
2021-08-09 09:25:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
2021-08-23 02:30:10 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_static_method() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
fn foo() {S::bar$0();}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
fn foo() {S::bar();}
|
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2021-08-23 02:30:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_static_method_within_an_impl() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
fn foo() {S::bar$0();}
|
|
|
|
impl S {}
|
|
|
|
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
fn foo() {S::bar();}
|
|
|
|
impl S {
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2021-08-23 02:30:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_static_method_from_different_module() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
mod s {
|
|
|
|
pub struct S;
|
|
|
|
}
|
|
|
|
fn foo() {s::S::bar$0();}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod s {
|
|
|
|
pub struct S;
|
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
pub(crate) fn bar() ${0:-> _} {
|
2021-08-23 02:30:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn foo() {s::S::bar();}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn create_static_method_with_cursor_anywhere_on_call_expresion() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
fn foo() {$0S::bar();}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct S;
|
|
|
|
fn foo() {S::bar();}
|
|
|
|
impl S {
|
|
|
|
|
|
|
|
|
2021-09-04 03:43:36 -05:00
|
|
|
fn bar() ${0:-> _} {
|
2021-08-23 02:30:10 -05:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 08:48:22 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
2021-09-16 11:03:37 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_panic_on_invalid_global_path() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
2021-09-16 13:45:41 -05:00
|
|
|
r"
|
2021-09-16 11:03:37 -05:00
|
|
|
fn main() {
|
|
|
|
::foo$0();
|
|
|
|
}
|
2021-09-16 13:45:41 -05:00
|
|
|
",
|
|
|
|
r"
|
2021-09-16 11:03:37 -05:00
|
|
|
fn main() {
|
|
|
|
::foo();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() ${0:-> _} {
|
|
|
|
todo!()
|
|
|
|
}
|
2021-09-16 13:45:41 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn handle_tuple_indexing() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
fn main() {
|
|
|
|
let a = ((),);
|
|
|
|
foo$0(a.0);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn main() {
|
|
|
|
let a = ((),);
|
|
|
|
foo(a.0);
|
|
|
|
}
|
|
|
|
|
2022-02-02 05:42:13 -06:00
|
|
|
fn foo(a: ()) ${0:-> _} {
|
2021-09-16 13:45:41 -05:00
|
|
|
todo!()
|
|
|
|
}
|
2021-09-21 11:15:57 -05:00
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_const_arg() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
const VALUE: usize = 0;
|
|
|
|
fn main() {
|
|
|
|
foo$0(VALUE);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
const VALUE: usize = 0;
|
|
|
|
fn main() {
|
|
|
|
foo(VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(value: usize) ${0:-> _} {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_static_arg() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
static VALUE: usize = 0;
|
|
|
|
fn main() {
|
|
|
|
foo$0(VALUE);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
static VALUE: usize = 0;
|
|
|
|
fn main() {
|
|
|
|
foo(VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(value: usize) ${0:-> _} {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_static_mut_arg() {
|
|
|
|
check_assist(
|
|
|
|
generate_function,
|
|
|
|
r"
|
|
|
|
static mut VALUE: usize = 0;
|
|
|
|
fn main() {
|
|
|
|
foo$0(VALUE);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
static mut VALUE: usize = 0;
|
|
|
|
fn main() {
|
|
|
|
foo(VALUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(value: usize) ${0:-> _} {
|
|
|
|
todo!()
|
|
|
|
}
|
2021-09-16 13:45:41 -05:00
|
|
|
",
|
2021-09-16 11:03:37 -05:00
|
|
|
)
|
|
|
|
}
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|