diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_assists/src/handlers/add_function.rs | 79 | ||||
-rw-r--r-- | crates/ra_assists/src/tests/generated.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/utils.rs | 24 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 8 |
4 files changed, 74 insertions, 39 deletions
diff --git a/crates/ra_assists/src/handlers/add_function.rs b/crates/ra_assists/src/handlers/add_function.rs index de016ae4e..69fede00f 100644 --- a/crates/ra_assists/src/handlers/add_function.rs +++ b/crates/ra_assists/src/handlers/add_function.rs | |||
@@ -10,7 +10,7 @@ use ra_syntax::{ | |||
10 | }; | 10 | }; |
11 | use rustc_hash::{FxHashMap, FxHashSet}; | 11 | use rustc_hash::{FxHashMap, FxHashSet}; |
12 | 12 | ||
13 | use crate::{AssistContext, AssistId, Assists}; | 13 | use crate::{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,18 +58,27 @@ 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 | match ctx.config.snippet_cap { |
65 | edit.insert(function_template.insert_offset, function_template.fn_def.to_string()); | 65 | Some(cap) => { |
66 | let snippet = render_snippet( | ||
67 | function_template.fn_def.syntax(), | ||
68 | function_template.placeholder_expr.syntax(), | ||
69 | ); | ||
70 | builder.insert_snippet(cap, function_template.insert_offset, snippet) | ||
71 | } | ||
72 | None => builder | ||
73 | .insert(function_template.insert_offset, function_template.fn_def.to_string()), | ||
74 | } | ||
66 | }) | 75 | }) |
67 | } | 76 | } |
68 | 77 | ||
69 | struct FunctionTemplate { | 78 | struct FunctionTemplate { |
70 | insert_offset: TextSize, | 79 | insert_offset: TextSize, |
71 | cursor_offset: TextSize, | ||
72 | fn_def: ast::SourceFile, | 80 | fn_def: ast::SourceFile, |
81 | placeholder_expr: ast::MacroCall, | ||
73 | file: FileId, | 82 | file: FileId, |
74 | } | 83 | } |
75 | 84 | ||
@@ -136,9 +145,7 @@ impl FunctionBuilder { | |||
136 | 145 | ||
137 | let placeholder_expr = | 146 | let placeholder_expr = |
138 | fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); | 147 | fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); |
139 | let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start(); | 148 | FunctionTemplate { insert_offset, placeholder_expr, fn_def, file: self.file } |
140 | let cursor_offset = insert_offset + cursor_offset_from_fn_start; | ||
141 | FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file } | ||
142 | } | 149 | } |
143 | } | 150 | } |
144 | 151 | ||
@@ -316,7 +323,7 @@ fn foo() { | |||
316 | } | 323 | } |
317 | 324 | ||
318 | fn bar() { | 325 | fn bar() { |
319 | <|>todo!() | 326 | ${0:todo!()} |
320 | } | 327 | } |
321 | ", | 328 | ", |
322 | ) | 329 | ) |
@@ -343,7 +350,7 @@ impl Foo { | |||
343 | } | 350 | } |
344 | 351 | ||
345 | fn bar() { | 352 | fn bar() { |
346 | <|>todo!() | 353 | ${0:todo!()} |
347 | } | 354 | } |
348 | ", | 355 | ", |
349 | ) | 356 | ) |
@@ -367,7 +374,7 @@ fn foo1() { | |||
367 | } | 374 | } |
368 | 375 | ||
369 | fn bar() { | 376 | fn bar() { |
370 | <|>todo!() | 377 | ${0:todo!()} |
371 | } | 378 | } |
372 | 379 | ||
373 | fn foo2() {} | 380 | fn foo2() {} |
@@ -393,7 +400,7 @@ mod baz { | |||
393 | } | 400 | } |
394 | 401 | ||
395 | fn bar() { | 402 | fn bar() { |
396 | <|>todo!() | 403 | ${0:todo!()} |
397 | } | 404 | } |
398 | } | 405 | } |
399 | ", | 406 | ", |
@@ -419,7 +426,7 @@ fn foo() { | |||
419 | } | 426 | } |
420 | 427 | ||
421 | fn bar(baz: Baz) { | 428 | fn bar(baz: Baz) { |
422 | <|>todo!() | 429 | ${0:todo!()} |
423 | } | 430 | } |
424 | ", | 431 | ", |
425 | ); | 432 | ); |
@@ -452,7 +459,7 @@ impl Baz { | |||
452 | } | 459 | } |
453 | 460 | ||
454 | fn bar(baz: Baz) { | 461 | fn bar(baz: Baz) { |
455 | <|>todo!() | 462 | ${0:todo!()} |
456 | } | 463 | } |
457 | ", | 464 | ", |
458 | ) | 465 | ) |
@@ -473,7 +480,7 @@ fn foo() { | |||
473 | } | 480 | } |
474 | 481 | ||
475 | fn bar(arg: &str) { | 482 | fn bar(arg: &str) { |
476 | <|>todo!() | 483 | ${0:todo!()} |
477 | } | 484 | } |
478 | "#, | 485 | "#, |
479 | ) | 486 | ) |
@@ -494,7 +501,7 @@ fn foo() { | |||
494 | } | 501 | } |
495 | 502 | ||
496 | fn bar(arg: char) { | 503 | fn bar(arg: char) { |
497 | <|>todo!() | 504 | ${0:todo!()} |
498 | } | 505 | } |
499 | "#, | 506 | "#, |
500 | ) | 507 | ) |
@@ -515,7 +522,7 @@ fn foo() { | |||
515 | } | 522 | } |
516 | 523 | ||
517 | fn bar(arg: i32) { | 524 | fn bar(arg: i32) { |
518 | <|>todo!() | 525 | ${0:todo!()} |
519 | } | 526 | } |
520 | ", | 527 | ", |
521 | ) | 528 | ) |
@@ -536,7 +543,7 @@ fn foo() { | |||
536 | } | 543 | } |
537 | 544 | ||
538 | fn bar(arg: u8) { | 545 | fn bar(arg: u8) { |
539 | <|>todo!() | 546 | ${0:todo!()} |
540 | } | 547 | } |
541 | ", | 548 | ", |
542 | ) | 549 | ) |
@@ -561,7 +568,7 @@ fn foo() { | |||
561 | } | 568 | } |
562 | 569 | ||
563 | fn bar(x: u8) { | 570 | fn bar(x: u8) { |
564 | <|>todo!() | 571 | ${0:todo!()} |
565 | } | 572 | } |
566 | ", | 573 | ", |
567 | ) | 574 | ) |
@@ -584,7 +591,7 @@ fn foo() { | |||
584 | } | 591 | } |
585 | 592 | ||
586 | fn bar(worble: ()) { | 593 | fn bar(worble: ()) { |
587 | <|>todo!() | 594 | ${0:todo!()} |
588 | } | 595 | } |
589 | ", | 596 | ", |
590 | ) | 597 | ) |
@@ -613,7 +620,7 @@ fn baz() { | |||
613 | } | 620 | } |
614 | 621 | ||
615 | fn bar(foo: impl Foo) { | 622 | fn bar(foo: impl Foo) { |
616 | <|>todo!() | 623 | ${0:todo!()} |
617 | } | 624 | } |
618 | ", | 625 | ", |
619 | ) | 626 | ) |
@@ -640,7 +647,7 @@ fn foo() { | |||
640 | } | 647 | } |
641 | 648 | ||
642 | fn bar(baz: &Baz) { | 649 | fn bar(baz: &Baz) { |
643 | <|>todo!() | 650 | ${0:todo!()} |
644 | } | 651 | } |
645 | ", | 652 | ", |
646 | ) | 653 | ) |
@@ -669,7 +676,7 @@ fn foo() { | |||
669 | } | 676 | } |
670 | 677 | ||
671 | fn bar(baz: Baz::Bof) { | 678 | fn bar(baz: Baz::Bof) { |
672 | <|>todo!() | 679 | ${0:todo!()} |
673 | } | 680 | } |
674 | ", | 681 | ", |
675 | ) | 682 | ) |
@@ -692,7 +699,7 @@ fn foo<T>(t: T) { | |||
692 | } | 699 | } |
693 | 700 | ||
694 | fn bar<T>(t: T) { | 701 | fn bar<T>(t: T) { |
695 | <|>todo!() | 702 | ${0:todo!()} |
696 | } | 703 | } |
697 | ", | 704 | ", |
698 | ) | 705 | ) |
@@ -723,7 +730,7 @@ fn foo() { | |||
723 | } | 730 | } |
724 | 731 | ||
725 | fn bar(arg: fn() -> Baz) { | 732 | fn bar(arg: fn() -> Baz) { |
726 | <|>todo!() | 733 | ${0:todo!()} |
727 | } | 734 | } |
728 | ", | 735 | ", |
729 | ) | 736 | ) |
@@ -748,7 +755,7 @@ fn foo() { | |||
748 | } | 755 | } |
749 | 756 | ||
750 | fn bar(closure: impl Fn(i64) -> i64) { | 757 | fn bar(closure: impl Fn(i64) -> i64) { |
751 | <|>todo!() | 758 | ${0:todo!()} |
752 | } | 759 | } |
753 | ", | 760 | ", |
754 | ) | 761 | ) |
@@ -769,7 +776,7 @@ fn foo() { | |||
769 | } | 776 | } |
770 | 777 | ||
771 | fn bar(baz: ()) { | 778 | fn bar(baz: ()) { |
772 | <|>todo!() | 779 | ${0:todo!()} |
773 | } | 780 | } |
774 | ", | 781 | ", |
775 | ) | 782 | ) |
@@ -794,7 +801,7 @@ fn foo() { | |||
794 | } | 801 | } |
795 | 802 | ||
796 | fn bar(baz_1: Baz, baz_2: Baz) { | 803 | fn bar(baz_1: Baz, baz_2: Baz) { |
797 | <|>todo!() | 804 | ${0:todo!()} |
798 | } | 805 | } |
799 | ", | 806 | ", |
800 | ) | 807 | ) |
@@ -819,7 +826,7 @@ fn foo() { | |||
819 | } | 826 | } |
820 | 827 | ||
821 | fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) { | 828 | fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) { |
822 | <|>todo!() | 829 | ${0:todo!()} |
823 | } | 830 | } |
824 | "#, | 831 | "#, |
825 | ) | 832 | ) |
@@ -839,7 +846,7 @@ fn foo() { | |||
839 | r" | 846 | r" |
840 | mod bar { | 847 | mod bar { |
841 | pub(crate) fn my_fn() { | 848 | pub(crate) fn my_fn() { |
842 | <|>todo!() | 849 | ${0:todo!()} |
843 | } | 850 | } |
844 | } | 851 | } |
845 | 852 | ||
@@ -878,7 +885,7 @@ fn bar() { | |||
878 | } | 885 | } |
879 | 886 | ||
880 | fn baz(foo: foo::Foo) { | 887 | fn baz(foo: foo::Foo) { |
881 | <|>todo!() | 888 | ${0:todo!()} |
882 | } | 889 | } |
883 | ", | 890 | ", |
884 | ) | 891 | ) |
@@ -902,7 +909,7 @@ mod bar { | |||
902 | fn something_else() {} | 909 | fn something_else() {} |
903 | 910 | ||
904 | pub(crate) fn my_fn() { | 911 | pub(crate) fn my_fn() { |
905 | <|>todo!() | 912 | ${0:todo!()} |
906 | } | 913 | } |
907 | } | 914 | } |
908 | 915 | ||
@@ -930,7 +937,7 @@ fn foo() { | |||
930 | mod bar { | 937 | mod bar { |
931 | mod baz { | 938 | mod baz { |
932 | pub(crate) fn my_fn() { | 939 | pub(crate) fn my_fn() { |
933 | <|>todo!() | 940 | ${0:todo!()} |
934 | } | 941 | } |
935 | } | 942 | } |
936 | } | 943 | } |
@@ -959,7 +966,7 @@ fn main() { | |||
959 | 966 | ||
960 | 967 | ||
961 | pub(crate) fn bar() { | 968 | pub(crate) fn bar() { |
962 | <|>todo!() | 969 | ${0:todo!()} |
963 | }", | 970 | }", |
964 | ) | 971 | ) |
965 | } | 972 | } |
diff --git a/crates/ra_assists/src/tests/generated.rs b/crates/ra_assists/src/tests/generated.rs index 32fbcdef4..1d82c245d 100644 --- a/crates/ra_assists/src/tests/generated.rs +++ b/crates/ra_assists/src/tests/generated.rs | |||
@@ -78,7 +78,7 @@ fn foo() { | |||
78 | } | 78 | } |
79 | 79 | ||
80 | fn bar(arg: &str, baz: Baz) { | 80 | fn bar(arg: &str, baz: Baz) { |
81 | todo!() | 81 | ${0:todo!()} |
82 | } | 82 | } |
83 | 83 | ||
84 | "#####, | 84 | "#####, |
diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index f3fc92ebf..bb9749b06 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs | |||
@@ -1,18 +1,38 @@ | |||
1 | //! Assorted functions shared by several assists. | 1 | //! Assorted functions shared by several assists. |
2 | pub(crate) mod insert_use; | 2 | pub(crate) mod insert_use; |
3 | 3 | ||
4 | use std::iter; | 4 | use std::{iter, ops}; |
5 | 5 | ||
6 | use hir::{Adt, Crate, Semantics, Trait, Type}; | 6 | use hir::{Adt, Crate, Semantics, Trait, Type}; |
7 | use ra_ide_db::RootDatabase; | 7 | use ra_ide_db::RootDatabase; |
8 | use ra_syntax::{ | 8 | use ra_syntax::{ |
9 | ast::{self, make, NameOwner}, | 9 | ast::{self, make, NameOwner}, |
10 | AstNode, T, | 10 | AstNode, SyntaxNode, T, |
11 | }; | 11 | }; |
12 | use rustc_hash::FxHashSet; | 12 | use rustc_hash::FxHashSet; |
13 | 13 | ||
14 | pub(crate) use insert_use::insert_use_statement; | 14 | pub(crate) use insert_use::insert_use_statement; |
15 | 15 | ||
16 | pub(crate) fn render_snippet(node: &SyntaxNode, placeholder: &SyntaxNode) -> String { | ||
17 | assert!(placeholder.ancestors().any(|it| it == *node)); | ||
18 | let range = placeholder.text_range() - node.text_range().start(); | ||
19 | let range: ops::Range<usize> = range.into(); | ||
20 | |||
21 | let mut placeholder = placeholder.to_string(); | ||
22 | escape(&mut placeholder); | ||
23 | let tab_stop = format!("${{0:{}}}", placeholder); | ||
24 | |||
25 | let mut buf = node.to_string(); | ||
26 | buf.replace_range(range, &tab_stop); | ||
27 | return buf; | ||
28 | |||
29 | fn escape(buf: &mut String) { | ||
30 | stdx::replace(buf, '{', r"\{"); | ||
31 | stdx::replace(buf, '}', r"\}"); | ||
32 | stdx::replace(buf, '$', r"\$"); | ||
33 | } | ||
34 | } | ||
35 | |||
16 | pub fn get_missing_assoc_items( | 36 | pub fn get_missing_assoc_items( |
17 | sema: &Semantics<RootDatabase>, | 37 | sema: &Semantics<RootDatabase>, |
18 | impl_def: &ast::ImplDef, | 38 | impl_def: &ast::ImplDef, |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 0f34ce70e..71a57fba2 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -116,3 +116,11 @@ pub fn to_lower_snake_case(s: &str) -> String { | |||
116 | } | 116 | } |
117 | buf | 117 | buf |
118 | } | 118 | } |
119 | |||
120 | pub fn replace(buf: &mut String, from: char, to: &str) { | ||
121 | if !buf.contains(from) { | ||
122 | return; | ||
123 | } | ||
124 | // FIXME: do this in place. | ||
125 | *buf = buf.replace(from, to) | ||
126 | } | ||