@@ -111,8 +111,6 @@ fn add_missing_impl_members_inner(
111
111
) -> Option < ( ) > {
112
112
let _p = profile:: span ( "add_missing_impl_members_inner" ) ;
113
113
let impl_def = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?;
114
- let impl_item_list = impl_def. assoc_item_list ( ) ?;
115
-
116
114
let trait_ = resolve_target_trait ( & ctx. sema , & impl_def) ?;
117
115
118
116
let def_name = |item : & ast:: AssocItem | -> Option < SmolStr > {
@@ -148,11 +146,14 @@ fn add_missing_impl_members_inner(
148
146
149
147
let target = impl_def. syntax ( ) . text_range ( ) ;
150
148
acc. add ( AssistId ( assist_id, AssistKind :: QuickFix ) , label, target, |builder| {
149
+ let impl_item_list = impl_def. assoc_item_list ( ) . unwrap_or ( make:: assoc_item_list ( ) ) ;
150
+
151
151
let n_existing_items = impl_item_list. assoc_items ( ) . count ( ) ;
152
152
let source_scope = ctx. sema . scope_for_def ( trait_) ;
153
- let target_scope = ctx. sema . scope ( impl_item_list . syntax ( ) ) ;
153
+ let target_scope = ctx. sema . scope ( impl_def . syntax ( ) ) ;
154
154
let ast_transform = QualifyPaths :: new ( & target_scope, & source_scope)
155
- . or ( SubstituteTypeParams :: for_trait_impl ( & source_scope, trait_, impl_def) ) ;
155
+ . or ( SubstituteTypeParams :: for_trait_impl ( & source_scope, trait_, impl_def. clone ( ) ) ) ;
156
+
156
157
let items = missing_items
157
158
. into_iter ( )
158
159
. map ( |it| ast_transform:: apply ( & * ast_transform, it) )
@@ -162,12 +163,14 @@ fn add_missing_impl_members_inner(
162
163
_ => it,
163
164
} )
164
165
. map ( |it| edit:: remove_attrs_and_docs ( & it) ) ;
166
+
165
167
let new_impl_item_list = impl_item_list. append_items ( items) ;
166
- let first_new_item = new_impl_item_list. assoc_items ( ) . nth ( n_existing_items) . unwrap ( ) ;
168
+ let new_impl_def = impl_def. with_items ( new_impl_item_list) ;
169
+ let first_new_item =
170
+ new_impl_def. assoc_item_list ( ) . unwrap ( ) . assoc_items ( ) . nth ( n_existing_items) . unwrap ( ) ;
167
171
168
- let original_range = impl_item_list. syntax ( ) . text_range ( ) ;
169
172
match ctx. config . snippet_cap {
170
- None => builder. replace ( original_range , new_impl_item_list . to_string ( ) ) ,
173
+ None => builder. replace ( target , new_impl_def . to_string ( ) ) ,
171
174
Some ( cap) => {
172
175
let mut cursor = Cursor :: Before ( first_new_item. syntax ( ) ) ;
173
176
let placeholder;
@@ -181,8 +184,8 @@ fn add_missing_impl_members_inner(
181
184
}
182
185
builder. replace_snippet (
183
186
cap,
184
- original_range ,
185
- render_snippet ( cap, new_impl_item_list . syntax ( ) , cursor) ,
187
+ target ,
188
+ render_snippet ( cap, new_impl_def . syntax ( ) , cursor) ,
186
189
)
187
190
}
188
191
} ;
0 commit comments