2020-03-26 14:59:35 -05:00
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode},
|
2020-04-24 16:40:41 -05:00
|
|
|
SyntaxKind, SyntaxNode, TextSize,
|
2020-03-26 14:59:35 -05:00
|
|
|
};
|
|
|
|
|
2020-04-18 09:12:21 -05:00
|
|
|
use crate::{Assist, AssistCtx, AssistFile, AssistId};
|
2020-04-16 16:53:25 -05:00
|
|
|
use ast::{edit::IndentLevel, ArgListOwner, ModuleItemOwner};
|
2020-03-26 14:59:35 -05:00
|
|
|
use hir::HirDisplay;
|
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
|
|
|
|
|
|
|
// Assist: add_function
|
|
|
|
//
|
|
|
|
// Adds a stub function with a signature matching the function under the cursor.
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// 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
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// 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
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fn bar(arg: &str, baz: Baz) {
|
2020-04-13 15:17:06 -05:00
|
|
|
// todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
// }
|
|
|
|
//
|
|
|
|
// ```
|
|
|
|
pub(crate) fn add_function(ctx: AssistCtx) -> Option<Assist> {
|
|
|
|
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()?;
|
|
|
|
|
|
|
|
if ctx.sema.resolve_path(&path).is_some() {
|
|
|
|
// The function call already resolves, no need to add a function
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:53:25 -05:00
|
|
|
let target_module = if let Some(qualifier) = path.qualifier() {
|
2020-04-18 09:12:21 -05:00
|
|
|
if let Some(hir::PathResolution::Def(hir::ModuleDef::Module(module))) =
|
2020-04-16 16:53:25 -05:00
|
|
|
ctx.sema.resolve_path(&qualifier)
|
|
|
|
{
|
2020-04-18 09:12:21 -05:00
|
|
|
Some(module.definition_source(ctx.sema.db))
|
2020-04-16 16:53:25 -05:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
|
2020-03-26 14:59:35 -05:00
|
|
|
|
|
|
|
ctx.add_assist(AssistId("add_function"), "Add function", |edit| {
|
|
|
|
edit.target(call.syntax().text_range());
|
|
|
|
|
2020-03-31 16:02:48 -05:00
|
|
|
if let Some(function_template) = function_builder.render() {
|
2020-04-18 09:12:21 -05:00
|
|
|
edit.set_file(function_template.file);
|
2020-03-31 16:02:48 -05:00
|
|
|
edit.set_cursor(function_template.cursor_offset);
|
|
|
|
edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
|
|
|
|
}
|
2020-03-26 14:59:35 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FunctionTemplate {
|
2020-04-24 16:40:41 -05:00
|
|
|
insert_offset: TextSize,
|
|
|
|
cursor_offset: TextSize,
|
2020-03-31 16:02:48 -05:00
|
|
|
fn_def: ast::SourceFile,
|
2020-04-18 09:12:21 -05:00
|
|
|
file: AssistFile,
|
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,
|
|
|
|
type_params: Option<ast::TypeParamList>,
|
|
|
|
params: ast::ParamList,
|
2020-04-18 09:12:21 -05:00
|
|
|
file: AssistFile,
|
2020-04-20 11:36:12 -05:00
|
|
|
needs_pub: bool,
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FunctionBuilder {
|
2020-04-16 16:53:25 -05:00
|
|
|
/// 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,
|
|
|
|
call: &ast::CallExpr,
|
|
|
|
path: &ast::Path,
|
2020-04-20 11:36:12 -05:00
|
|
|
target_module: Option<hir::InFile<hir::ModuleSource>>,
|
2020-04-16 16:53:25 -05:00
|
|
|
) -> Option<Self> {
|
2020-04-20 11:36:12 -05:00
|
|
|
let needs_pub = target_module.is_some();
|
2020-04-18 09:12:21 -05:00
|
|
|
let mut file = AssistFile::default();
|
2020-04-20 11:36:12 -05:00
|
|
|
let target = if let Some(target_module) = target_module {
|
|
|
|
let (in_file, target) = next_space_for_fn_in_module(ctx.sema.db, target_module)?;
|
2020-04-18 09:12:21 -05:00
|
|
|
file = in_file;
|
|
|
|
target
|
2020-04-16 16:53:25 -05:00
|
|
|
} else {
|
|
|
|
next_space_for_fn_after_call_site(&call)?
|
|
|
|
};
|
|
|
|
let fn_name = fn_name(&path)?;
|
2020-03-31 16:02:48 -05:00
|
|
|
let (type_params, params) = fn_args(ctx, &call)?;
|
2020-04-20 11:36:12 -05:00
|
|
|
Some(Self { target, fn_name, type_params, params, file, needs_pub })
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
2020-04-20 11:36:12 -05:00
|
|
|
|
2020-03-31 16:02:48 -05:00
|
|
|
fn render(self) -> Option<FunctionTemplate> {
|
2020-04-13 15:17:06 -05:00
|
|
|
let placeholder_expr = ast::make::expr_todo();
|
2020-03-31 16:02:48 -05:00
|
|
|
let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr));
|
2020-04-20 11:36:12 -05:00
|
|
|
let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body);
|
|
|
|
if self.needs_pub {
|
|
|
|
fn_def = ast::make::add_pub_crate_modifier(fn_def);
|
|
|
|
}
|
2020-04-16 16:53:25 -05:00
|
|
|
|
|
|
|
let (fn_def, insert_offset) = match self.target {
|
|
|
|
GeneratedFunctionTarget::BehindItem(it) => {
|
|
|
|
let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def);
|
|
|
|
let indented = IndentLevel::from_node(&it).increase_indent(with_leading_blank_line);
|
|
|
|
(indented, it.text_range().end())
|
|
|
|
}
|
|
|
|
GeneratedFunctionTarget::InEmptyItemList(it) => {
|
2020-04-20 11:36:12 -05:00
|
|
|
let indent_once = IndentLevel(1);
|
|
|
|
let indent = IndentLevel::from_node(it.syntax());
|
|
|
|
|
|
|
|
let fn_def = ast::make::add_leading_newlines(1, fn_def);
|
|
|
|
let fn_def = indent_once.increase_indent(fn_def);
|
|
|
|
let fn_def = ast::make::add_trailing_newlines(1, fn_def);
|
|
|
|
let fn_def = indent.increase_indent(fn_def);
|
2020-04-24 17:57:47 -05:00
|
|
|
(fn_def, it.syntax().text_range().start() + TextSize::of('{'))
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-31 16:02:48 -05:00
|
|
|
let cursor_offset_from_fn_start = fn_def
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::MacroCall::cast)?
|
|
|
|
.syntax()
|
|
|
|
.text_range()
|
|
|
|
.start();
|
|
|
|
let cursor_offset = insert_offset + cursor_offset_from_fn_start;
|
2020-04-18 09:12:21 -05:00
|
|
|
Some(FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file })
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:53:25 -05:00
|
|
|
enum GeneratedFunctionTarget {
|
|
|
|
BehindItem(SyntaxNode),
|
|
|
|
InEmptyItemList(ast::ItemList),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_name(call: &ast::Path) -> Option<ast::Name> {
|
|
|
|
let name = call.segment()?.syntax().to_string();
|
2020-03-31 16:02:48 -05:00
|
|
|
Some(ast::make::name(&name))
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
2020-03-31 16:02:48 -05:00
|
|
|
/// Computes the type variables and arguments required for the generated function
|
|
|
|
fn fn_args(
|
|
|
|
ctx: &AssistCtx,
|
2020-04-16 16:53:25 -05:00
|
|
|
call: &ast::CallExpr,
|
2020-03-31 16:02:48 -05:00
|
|
|
) -> Option<(Option<ast::TypeParamList>, 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() {
|
|
|
|
let arg_name = match fn_arg_name(&arg) {
|
|
|
|
Some(name) => name,
|
|
|
|
None => String::from("arg"),
|
|
|
|
};
|
|
|
|
arg_names.push(arg_name);
|
|
|
|
arg_types.push(match fn_arg_type(ctx, &arg) {
|
|
|
|
Some(ty) => ty,
|
|
|
|
None => String::from("()"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
deduplicate_arg_names(&mut arg_names);
|
2020-03-31 16:02:48 -05:00
|
|
|
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| ast::make::param(name, ty));
|
|
|
|
Some((None, ast::make::param_list(params)))
|
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>) {
|
|
|
|
let arg_name_counts = arg_names.iter().fold(FxHashMap::default(), |mut m, name| {
|
|
|
|
*m.entry(name).or_insert(0) += 1;
|
|
|
|
m
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:53:25 -05:00
|
|
|
fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
|
2020-03-26 14:59:35 -05:00
|
|
|
match fn_arg {
|
2020-04-16 16:53:25 -05:00
|
|
|
ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
|
2020-03-26 14:59:35 -05:00
|
|
|
_ => Some(
|
|
|
|
fn_arg
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.filter(|d| ast::NameRef::can_cast(d.kind()))
|
|
|
|
.last()?
|
|
|
|
.to_string(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 16:53:25 -05:00
|
|
|
fn fn_arg_type(ctx: &AssistCtx, fn_arg: &ast::Expr) -> Option<String> {
|
2020-03-26 14:59:35 -05:00
|
|
|
let ty = ctx.sema.type_of_expr(fn_arg)?;
|
|
|
|
if ty.is_unknown() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(ty.display(ctx.sema.db).to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2020-04-16 16:53:25 -05:00
|
|
|
fn next_space_for_fn_after_call_site(expr: &ast::CallExpr) -> Option<GeneratedFunctionTarget> {
|
2020-03-26 14:59:35 -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);
|
|
|
|
}
|
2020-04-16 16:53:25 -05:00
|
|
|
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,
|
|
|
|
module: hir::InFile<hir::ModuleSource>,
|
|
|
|
) -> Option<(AssistFile, GeneratedFunctionTarget)> {
|
|
|
|
let file = module.file_id.original_file(db);
|
|
|
|
let assist_file = AssistFile::TargetFile(file);
|
|
|
|
let assist_item = match module.value {
|
2020-04-16 16:53:25 -05:00
|
|
|
hir::ModuleSource::SourceFile(it) => {
|
|
|
|
if let Some(last_item) = it.items().last() {
|
2020-04-18 09:12:21 -05:00
|
|
|
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
|
2020-04-16 16:53:25 -05:00
|
|
|
} else {
|
2020-04-18 09:12:21 -05:00
|
|
|
GeneratedFunctionTarget::BehindItem(it.syntax().clone())
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
hir::ModuleSource::Module(it) => {
|
|
|
|
if let Some(last_item) = it.item_list().and_then(|it| it.items().last()) {
|
2020-04-18 09:12:21 -05:00
|
|
|
GeneratedFunctionTarget::BehindItem(last_item.syntax().clone())
|
2020-04-16 16:53:25 -05:00
|
|
|
} else {
|
2020-04-18 09:12:21 -05:00
|
|
|
GeneratedFunctionTarget::InEmptyItemList(it.item_list()?)
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-18 09:12:21 -05:00
|
|
|
};
|
|
|
|
Some((assist_file, assist_item))
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_no_args() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar<|>();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
2020-04-13 15:17:06 -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(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
|
|
|
bar<|>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
impl Foo {
|
|
|
|
fn foo() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
2020-04-13 15:17:06 -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(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo1() {
|
|
|
|
bar<|>();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo2() {}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo1() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
2020-04-13 15:17:06 -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(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
mod baz {
|
|
|
|
fn foo() {
|
|
|
|
bar<|>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod baz {
|
|
|
|
fn foo() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_function_call_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
",
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz: Baz) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_method_call_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn foo(&self) -> Baz {
|
|
|
|
ba<|>r(self.baz())
|
|
|
|
}
|
|
|
|
fn baz(&self) -> Baz {
|
|
|
|
Baz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn foo(&self) -> Baz {
|
|
|
|
bar(self.baz())
|
|
|
|
}
|
|
|
|
fn baz(&self) -> Baz {
|
|
|
|
Baz
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz: Baz) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_string_literal_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
<|>bar("bar")
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
bar("bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(arg: &str) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_char_literal_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
<|>bar('x')
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
bar('x')
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(arg: char) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_int_literal_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(42)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(42)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(arg: i32) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_cast_int_literal_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(42 as u8)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(42 as u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(arg: u8) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>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(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x = 42;
|
|
|
|
bar<|>(x as u8)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let x = 42;
|
|
|
|
bar(x as u8)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(x: u8) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_variable_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let worble = ();
|
|
|
|
<|>bar(worble)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let worble = ();
|
|
|
|
bar(worble)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(worble: ()) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_with_impl_trait_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(foo: impl Foo) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
// FIXME print paths properly to make this test pass
|
|
|
|
fn add_function_with_qualified_path_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
mod Baz {
|
|
|
|
pub struct Bof;
|
|
|
|
pub fn baz() -> Bof { Bof }
|
|
|
|
}
|
|
|
|
mod Foo {
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(super::Baz::baz())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod Baz {
|
|
|
|
pub struct Bof;
|
|
|
|
pub fn baz() -> Bof { Bof }
|
|
|
|
}
|
|
|
|
mod Foo {
|
|
|
|
fn foo() {
|
|
|
|
bar(super::Baz::baz())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz: super::Baz::Bof) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
// FIXME fix printing the generics of a `Ty` to make this test pass
|
|
|
|
fn add_function_with_generic_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo<T>(t: T) {
|
|
|
|
<|>bar(t)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo<T>(t: T) {
|
|
|
|
bar(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar<T>(t: T) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
// FIXME Fix function type printing to make this test pass
|
|
|
|
fn add_function_with_fn_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn new() -> Self { Baz }
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(Baz::new);
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
impl Baz {
|
|
|
|
fn new() -> Self { Baz }
|
|
|
|
}
|
|
|
|
fn foo() {
|
|
|
|
bar(Baz::new);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(arg: fn() -> Baz) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
// FIXME Fix closure type printing to make this test pass
|
|
|
|
fn add_function_with_closure_arg() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let closure = |x: i64| x - 1;
|
|
|
|
<|>bar(closure)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
let closure = |x: i64| x - 1;
|
|
|
|
bar(closure)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(closure: impl Fn(i64) -> i64) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unresolveable_types_default_to_unit() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(baz)
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(baz)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz: ()) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_names_dont_overlap() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(baz(), baz())
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
|
|
|
bar(baz(), baz())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz_1: Baz, baz_2: Baz) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>todo!()
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn arg_name_counters_start_at_1_per_name() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r#"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
|
|
|
<|>bar(baz(), baz(), "foo", "bar")
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Baz;
|
|
|
|
fn baz() -> Baz { Baz }
|
|
|
|
fn foo() {
|
|
|
|
bar(baz(), baz(), "foo", "bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
|
2020-04-13 15:17:06 -05:00
|
|
|
<|>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(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
mod bar {}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::my_fn<|>()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod bar {
|
2020-04-20 11:36:12 -05:00
|
|
|
pub(crate) fn my_fn() {
|
2020-04-16 16:53:25 -05:00
|
|
|
<|>todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::my_fn()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_in_module_containing_other_items() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
fn something_else() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::my_fn<|>()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
fn something_else() {}
|
|
|
|
|
2020-04-20 11:36:12 -05:00
|
|
|
pub(crate) fn my_fn() {
|
2020-04-16 16:53:25 -05:00
|
|
|
<|>todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::my_fn()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_in_nested_module() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
mod bar {
|
2020-04-20 11:36:12 -05:00
|
|
|
mod baz {}
|
2020-04-16 16:53:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::baz::my_fn<|>()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
mod bar {
|
|
|
|
mod baz {
|
2020-04-20 11:36:12 -05:00
|
|
|
pub(crate) fn my_fn() {
|
2020-04-16 16:53:25 -05:00
|
|
|
<|>todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
bar::baz::my_fn()
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-04-18 09:12:21 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_in_another_file() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
//- /main.rs
|
|
|
|
mod foo;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
foo::bar<|>()
|
|
|
|
}
|
|
|
|
//- /foo.rs
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
|
|
|
|
|
2020-04-20 11:02:36 -05:00
|
|
|
pub(crate) fn bar() {
|
2020-04-18 09:12:21 -05:00
|
|
|
<|>todo!()
|
2020-04-20 11:02:36 -05:00
|
|
|
}",
|
2020-04-18 09:12:21 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:59:35 -05:00
|
|
|
#[test]
|
|
|
|
fn add_function_not_applicable_if_function_already_exists() {
|
|
|
|
check_assist_not_applicable(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar<|>();
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
bar(b<|>az);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bar(baz: ()) {}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn add_function_not_applicable_if_function_path_not_singleton() {
|
|
|
|
// In the future this assist could be extended to generate functions
|
|
|
|
// if the path is in the same crate (or even the same workspace).
|
|
|
|
// For the beginning, I think this is fine.
|
|
|
|
check_assist_not_applicable(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
fn foo() {
|
|
|
|
other_crate::bar<|>();
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn create_method_with_no_args() {
|
|
|
|
check_assist(
|
|
|
|
add_function,
|
|
|
|
r"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self.bar()<|>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
r"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(&self) {
|
|
|
|
self.bar();
|
|
|
|
}
|
|
|
|
fn bar(&self) {
|
2020-04-13 15:17:06 -05:00
|
|
|
todo!();
|
2020-03-26 14:59:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|