@@ -71,12 +71,13 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
7171 let path_expr: ast:: PathExpr = ctx. find_node_at_offset ( ) ?;
7272 let call = path_expr. syntax ( ) . parent ( ) . and_then ( ast:: CallExpr :: cast) ?;
7373 let path = path_expr. path ( ) ?;
74- let fn_name = fn_name ( & path) ?;
74+ let name_ref = path. segment ( ) ? . name_ref ( ) ?;
7575 if ctx. sema . resolve_path ( & path) . is_some ( ) {
7676 // The function call already resolves, no need to add a function
7777 return None ;
7878 }
7979
80+ let fn_name = & * name_ref. text ( ) ;
8081 let target_module;
8182 let mut adt_name = None ;
8283
@@ -93,7 +94,7 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
9394 if current_module. krate ( ) != module. krate ( ) {
9495 return None ;
9596 }
96- let ( impl_, file) = get_adt_source ( ctx, & adt, fn_name. text ( ) . as_str ( ) ) ?;
97+ let ( impl_, file) = get_adt_source ( ctx, & adt, fn_name) ?;
9798 let ( target, insert_offset) = get_method_target ( ctx, & module, & impl_) ?;
9899 adt_name = if impl_. is_none ( ) { Some ( adt. name ( ctx. sema . db ) ) } else { None } ;
99100 ( target, file, insert_offset)
@@ -107,7 +108,7 @@ fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
107108 get_fn_target ( ctx, & target_module, call. clone ( ) ) ?
108109 }
109110 } ;
110- let function_builder = FunctionBuilder :: from_call ( ctx, & call, & path , target_module, target) ?;
111+ let function_builder = FunctionBuilder :: from_call ( ctx, & call, fn_name , target_module, target) ?;
111112 let text_range = call. syntax ( ) . text_range ( ) ;
112113 let label = format ! ( "Generate {} function" , function_builder. fn_name) ;
113114 add_func_to_accumulator (
@@ -241,13 +242,13 @@ impl FunctionBuilder {
241242 fn from_call (
242243 ctx : & AssistContext ,
243244 call : & ast:: CallExpr ,
244- path : & ast :: Path ,
245+ fn_name : & str ,
245246 target_module : Option < hir:: Module > ,
246247 target : GeneratedFunctionTarget ,
247248 ) -> Option < Self > {
248249 let needs_pub = target_module. is_some ( ) ;
249250 let target_module = target_module. or_else ( || current_module ( target. syntax ( ) , ctx) ) ?;
250- let fn_name = fn_name ( path ) ? ;
251+ let fn_name = make :: name ( fn_name ) ;
251252 let ( type_params, params) = fn_args ( ctx, target_module, FuncExpr :: Func ( call. clone ( ) ) ) ?;
252253
253254 let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
@@ -428,11 +429,6 @@ impl GeneratedFunctionTarget {
428429 }
429430}
430431
431- fn fn_name ( call : & ast:: Path ) -> Option < ast:: Name > {
432- let name = call. segment ( ) ?. syntax ( ) . to_string ( ) ;
433- Some ( make:: name ( & name) )
434- }
435-
436432/// Computes the type variables and arguments required for the generated function
437433fn fn_args (
438434 ctx : & AssistContext ,
@@ -1646,4 +1642,25 @@ fn bar() ${0:-> _} {
16461642" ,
16471643 )
16481644 }
1645+
1646+ #[ test]
1647+ fn no_panic_on_invalid_global_path ( ) {
1648+ check_assist (
1649+ generate_function,
1650+ r#"
1651+ fn main() {
1652+ ::foo$0();
1653+ }
1654+ "# ,
1655+ r#"
1656+ fn main() {
1657+ ::foo();
1658+ }
1659+
1660+ fn foo() ${0:-> _} {
1661+ todo!()
1662+ }
1663+ "# ,
1664+ )
1665+ }
16491666}
0 commit comments