@@ -111,8 +111,6 @@ fn add_missing_impl_members_inner(
111111) -> Option < ( ) > {
112112 let _p = profile:: span ( "add_missing_impl_members_inner" ) ;
113113 let impl_def = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?;
114- let impl_item_list = impl_def. assoc_item_list ( ) ?;
115-
116114 let trait_ = resolve_target_trait ( & ctx. sema , & impl_def) ?;
117115
118116 let def_name = |item : & ast:: AssocItem | -> Option < SmolStr > {
@@ -148,11 +146,14 @@ fn add_missing_impl_members_inner(
148146
149147 let target = impl_def. syntax ( ) . text_range ( ) ;
150148 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+
151151 let n_existing_items = impl_item_list. assoc_items ( ) . count ( ) ;
152152 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 ( ) ) ;
154154 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+
156157 let items = missing_items
157158 . into_iter ( )
158159 . map ( |it| ast_transform:: apply ( & * ast_transform, it) )
@@ -162,12 +163,14 @@ fn add_missing_impl_members_inner(
162163 _ => it,
163164 } )
164165 . map ( |it| edit:: remove_attrs_and_docs ( & it) ) ;
166+
165167 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 ( ) ;
167171
168- let original_range = impl_item_list. syntax ( ) . text_range ( ) ;
169172 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 ( ) ) ,
171174 Some ( cap) => {
172175 let mut cursor = Cursor :: Before ( first_new_item. syntax ( ) ) ;
173176 let placeholder;
@@ -181,8 +184,8 @@ fn add_missing_impl_members_inner(
181184 }
182185 builder. replace_snippet (
183186 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) ,
186189 )
187190 }
188191 } ;
0 commit comments