aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-10-04 20:21:30 +0100
committerBenjamin Coenen <[email protected]>2020-10-08 19:57:18 +0100
commit3bfa3e8123dd2d652019ad270622025d10b87cc8 (patch)
tree45c5761137519e63ea26fb097924394e8e7b268b /crates
parent636b413e142e2b831ded74642c8193a0dc39b4a7 (diff)
when generating new function, focus on return type instead of body
Signed-off-by: Benjamin Coenen <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/assists/src/handlers/generate_function.rs132
-rw-r--r--crates/assists/src/tests/generated.rs4
-rw-r--r--crates/syntax/src/ast/make.rs12
3 files changed, 80 insertions, 68 deletions
diff --git a/crates/assists/src/handlers/generate_function.rs b/crates/assists/src/handlers/generate_function.rs
index b38d64058..d23f4293b 100644
--- a/crates/assists/src/handlers/generate_function.rs
+++ b/crates/assists/src/handlers/generate_function.rs
@@ -36,8 +36,8 @@ use crate::{
36// bar("", baz()); 36// bar("", baz());
37// } 37// }
38// 38//
39// fn bar(arg: &str, baz: Baz) { 39// fn bar(arg: &str, baz: Baz) ${0:-> ()} {
40// ${0:todo!()} 40// todo!()
41// } 41// }
42// 42//
43// ``` 43// ```
@@ -80,9 +80,9 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
80 80
81struct FunctionTemplate { 81struct FunctionTemplate {
82 insert_offset: TextSize, 82 insert_offset: TextSize,
83 placeholder_expr: ast::MacroCall,
84 leading_ws: String, 83 leading_ws: String,
85 fn_def: ast::Fn, 84 fn_def: ast::Fn,
85 ret_type: ast::RetType,
86 trailing_ws: String, 86 trailing_ws: String,
87 file: FileId, 87 file: FileId,
88} 88}
@@ -90,11 +90,9 @@ struct FunctionTemplate {
90impl FunctionTemplate { 90impl FunctionTemplate {
91 fn to_string(&self, cap: Option<SnippetCap>) -> String { 91 fn to_string(&self, cap: Option<SnippetCap>) -> String {
92 let f = match cap { 92 let f = match cap {
93 Some(cap) => render_snippet( 93 Some(cap) => {
94 cap, 94 render_snippet(cap, self.fn_def.syntax(), Cursor::Replace(self.ret_type.syntax()))
95 self.fn_def.syntax(), 95 }
96 Cursor::Replace(self.placeholder_expr.syntax()),
97 ),
98 None => self.fn_def.to_string(), 96 None => self.fn_def.to_string(),
99 }; 97 };
100 format!("{}{}{}", self.leading_ws, f, self.trailing_ws) 98 format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
@@ -141,8 +139,14 @@ impl FunctionBuilder {
141 let placeholder_expr = make::expr_todo(); 139 let placeholder_expr = make::expr_todo();
142 let fn_body = make::block_expr(vec![], Some(placeholder_expr)); 140 let fn_body = make::block_expr(vec![], Some(placeholder_expr));
143 let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None }; 141 let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
144 let mut fn_def = 142 let mut fn_def = make::fn_(
145 make::fn_(visibility, self.fn_name, self.type_params, self.params, fn_body); 143 visibility,
144 self.fn_name,
145 self.type_params,
146 self.params,
147 fn_body,
148 Some(make::ret_type(make::ty("()"))),
149 );
146 let leading_ws; 150 let leading_ws;
147 let trailing_ws; 151 let trailing_ws;
148 152
@@ -163,12 +167,10 @@ impl FunctionBuilder {
163 } 167 }
164 }; 168 };
165 169
166 let placeholder_expr =
167 fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
168 FunctionTemplate { 170 FunctionTemplate {
169 insert_offset, 171 insert_offset,
170 placeholder_expr,
171 leading_ws, 172 leading_ws,
173 ret_type: fn_def.ret_type().unwrap(),
172 fn_def, 174 fn_def,
173 trailing_ws, 175 trailing_ws,
174 file: self.file, 176 file: self.file,
@@ -349,8 +351,8 @@ fn foo() {
349 bar(); 351 bar();
350} 352}
351 353
352fn bar() { 354fn bar() ${0:-> ()} {
353 ${0:todo!()} 355 todo!()
354} 356}
355", 357",
356 ) 358 )
@@ -376,8 +378,8 @@ impl Foo {
376 } 378 }
377} 379}
378 380
379fn bar() { 381fn bar() ${0:-> ()} {
380 ${0:todo!()} 382 todo!()
381} 383}
382", 384",
383 ) 385 )
@@ -400,8 +402,8 @@ fn foo1() {
400 bar(); 402 bar();
401} 403}
402 404
403fn bar() { 405fn bar() ${0:-> ()} {
404 ${0:todo!()} 406 todo!()
405} 407}
406 408
407fn foo2() {} 409fn foo2() {}
@@ -426,8 +428,8 @@ mod baz {
426 bar(); 428 bar();
427 } 429 }
428 430
429 fn bar() { 431 fn bar() ${0:-> ()} {
430 ${0:todo!()} 432 todo!()
431 } 433 }
432} 434}
433", 435",
@@ -452,8 +454,8 @@ fn foo() {
452 bar(baz()); 454 bar(baz());
453} 455}
454 456
455fn bar(baz: Baz) { 457fn bar(baz: Baz) ${0:-> ()} {
456 ${0:todo!()} 458 todo!()
457} 459}
458", 460",
459 ); 461 );
@@ -485,8 +487,8 @@ impl Baz {
485 } 487 }
486} 488}
487 489
488fn bar(baz: Baz) { 490fn bar(baz: Baz) ${0:-> ()} {
489 ${0:todo!()} 491 todo!()
490} 492}
491", 493",
492 ) 494 )
@@ -506,8 +508,8 @@ fn foo() {
506 bar("bar") 508 bar("bar")
507} 509}
508 510
509fn bar(arg: &str) { 511fn bar(arg: &str) ${0:-> ()} {
510 ${0:todo!()} 512 todo!()
511} 513}
512"#, 514"#,
513 ) 515 )
@@ -527,8 +529,8 @@ fn foo() {
527 bar('x') 529 bar('x')
528} 530}
529 531
530fn bar(arg: char) { 532fn bar(arg: char) ${0:-> ()} {
531 ${0:todo!()} 533 todo!()
532} 534}
533"#, 535"#,
534 ) 536 )
@@ -548,8 +550,8 @@ fn foo() {
548 bar(42) 550 bar(42)
549} 551}
550 552
551fn bar(arg: i32) { 553fn bar(arg: i32) ${0:-> ()} {
552 ${0:todo!()} 554 todo!()
553} 555}
554", 556",
555 ) 557 )
@@ -569,8 +571,8 @@ fn foo() {
569 bar(42 as u8) 571 bar(42 as u8)
570} 572}
571 573
572fn bar(arg: u8) { 574fn bar(arg: u8) ${0:-> ()} {
573 ${0:todo!()} 575 todo!()
574} 576}
575", 577",
576 ) 578 )
@@ -594,8 +596,8 @@ fn foo() {
594 bar(x as u8) 596 bar(x as u8)
595} 597}
596 598
597fn bar(x: u8) { 599fn bar(x: u8) ${0:-> ()} {
598 ${0:todo!()} 600 todo!()
599} 601}
600", 602",
601 ) 603 )
@@ -617,8 +619,8 @@ fn foo() {
617 bar(worble) 619 bar(worble)
618} 620}
619 621
620fn bar(worble: ()) { 622fn bar(worble: ()) ${0:-> ()} {
621 ${0:todo!()} 623 todo!()
622} 624}
623", 625",
624 ) 626 )
@@ -646,8 +648,8 @@ fn baz() {
646 bar(foo()) 648 bar(foo())
647} 649}
648 650
649fn bar(foo: impl Foo) { 651fn bar(foo: impl Foo) ${0:-> ()} {
650 ${0:todo!()} 652 todo!()
651} 653}
652", 654",
653 ) 655 )
@@ -673,8 +675,8 @@ fn foo() {
673 bar(&baz()) 675 bar(&baz())
674} 676}
675 677
676fn bar(baz: &Baz) { 678fn bar(baz: &Baz) ${0:-> ()} {
677 ${0:todo!()} 679 todo!()
678} 680}
679", 681",
680 ) 682 )
@@ -702,8 +704,8 @@ fn foo() {
702 bar(Baz::baz()) 704 bar(Baz::baz())
703} 705}
704 706
705fn bar(baz: Baz::Bof) { 707fn bar(baz: Baz::Bof) ${0:-> ()} {
706 ${0:todo!()} 708 todo!()
707} 709}
708", 710",
709 ) 711 )
@@ -725,8 +727,8 @@ fn foo<T>(t: T) {
725 bar(t) 727 bar(t)
726} 728}
727 729
728fn bar<T>(t: T) { 730fn bar<T>(t: T) ${0:-> ()} {
729 ${0:todo!()} 731 todo!()
730} 732}
731", 733",
732 ) 734 )
@@ -756,8 +758,8 @@ fn foo() {
756 bar(Baz::new); 758 bar(Baz::new);
757} 759}
758 760
759fn bar(arg: fn() -> Baz) { 761fn bar(arg: fn() -> Baz) ${0:-> ()} {
760 ${0:todo!()} 762 todo!()
761} 763}
762", 764",
763 ) 765 )
@@ -781,8 +783,8 @@ fn foo() {
781 bar(closure) 783 bar(closure)
782} 784}
783 785
784fn bar(closure: impl Fn(i64) -> i64) { 786fn bar(closure: impl Fn(i64) -> i64) ${0:-> ()} {
785 ${0:todo!()} 787 todo!()
786} 788}
787", 789",
788 ) 790 )
@@ -802,8 +804,8 @@ fn foo() {
802 bar(baz) 804 bar(baz)
803} 805}
804 806
805fn bar(baz: ()) { 807fn bar(baz: ()) ${0:-> ()} {
806 ${0:todo!()} 808 todo!()
807} 809}
808", 810",
809 ) 811 )
@@ -827,8 +829,8 @@ fn foo() {
827 bar(baz(), baz()) 829 bar(baz(), baz())
828} 830}
829 831
830fn bar(baz_1: Baz, baz_2: Baz) { 832fn bar(baz_1: Baz, baz_2: Baz) ${0:-> ()} {
831 ${0:todo!()} 833 todo!()
832} 834}
833", 835",
834 ) 836 )
@@ -852,8 +854,8 @@ fn foo() {
852 bar(baz(), baz(), "foo", "bar") 854 bar(baz(), baz(), "foo", "bar")
853} 855}
854 856
855fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) { 857fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) ${0:-> ()} {
856 ${0:todo!()} 858 todo!()
857} 859}
858"#, 860"#,
859 ) 861 )
@@ -872,8 +874,8 @@ fn foo() {
872", 874",
873 r" 875 r"
874mod bar { 876mod bar {
875 pub(crate) fn my_fn() { 877 pub(crate) fn my_fn() ${0:-> ()} {
876 ${0:todo!()} 878 todo!()
877 } 879 }
878} 880}
879 881
@@ -911,8 +913,8 @@ fn bar() {
911 baz(foo) 913 baz(foo)
912} 914}
913 915
914fn baz(foo: foo::Foo) { 916fn baz(foo: foo::Foo) ${0:-> ()} {
915 ${0:todo!()} 917 todo!()
916} 918}
917", 919",
918 ) 920 )
@@ -935,8 +937,8 @@ fn foo() {
935mod bar { 937mod bar {
936 fn something_else() {} 938 fn something_else() {}
937 939
938 pub(crate) fn my_fn() { 940 pub(crate) fn my_fn() ${0:-> ()} {
939 ${0:todo!()} 941 todo!()
940 } 942 }
941} 943}
942 944
@@ -963,8 +965,8 @@ fn foo() {
963 r" 965 r"
964mod bar { 966mod bar {
965 mod baz { 967 mod baz {
966 pub(crate) fn my_fn() { 968 pub(crate) fn my_fn() ${0:-> ()} {
967 ${0:todo!()} 969 todo!()
968 } 970 }
969 } 971 }
970} 972}
@@ -992,8 +994,8 @@ fn main() {
992 r" 994 r"
993 995
994 996
995pub(crate) fn bar() { 997pub(crate) fn bar() ${0:-> ()} {
996 ${0:todo!()} 998 todo!()
997}", 999}",
998 ) 1000 )
999 } 1001 }
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs
index 7f6e98a54..41f536574 100644
--- a/crates/assists/src/tests/generated.rs
+++ b/crates/assists/src/tests/generated.rs
@@ -454,8 +454,8 @@ fn foo() {
454 bar("", baz()); 454 bar("", baz());
455} 455}
456 456
457fn bar(arg: &str, baz: Baz) { 457fn bar(arg: &str, baz: Baz) ${0:-> ()} {
458 ${0:todo!()} 458 todo!()
459} 459}
460 460
461"#####, 461"#####,
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index 3a184094c..74dbdfaf7 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -320,6 +320,10 @@ pub fn param(name: String, ty: String) -> ast::Param {
320 ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty)) 320 ast_from_text(&format!("fn f({}: {}) {{ }}", name, ty))
321} 321}
322 322
323pub fn ret_type(ty: ast::Type) -> ast::RetType {
324 ast_from_text(&format!("fn f() -> {} {{ }}", ty))
325}
326
323pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList { 327pub fn param_list(pats: impl IntoIterator<Item = ast::Param>) -> ast::ParamList {
324 let args = pats.into_iter().join(", "); 328 let args = pats.into_iter().join(", ");
325 ast_from_text(&format!("fn f({}) {{ }}", args)) 329 ast_from_text(&format!("fn f({}) {{ }}", args))
@@ -350,14 +354,20 @@ pub fn fn_(
350 type_params: Option<ast::GenericParamList>, 354 type_params: Option<ast::GenericParamList>,
351 params: ast::ParamList, 355 params: ast::ParamList,
352 body: ast::BlockExpr, 356 body: ast::BlockExpr,
357 ret_type: Option<ast::RetType>,
353) -> ast::Fn { 358) -> ast::Fn {
354 let type_params = 359 let type_params =
355 if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() }; 360 if let Some(type_params) = type_params { format!("<{}>", type_params) } else { "".into() };
361 let ret_type = if let Some(ret_type) = ret_type { format!("{} ", ret_type) } else { "".into() };
356 let visibility = match visibility { 362 let visibility = match visibility {
357 None => String::new(), 363 None => String::new(),
358 Some(it) => format!("{} ", it), 364 Some(it) => format!("{} ", it),
359 }; 365 };
360 ast_from_text(&format!("{}fn {}{}{} {}", visibility, fn_name, type_params, params, body)) 366
367 ast_from_text(&format!(
368 "{}fn {}{}{} {}{}",
369 visibility, fn_name, type_params, params, ret_type, body
370 ))
361} 371}
362 372
363fn ast_from_text<N: AstNode>(text: &str) -> N { 373fn ast_from_text<N: AstNode>(text: &str) -> N {