aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/generate_new.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/generate_new.rs')
-rw-r--r--crates/ra_assists/src/handlers/generate_new.rs24
1 files changed, 10 insertions, 14 deletions
diff --git a/crates/ra_assists/src/handlers/generate_new.rs b/crates/ra_assists/src/handlers/generate_new.rs
index e27def1d8..b84aa24b6 100644
--- a/crates/ra_assists/src/handlers/generate_new.rs
+++ b/crates/ra_assists/src/handlers/generate_new.rs
@@ -1,8 +1,6 @@
1use hir::Adt; 1use hir::Adt;
2use ra_syntax::{ 2use ra_syntax::{
3 ast::{ 3 ast::{self, AstNode, GenericParamsOwner, NameOwner, StructKind, VisibilityOwner},
4 self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner,
5 },
6 T, 4 T,
7}; 5};
8use stdx::{format_to, SepBy}; 6use stdx::{format_to, SepBy};
@@ -30,7 +28,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
30// 28//
31// ``` 29// ```
32pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 30pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
33 let strukt = ctx.find_node_at_offset::<ast::StructDef>()?; 31 let strukt = ctx.find_node_at_offset::<ast::Struct>()?;
34 32
35 // We want to only apply this to non-union structs with named fields 33 // We want to only apply this to non-union structs with named fields
36 let field_list = match strukt.kind() { 34 let field_list = match strukt.kind() {
@@ -53,9 +51,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
53 51
54 let params = field_list 52 let params = field_list
55 .fields() 53 .fields()
56 .filter_map(|f| { 54 .filter_map(|f| Some(format!("{}: {}", f.name()?.syntax(), f.ty()?.syntax())))
57 Some(format!("{}: {}", f.name()?.syntax(), f.ascribed_type()?.syntax()))
58 })
59 .sep_by(", "); 55 .sep_by(", ");
60 let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", "); 56 let fields = field_list.fields().filter_map(|f| f.name()).sep_by(", ");
61 57
@@ -90,8 +86,8 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext) -> Option<()>
90 86
91// Generates the surrounding `impl Type { <code> }` including type and lifetime 87// Generates the surrounding `impl Type { <code> }` including type and lifetime
92// parameters 88// parameters
93fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String { 89fn generate_impl_text(strukt: &ast::Struct, code: &str) -> String {
94 let type_params = strukt.type_param_list(); 90 let type_params = strukt.generic_param_list();
95 let mut buf = String::with_capacity(code.len()); 91 let mut buf = String::with_capacity(code.len());
96 buf.push_str("\n\nimpl"); 92 buf.push_str("\n\nimpl");
97 if let Some(type_params) = &type_params { 93 if let Some(type_params) = &type_params {
@@ -121,7 +117,7 @@ fn generate_impl_text(strukt: &ast::StructDef, code: &str) -> String {
121// 117//
122// FIXME: change the new fn checking to a more semantic approach when that's more 118// FIXME: change the new fn checking to a more semantic approach when that's more
123// viable (e.g. we process proc macros, etc) 119// viable (e.g. we process proc macros, etc)
124fn find_struct_impl(ctx: &AssistContext, strukt: &ast::StructDef) -> Option<Option<ast::ImplDef>> { 120fn find_struct_impl(ctx: &AssistContext, strukt: &ast::Struct) -> Option<Option<ast::Impl>> {
125 let db = ctx.db(); 121 let db = ctx.db();
126 let module = strukt.syntax().ancestors().find(|node| { 122 let module = strukt.syntax().ancestors().find(|node| {
127 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind()) 123 ast::Module::can_cast(node.kind()) || ast::SourceFile::can_cast(node.kind())
@@ -129,7 +125,7 @@ fn find_struct_impl(ctx: &AssistContext, strukt: &ast::StructDef) -> Option<Opti
129 125
130 let struct_def = ctx.sema.to_def(strukt)?; 126 let struct_def = ctx.sema.to_def(strukt)?;
131 127
132 let block = module.descendants().filter_map(ast::ImplDef::cast).find_map(|impl_blk| { 128 let block = module.descendants().filter_map(ast::Impl::cast).find_map(|impl_blk| {
133 let blk = ctx.sema.to_def(&impl_blk)?; 129 let blk = ctx.sema.to_def(&impl_blk)?;
134 130
135 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}` 131 // FIXME: handle e.g. `struct S<T>; impl<U> S<U> {}`
@@ -157,10 +153,10 @@ fn find_struct_impl(ctx: &AssistContext, strukt: &ast::StructDef) -> Option<Opti
157 Some(block) 153 Some(block)
158} 154}
159 155
160fn has_new_fn(imp: &ast::ImplDef) -> bool { 156fn has_new_fn(imp: &ast::Impl) -> bool {
161 if let Some(il) = imp.item_list() { 157 if let Some(il) = imp.assoc_item_list() {
162 for item in il.assoc_items() { 158 for item in il.assoc_items() {
163 if let ast::AssocItem::FnDef(f) = item { 159 if let ast::AssocItem::Fn(f) = item {
164 if let Some(name) = f.name() { 160 if let Some(name) = f.name() {
165 if name.text().eq_ignore_ascii_case("new") { 161 if name.text().eq_ignore_ascii_case("new") {
166 return true; 162 return true;