diff --git a/crates/ra_assists/src/doc_tests/generated.rs b/crates/ra_assists/src/doc_tests/generated.rs index 0848ab6bc25c..0941735ceb7d 100644 --- a/crates/ra_assists/src/doc_tests/generated.rs +++ b/crates/ra_assists/src/doc_tests/generated.rs @@ -58,6 +58,33 @@ fn main() { ) } +#[test] +fn doctest_add_function() { + check( + "add_function", + r#####" +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar<|>("", baz()); +} + +"#####, + r#####" +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar("", baz()); +} + +fn bar(arg: &str, baz: Baz) { + <|>todo!() +} + +"#####, + ) +} + #[test] fn doctest_add_hash() { check( diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs new file mode 100644 index 000000000000..d70189882870 --- /dev/null +++ b/crates/ra_assists/src/handlers/add_function.rs @@ -0,0 +1,720 @@ +use ra_syntax::{ + ast::{self, AstNode}, + SmolStr, SyntaxKind, SyntaxNode, TextUnit, +}; + +use crate::{Assist, AssistCtx, AssistId}; +use ast::{ArgListOwner, CallExpr, Expr}; +use hir::HirDisplay; +use ra_fmt::leading_indent; +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() { +// bar<|>("", baz()); +// } +// +// ``` +// -> +// ``` +// struct Baz; +// fn baz() -> Baz { Baz } +// fn foo() { +// bar("", baz()); +// } +// +// fn bar(arg: &str, baz: Baz) { +// <|>todo!() +// } +// +// ``` +pub(crate) fn add_function(ctx: AssistCtx) -> Option { + let path: ast::Path = ctx.find_node_at_offset()?; + + if ctx.sema.resolve_path(&path).is_some() { + // The function call already resolves, no need to add a function + return None; + } + + if path.qualifier().is_some() { + return None; + } + + let call: ast::CallExpr = ctx.find_node_at_offset()?; + + if unresolved_path_is_not_call_expr(&path, &call)? { + return None; + } + + let function_builder = FunctionBuilder::from_call(&ctx, &call)?; + + ctx.add_assist(AssistId("add_function"), "Add function", |edit| { + edit.target(call.syntax().text_range()); + + let function_template = function_builder.render(); + edit.set_cursor(function_template.cursor_offset); + edit.insert(function_template.insert_offset, function_template.fn_text); + }) +} + +/// Checks whether the path of the called function in `call_expr` equals `path`. +/// +/// If it doesn't, the cursor is on an unresolved path of a non-function item in +/// `call_expr` and we don't want to generate a function with that item's name. +fn unresolved_path_is_not_call_expr(path: &ast::Path, call_expr: &ast::CallExpr) -> Option { + let call_path = call_expr.syntax().descendants().find_map(ast::Path::cast)?; + Some(call_path != *path) +} + +struct FunctionTemplate { + insert_offset: TextUnit, + cursor_offset: TextUnit, + fn_text: String, +} + +struct FunctionBuilder { + start_offset: TextUnit, + fn_name: String, + fn_generics: String, + fn_args: String, + indent: String, +} + +impl FunctionBuilder { + fn from_call(ctx: &AssistCtx, call: &ast::CallExpr) -> Option { + let (start, indent) = next_space_for_fn(&call)?; + let fn_name = fn_name(&call)?; + let fn_generics = fn_generics(&call)?; + let fn_args = fn_args(ctx, &call)?; + let indent = if let Some(i) = &indent { i.to_string() } else { String::new() }; + Some(Self { start_offset: start, fn_name, fn_generics, fn_args, indent }) + } + fn render(&self) -> FunctionTemplate { + let mut fn_buf = String::with_capacity(128); + fn_buf.push_str("\n\n"); + fn_buf.push_str(&self.indent); + fn_buf.push_str("fn "); + fn_buf.push_str(&self.fn_name); + fn_buf.push_str(&self.fn_generics); + fn_buf.push_str(&self.fn_args); + fn_buf.push_str(" {\n"); + fn_buf.push_str(&self.indent); + fn_buf.push_str(" "); + + // We take the offset here to put the cursor in front of the `todo` body + let offset = TextUnit::of_str(&fn_buf); + + fn_buf.push_str("todo!()\n"); + fn_buf.push_str(&self.indent); + fn_buf.push_str("}"); + + let cursor_pos = self.start_offset + offset; + FunctionTemplate { + fn_text: fn_buf, + cursor_offset: cursor_pos, + insert_offset: self.start_offset, + } + } +} + +fn fn_name(call: &CallExpr) -> Option { + Some(call.expr()?.syntax().to_string()) +} + +fn fn_generics(_call: &CallExpr) -> Option { + // TODO + Some("".into()) +} + +fn fn_args(ctx: &AssistCtx, call: &CallExpr) -> Option { + let mut arg_names = Vec::new(); + let mut arg_types = Vec::new(); + for arg in call.arg_list()?.args() { + dbg!(&arg); + 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); + Some(format!( + "({})", + arg_names + .into_iter() + .zip(arg_types) + .map(|(name, ty)| format!("{}: {}", name, ty)) + .collect::>() + .join(", ") + )) +} + +/// Makes duplicate argument names unique by appending incrementing numbers. +/// +/// ``` +/// let mut names: Vec = +/// vec!["foo".into(), "foo".into(), "bar".into(), "baz".into(), "bar".into()]; +/// deduplicate_arg_names(&mut names); +/// let expected: Vec = +/// 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) { + 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 = 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; + } + } +} + +fn fn_arg_name(fn_arg: &Expr) -> Option { + if let Some(cast_expr) = ast::CastExpr::cast(fn_arg.syntax().clone()) { + dbg!(cast_expr.expr()); + } + match fn_arg { + Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?), + _ => Some( + fn_arg + .syntax() + .descendants() + .filter(|d| ast::NameRef::can_cast(d.kind())) + .last()? + .to_string(), + ), + } +} + +fn fn_arg_type(ctx: &AssistCtx, fn_arg: &Expr) -> Option { + let ty = ctx.sema.type_of_expr(fn_arg)?; + dbg!(&ty); + 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 +fn next_space_for_fn(expr: &CallExpr) -> Option<(TextUnit, Option)> { + let mut ancestors = expr.syntax().ancestors().peekable(); + let mut last_ancestor: Option = 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(|a| (a.text_range().end(), leading_indent(&a))) +} + +#[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() { + <|>todo!() +} +", + ) + } + + #[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() { + <|>todo!() +} +", + ) + } + + #[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() { + <|>todo!() +} + +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() { + <|>todo!() + } +} +", + ) + } + + #[test] + fn add_function_with_function_call_arg() { + check_assist( + add_function, + r" +struct Baz; +fn baz() -> Baz { todo!() } +fn foo() { + bar<|>(baz()); +} +", + r" +struct Baz; +fn baz() -> Baz { todo!() } +fn foo() { + bar(baz()); +} + +fn bar(baz: Baz) { + <|>todo!() +} +", + ); + } + + #[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) { + <|>todo!() +} +", + ) + } + + #[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) { + <|>todo!() +} +"#, + ) + } + + #[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) { + <|>todo!() +} +"#, + ) + } + + #[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) { + <|>todo!() +} +", + ) + } + + #[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) { + <|>todo!() +} +", + ) + } + + #[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: ()) { + <|>todo!() +} +", + ) + } + + #[test] + fn add_function_with_expression_arg() { + check_assist( + add_function, + r" +struct Foo; +impl Foo { + fn len(&self) -> usize { 15 } +} +fn foo() { + <|>bar(Foo.len()) +} +", + r" +struct Foo; +impl Foo { + fn len(&self) -> usize { 15 } +} +fn foo() { + bar(Foo.len()) +} + +fn bar(len: usize) { + <|>todo!() +} +", + ) + } + + #[test] + fn add_function_with_impl_trait_arg() { + check_assist( + add_function, + r" +trait Foo {} +fn foo() -> impl Foo { + todo!() +} +fn baz() { + <|>bar(foo()) +} +", + r" +trait Foo {} +fn foo() -> impl Foo { + todo!() +} +fn baz() { + bar(foo()) +} + +fn bar(foo: impl Foo) { + <|>todo!() +} +", + ) + } + + #[test] + 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) { + <|>todo!() +} +", + ) + } + + #[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) { + <|>todo!() +} +", + ) + } + + #[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) { + <|>todo!() +} +"#, + ) + } + + #[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) { + todo!(); + } +} + ", + ) + } +} diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index fa1f3dd26f44..66854cb5a401 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -96,6 +96,7 @@ mod handlers { mod add_custom_impl; mod add_derive; mod add_explicit_type; + mod add_function; mod add_impl; mod add_missing_impl_members; mod add_new; @@ -128,6 +129,7 @@ mod handlers { add_custom_impl::add_custom_impl, add_derive::add_derive, add_explicit_type::add_explicit_type, + add_function::add_function, add_impl::add_impl, add_missing_impl_members::add_missing_default_members, add_missing_impl_members::add_missing_impl_members, diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index cd2a8fc62083..36bafaaf0f46 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -610,6 +610,10 @@ impl Function { db.function_data(self.id).params.clone() } + pub fn ret_type(self, db: &dyn HirDatabase) -> TypeRef { + db.function_data(self.id).ret_type.clone() + } + pub fn diagnostics(self, db: &dyn HirDatabase, sink: &mut DiagnosticSink) { let _p = profile("Function::diagnostics"); let infer = db.infer(self.id.into()); diff --git a/docs/user/assists.md b/docs/user/assists.md index 94b5ef85d6d3..09410cd41803 100644 --- a/docs/user/assists.md +++ b/docs/user/assists.md @@ -56,6 +56,32 @@ fn main() { } ``` +## `add_function` + +Adds a stub function with a signature matching the function under the cursor. + +```rust +// BEFORE +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar┃("", baz()); +} + + +// AFTER +struct Baz; +fn baz() -> Baz { Baz } +fn foo() { + bar("", baz()); +} + +fn bar(arg: &str, baz: Baz) { + ┃todo!() +} + +``` + ## `add_hash` Adds a hash to a raw string literal.