aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorDomantas Jadenkus <[email protected]>2021-02-13 20:38:52 +0000
committerDomantas Jadenkus <[email protected]>2021-02-13 20:45:22 +0000
commit3364ac8b1106f6f0d8a52a8981c6c598152b5642 (patch)
treee68744a8faf0ba5762df77e7e2d6670ae432a496 /crates/assists
parentff7ea7c30843e576be6ab3fb70f79d8053006652 (diff)
use generate_impl_text in generate_impl
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/generate_impl.rs46
-rw-r--r--crates/assists/src/utils.rs9
2 files changed, 14 insertions, 41 deletions
diff --git a/crates/assists/src/handlers/generate_impl.rs b/crates/assists/src/handlers/generate_impl.rs
index 61d1bd25c..e2762e5d2 100644
--- a/crates/assists/src/handlers/generate_impl.rs
+++ b/crates/assists/src/handlers/generate_impl.rs
@@ -1,11 +1,6 @@
1use itertools::Itertools; 1use syntax::ast::{self, AstNode, NameOwner};
2use stdx::format_to;
3use syntax::{
4 ast::{self, AstNode, AttrsOwner, GenericParamsOwner, NameOwner},
5 SmolStr,
6};
7 2
8use crate::{AssistContext, AssistId, AssistKind, Assists}; 3use crate::{AssistContext, AssistId, AssistKind, Assists, utils::generate_impl_text};
9 4
10// Assist: generate_impl 5// Assist: generate_impl
11// 6//
@@ -36,44 +31,15 @@ pub(crate) fn generate_impl(acc: &mut Assists, ctx: &AssistContext) -> Option<()
36 format!("Generate impl for `{}`", name), 31 format!("Generate impl for `{}`", name),
37 target, 32 target,
38 |edit| { 33 |edit| {
39 let type_params = nominal.generic_param_list();
40 let start_offset = nominal.syntax().text_range().end(); 34 let start_offset = nominal.syntax().text_range().end();
41 let mut buf = String::new();
42 buf.push_str("\n\n");
43 nominal
44 .attrs()
45 .filter(|attr| {
46 attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false)
47 })
48 .for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str()));
49
50 buf.push_str("impl");
51 if let Some(type_params) = &type_params {
52 format_to!(buf, "{}", type_params.syntax());
53 }
54 buf.push_str(" ");
55 buf.push_str(name.text());
56 if let Some(type_params) = type_params {
57 let lifetime_params = type_params
58 .lifetime_params()
59 .filter_map(|it| it.lifetime())
60 .map(|it| SmolStr::from(it.text()));
61 let type_params = type_params
62 .type_params()
63 .filter_map(|it| it.name())
64 .map(|it| SmolStr::from(it.text()));
65
66 let generic_params = lifetime_params.chain(type_params).format(", ");
67 format_to!(buf, "<{}>", generic_params)
68 }
69 match ctx.config.snippet_cap { 35 match ctx.config.snippet_cap {
70 Some(cap) => { 36 Some(cap) => {
71 buf.push_str(" {\n $0\n}"); 37 let snippet = generate_impl_text(&nominal, " $0");
72 edit.insert_snippet(cap, start_offset, buf); 38 edit.insert_snippet(cap, start_offset, snippet);
73 } 39 }
74 None => { 40 None => {
75 buf.push_str(" {\n}"); 41 let snippet = generate_impl_text(&nominal, "");
76 edit.insert(start_offset, buf); 42 edit.insert(start_offset, snippet);
77 } 43 }
78 } 44 }
79 }, 45 },
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index 69c107f63..b51d703db 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -379,7 +379,14 @@ pub(crate) fn generate_trait_impl_text(adt: &ast::Adt, trait_text: &str, code: &
379fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str) -> String { 379fn generate_impl_text_inner(adt: &ast::Adt, trait_text: Option<&str>, code: &str) -> String {
380 let type_params = adt.generic_param_list(); 380 let type_params = adt.generic_param_list();
381 let mut buf = String::with_capacity(code.len()); 381 let mut buf = String::with_capacity(code.len());
382 buf.push_str("\n\nimpl"); 382 buf.push_str("\n\n");
383 adt
384 .attrs()
385 .filter(|attr| {
386 attr.as_simple_call().map(|(name, _arg)| name == "cfg").unwrap_or(false)
387 })
388 .for_each(|attr| buf.push_str(format!("{}\n", attr.to_string()).as_str()));
389 buf.push_str("impl");
383 if let Some(type_params) = &type_params { 390 if let Some(type_params) = &type_params {
384 format_to!(buf, "{}", type_params.syntax()); 391 format_to!(buf, "{}", type_params.syntax());
385 } 392 }