diff options
-rw-r--r-- | crates/assists/src/handlers/generate_impl.rs | 42 | ||||
-rw-r--r-- | editors/code/rust.tmGrammar.json | 53 |
2 files changed, 74 insertions, 21 deletions
diff --git a/crates/assists/src/handlers/generate_impl.rs b/crates/assists/src/handlers/generate_impl.rs index 9989109b5..114974465 100644 --- a/crates/assists/src/handlers/generate_impl.rs +++ b/crates/assists/src/handlers/generate_impl.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use itertools::Itertools; | 1 | use itertools::Itertools; |
2 | use stdx::format_to; | 2 | use stdx::format_to; |
3 | use syntax::ast::{self, AstNode, GenericParamsOwner, NameOwner}; | 3 | use syntax::ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner}; |
4 | 4 | ||
5 | use crate::{AssistContext, AssistId, AssistKind, Assists}; | 5 | use crate::{AssistContext, AssistId, AssistKind, Assists}; |
6 | 6 | ||
@@ -27,6 +27,7 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<() | |||
27 | let nominal = ctx.find_node_at_offset::<ast::AdtDef>()?; | 27 | let nominal = ctx.find_node_at_offset::<ast::AdtDef>()?; |
28 | let name = nominal.name()?; | 28 | let name = nominal.name()?; |
29 | let target = nominal.syntax().text_range(); | 29 | let target = nominal.syntax().text_range(); |
30 | |||
30 | acc.add( | 31 | acc.add( |
31 | AssistId("generate_impl", AssistKind::Generate), | 32 | AssistId("generate_impl", AssistKind::Generate), |
32 | format!("Generate impl for `{}`", name), | 33 | format!("Generate impl for `{}`", name), |
@@ -35,7 +36,15 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<() | |||
35 | let type_params = nominal.generic_param_list(); | 36 | let type_params = nominal.generic_param_list(); |
36 | let start_offset = nominal.syntax().text_range().end(); | 37 | let start_offset = nominal.syntax().text_range().end(); |
37 | let mut buf = String::new(); | 38 | let mut buf = String::new(); |
38 | buf.push_str("\n\nimpl"); | 39 | buf.push_str("\n\n"); |
40 | nominal | ||
41 | .attrs() | ||
42 | .filter(|attr| { | ||
43 | attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false) | ||
44 | }) | ||
45 | .for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str())); | ||
46 | |||
47 | buf.push_str("impl"); | ||
39 | if let Some(type_params) = &type_params { | 48 | if let Some(type_params) = &type_params { |
40 | format_to!(buf, "{}", type_params.syntax()); | 49 | format_to!(buf, "{}", type_params.syntax()); |
41 | } | 50 | } |
@@ -91,6 +100,35 @@ mod tests { | |||
91 | "struct Foo<'a, T: Foo<'a>> {<|>}", | 100 | "struct Foo<'a, T: Foo<'a>> {<|>}", |
92 | "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}", | 101 | "struct Foo<'a, T: Foo<'a>> {}\n\nimpl<'a, T: Foo<'a>> Foo<'a, T> {\n $0\n}", |
93 | ); | 102 | ); |
103 | check_assist( | ||
104 | generate_impl, | ||
105 | r#" | ||
106 | #[cfg(feature = "foo")] | ||
107 | struct Foo<'a, T: Foo<'a>> {<|>}"#, | ||
108 | r#" | ||
109 | #[cfg(feature = "foo")] | ||
110 | struct Foo<'a, T: Foo<'a>> {} | ||
111 | |||
112 | #[cfg(feature = "foo")] | ||
113 | impl<'a, T: Foo<'a>> Foo<'a, T> { | ||
114 | $0 | ||
115 | }"#, | ||
116 | ); | ||
117 | |||
118 | check_assist( | ||
119 | generate_impl, | ||
120 | r#" | ||
121 | #[cfg(not(feature = "foo"))] | ||
122 | struct Foo<'a, T: Foo<'a>> {<|>}"#, | ||
123 | r#" | ||
124 | #[cfg(not(feature = "foo"))] | ||
125 | struct Foo<'a, T: Foo<'a>> {} | ||
126 | |||
127 | #[cfg(not(feature = "foo"))] | ||
128 | impl<'a, T: Foo<'a>> Foo<'a, T> { | ||
129 | $0 | ||
130 | }"#, | ||
131 | ); | ||
94 | } | 132 | } |
95 | 133 | ||
96 | #[test] | 134 | #[test] |
diff --git a/editors/code/rust.tmGrammar.json b/editors/code/rust.tmGrammar.json index 3be565195..f0c5c3cf3 100644 --- a/editors/code/rust.tmGrammar.json +++ b/editors/code/rust.tmGrammar.json | |||
@@ -25,6 +25,9 @@ | |||
25 | }, | 25 | }, |
26 | "patterns": [ | 26 | "patterns": [ |
27 | { | 27 | { |
28 | "include": "#block-comments" | ||
29 | }, | ||
30 | { | ||
28 | "include": "#comments" | 31 | "include": "#comments" |
29 | }, | 32 | }, |
30 | { | 33 | { |
@@ -185,6 +188,9 @@ | |||
185 | }, | 188 | }, |
186 | "patterns": [ | 189 | "patterns": [ |
187 | { | 190 | { |
191 | "include": "#block-comments" | ||
192 | }, | ||
193 | { | ||
188 | "include": "#comments" | 194 | "include": "#comments" |
189 | }, | 195 | }, |
190 | { | 196 | { |
@@ -212,6 +218,9 @@ | |||
212 | }, | 218 | }, |
213 | "patterns": [ | 219 | "patterns": [ |
214 | { | 220 | { |
221 | "include": "#block-comments" | ||
222 | }, | ||
223 | { | ||
215 | "include": "#comments" | 224 | "include": "#comments" |
216 | }, | 225 | }, |
217 | { | 226 | { |
@@ -232,6 +241,9 @@ | |||
232 | ] | 241 | ] |
233 | }, | 242 | }, |
234 | { | 243 | { |
244 | "include": "#block-comments" | ||
245 | }, | ||
246 | { | ||
235 | "include": "#comments" | 247 | "include": "#comments" |
236 | }, | 248 | }, |
237 | { | 249 | { |
@@ -277,31 +289,30 @@ | |||
277 | { | 289 | { |
278 | "comment": "documentation comments", | 290 | "comment": "documentation comments", |
279 | "name": "comment.line.documentation.rust", | 291 | "name": "comment.line.documentation.rust", |
280 | "match": "^\\s*///.*", | 292 | "match": "^\\s*///.*" |
281 | "patterns": [ | ||
282 | { | ||
283 | "include": "#comments" | ||
284 | } | ||
285 | ] | ||
286 | }, | 293 | }, |
287 | { | 294 | { |
288 | "comment": "line comments", | 295 | "comment": "line comments", |
289 | "name": "comment.line.double-slash.rust", | 296 | "name": "comment.line.double-slash.rust", |
290 | "match": "\\s*//.*", | 297 | "match": "\\s*//.*" |
291 | "patterns": [ | ||
292 | { | ||
293 | "include": "#comments" | ||
294 | } | ||
295 | ] | ||
296 | }, | 298 | }, |
297 | { | 299 | { |
300 | "comment": "inferred types, wildcard patterns, ignored params", | ||
301 | "name": "comment.char.underscore.rust", | ||
302 | "match": "\\b_\\w*\\b[^!(]" | ||
303 | } | ||
304 | ] | ||
305 | }, | ||
306 | "block-comments": { | ||
307 | "patterns": [ | ||
308 | { | ||
298 | "comment": "block comments", | 309 | "comment": "block comments", |
299 | "name": "comment.block.rust", | 310 | "name": "comment.block.rust", |
300 | "begin": "/\\*(?!\\*)", | 311 | "begin": "/\\*(?!\\*)", |
301 | "end": "\\*/", | 312 | "end": "\\*/", |
302 | "patterns": [ | 313 | "patterns": [ |
303 | { | 314 | { |
304 | "include": "#comments" | 315 | "include": "#block-comments" |
305 | } | 316 | } |
306 | ] | 317 | ] |
307 | }, | 318 | }, |
@@ -312,14 +323,9 @@ | |||
312 | "end": "\\*/", | 323 | "end": "\\*/", |
313 | "patterns": [ | 324 | "patterns": [ |
314 | { | 325 | { |
315 | "include": "#comments" | 326 | "include": "#block-comments" |
316 | } | 327 | } |
317 | ] | 328 | ] |
318 | }, | ||
319 | { | ||
320 | "comment": "inferred types, wildcard patterns, ignored params", | ||
321 | "name": "comment.char.underscore.rust", | ||
322 | "match": "\\b_\\w*\\b" | ||
323 | } | 329 | } |
324 | ] | 330 | ] |
325 | }, | 331 | }, |
@@ -451,6 +457,9 @@ | |||
451 | }, | 457 | }, |
452 | "patterns": [ | 458 | "patterns": [ |
453 | { | 459 | { |
460 | "include": "#block-comments" | ||
461 | }, | ||
462 | { | ||
454 | "include": "#comments" | 463 | "include": "#comments" |
455 | }, | 464 | }, |
456 | { | 465 | { |
@@ -517,6 +526,9 @@ | |||
517 | }, | 526 | }, |
518 | "patterns": [ | 527 | "patterns": [ |
519 | { | 528 | { |
529 | "include": "#block-comments" | ||
530 | }, | ||
531 | { | ||
520 | "include": "#comments" | 532 | "include": "#comments" |
521 | }, | 533 | }, |
522 | { | 534 | { |
@@ -798,6 +810,9 @@ | |||
798 | }, | 810 | }, |
799 | "patterns": [ | 811 | "patterns": [ |
800 | { | 812 | { |
813 | "include": "#block-comments" | ||
814 | }, | ||
815 | { | ||
801 | "include": "#comments" | 816 | "include": "#comments" |
802 | }, | 817 | }, |
803 | { | 818 | { |