aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/add_function.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/add_function.rs')
-rw-r--r--crates/ra_assists/src/handlers/add_function.rs137
1 files changed, 79 insertions, 58 deletions
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs
index de016ae4e..a0709630d 100644
--- a/crates/ra_assists/src/handlers/add_function.rs
+++ b/crates/ra_assists/src/handlers/add_function.rs
@@ -4,13 +4,13 @@ use ra_syntax::{
4 ast::{ 4 ast::{
5 self, 5 self,
6 edit::{AstNodeEdit, IndentLevel}, 6 edit::{AstNodeEdit, IndentLevel},
7 ArgListOwner, AstNode, ModuleItemOwner, 7 make, ArgListOwner, AstNode, ModuleItemOwner,
8 }, 8 },
9 SyntaxKind, SyntaxNode, TextSize, 9 SyntaxKind, SyntaxNode, TextSize,
10}; 10};
11use rustc_hash::{FxHashMap, FxHashSet}; 11use rustc_hash::{FxHashMap, FxHashSet};
12 12
13use crate::{AssistContext, AssistId, Assists}; 13use crate::{assist_config::SnippetCap, utils::render_snippet, AssistContext, AssistId, Assists};
14 14
15// Assist: add_function 15// Assist: add_function
16// 16//
@@ -33,7 +33,7 @@ use crate::{AssistContext, AssistId, Assists};
33// } 33// }
34// 34//
35// fn bar(arg: &str, baz: Baz) { 35// fn bar(arg: &str, baz: Baz) {
36// todo!() 36// ${0:todo!()}
37// } 37// }
38// 38//
39// ``` 39// ```
@@ -58,21 +58,36 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?; 58 let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
59 59
60 let target = call.syntax().text_range(); 60 let target = call.syntax().text_range();
61 acc.add(AssistId("add_function"), "Add function", target, |edit| { 61 acc.add(AssistId("add_function"), "Add function", target, |builder| {
62 let function_template = function_builder.render(); 62 let function_template = function_builder.render();
63 edit.set_file(function_template.file); 63 builder.set_file(function_template.file);
64 edit.set_cursor(function_template.cursor_offset); 64 let new_fn = function_template.to_string(ctx.config.snippet_cap);
65 edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); 65 match ctx.config.snippet_cap {
66 Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
67 None => builder.insert(function_template.insert_offset, new_fn),
68 }
66 }) 69 })
67} 70}
68 71
69struct FunctionTemplate { 72struct FunctionTemplate {
70 insert_offset: TextSize, 73 insert_offset: TextSize,
71 cursor_offset: TextSize, 74 placeholder_expr: ast::MacroCall,
72 fn_def: ast::SourceFile, 75 leading_ws: String,
76 fn_def: ast::FnDef,
77 trailing_ws: String,
73 file: FileId, 78 file: FileId,
74} 79}
75 80
81impl FunctionTemplate {
82 fn to_string(&self, cap: Option<SnippetCap>) -> String {
83 let f = match cap {
84 Some(cap) => render_snippet(cap, self.fn_def.syntax(), self.placeholder_expr.syntax()),
85 None => self.fn_def.to_string(),
86 };
87 format!("{}{}{}", self.leading_ws, f, self.trailing_ws)
88 }
89}
90
76struct FunctionBuilder { 91struct FunctionBuilder {
77 target: GeneratedFunctionTarget, 92 target: GeneratedFunctionTarget,
78 fn_name: ast::Name, 93 fn_name: ast::Name,
@@ -110,35 +125,41 @@ impl FunctionBuilder {
110 } 125 }
111 126
112 fn render(self) -> FunctionTemplate { 127 fn render(self) -> FunctionTemplate {
113 let placeholder_expr = ast::make::expr_todo(); 128 let placeholder_expr = make::expr_todo();
114 let fn_body = ast::make::block_expr(vec![], Some(placeholder_expr)); 129 let fn_body = make::block_expr(vec![], Some(placeholder_expr));
115 let mut fn_def = ast::make::fn_def(self.fn_name, self.type_params, self.params, fn_body); 130 let visibility = if self.needs_pub { Some(make::visibility_pub_crate()) } else { None };
116 if self.needs_pub { 131 let mut fn_def =
117 fn_def = ast::make::add_pub_crate_modifier(fn_def); 132 make::fn_def(visibility, self.fn_name, self.type_params, self.params, fn_body);
118 } 133 let leading_ws;
119 134 let trailing_ws;
120 let (fn_def, insert_offset) = match self.target { 135
136 let insert_offset = match self.target {
121 GeneratedFunctionTarget::BehindItem(it) => { 137 GeneratedFunctionTarget::BehindItem(it) => {
122 let with_leading_blank_line = ast::make::add_leading_newlines(2, fn_def); 138 let indent = IndentLevel::from_node(&it);
123 let indented = with_leading_blank_line.indent(IndentLevel::from_node(&it)); 139 leading_ws = format!("\n\n{}", indent);
124 (indented, it.text_range().end()) 140 fn_def = fn_def.indent(indent);
141 trailing_ws = String::new();
142 it.text_range().end()
125 } 143 }
126 GeneratedFunctionTarget::InEmptyItemList(it) => { 144 GeneratedFunctionTarget::InEmptyItemList(it) => {
127 let indent_once = IndentLevel(1);
128 let indent = IndentLevel::from_node(it.syntax()); 145 let indent = IndentLevel::from_node(it.syntax());
129 let fn_def = ast::make::add_leading_newlines(1, fn_def); 146 leading_ws = format!("\n{}", indent + 1);
130 let fn_def = fn_def.indent(indent_once); 147 fn_def = fn_def.indent(indent + 1);
131 let fn_def = ast::make::add_trailing_newlines(1, fn_def); 148 trailing_ws = format!("\n{}", indent);
132 let fn_def = fn_def.indent(indent); 149 it.syntax().text_range().start() + TextSize::of('{')
133 (fn_def, it.syntax().text_range().start() + TextSize::of('{'))
134 } 150 }
135 }; 151 };
136 152
137 let placeholder_expr = 153 let placeholder_expr =
138 fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); 154 fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
139 let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start(); 155 FunctionTemplate {
140 let cursor_offset = insert_offset + cursor_offset_from_fn_start; 156 insert_offset,
141 FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file } 157 placeholder_expr,
158 leading_ws,
159 fn_def,
160 trailing_ws,
161 file: self.file,
162 }
142 } 163 }
143} 164}
144 165
@@ -158,7 +179,7 @@ impl GeneratedFunctionTarget {
158 179
159fn fn_name(call: &ast::Path) -> Option<ast::Name> { 180fn fn_name(call: &ast::Path) -> Option<ast::Name> {
160 let name = call.segment()?.syntax().to_string(); 181 let name = call.segment()?.syntax().to_string();
161 Some(ast::make::name(&name)) 182 Some(make::name(&name))
162} 183}
163 184
164/// Computes the type variables and arguments required for the generated function 185/// Computes the type variables and arguments required for the generated function
@@ -180,8 +201,8 @@ fn fn_args(
180 }); 201 });
181 } 202 }
182 deduplicate_arg_names(&mut arg_names); 203 deduplicate_arg_names(&mut arg_names);
183 let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| ast::make::param(name, ty)); 204 let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| make::param(name, ty));
184 Some((None, ast::make::param_list(params))) 205 Some((None, make::param_list(params)))
185} 206}
186 207
187/// Makes duplicate argument names unique by appending incrementing numbers. 208/// Makes duplicate argument names unique by appending incrementing numbers.
@@ -316,7 +337,7 @@ fn foo() {
316} 337}
317 338
318fn bar() { 339fn bar() {
319 <|>todo!() 340 ${0:todo!()}
320} 341}
321", 342",
322 ) 343 )
@@ -343,7 +364,7 @@ impl Foo {
343} 364}
344 365
345fn bar() { 366fn bar() {
346 <|>todo!() 367 ${0:todo!()}
347} 368}
348", 369",
349 ) 370 )
@@ -367,7 +388,7 @@ fn foo1() {
367} 388}
368 389
369fn bar() { 390fn bar() {
370 <|>todo!() 391 ${0:todo!()}
371} 392}
372 393
373fn foo2() {} 394fn foo2() {}
@@ -393,7 +414,7 @@ mod baz {
393 } 414 }
394 415
395 fn bar() { 416 fn bar() {
396 <|>todo!() 417 ${0:todo!()}
397 } 418 }
398} 419}
399", 420",
@@ -419,7 +440,7 @@ fn foo() {
419} 440}
420 441
421fn bar(baz: Baz) { 442fn bar(baz: Baz) {
422 <|>todo!() 443 ${0:todo!()}
423} 444}
424", 445",
425 ); 446 );
@@ -452,7 +473,7 @@ impl Baz {
452} 473}
453 474
454fn bar(baz: Baz) { 475fn bar(baz: Baz) {
455 <|>todo!() 476 ${0:todo!()}
456} 477}
457", 478",
458 ) 479 )
@@ -473,7 +494,7 @@ fn foo() {
473} 494}
474 495
475fn bar(arg: &str) { 496fn bar(arg: &str) {
476 <|>todo!() 497 ${0:todo!()}
477} 498}
478"#, 499"#,
479 ) 500 )
@@ -494,7 +515,7 @@ fn foo() {
494} 515}
495 516
496fn bar(arg: char) { 517fn bar(arg: char) {
497 <|>todo!() 518 ${0:todo!()}
498} 519}
499"#, 520"#,
500 ) 521 )
@@ -515,7 +536,7 @@ fn foo() {
515} 536}
516 537
517fn bar(arg: i32) { 538fn bar(arg: i32) {
518 <|>todo!() 539 ${0:todo!()}
519} 540}
520", 541",
521 ) 542 )
@@ -536,7 +557,7 @@ fn foo() {
536} 557}
537 558
538fn bar(arg: u8) { 559fn bar(arg: u8) {
539 <|>todo!() 560 ${0:todo!()}
540} 561}
541", 562",
542 ) 563 )
@@ -561,7 +582,7 @@ fn foo() {
561} 582}
562 583
563fn bar(x: u8) { 584fn bar(x: u8) {
564 <|>todo!() 585 ${0:todo!()}
565} 586}
566", 587",
567 ) 588 )
@@ -584,7 +605,7 @@ fn foo() {
584} 605}
585 606
586fn bar(worble: ()) { 607fn bar(worble: ()) {
587 <|>todo!() 608 ${0:todo!()}
588} 609}
589", 610",
590 ) 611 )
@@ -613,7 +634,7 @@ fn baz() {
613} 634}
614 635
615fn bar(foo: impl Foo) { 636fn bar(foo: impl Foo) {
616 <|>todo!() 637 ${0:todo!()}
617} 638}
618", 639",
619 ) 640 )
@@ -640,7 +661,7 @@ fn foo() {
640} 661}
641 662
642fn bar(baz: &Baz) { 663fn bar(baz: &Baz) {
643 <|>todo!() 664 ${0:todo!()}
644} 665}
645", 666",
646 ) 667 )
@@ -669,7 +690,7 @@ fn foo() {
669} 690}
670 691
671fn bar(baz: Baz::Bof) { 692fn bar(baz: Baz::Bof) {
672 <|>todo!() 693 ${0:todo!()}
673} 694}
674", 695",
675 ) 696 )
@@ -692,7 +713,7 @@ fn foo<T>(t: T) {
692} 713}
693 714
694fn bar<T>(t: T) { 715fn bar<T>(t: T) {
695 <|>todo!() 716 ${0:todo!()}
696} 717}
697", 718",
698 ) 719 )
@@ -723,7 +744,7 @@ fn foo() {
723} 744}
724 745
725fn bar(arg: fn() -> Baz) { 746fn bar(arg: fn() -> Baz) {
726 <|>todo!() 747 ${0:todo!()}
727} 748}
728", 749",
729 ) 750 )
@@ -748,7 +769,7 @@ fn foo() {
748} 769}
749 770
750fn bar(closure: impl Fn(i64) -> i64) { 771fn bar(closure: impl Fn(i64) -> i64) {
751 <|>todo!() 772 ${0:todo!()}
752} 773}
753", 774",
754 ) 775 )
@@ -769,7 +790,7 @@ fn foo() {
769} 790}
770 791
771fn bar(baz: ()) { 792fn bar(baz: ()) {
772 <|>todo!() 793 ${0:todo!()}
773} 794}
774", 795",
775 ) 796 )
@@ -794,7 +815,7 @@ fn foo() {
794} 815}
795 816
796fn bar(baz_1: Baz, baz_2: Baz) { 817fn bar(baz_1: Baz, baz_2: Baz) {
797 <|>todo!() 818 ${0:todo!()}
798} 819}
799", 820",
800 ) 821 )
@@ -819,7 +840,7 @@ fn foo() {
819} 840}
820 841
821fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) { 842fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
822 <|>todo!() 843 ${0:todo!()}
823} 844}
824"#, 845"#,
825 ) 846 )
@@ -839,7 +860,7 @@ fn foo() {
839 r" 860 r"
840mod bar { 861mod bar {
841 pub(crate) fn my_fn() { 862 pub(crate) fn my_fn() {
842 <|>todo!() 863 ${0:todo!()}
843 } 864 }
844} 865}
845 866
@@ -878,7 +899,7 @@ fn bar() {
878} 899}
879 900
880fn baz(foo: foo::Foo) { 901fn baz(foo: foo::Foo) {
881 <|>todo!() 902 ${0:todo!()}
882} 903}
883", 904",
884 ) 905 )
@@ -902,7 +923,7 @@ mod bar {
902 fn something_else() {} 923 fn something_else() {}
903 924
904 pub(crate) fn my_fn() { 925 pub(crate) fn my_fn() {
905 <|>todo!() 926 ${0:todo!()}
906 } 927 }
907} 928}
908 929
@@ -930,7 +951,7 @@ fn foo() {
930mod bar { 951mod bar {
931 mod baz { 952 mod baz {
932 pub(crate) fn my_fn() { 953 pub(crate) fn my_fn() {
933 <|>todo!() 954 ${0:todo!()}
934 } 955 }
935 } 956 }
936} 957}
@@ -959,7 +980,7 @@ fn main() {
959 980
960 981
961pub(crate) fn bar() { 982pub(crate) fn bar() {
962 <|>todo!() 983 ${0:todo!()}
963}", 984}",
964 ) 985 )
965 } 986 }