Skip to content

Commit 97994db

Browse files
committed
Add snippet support for some assists
1 parent 9c87c96 commit 97994db

File tree

5 files changed

+72
-61
lines changed

5 files changed

+72
-61
lines changed

crates/ra_assists/src/handlers/add_custom_impl.rs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::{
2525
// struct S;
2626
//
2727
// impl Debug for S {
28-
//
28+
// $0
2929
// }
3030
// ```
3131
pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
@@ -52,7 +52,7 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<
5252
format!("Add custom impl `{}` for `{}`", trait_token.text().as_str(), annotated_name);
5353

5454
let target = attr.syntax().text_range();
55-
acc.add(AssistId("add_custom_impl"), label, target, |edit| {
55+
acc.add(AssistId("add_custom_impl"), label, target, |builder| {
5656
let new_attr_input = input
5757
.syntax()
5858
.descendants_with_tokens()
@@ -63,35 +63,36 @@ pub(crate) fn add_custom_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<
6363
let has_more_derives = !new_attr_input.is_empty();
6464
let new_attr_input = new_attr_input.iter().sep_by(", ").surround_with("(", ")").to_string();
6565

66-
let mut buf = String::new();
67-
buf.push_str("\n\nimpl ");
68-
buf.push_str(trait_token.text().as_str());
69-
buf.push_str(" for ");
70-
buf.push_str(annotated_name.as_str());
71-
buf.push_str(" {\n");
72-
73-
let cursor_delta = if has_more_derives {
74-
let delta = input.syntax().text_range().len() - TextSize::of(&new_attr_input);
75-
edit.replace(input.syntax().text_range(), new_attr_input);
76-
delta
66+
if has_more_derives {
67+
builder.replace(input.syntax().text_range(), new_attr_input);
7768
} else {
7869
let attr_range = attr.syntax().text_range();
79-
edit.delete(attr_range);
70+
builder.delete(attr_range);
8071

8172
let line_break_range = attr
8273
.syntax()
8374
.next_sibling_or_token()
8475
.filter(|t| t.kind() == WHITESPACE)
8576
.map(|t| t.text_range())
8677
.unwrap_or_else(|| TextRange::new(TextSize::from(0), TextSize::from(0)));
87-
edit.delete(line_break_range);
88-
89-
attr_range.len() + line_break_range.len()
90-
};
91-
92-
edit.set_cursor(start_offset + TextSize::of(&buf) - cursor_delta);
93-
buf.push_str("\n}");
94-
edit.insert(start_offset, buf);
78+
builder.delete(line_break_range);
79+
}
80+
81+
match ctx.config.snippet_cap {
82+
Some(cap) => {
83+
builder.insert_snippet(
84+
cap,
85+
start_offset,
86+
format!("\n\nimpl {} for {} {{\n $0\n}}", trait_token, annotated_name),
87+
);
88+
}
89+
None => {
90+
builder.insert(
91+
start_offset,
92+
format!("\n\nimpl {} for {} {{\n\n}}", trait_token, annotated_name),
93+
);
94+
}
95+
}
9596
})
9697
}
9798

@@ -117,7 +118,7 @@ struct Foo {
117118
}
118119
119120
impl Debug for Foo {
120-
<|>
121+
$0
121122
}
122123
",
123124
)
@@ -139,7 +140,7 @@ pub struct Foo {
139140
}
140141
141142
impl Debug for Foo {
142-
<|>
143+
$0
143144
}
144145
",
145146
)
@@ -158,7 +159,7 @@ struct Foo {}
158159
struct Foo {}
159160
160161
impl Debug for Foo {
161-
<|>
162+
$0
162163
}
163164
",
164165
)

crates/ra_assists/src/handlers/add_derive.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,37 @@ use crate::{AssistContext, AssistId, Assists};
1818
// ```
1919
// ->
2020
// ```
21-
// #[derive()]
21+
// #[derive($0)]
2222
// struct Point {
2323
// x: u32,
2424
// y: u32,
2525
// }
2626
// ```
2727
pub(crate) fn add_derive(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
28+
let cap = ctx.config.snippet_cap?;
2829
let nominal = ctx.find_node_at_offset::<ast::NominalDef>()?;
2930
let node_start = derive_insertion_offset(&nominal)?;
3031
let target = nominal.syntax().text_range();
31-
acc.add(AssistId("add_derive"), "Add `#[derive]`", target, |edit| {
32+
acc.add(AssistId("add_derive"), "Add `#[derive]`", target, |builder| {
3233
let derive_attr = nominal
3334
.attrs()
3435
.filter_map(|x| x.as_simple_call())
3536
.filter(|(name, _arg)| name == "derive")
3637
.map(|(_name, arg)| arg)
3738
.next();
38-
let offset = match derive_attr {
39+
match derive_attr {
3940
None => {
40-
edit.insert(node_start, "#[derive()]\n");
41-
node_start + TextSize::of("#[derive(")
41+
builder.insert_snippet(cap, node_start, "#[derive($0)]\n");
42+
}
43+
Some(tt) => {
44+
// Just move the cursor.
45+
builder.insert_snippet(
46+
cap,
47+
tt.syntax().text_range().end() - TextSize::of(')'),
48+
"$0",
49+
)
4250
}
43-
Some(tt) => tt.syntax().text_range().end() - TextSize::of(')'),
4451
};
45-
edit.set_cursor(offset)
4652
})
4753
}
4854

@@ -66,12 +72,12 @@ mod tests {
6672
check_assist(
6773
add_derive,
6874
"struct Foo { a: i32, <|>}",
69-
"#[derive(<|>)]\nstruct Foo { a: i32, }",
75+
"#[derive($0)]\nstruct Foo { a: i32, }",
7076
);
7177
check_assist(
7278
add_derive,
7379
"struct Foo { <|> a: i32, }",
74-
"#[derive(<|>)]\nstruct Foo { a: i32, }",
80+
"#[derive($0)]\nstruct Foo { a: i32, }",
7581
);
7682
}
7783

@@ -80,7 +86,7 @@ mod tests {
8086
check_assist(
8187
add_derive,
8288
"#[derive(Clone)]\nstruct Foo { a: i32<|>, }",
83-
"#[derive(Clone<|>)]\nstruct Foo { a: i32, }",
89+
"#[derive(Clone$0)]\nstruct Foo { a: i32, }",
8490
);
8591
}
8692

@@ -96,7 +102,7 @@ struct Foo { a: i32<|>, }
96102
"
97103
/// `Foo` is a pretty important struct.
98104
/// It does stuff.
99-
#[derive(<|>)]
105+
#[derive($0)]
100106
struct Foo { a: i32, }
101107
",
102108
);

crates/ra_assists/src/handlers/add_impl.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use ra_syntax::{
2-
ast::{self, AstNode, NameOwner, TypeParamsOwner},
3-
TextSize,
4-
};
1+
use ra_syntax::ast::{self, AstNode, NameOwner, TypeParamsOwner};
52
use stdx::{format_to, SepBy};
63

74
use crate::{AssistContext, AssistId, Assists};
@@ -12,17 +9,17 @@ use crate::{AssistContext, AssistId, Assists};
129
//
1310
// ```
1411
// struct Ctx<T: Clone> {
15-
// data: T,<|>
12+
// data: T,<|>
1613
// }
1714
// ```
1815
// ->
1916
// ```
2017
// struct Ctx<T: Clone> {
21-
// data: T,
18+
// data: T,
2219
// }
2320
//
2421
// impl<T: Clone> Ctx<T> {
25-
//
22+
// $0
2623
// }
2724
// ```
2825
pub(crate) fn add_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
@@ -50,30 +47,37 @@ pub(crate) fn add_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
5047
let generic_params = lifetime_params.chain(type_params).sep_by(", ");
5148
format_to!(buf, "<{}>", generic_params)
5249
}
53-
buf.push_str(" {\n");
54-
edit.set_cursor(start_offset + TextSize::of(&buf));
55-
buf.push_str("\n}");
56-
edit.insert(start_offset, buf);
50+
match ctx.config.snippet_cap {
51+
Some(cap) => {
52+
buf.push_str(" {\n $0\n}");
53+
edit.insert_snippet(cap, start_offset, buf);
54+
}
55+
None => {
56+
buf.push_str(" {\n}");
57+
edit.insert(start_offset, buf);
58+
}
59+
}
5760
})
5861
}
5962

6063
#[cfg(test)]
6164
mod tests {
62-
use super::*;
6365
use crate::tests::{check_assist, check_assist_target};
6466

67+
use super::*;
68+
6569
#[test]
6670
fn test_add_impl() {
67-
check_assist(add_impl, "struct Foo {<|>}\n", "struct Foo {}\n\nimpl Foo {\n<|>\n}\n");
71+
check_assist(add_impl, "struct Foo {<|>}\n", "struct Foo {}\n\nimpl Foo {\n $0\n}\n");
6872
check_assist(
6973
add_impl,
7074
"struct Foo<T: Clone> {<|>}",
71-
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n<|>\n}",
75+
"struct Foo<T: Clone> {}\n\nimpl<T: Clone> Foo<T> {\n $0\n}",
7276
);
7377
check_assist(
7478
add_impl,
7579
"struct Foo<'a, T: Foo<'a>> {<|>}",
76-
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n<|>\n}",
80+
"struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}",
7781
);
7882
}
7983

crates/ra_assists/src/tests/generated.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct S;
1515
struct S;
1616
1717
impl Debug for S {
18-
18+
$0
1919
}
2020
"#####,
2121
)
@@ -32,7 +32,7 @@ struct Point {
3232
}
3333
"#####,
3434
r#####"
35-
#[derive()]
35+
#[derive($0)]
3636
struct Point {
3737
x: u32,
3838
y: u32,
@@ -108,16 +108,16 @@ fn doctest_add_impl() {
108108
"add_impl",
109109
r#####"
110110
struct Ctx<T: Clone> {
111-
data: T,<|>
111+
data: T,<|>
112112
}
113113
"#####,
114114
r#####"
115115
struct Ctx<T: Clone> {
116-
data: T,
116+
data: T,
117117
}
118118
119119
impl<T: Clone> Ctx<T> {
120-
120+
$0
121121
}
122122
"#####,
123123
)

docs/user/assists.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ struct S;
1717
struct S;
1818

1919
impl Debug for S {
20-
20+
$0
2121
}
2222
```
2323

@@ -33,7 +33,7 @@ struct Point {
3333
}
3434

3535
// AFTER
36-
#[derive()]
36+
#[derive($0)]
3737
struct Point {
3838
x: u32,
3939
y: u32,
@@ -105,16 +105,16 @@ Adds a new inherent impl for a type.
105105
```rust
106106
// BEFORE
107107
struct Ctx<T: Clone> {
108-
data: T,┃
108+
data: T,┃
109109
}
110110

111111
// AFTER
112112
struct Ctx<T: Clone> {
113-
data: T,
113+
data: T,
114114
}
115115

116116
impl<T: Clone> Ctx<T> {
117-
117+
$0
118118
}
119119
```
120120

0 commit comments

Comments
 (0)