1 use hir
::{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
, &target_module
, &impl_
)?
;
149 let function_builder
=
150 FunctionBuilder
::from_method_call(ctx
, &call
, &fn_name
, target_module
, target
)?
;
151 let text_range
= call
.syntax().text_range();
152 let adt_name
= if impl_
.is_none() { Some(adt.name(ctx.sema.db)) }
else { None }
;
153 let label
= format
!("Generate {} method", function_builder
.fn_name
);
154 add_func_to_accumulator(
166 fn add_func_to_accumulator(
168 ctx
: &AssistContext
<'_
>,
169 text_range
: TextRange
,
170 function_builder
: FunctionBuilder
,
171 insert_offset
: TextSize
,
173 adt_name
: Option
<hir
::Name
>,
176 acc
.add(AssistId("generate_function", AssistKind
::Generate
), label
, text_range
, |builder
| {
177 let function_template
= function_builder
.render();
178 let mut func
= function_template
.to_string(ctx
.config
.snippet_cap
);
179 if let Some(name
) = adt_name
{
180 func
= format
!("\nimpl {} {{\n{}\n}}", name
, func
);
182 builder
.edit_file(file
);
183 match ctx
.config
.snippet_cap
{
184 Some(cap
) => builder
.insert_snippet(cap
, insert_offset
, func
),
185 None
=> builder
.insert(insert_offset
, func
),
191 ctx
: &AssistContext
<'_
>,
194 ) -> Option
<(Option
<ast
::Impl
>, FileId
)> {
195 let range
= adt
.source(ctx
.sema
.db
)?
.syntax().original_file_range(ctx
.sema
.db
);
196 let file
= ctx
.sema
.parse(range
.file_id
);
198 ctx
.sema
.find_node_at_offset_with_macros(file
.syntax(), range
.range
.start())?
;
199 find_struct_impl(ctx
, &adt_source
, fn_name
).map(|impl_
| (impl_
, range
.file_id
))
202 struct FunctionTemplate
{
205 ret_type
: Option
<ast
::RetType
>,
206 should_focus_return_type
: bool
,
208 tail_expr
: ast
::Expr
,
211 impl FunctionTemplate
{
212 fn to_string(&self, cap
: Option
<SnippetCap
>) -> String
{
215 let cursor
= if self.should_focus_return_type
{
216 // Focus the return type if there is one
217 match self.ret_type
{
218 Some(ref ret_type
) => ret_type
.syntax(),
219 None
=> self.tail_expr
.syntax(),
222 self.tail_expr
.syntax()
224 render_snippet(cap
, self.fn_def
.syntax(), Cursor
::Replace(cursor
))
226 None
=> self.fn_def
.to_string(),
229 format
!("{}{}{}", self.leading_ws
, f
, self.trailing_ws
)
233 struct FunctionBuilder
{
234 target
: GeneratedFunctionTarget
,
236 type_params
: Option
<ast
::GenericParamList
>,
237 params
: ast
::ParamList
,
238 ret_type
: Option
<ast
::RetType
>,
239 should_focus_return_type
: bool
,
244 impl FunctionBuilder
{
245 /// Prepares a generated function that matches `call`.
246 /// The function is generated in `target_module` or next to `call`
248 ctx
: &AssistContext
<'_
>,
249 call
: &ast
::CallExpr
,
251 target_module
: Option
<hir
::Module
>,
252 target
: GeneratedFunctionTarget
,
254 let needs_pub
= target_module
.is_some();
256 target_module
.or_else(|| ctx
.sema
.scope(target
.syntax()).map(|it
| it
.module()))?
;
257 let fn_name
= make
::name(fn_name
);
258 let (type_params
, params
) =
259 fn_args(ctx
, target_module
, ast
::CallableExpr
::Call(call
.clone()))?
;
261 let await_expr
= call
.syntax().parent().and_then(ast
::AwaitExpr
::cast
);
262 let is_async
= await_expr
.is_some();
264 let (ret_type
, should_focus_return_type
) =
265 make_return_type(ctx
, &ast
::Expr
::CallExpr(call
.clone()), target_module
);
273 should_focus_return_type
,
280 ctx
: &AssistContext
<'_
>,
281 call
: &ast
::MethodCallExpr
,
283 target_module
: Module
,
284 target
: GeneratedFunctionTarget
,
287 !module_is_descendant(&ctx
.sema
.scope(call
.syntax())?
.module(), &target_module
, ctx
);
288 let fn_name
= make
::name(&name
.text());
289 let (type_params
, params
) =
290 fn_args(ctx
, target_module
, ast
::CallableExpr
::MethodCall(call
.clone()))?
;
292 let await_expr
= call
.syntax().parent().and_then(ast
::AwaitExpr
::cast
);
293 let is_async
= await_expr
.is_some();
295 let (ret_type
, should_focus_return_type
) =
296 make_return_type(ctx
, &ast
::Expr
::MethodCallExpr(call
.clone()), target_module
);
304 should_focus_return_type
,
310 fn render(self) -> FunctionTemplate
{
311 let placeholder_expr
= make
::ext
::expr_todo();
312 let fn_body
= make
::block_expr(vec
![], Some(placeholder_expr
));
313 let visibility
= if self.needs_pub { Some(make::visibility_pub_crate()) }
else { None }
;
314 let mut fn_def
= make
::fn_(
327 GeneratedFunctionTarget
::BehindItem(it
) => {
328 let indent
= IndentLevel
::from_node(&it
);
329 leading_ws
= format
!("\n\n{}", indent
);
330 fn_def
= fn_def
.indent(indent
);
331 trailing_ws
= String
::new();
333 GeneratedFunctionTarget
::InEmptyItemList(it
) => {
334 let indent
= IndentLevel
::from_node(&it
);
335 leading_ws
= format
!("\n{}", indent
+ 1);
336 fn_def
= fn_def
.indent(indent
+ 1);
337 trailing_ws
= format
!("\n{}", indent
);
343 ret_type
: fn_def
.ret_type(),
344 // PANIC: we guarantee we always create a function body with a tail expr
345 tail_expr
: fn_def
.body().unwrap().tail_expr().unwrap(),
346 should_focus_return_type
: self.should_focus_return_type
,
353 /// Makes an optional return type along with whether the return type should be focused by the cursor.
354 /// If we cannot infer what the return type should be, we create a placeholder type.
356 /// The rule for whether we focus a return type or not (and thus focus the function body),
357 /// is rather simple:
358 /// * If we could *not* infer what the return type should be, focus it (so the user can fill-in
359 /// the correct return type).
360 /// * If we could infer the return type, don't focus it (and thus focus the function body) so the
361 /// user can change the `todo!` function body.
363 ctx
: &AssistContext
<'_
>,
365 target_module
: Module
,
366 ) -> (Option
<ast
::RetType
>, bool
) {
367 let (ret_ty
, should_focus_return_type
) = {
368 match ctx
.sema
.type_of_expr(call
).map(TypeInfo
::original
) {
369 Some(ty
) if ty
.is_unknown() => (Some(make
::ty_placeholder()), true),
370 None
=> (Some(make
::ty_placeholder()), true),
371 Some(ty
) if ty
.is_unit() => (None
, false),
373 let rendered
= ty
.display_source_code(ctx
.db(), target_module
.into());
375 Ok(rendered
) => (Some(make
::ty(&rendered
)), false),
376 Err(_
) => (Some(make
::ty_placeholder()), true),
381 let ret_type
= ret_ty
.map(make
::ret_type
);
382 (ret_type
, should_focus_return_type
)
385 fn get_fn_target_info(
386 ctx
: &AssistContext
<'_
>,
387 target_module
: &Option
<Module
>,
389 ) -> Option
<TargetInfo
> {
390 let (target
, file
, insert_offset
) = get_fn_target(ctx
, target_module
, call
)?
;
391 Some(TargetInfo
::new(*target_module
, None
, target
, file
, insert_offset
))
395 ctx
: &AssistContext
<'_
>,
396 target_module
: &Option
<Module
>,
398 ) -> Option
<(GeneratedFunctionTarget
, FileId
, TextSize
)> {
399 let mut file
= ctx
.file_id();
400 let target
= match target_module
{
401 Some(target_module
) => {
402 let module_source
= target_module
.definition_source(ctx
.db());
403 let (in_file
, target
) = next_space_for_fn_in_module(ctx
.sema
.db
, &module_source
)?
;
407 None
=> next_space_for_fn_after_call_site(ast
::CallableExpr
::Call(call
))?
,
409 Some((target
.clone(), file
, get_insert_offset(&target
)))
412 fn get_method_target(
413 ctx
: &AssistContext
<'_
>,
414 target_module
: &Module
,
415 impl_
: &Option
<ast
::Impl
>,
416 ) -> Option
<(GeneratedFunctionTarget
, TextSize
)> {
417 let target
= match impl_
{
418 Some(impl_
) => next_space_for_fn_in_impl(impl_
)?
,
420 next_space_for_fn_in_module(ctx
.sema
.db
, &target_module
.definition_source(ctx
.sema
.db
))?
424 Some((target
.clone(), get_insert_offset(&target
)))
427 fn assoc_fn_target_info(
428 ctx
: &AssistContext
<'_
>,
432 ) -> Option
<TargetInfo
> {
433 let current_module
= ctx
.sema
.scope(call
.syntax())?
.module();
434 let module
= adt
.module(ctx
.sema
.db
);
435 let target_module
= if current_module
== module { None }
else { Some(module) }
;
436 if current_module
.krate() != module
.krate() {
439 let (impl_
, file
) = get_adt_source(ctx
, &adt
, fn_name
)?
;
440 let (target
, insert_offset
) = get_method_target(ctx
, &module
, &impl_
)?
;
441 let adt_name
= if impl_
.is_none() { Some(adt.name(ctx.sema.db)) }
else { None }
;
442 Some(TargetInfo
::new(target_module
, adt_name
, target
, file
, insert_offset
))
445 fn get_insert_offset(target
: &GeneratedFunctionTarget
) -> TextSize
{
447 GeneratedFunctionTarget
::BehindItem(it
) => it
.text_range().end(),
448 GeneratedFunctionTarget
::InEmptyItemList(it
) => it
.text_range().start() + TextSize
::of('
{'
),
453 enum GeneratedFunctionTarget
{
454 BehindItem(SyntaxNode
),
455 InEmptyItemList(SyntaxNode
),
458 impl GeneratedFunctionTarget
{
459 fn syntax(&self) -> &SyntaxNode
{
461 GeneratedFunctionTarget
::BehindItem(it
) => it
,
462 GeneratedFunctionTarget
::InEmptyItemList(it
) => it
,
467 /// Computes the type variables and arguments required for the generated function
469 ctx
: &AssistContext
<'_
>,
470 target_module
: hir
::Module
,
471 call
: ast
::CallableExpr
,
472 ) -> Option
<(Option
<ast
::GenericParamList
>, ast
::ParamList
)> {
473 let mut arg_names
= Vec
::new();
474 let mut arg_types
= Vec
::new();
475 for arg
in call
.arg_list()?
.args() {
476 arg_names
.push(fn_arg_name(&ctx
.sema
, &arg
));
477 arg_types
.push(fn_arg_type(ctx
, target_module
, &arg
));
479 deduplicate_arg_names(&mut arg_names
);
480 let params
= arg_names
.into_iter().zip(arg_types
).map(|(name
, ty
)| {
481 make
::param(make
::ext
::simple_ident_pat(make
::name(&name
)).into(), make
::ty(&ty
))
488 ast
::CallableExpr
::Call(_
) => None
,
489 ast
::CallableExpr
::MethodCall(_
) => Some(make
::self_param()),
496 /// Makes duplicate argument names unique by appending incrementing numbers.
499 /// let mut names: Vec<String> =
500 /// vec!["foo".into(), "foo".into(), "bar".into(), "baz".into(), "bar".into()];
501 /// deduplicate_arg_names(&mut names);
502 /// let expected: Vec<String> =
503 /// vec!["foo_1".into(), "foo_2".into(), "bar_1".into(), "baz".into(), "bar_2".into()];
504 /// assert_eq!(names, expected);
506 fn deduplicate_arg_names(arg_names
: &mut Vec
<String
>) {
507 let mut arg_name_counts
= FxHashMap
::default();
508 for name
in arg_names
.iter() {
509 *arg_name_counts
.entry(name
).or_insert(0) += 1;
511 let duplicate_arg_names
: FxHashSet
<String
> = arg_name_counts
513 .filter(|(_
, count
)| *count
>= 2)
514 .map(|(name
, _
)| name
.clone())
517 let mut counter_per_name
= FxHashMap
::default();
518 for arg_name
in arg_names
.iter_mut() {
519 if duplicate_arg_names
.contains(arg_name
) {
520 let counter
= counter_per_name
.entry(arg_name
.clone()).or_insert(1);
522 arg_name
.push_str(&counter
.to_string());
528 fn fn_arg_name(sema
: &Semantics
<'_
, RootDatabase
>, arg_expr
: &ast
::Expr
) -> String
{
529 let name
= (|| match arg_expr
{
530 ast
::Expr
::CastExpr(cast_expr
) => Some(fn_arg_name(sema
, &cast_expr
.expr()?
)),
535 .filter_map(ast
::NameRef
::cast
)
536 .filter(|name
| name
.ident_token().is_some())
538 if let Some(NameRefClass
::Definition(Definition
::Const(_
) | Definition
::Static(_
))) =
539 NameRefClass
::classify(sema
, &name_ref
)
541 return Some(name_ref
.to_string().to_lowercase());
543 Some(to_lower_snake_case(&name_ref
.to_string()))
547 Some(mut name
) if name
.starts_with(|c
: char| c
.is_ascii_digit()) => {
548 name
.insert_str(0, "arg");
552 None
=> "arg".to_string(),
556 fn fn_arg_type(ctx
: &AssistContext
<'_
>, target_module
: hir
::Module
, fn_arg
: &ast
::Expr
) -> String
{
557 fn maybe_displayed_type(
558 ctx
: &AssistContext
<'_
>,
559 target_module
: hir
::Module
,
561 ) -> Option
<String
> {
562 let ty
= ctx
.sema
.type_of_expr(fn_arg
)?
.adjusted();
567 if ty
.is_reference() || ty
.is_mutable_reference() {
568 let famous_defs
= &FamousDefs(&ctx
.sema
, ctx
.sema
.scope(fn_arg
.syntax())?
.krate());
569 convert_reference_type(ty
.strip_references(), ctx
.db(), famous_defs
)
570 .map(|conversion
| conversion
.convert_type(ctx
.db()))
571 .or_else(|| ty
.display_source_code(ctx
.db(), target_module
.into()).ok())
573 ty
.display_source_code(ctx
.db(), target_module
.into()).ok()
577 maybe_displayed_type(ctx
, target_module
, fn_arg
).unwrap_or_else(|| String
::from("_"))
580 /// Returns the position inside the current mod or file
581 /// directly after the current block
582 /// We want to write the generated function directly after
583 /// fns, impls or macro calls, but inside mods
584 fn next_space_for_fn_after_call_site(expr
: ast
::CallableExpr
) -> Option
<GeneratedFunctionTarget
> {
585 let mut ancestors
= expr
.syntax().ancestors().peekable();
586 let mut last_ancestor
: Option
<SyntaxNode
> = None
;
587 while let Some(next_ancestor
) = ancestors
.next() {
588 match next_ancestor
.kind() {
589 SyntaxKind
::SOURCE_FILE
=> {
592 SyntaxKind
::ITEM_LIST
=> {
593 if ancestors
.peek().map(|a
| a
.kind()) == Some(SyntaxKind
::MODULE
) {
599 last_ancestor
= Some(next_ancestor
);
601 last_ancestor
.map(GeneratedFunctionTarget
::BehindItem
)
604 fn next_space_for_fn_in_module(
605 db
: &dyn hir
::db
::AstDatabase
,
606 module_source
: &hir
::InFile
<hir
::ModuleSource
>,
607 ) -> Option
<(FileId
, GeneratedFunctionTarget
)> {
608 let file
= module_source
.file_id
.original_file(db
);
609 let assist_item
= match &module_source
.value
{
610 hir
::ModuleSource
::SourceFile(it
) => match it
.items().last() {
611 Some(last_item
) => GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone()),
612 None
=> GeneratedFunctionTarget
::BehindItem(it
.syntax().clone()),
614 hir
::ModuleSource
::Module(it
) => match it
.item_list().and_then(|it
| it
.items().last()) {
615 Some(last_item
) => GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone()),
616 None
=> GeneratedFunctionTarget
::InEmptyItemList(it
.item_list()?
.syntax().clone()),
618 hir
::ModuleSource
::BlockExpr(it
) => {
619 if let Some(last_item
) =
620 it
.statements().take_while(|stmt
| matches
!(stmt
, ast
::Stmt
::Item(_
))).last()
622 GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone())
624 GeneratedFunctionTarget
::InEmptyItemList(it
.syntax().clone())
628 Some((file
, assist_item
))
631 fn next_space_for_fn_in_impl(impl_
: &ast
::Impl
) -> Option
<GeneratedFunctionTarget
> {
632 if let Some(last_item
) = impl_
.assoc_item_list().and_then(|it
| it
.assoc_items().last()) {
633 Some(GeneratedFunctionTarget
::BehindItem(last_item
.syntax().clone()))
635 Some(GeneratedFunctionTarget
::InEmptyItemList(impl_
.assoc_item_list()?
.syntax().clone()))
639 fn module_is_descendant(module
: &hir
::Module
, ans
: &hir
::Module
, ctx
: &AssistContext
<'_
>) -> bool
{
643 for c
in ans
.children(ctx
.sema
.db
) {
644 if module_is_descendant(module
, &c
, ctx
) {
653 use crate::tests
::{check_assist, check_assist_not_applicable}
;
658 fn add_function_with_no_args() {
679 fn add_function_from_method() {
680 // This ensures that the function is correctly generated
681 // in the next outer mod or file
706 fn add_function_directly_after_current_block() {
707 // The new fn should not be created at the end of the file or module
732 fn add_function_with_no_args_in_same_module() {
757 fn add_function_with_upper_camel_case_arg() {
772 fn bar(baz_baz: BazBaz) ${0:-> _} {
780 fn add_function_with_upper_camel_case_arg_as_cast() {
786 bar$0(&BazBaz as *const BazBaz);
792 bar(&BazBaz as *const BazBaz);
795 fn bar(baz_baz: *const BazBaz) ${0:-> _} {
803 fn add_function_with_function_call_arg() {
808 fn baz() -> Baz { todo!() }
815 fn baz() -> Baz { todo!() }
820 fn bar(baz: Baz) ${0:-> _} {
828 fn add_function_with_method_call_arg() {
834 fn foo(&self) -> Baz {
837 fn baz(&self) -> Baz {
845 fn foo(&self) -> Baz {
848 fn baz(&self) -> Baz {
853 fn bar(baz: Baz) -> Baz {
861 fn add_function_with_string_literal_arg() {
882 fn add_function_with_char_literal_arg() {
903 fn add_function_with_int_literal_arg() {
924 fn add_function_with_cast_int_literal_arg() {
945 fn name_of_cast_variable_is_used() {
946 // Ensures that the name of the cast type isn't used
947 // in the generated function signature.
970 fn add_function_with_variable_arg() {
993 fn add_function_with_impl_trait_arg() {
999 fn foo() -> impl Foo {
1008 fn foo() -> impl Foo {
1015 fn bar(foo: impl Foo) {
1028 fn baz() -> Baz { todo!() }
1036 fn baz() -> Baz { todo!() }
1050 fn add_function_with_qualified_path_arg() {
1056 pub fn baz() -> Bof { Bof }
1065 pub fn baz() -> Bof { Bof }
1071 fn bar(baz: Baz::Bof) {
1079 fn add_function_with_generic_arg() {
1080 // FIXME: This is wrong, generated `bar` should include generic parameter.
1101 fn add_function_with_fn_arg() {
1102 // FIXME: The argument in `bar` is wrong.
1108 fn new() -> Self { Baz }
1117 fn new() -> Self { Baz }
1123 fn bar(new: fn) ${0:-> _} {
1131 fn add_function_with_closure_arg() {
1132 // FIXME: The argument in `bar` is wrong.
1137 let closure = |x: i64| x - 1;
1143 let closure = |x: i64| x - 1;
1147 fn bar(closure: _) {
1155 fn unresolveable_types_default_to_placeholder() {
1176 fn arg_names_dont_overlap() {
1181 fn baz() -> Baz { Baz }
1188 fn baz() -> Baz { Baz }
1193 fn bar(baz_1: Baz, baz_2: Baz) {
1201 fn arg_name_counters_start_at_1_per_name() {
1206 fn baz() -> Baz { Baz }
1208 $0bar(baz(), baz(), "foo", "bar")
1213 fn baz() -> Baz { Baz }
1215 bar(baz(), baz(), "foo", "bar")
1218 fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
1226 fn add_function_in_module() {
1238 pub(crate) fn my_fn() {
1251 fn qualified_path_uses_correct_scope() {
1274 fn baz(foo: foo::Foo) {
1282 fn add_function_in_module_containing_other_items() {
1287 fn something_else() {}
1296 fn something_else() {}
1298 pub(crate) fn my_fn() {
1311 fn add_function_in_nested_module() {
1326 pub(crate) fn my_fn() {
1340 fn add_function_in_another_file() {
1355 pub(crate) fn bar() {
1362 fn add_function_with_return_type() {
1367 let x: u32 = foo$0();
1383 fn add_function_not_applicable_if_function_already_exists() {
1384 check_assist_not_applicable(
1397 fn add_function_not_applicable_if_unresolved_variable_in_call_is_selected() {
1398 check_assist_not_applicable(
1399 // bar is resolved, but baz isn't.
1400 // The assist is only active if the cursor is on an unresolved path,
1401 // but the assist should only be offered if the path is a function call.
1414 fn create_method_with_no_args() {
1432 fn bar(&self) ${0:-> _} {
1441 fn create_function_with_async() {
1454 async fn bar(arg: i32) ${0:-> _} {
1462 fn create_method() {
1467 fn foo() {S.bar$0();}
1475 fn bar(&self) ${0:-> _} {
1484 fn create_method_within_an_impl() {
1489 fn foo() {S.bar$0();}
1497 fn bar(&self) ${0:-> _} {
1507 fn create_method_from_different_module() {
1514 fn foo() {s::S.bar$0();}
1522 pub(crate) fn bar(&self) ${0:-> _} {
1527 fn foo() {s::S.bar();}
1533 fn create_method_from_descendant_module() {
1555 fn bar(&self) ${0:-> _} {
1565 fn create_method_with_cursor_anywhere_on_call_expresion() {
1570 fn foo() {$0S.bar();}
1578 fn bar(&self) ${0:-> _} {
1587 fn create_static_method() {
1592 fn foo() {S::bar$0();}
1596 fn foo() {S::bar();}
1600 fn bar() ${0:-> _} {
1609 fn create_static_method_within_an_impl() {
1614 fn foo() {S::bar$0();}
1620 fn foo() {S::bar();}
1622 fn bar() ${0:-> _} {
1632 fn create_static_method_from_different_module() {
1639 fn foo() {s::S::bar$0();}
1647 pub(crate) fn bar() ${0:-> _} {
1652 fn foo() {s::S::bar();}
1658 fn create_static_method_with_cursor_anywhere_on_call_expresion() {
1663 fn foo() {$0S::bar();}
1667 fn foo() {S::bar();}
1671 fn bar() ${0:-> _} {
1680 fn create_static_method_within_an_impl_with_self_syntax() {
1698 fn bar() ${0:-> _} {
1707 fn no_panic_on_invalid_global_path() {
1720 fn foo() ${0:-> _} {
1728 fn handle_tuple_indexing() {
1743 fn foo(a: ()) ${0:-> _} {
1751 fn add_function_with_const_arg() {
1755 const VALUE: usize = 0;
1761 const VALUE: usize = 0;
1766 fn foo(value: usize) ${0:-> _} {
1774 fn add_function_with_static_arg() {
1778 static VALUE: usize = 0;
1784 static VALUE: usize = 0;
1789 fn foo(value: usize) ${0:-> _} {
1797 fn add_function_with_static_mut_arg() {
1801 static mut VALUE: usize = 0;
1807 static mut VALUE: usize = 0;
1812 fn foo(value: usize) ${0:-> _} {
1820 fn not_applicable_for_enum_variant() {
1821 check_assist_not_applicable(
1833 fn applicable_for_enum_method() {
1850 fn new() ${0:-> _} {