1 use hir
::{Adt, HasSource, HirDisplay, Module, Semantics, TypeInfo}
;
4 defs
::{Definition, NameRefClass}
,
5 famous_defs
::FamousDefs
,
6 FxHashMap
, FxHashSet
, RootDatabase
, SnippetCap
,
8 use stdx
::to_lower_snake_case
;
12 edit
::{AstNodeEdit, IndentLevel}
,
13 make
, AstNode
, CallExpr
, HasArgList
, HasModuleItem
,
15 SyntaxKind
, SyntaxNode
, TextRange
, TextSize
,
19 utils
::convert_reference_type
,
20 utils
::{find_struct_impl, render_snippet, Cursor}
,
21 AssistContext
, AssistId
, AssistKind
, Assists
,
24 // Assist: generate_function
26 // Adds a stub function with a signature matching the function under the cursor.
30 // fn baz() -> Baz { Baz }
39 // fn baz() -> Baz { Baz }
44 // fn bar(arg: &str, baz: Baz) ${0:-> _} {
49 pub(crate) fn generate_function(acc
: &mut Assists
, ctx
: &AssistContext
<'_
>) -> Option
<()> {
50 gen_fn(acc
, ctx
).or_else(|| gen_method(acc
, ctx
))
53 fn gen_fn(acc
: &mut Assists
, ctx
: &AssistContext
<'_
>) -> Option
<()> {
54 let path_expr
: ast
::PathExpr
= ctx
.find_node_at_offset()?
;
55 let call
= path_expr
.syntax().parent().and_then(ast
::CallExpr
::cast
)?
;
56 let path
= path_expr
.path()?
;
57 let name_ref
= path
.segment()?
.name_ref()?
;
58 if ctx
.sema
.resolve_path(&path
).is_some() {
59 // The function call already resolves, no need to add a function
63 let fn_name
= &*name_ref
.text();
64 let TargetInfo { target_module, adt_name, target, file, insert_offset }
=
65 fn_target_info(ctx
, path
, &call
, fn_name
)?
;
66 let function_builder
= FunctionBuilder
::from_call(ctx
, &call
, fn_name
, target_module
, target
)?
;
67 let text_range
= call
.syntax().text_range();
68 let label
= format
!("Generate {} function", function_builder
.fn_name
);
69 add_func_to_accumulator(
82 target_module
: Option
<Module
>,
83 adt_name
: Option
<hir
::Name
>,
84 target
: GeneratedFunctionTarget
,
86 insert_offset
: TextSize
,
91 target_module
: Option
<Module
>,
92 adt_name
: Option
<hir
::Name
>,
93 target
: GeneratedFunctionTarget
,
95 insert_offset
: TextSize
,
97 Self { target_module, adt_name, target, file, insert_offset }
102 ctx
: &AssistContext
<'_
>,
106 ) -> Option
<TargetInfo
> {
107 match path
.qualifier() {
108 Some(qualifier
) => match ctx
.sema
.resolve_path(&qualifier
) {
109 Some(hir
::PathResolution
::Def(hir
::ModuleDef
::Module(module
))) => {
110 get_fn_target_info(ctx
, &Some(module
), call
.clone())
112 Some(hir
::PathResolution
::Def(hir
::ModuleDef
::Adt(adt
))) => {
113 if let hir
::Adt
::Enum(_
) = adt
{
114 // Don't suggest generating function if the name starts with an uppercase letter
115 if fn_name
.starts_with(char::is_uppercase
) {
120 assoc_fn_target_info(ctx
, call
, adt
, fn_name
)
122 Some(hir
::PathResolution
::SelfType(impl_
)) => {
123 let adt
= impl_
.self_ty(ctx
.db()).as_adt()?
;
124 assoc_fn_target_info(ctx
, call
, adt
, fn_name
)
128 _
=> get_fn_target_info(ctx
, &None
, call
.clone()),
132 fn gen_method(acc
: &mut Assists
, ctx
: &AssistContext
<'_
>) -> Option
<()> {
133 let call
: ast
::MethodCallExpr
= ctx
.find_node_at_offset()?
;
134 if ctx
.sema
.resolve_method_call(&call
).is_some() {
138 let fn_name
= call
.name_ref()?
;
139 let adt
= ctx
.sema
.type_of_expr(&call
.receiver()?
)?
.original().strip_references().as_adt()?
;
141 let current_module
= ctx
.sema
.scope(call
.syntax())?
.module();
142 let target_module
= adt
.module(ctx
.sema
.db
);
144 if current_module
.krate() != target_module
.krate() {
147 let (impl_
, file
) = get_adt_source(ctx
, &adt
, fn_name
.text().as_str())?
;
148 let (target
, insert_offset
) = get_method_target(ctx
, &impl_
, &adt
)?
;
150 let function_builder
=
151 FunctionBuilder
::from_method_call(ctx
, &call
, &fn_name
, target_module
, target
)?
;
152 let text_range
= call
.syntax().text_range();
153 let adt_name
= if impl_
.is_none() { Some(adt.name(ctx.sema.db)) }
else { None }
;
154 let label
= format
!("Generate {} method", function_builder
.fn_name
);
155 add_func_to_accumulator(
167 fn add_func_to_accumulator(
169 ctx
: &AssistContext
<'_
>,
170 text_range
: TextRange
,
171 function_builder
: FunctionBuilder
,
172 insert_offset
: TextSize
,
174 adt_name
: Option
<hir
::Name
>,
177 acc
.add(AssistId("generate_function", AssistKind
::Generate
), label
, text_range
, |builder
| {
178 let indent
= IndentLevel
::from_node(function_builder
.target
.syntax());
179 let function_template
= function_builder
.render(adt_name
.is_some());
180 let mut func
= function_template
.to_string(ctx
.config
.snippet_cap
);
181 if let Some(name
) = adt_name
{
182 func
= format
!("\n{indent}impl {name} {{\n{func}\n{indent}}}");
184 builder
.edit_file(file
);
185 match ctx
.config
.snippet_cap
{
186 Some(cap
) => builder
.insert_snippet(cap
, insert_offset
, func
),
187 None
=> builder
.insert(insert_offset
, func
),
193 ctx
: &AssistContext
<'_
>,
196 ) -> Option
<(Option
<ast
::Impl
>, FileId
)> {
197 let range
= adt
.source(ctx
.sema
.db
)?
.syntax().original_file_range(ctx
.sema
.db
);
198 let file
= ctx
.sema
.parse(range
.file_id
);
200 ctx
.sema
.find_node_at_offset_with_macros(file
.syntax(), range
.range
.start())?
;
201 find_struct_impl(ctx
, &adt_source
, &[fn_name
.to_string()]).map(|impl_
| (impl_
, range
.file_id
))
204 struct FunctionTemplate
{
207 ret_type
: Option
<ast
::RetType
>,
208 should_focus_return_type
: bool
,
210 tail_expr
: ast
::Expr
,
213 impl FunctionTemplate
{
214 fn to_string(&self, cap
: Option
<SnippetCap
>) -> String
{
215 let Self { leading_ws, fn_def, ret_type, should_focus_return_type, trailing_ws, tail_expr }
=
220 let cursor
= if *should_focus_return_type
{
221 // Focus the return type if there is one
223 Some(ret_type
) => ret_type
.syntax(),
224 None
=> tail_expr
.syntax(),
229 render_snippet(cap
, fn_def
.syntax(), Cursor
::Replace(cursor
))
231 None
=> fn_def
.to_string(),
234 format
!("{leading_ws}{f}{trailing_ws}")
238 struct FunctionBuilder
{
239 target
: GeneratedFunctionTarget
,
241 type_params
: Option
<ast
::GenericParamList
>,
242 params
: ast
::ParamList
,
243 ret_type
: Option
<ast
::RetType
>,
244 should_focus_return_type
: bool
,
249 impl FunctionBuilder
{
250 /// Prepares a generated function that matches `call`.
251 /// The function is generated in `target_module` or next to `call`
253 ctx
: &AssistContext
<'_
>,
254 call
: &ast
::CallExpr
,
256 target_module
: Option
<hir
::Module
>,
257 target
: GeneratedFunctionTarget
,
259 let needs_pub
= target_module
.is_some();
261 target_module
.or_else(|| ctx
.sema
.scope(target
.syntax()).map(|it
| it
.module()))?
;
262 let fn_name
= make
::name(fn_name
);
263 let (type_params
, params
) =
264 fn_args(ctx
, target_module
, ast
::CallableExpr
::Call(call
.clone()))?
;
266 let await_expr
= call
.syntax().parent().and_then(ast
::AwaitExpr
::cast
);
267 let is_async
= await_expr
.is_some();
269 let (ret_type
, should_focus_return_type
) =
270 make_return_type(ctx
, &ast
::Expr
::CallExpr(call
.clone()), target_module
);
278 should_focus_return_type
,
285 ctx
: &AssistContext
<'_
>,
286 call
: &ast
::MethodCallExpr
,
288 target_module
: Module
,
289 target
: GeneratedFunctionTarget
,
292 !module_is_descendant(&ctx
.sema
.scope(call
.syntax())?
.module(), &target_module
, ctx
);
293 let fn_name
= make
::name(&name
.text());
294 let (type_params
, params
) =
295 fn_args(ctx
, target_module
, ast
::CallableExpr
::MethodCall(call
.clone()))?
;
297 let await_expr
= call
.syntax().parent().and_then(ast
::AwaitExpr
::cast
);
298 let is_async
= await_expr
.is_some();
300 let (ret_type
, should_focus_return_type
) =
301 make_return_type(ctx
, &ast
::Expr
::MethodCallExpr(call
.clone()), target_module
);
309 should_focus_return_type
,
315 fn render(self, is_method
: bool
) -> FunctionTemplate
{
316 let placeholder_expr
= make
::ext
::expr_todo();
317 let fn_body
= make
::block_expr(vec
![], Some(placeholder_expr
));
318 let visibility
= if self.needs_pub { Some(make::visibility_pub_crate()) }
else { None }
;
319 let mut fn_def
= make
::fn_(
332 GeneratedFunctionTarget
::BehindItem(it
) => {
333 let mut indent
= IndentLevel
::from_node(&it
);
336 leading_ws
= format
!("{indent}");
338 leading_ws
= format
!("\n\n{indent}");
341 fn_def
= fn_def
.indent(indent
);
342 trailing_ws
= String
::new();
344 GeneratedFunctionTarget
::InEmptyItemList(it
) => {
345 let indent
= IndentLevel
::from_node(&it
);
346 let leading_indent
= indent
+ 1;
347 leading_ws
= format
!("\n{leading_indent}");
348 fn_def
= fn_def
.indent(leading_indent
);
349 trailing_ws
= format
!("\n{indent}");
355 ret_type
: fn_def
.ret_type(),
356 // PANIC: we guarantee we always create a function body with a tail expr
357 tail_expr
: fn_def
.body().unwrap().tail_expr().unwrap(),
358 should_focus_return_type
: self.should_focus_return_type
,
365 /// Makes an optional return type along with whether the return type should be focused by the cursor.
366 /// If we cannot infer what the return type should be, we create a placeholder type.
368 /// The rule for whether we focus a return type or not (and thus focus the function body),
369 /// is rather simple:
370 /// * If we could *not* infer what the return type should be, focus it (so the user can fill-in
371 /// the correct return type).
372 /// * If we could infer the return type, don't focus it (and thus focus the function body) so the
373 /// user can change the `todo!` function body.
375 ctx
: &AssistContext
<'_
>,
377 target_module
: Module
,
378 ) -> (Option
<ast
::RetType
>, bool
) {
379 let (ret_ty
, should_focus_return_type
) = {
380 match ctx
.sema
.type_of_expr(call
).map(TypeInfo
::original
) {
381 Some(ty
) if ty
.is_unknown() => (Some(make
::ty_placeholder()), true),
382 None
=> (Some(make
::ty_placeholder()), true),
383 Some(ty
) if ty
.is_unit() => (None
, false),
385 let rendered
= ty
.display_source_code(ctx
.db(), target_module
.into());
387 Ok(rendered
) => (Some(make
::ty(&rendered
)), false),
388 Err(_
) => (Some(make
::ty_placeholder()), true),
393 let ret_type
= ret_ty
.map(make
::ret_type
);
394 (ret_type
, should_focus_return_type
)
397 fn get_fn_target_info(
398 ctx
: &AssistContext
<'_
>,
399 target_module
: &Option
<Module
>,
401 ) -> Option
<TargetInfo
> {
402 let (target
, file
, insert_offset
) = get_fn_target(ctx
, target_module
, call
)?
;
403 Some(TargetInfo
::new(*target_module
, None
, target
, file
, insert_offset
))
407 ctx
: &AssistContext
<'_
>,
408 target_module
: &Option
<Module
>,
410 ) -> Option
<(GeneratedFunctionTarget
, FileId
, TextSize
)> {
411 let mut file
= ctx
.file_id();
412 let target
= match target_module
{
413 Some(target_module
) => {
414 let module_source
= target_module
.definition_source(ctx
.db());
415 let (in_file
, target
) = next_space_for_fn_in_module(ctx
.sema
.db
, &module_source
)?
;
419 None
=> next_space_for_fn_after_call_site(ast
::CallableExpr
::Call(call
))?
,
421 Some((target
.clone(), file
, get_insert_offset(&target
)))
424 fn get_method_target(
425 ctx
: &AssistContext
<'_
>,
426 impl_
: &Option
<ast
::Impl
>,
428 ) -> Option
<(GeneratedFunctionTarget
, TextSize
)> {
429 let target
= match impl_
{
430 Some(impl_
) => next_space_for_fn_in_impl(impl_
)?
,
432 GeneratedFunctionTarget
::BehindItem(adt
.source(ctx
.sema
.db
)?
.syntax().value
.clone())
435 Some((target
.clone(), get_insert_offset(&target
)))
438 fn assoc_fn_target_info(
439 ctx
: &AssistContext
<'_
>,
443 ) -> Option
<TargetInfo
> {
444 let current_module
= ctx
.sema
.scope(call
.syntax())?
.module();
445 let module
= adt
.module(ctx
.sema
.db
);
446 let target_module
= if current_module
== module { None }
else { Some(module) }
;
447 if current_module
.krate() != module
.krate() {
450 let (impl_
, file
) = get_adt_source(ctx
, &adt
, fn_name
)?
;
451 let (target
, insert_offset
) = get_method_target(ctx
, &impl_
, &adt
)?
;
452 let adt_name
= if impl_
.is_none() { Some(adt.name(ctx.sema.db)) }
else { None }
;
453 Some(TargetInfo
::new(target_module
, adt_name
, target
, file
, insert_offset
))
456 fn get_insert_offset(target
: &GeneratedFunctionTarget
) -> TextSize
{
458 GeneratedFunctionTarget
::BehindItem(it
) => it
.text_range().end(),
459 GeneratedFunctionTarget
::InEmptyItemList(it
) => it
.text_range().start() + TextSize
::of('
{'
),
464 enum GeneratedFunctionTarget
{
465 BehindItem(SyntaxNode
),
466 InEmptyItemList(SyntaxNode
),
469 impl GeneratedFunctionTarget
{
470 fn syntax(&self) -> &SyntaxNode
{
472 GeneratedFunctionTarget
::BehindItem(it
) => it
,
473 GeneratedFunctionTarget
::InEmptyItemList(it
) => it
,
478 /// Computes the type variables and arguments required for the generated function
480 ctx
: &AssistContext
<'_
>,
481 target_module
: hir
::Module
,
482 call
: ast
::CallableExpr
,
483 ) -> Option
<(Option
<ast
::GenericParamList
>, ast
::ParamList
)> {
484 let mut arg_names
= Vec
::new();
485 let mut arg_types
= Vec
::new();
486 for arg
in call
.arg_list()?
.args() {
487 arg_names
.push(fn_arg_name(&ctx
.sema
, &arg
));
488 arg_types
.push(fn_arg_type(ctx
, target_module
, &arg
));
490 deduplicate_arg_names(&mut arg_names
);
491 let params
= arg_names
.into_iter().zip(arg_types
).map(|(name
, ty
)| {
492 make
::param(make
::ext
::simple_ident_pat(make
::name(&name
)).into(), make
::ty(&ty
))
499 ast
::CallableExpr
::Call(_
) => None
,
500 ast
::CallableExpr
::MethodCall(_
) => Some(make
::self_param()),
507 /// Makes duplicate argument names unique by appending incrementing numbers.
510 /// let mut names: Vec<String> =
511 /// vec!["foo".into(), "foo".into(), "bar".into(), "baz".into(), "bar".into()];
512 /// deduplicate_arg_names(&mut names);
513 /// let expected: Vec<String> =
514 /// vec!["foo_1".into(), "foo_2".into(), "bar_1".into(), "baz".into(), "bar_2".into()];
515 /// assert_eq!(names, expected);
517 fn deduplicate_arg_names(arg_names
: &mut [String
]) {
518 let mut arg_name_counts
= FxHashMap
::default();
519 for name
in arg_names
.iter() {
520 *arg_name_counts
.entry(name
).or_insert(0) += 1;
522 let duplicate_arg_names
: FxHashSet
<String
> = arg_name_counts
524 .filter(|(_
, count
)| *count
>= 2)
525 .map(|(name
, _
)| name
.clone())
528 let mut counter_per_name
= FxHashMap
::default();
529 for arg_name
in arg_names
.iter_mut() {
530 if duplicate_arg_names
.contains(arg_name
) {
531 let counter
= counter_per_name
.entry(arg_name
.clone()).or_insert(1);
533 arg_name
.push_str(&counter
.to_string());
539 fn fn_arg_name(sema
: &Semantics
<'_
, RootDatabase
>, arg_expr
: &ast
::Expr
) -> String
{
540 let name
= (|| match arg_expr
{
541 ast
::Expr
::CastExpr(cast_expr
) => Some(fn_arg_name(sema
, &cast_expr
.expr()?
)),
546 .filter_map(ast
::NameRef
::cast
)
547 .filter(|name
| name
.ident_token().is_some())
549 if let Some(NameRefClass
::Definition(Definition
::Const(_
) | Definition
::Static(_
))) =
550 NameRefClass
::classify(sema
, &name_ref
)
552 return Some(name_ref
.to_string().to_lowercase());
554 Some(to_lower_snake_case(&name_ref
.to_string()))
558 Some(mut name
) if name
.starts_with(|c
: char| c
.is_ascii_digit()) => {
559 name
.insert_str(0, "arg");
563 None
=> "arg".to_string(),
567 fn fn_arg_type(ctx
: &AssistContext
<'_
>, target_module
: hir
::Module
, fn_arg
: &ast
::Expr
) -> String
{
568 fn maybe_displayed_type(
569 ctx
: &AssistContext
<'_
>,
570 target_module
: hir
::Module
,
572 ) -> Option
<String
> {
573 let ty
= ctx
.sema
.type_of_expr(fn_arg
)?
.adjusted();
578 if ty
.is_reference() || ty
.is_mutable_reference() {
579 let famous_defs
= &FamousDefs(&ctx
.sema
, ctx
.sema
.scope(fn_arg
.syntax())?
.krate());
580 convert_reference_type(ty
.strip_references(), ctx
.db(), famous_defs
)
581 .map(|conversion
| conversion
.convert_type(ctx
.db()))
582 .or_else(|| ty
.display_source_code(ctx
.db(), target_module
.into()).ok())
584 ty
.display_source_code(ctx
.db(), target_module
.into()).ok()
588 maybe_displayed_type(ctx
, target_module
, fn_arg
).unwrap_or_else(|| String
::from("_"))
591 /// Returns the position inside the current mod or file
592 /// directly after the current block
593 /// We want to write the generated function directly after
594 /// fns, impls or macro calls, but inside mods
595 fn next_space_for_fn_after_call_site(expr
: ast
::CallableExpr
) -> Option
<GeneratedFunctionTarget
> {
596 let mut ancestors
= expr
.syntax().ancestors().peekable();
597 let mut last_ancestor
: Option
<SyntaxNode
> = None
;
598 while let Some(next_ancestor
) = ancestors
.next() {
599 match next_ancestor
.kind() {
600 SyntaxKind
::SOURCE_FILE
=> {
603 SyntaxKind
::ITEM_LIST
=> {
604 if ancestors
.peek().map(|a
| a
.kind()) == Some(SyntaxKind
::MODULE
) {
610 last_ancestor
= Some(next_ancestor
);
612 last_ancestor
.map(GeneratedFunctionTarget
::BehindItem
)
615 fn next_space_for_fn_in_module(
616 db
: &dyn hir
::db
::AstDatabase
,
617 module_source
: &hir
::InFile
<hir
::ModuleSource
>,
618 ) -> Option
<(FileId
, GeneratedFunctionTarget
)> {
619 let file
= module_source
.file_id
.original_file(db
);
620 let assist_item
= match &module_source
.value
{
621 hir
::ModuleSource
::SourceFile(it
) => match it
.items().last() {
622 Some(last_item
) => GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone()),
623 None
=> GeneratedFunctionTarget
::BehindItem(it
.syntax().clone()),
625 hir
::ModuleSource
::Module(it
) => match it
.item_list().and_then(|it
| it
.items().last()) {
626 Some(last_item
) => GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone()),
627 None
=> GeneratedFunctionTarget
::InEmptyItemList(it
.item_list()?
.syntax().clone()),
629 hir
::ModuleSource
::BlockExpr(it
) => {
630 if let Some(last_item
) =
631 it
.statements().take_while(|stmt
| matches
!(stmt
, ast
::Stmt
::Item(_
))).last()
633 GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone())
635 GeneratedFunctionTarget
::InEmptyItemList(it
.syntax().clone())
639 Some((file
, assist_item
))
642 fn next_space_for_fn_in_impl(impl_
: &ast
::Impl
) -> Option
<GeneratedFunctionTarget
> {
643 if let Some(last_item
) = impl_
.assoc_item_list().and_then(|it
| it
.assoc_items().last()) {
644 Some(GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone()))
646 Some(GeneratedFunctionTarget
::InEmptyItemList(impl_
.assoc_item_list()?
.syntax().clone()))
650 fn module_is_descendant(module
: &hir
::Module
, ans
: &hir
::Module
, ctx
: &AssistContext
<'_
>) -> bool
{
654 for c
in ans
.children(ctx
.sema
.db
) {
655 if module_is_descendant(module
, &c
, ctx
) {
664 use crate::tests
::{check_assist, check_assist_not_applicable}
;
669 fn add_function_with_no_args() {
690 fn add_function_from_method() {
691 // This ensures that the function is correctly generated
692 // in the next outer mod or file
717 fn add_function_directly_after_current_block() {
718 // The new fn should not be created at the end of the file or module
743 fn add_function_with_no_args_in_same_module() {
768 fn add_function_with_upper_camel_case_arg() {
783 fn bar(baz_baz: BazBaz) ${0:-> _} {
791 fn add_function_with_upper_camel_case_arg_as_cast() {
797 bar$0(&BazBaz as *const BazBaz);
803 bar(&BazBaz as *const BazBaz);
806 fn bar(baz_baz: *const BazBaz) ${0:-> _} {
814 fn add_function_with_function_call_arg() {
819 fn baz() -> Baz { todo!() }
826 fn baz() -> Baz { todo!() }
831 fn bar(baz: Baz) ${0:-> _} {
839 fn add_function_with_method_call_arg() {
845 fn foo(&self) -> Baz {
848 fn baz(&self) -> Baz {
856 fn foo(&self) -> Baz {
859 fn baz(&self) -> Baz {
864 fn bar(baz: Baz) -> Baz {
872 fn add_function_with_string_literal_arg() {
893 fn add_function_with_char_literal_arg() {
914 fn add_function_with_int_literal_arg() {
935 fn add_function_with_cast_int_literal_arg() {
956 fn name_of_cast_variable_is_used() {
957 // Ensures that the name of the cast type isn't used
958 // in the generated function signature.
981 fn add_function_with_variable_arg() {
1004 fn add_function_with_impl_trait_arg() {
1010 fn foo() -> impl Foo {
1019 fn foo() -> impl Foo {
1026 fn bar(foo: impl Foo) {
1039 fn baz() -> Baz { todo!() }
1047 fn baz() -> Baz { todo!() }
1061 fn add_function_with_qualified_path_arg() {
1067 pub fn baz() -> Bof { Bof }
1076 pub fn baz() -> Bof { Bof }
1082 fn bar(baz: Baz::Bof) {
1090 fn add_function_with_generic_arg() {
1091 // FIXME: This is wrong, generated `bar` should include generic parameter.
1112 fn add_function_with_fn_arg() {
1113 // FIXME: The argument in `bar` is wrong.
1119 fn new() -> Self { Baz }
1128 fn new() -> Self { Baz }
1134 fn bar(new: fn) ${0:-> _} {
1142 fn add_function_with_closure_arg() {
1143 // FIXME: The argument in `bar` is wrong.
1148 let closure = |x: i64| x - 1;
1154 let closure = |x: i64| x - 1;
1158 fn bar(closure: _) {
1166 fn unresolveable_types_default_to_placeholder() {
1187 fn arg_names_dont_overlap() {
1192 fn baz() -> Baz { Baz }
1199 fn baz() -> Baz { Baz }
1204 fn bar(baz_1: Baz, baz_2: Baz) {
1212 fn arg_name_counters_start_at_1_per_name() {
1217 fn baz() -> Baz { Baz }
1219 $0bar(baz(), baz(), "foo", "bar")
1224 fn baz() -> Baz { Baz }
1226 bar(baz(), baz(), "foo", "bar")
1229 fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
1237 fn add_function_in_module() {
1249 pub(crate) fn my_fn() {
1262 fn qualified_path_uses_correct_scope() {
1285 fn baz(foo: foo::Foo) {
1293 fn add_function_in_module_containing_other_items() {
1298 fn something_else() {}
1307 fn something_else() {}
1309 pub(crate) fn my_fn() {
1322 fn add_function_in_nested_module() {
1337 pub(crate) fn my_fn() {
1351 fn add_function_in_another_file() {
1366 pub(crate) fn bar() {
1373 fn add_function_with_return_type() {
1378 let x: u32 = foo$0();
1394 fn add_function_not_applicable_if_function_already_exists() {
1395 check_assist_not_applicable(
1408 fn add_function_not_applicable_if_unresolved_variable_in_call_is_selected() {
1409 check_assist_not_applicable(
1410 // bar is resolved, but baz isn't.
1411 // The assist is only active if the cursor is on an unresolved path,
1412 // but the assist should only be offered if the path is a function call.
1425 fn create_method_with_no_args() {
1443 fn bar(&self) ${0:-> _} {
1452 fn create_function_with_async() {
1465 async fn bar(arg: i32) ${0:-> _} {
1473 fn create_method() {
1478 fn foo() {S.bar$0();}
1483 fn bar(&self) ${0:-> _} {
1493 fn create_method_within_an_impl() {
1498 fn foo() {S.bar$0();}
1506 fn bar(&self) ${0:-> _} {
1516 fn create_method_from_different_module() {
1523 fn foo() {s::S.bar$0();}
1529 pub(crate) fn bar(&self) ${0:-> _} {
1534 fn foo() {s::S.bar();}
1540 fn create_method_from_descendant_module() {
1555 fn bar(&self) ${0:-> _} {
1570 fn create_method_with_cursor_anywhere_on_call_expresion() {
1575 fn foo() {$0S.bar();}
1580 fn bar(&self) ${0:-> _} {
1590 fn create_static_method() {
1595 fn foo() {S::bar$0();}
1600 fn bar() ${0:-> _} {
1604 fn foo() {S::bar();}
1610 fn create_static_method_within_an_impl() {
1615 fn foo() {S::bar$0();}
1621 fn foo() {S::bar();}
1623 fn bar() ${0:-> _} {
1633 fn create_static_method_from_different_module() {
1640 fn foo() {s::S::bar$0();}
1646 pub(crate) fn bar() ${0:-> _} {
1651 fn foo() {s::S::bar();}
1657 fn create_static_method_with_cursor_anywhere_on_call_expresion() {
1662 fn foo() {$0S::bar();}
1667 fn bar() ${0:-> _} {
1671 fn foo() {S::bar();}
1677 fn create_static_method_within_an_impl_with_self_syntax() {
1695 fn bar() ${0:-> _} {
1704 fn no_panic_on_invalid_global_path() {
1717 fn foo() ${0:-> _} {
1725 fn handle_tuple_indexing() {
1740 fn foo(a: ()) ${0:-> _} {
1748 fn add_function_with_const_arg() {
1752 const VALUE: usize = 0;
1758 const VALUE: usize = 0;
1763 fn foo(value: usize) ${0:-> _} {
1771 fn add_function_with_static_arg() {
1775 static VALUE: usize = 0;
1781 static VALUE: usize = 0;
1786 fn foo(value: usize) ${0:-> _} {
1794 fn add_function_with_static_mut_arg() {
1798 static mut VALUE: usize = 0;
1804 static mut VALUE: usize = 0;
1809 fn foo(value: usize) ${0:-> _} {
1817 fn not_applicable_for_enum_variant() {
1818 check_assist_not_applicable(
1830 fn applicable_for_enum_method() {
1842 fn new() ${0:-> _} {