Skip to content

Commit 8eb3272

Browse files
committed
Use snippets in add function
1 parent 9c3acd3 commit 8eb3272

File tree

5 files changed

+75
-40
lines changed

5 files changed

+75
-40
lines changed

crates/ra_assists/src/handlers/add_function.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ra_syntax::{
1010
};
1111
use rustc_hash::{FxHashMap, FxHashSet};
1212

13-
use crate::{AssistContext, AssistId, Assists};
13+
use crate::{utils::render_snippet, AssistContext, AssistId, Assists};
1414

1515
// Assist: add_function
1616
//
@@ -33,7 +33,7 @@ use crate::{AssistContext, AssistId, Assists};
3333
// }
3434
//
3535
// fn bar(arg: &str, baz: Baz) {
36-
// todo!()
36+
// ${0:todo!()}
3737
// }
3838
//
3939
// ```
@@ -58,18 +58,27 @@ pub(crate) fn add_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
5858
let function_builder = FunctionBuilder::from_call(&ctx, &call, &path, target_module)?;
5959

6060
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| {
6262
let function_template = function_builder.render();
63-
edit.set_file(function_template.file);
64-
edit.set_cursor(function_template.cursor_offset);
65-
edit.insert(function_template.insert_offset, function_template.fn_def.to_string());
63+
builder.set_file(function_template.file);
64+
match ctx.config.snippet_cap {
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+
}
6675
})
6776
}
6877

6978
struct FunctionTemplate {
7079
insert_offset: TextSize,
71-
cursor_offset: TextSize,
7280
fn_def: ast::SourceFile,
81+
placeholder_expr: ast::MacroCall,
7382
file: FileId,
7483
}
7584

@@ -136,9 +145,7 @@ impl FunctionBuilder {
136145

137146
let placeholder_expr =
138147
fn_def.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
139-
let cursor_offset_from_fn_start = placeholder_expr.syntax().text_range().start();
140-
let cursor_offset = insert_offset + cursor_offset_from_fn_start;
141-
FunctionTemplate { insert_offset, cursor_offset, fn_def, file: self.file }
148+
FunctionTemplate { insert_offset, placeholder_expr, fn_def, file: self.file }
142149
}
143150
}
144151

@@ -316,7 +323,7 @@ fn foo() {
316323
}
317324
318325
fn bar() {
319-
<|>todo!()
326+
${0:todo!()}
320327
}
321328
",
322329
)
@@ -343,7 +350,7 @@ impl Foo {
343350
}
344351
345352
fn bar() {
346-
<|>todo!()
353+
${0:todo!()}
347354
}
348355
",
349356
)
@@ -367,7 +374,7 @@ fn foo1() {
367374
}
368375
369376
fn bar() {
370-
<|>todo!()
377+
${0:todo!()}
371378
}
372379
373380
fn foo2() {}
@@ -393,7 +400,7 @@ mod baz {
393400
}
394401
395402
fn bar() {
396-
<|>todo!()
403+
${0:todo!()}
397404
}
398405
}
399406
",
@@ -419,7 +426,7 @@ fn foo() {
419426
}
420427
421428
fn bar(baz: Baz) {
422-
<|>todo!()
429+
${0:todo!()}
423430
}
424431
",
425432
);
@@ -452,7 +459,7 @@ impl Baz {
452459
}
453460
454461
fn bar(baz: Baz) {
455-
<|>todo!()
462+
${0:todo!()}
456463
}
457464
",
458465
)
@@ -473,7 +480,7 @@ fn foo() {
473480
}
474481
475482
fn bar(arg: &str) {
476-
<|>todo!()
483+
${0:todo!()}
477484
}
478485
"#,
479486
)
@@ -494,7 +501,7 @@ fn foo() {
494501
}
495502
496503
fn bar(arg: char) {
497-
<|>todo!()
504+
${0:todo!()}
498505
}
499506
"#,
500507
)
@@ -515,7 +522,7 @@ fn foo() {
515522
}
516523
517524
fn bar(arg: i32) {
518-
<|>todo!()
525+
${0:todo!()}
519526
}
520527
",
521528
)
@@ -536,7 +543,7 @@ fn foo() {
536543
}
537544
538545
fn bar(arg: u8) {
539-
<|>todo!()
546+
${0:todo!()}
540547
}
541548
",
542549
)
@@ -561,7 +568,7 @@ fn foo() {
561568
}
562569
563570
fn bar(x: u8) {
564-
<|>todo!()
571+
${0:todo!()}
565572
}
566573
",
567574
)
@@ -584,7 +591,7 @@ fn foo() {
584591
}
585592
586593
fn bar(worble: ()) {
587-
<|>todo!()
594+
${0:todo!()}
588595
}
589596
",
590597
)
@@ -613,7 +620,7 @@ fn baz() {
613620
}
614621
615622
fn bar(foo: impl Foo) {
616-
<|>todo!()
623+
${0:todo!()}
617624
}
618625
",
619626
)
@@ -640,7 +647,7 @@ fn foo() {
640647
}
641648
642649
fn bar(baz: &Baz) {
643-
<|>todo!()
650+
${0:todo!()}
644651
}
645652
",
646653
)
@@ -669,7 +676,7 @@ fn foo() {
669676
}
670677
671678
fn bar(baz: Baz::Bof) {
672-
<|>todo!()
679+
${0:todo!()}
673680
}
674681
",
675682
)
@@ -692,7 +699,7 @@ fn foo<T>(t: T) {
692699
}
693700
694701
fn bar<T>(t: T) {
695-
<|>todo!()
702+
${0:todo!()}
696703
}
697704
",
698705
)
@@ -723,7 +730,7 @@ fn foo() {
723730
}
724731
725732
fn bar(arg: fn() -> Baz) {
726-
<|>todo!()
733+
${0:todo!()}
727734
}
728735
",
729736
)
@@ -748,7 +755,7 @@ fn foo() {
748755
}
749756
750757
fn bar(closure: impl Fn(i64) -> i64) {
751-
<|>todo!()
758+
${0:todo!()}
752759
}
753760
",
754761
)
@@ -769,7 +776,7 @@ fn foo() {
769776
}
770777
771778
fn bar(baz: ()) {
772-
<|>todo!()
779+
${0:todo!()}
773780
}
774781
",
775782
)
@@ -794,7 +801,7 @@ fn foo() {
794801
}
795802
796803
fn bar(baz_1: Baz, baz_2: Baz) {
797-
<|>todo!()
804+
${0:todo!()}
798805
}
799806
",
800807
)
@@ -819,7 +826,7 @@ fn foo() {
819826
}
820827
821828
fn bar(baz_1: Baz, baz_2: Baz, arg_1: &str, arg_2: &str) {
822-
<|>todo!()
829+
${0:todo!()}
823830
}
824831
"#,
825832
)
@@ -839,7 +846,7 @@ fn foo() {
839846
r"
840847
mod bar {
841848
pub(crate) fn my_fn() {
842-
<|>todo!()
849+
${0:todo!()}
843850
}
844851
}
845852
@@ -878,7 +885,7 @@ fn bar() {
878885
}
879886
880887
fn baz(foo: foo::Foo) {
881-
<|>todo!()
888+
${0:todo!()}
882889
}
883890
",
884891
)
@@ -902,7 +909,7 @@ mod bar {
902909
fn something_else() {}
903910
904911
pub(crate) fn my_fn() {
905-
<|>todo!()
912+
${0:todo!()}
906913
}
907914
}
908915
@@ -930,7 +937,7 @@ fn foo() {
930937
mod bar {
931938
mod baz {
932939
pub(crate) fn my_fn() {
933-
<|>todo!()
940+
${0:todo!()}
934941
}
935942
}
936943
}
@@ -959,7 +966,7 @@ fn main() {
959966
960967
961968
pub(crate) fn bar() {
962-
<|>todo!()
969+
${0:todo!()}
963970
}",
964971
)
965972
}

crates/ra_assists/src/tests/generated.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn foo() {
7878
}
7979
8080
fn bar(arg: &str, baz: Baz) {
81-
todo!()
81+
${0:todo!()}
8282
}
8383
8484
"#####,

crates/ra_assists/src/utils.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
//! Assorted functions shared by several assists.
22
pub(crate) mod insert_use;
33

4-
use std::iter;
4+
use std::{iter, ops};
55

66
use hir::{Adt, Crate, Semantics, Trait, Type};
77
use ra_ide_db::RootDatabase;
88
use ra_syntax::{
99
ast::{self, make, NameOwner},
10-
AstNode, T,
10+
AstNode, SyntaxNode, T,
1111
};
1212
use rustc_hash::FxHashSet;
1313

1414
pub(crate) use insert_use::insert_use_statement;
1515

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+
1636
pub fn get_missing_assoc_items(
1737
sema: &Semantics<RootDatabase>,
1838
impl_def: &ast::ImplDef,

crates/stdx/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,11 @@ pub fn to_lower_snake_case(s: &str) -> String {
116116
}
117117
buf
118118
}
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+
}

docs/user/assists.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn foo() {
7777
}
7878

7979
fn bar(arg: &str, baz: Baz) {
80-
todo!()
80+
${0:todo!()}
8181
}
8282

8383
```

0 commit comments

Comments
 (0)