@@ -3,7 +3,7 @@ use ide_db::{FxHashMap, RootDatabase};
33use itertools:: Itertools ;
44use syntax:: {
55 ast:: { self , HasName } ,
6- ted , AstNode ,
6+ AstNode , SyntaxElement ,
77} ;
88
99use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
@@ -46,6 +46,11 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4646 let impl_ast = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?;
4747 let items = impl_ast. assoc_item_list ( ) ?;
4848
49+ let parent_node = match ctx. covering_element ( ) {
50+ SyntaxElement :: Node ( n) => n,
51+ SyntaxElement :: Token ( t) => t. parent ( ) ?,
52+ } ;
53+
4954 // restrict the range
5055 // if cursor is in assoc_items, abort
5156 let assoc_range = items. syntax ( ) . text_range ( ) ;
@@ -94,12 +99,14 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
9499 "Sort items by trait definition" ,
95100 target,
96101 |builder| {
97- let assoc_items =
98- assoc_items. into_iter ( ) . map ( |item| builder. make_mut ( item) ) . collect :: < Vec < _ > > ( ) ;
99- assoc_items
100- . into_iter ( )
101- . zip ( sorted)
102- . for_each ( |( old, new) | ted:: replace ( old. syntax ( ) , new. clone_for_update ( ) . syntax ( ) ) ) ;
102+ let mut editor = builder. make_editor ( & parent_node) ;
103+
104+ assoc_items. into_iter ( ) . zip ( sorted) . for_each ( |( old, new) | {
105+ // FIXME: remove `clone_for_update` when `SyntaxEditor` handles it for us
106+ editor. replace ( old. syntax ( ) , new. clone_for_update ( ) . syntax ( ) )
107+ } ) ;
108+
109+ builder. add_file_edits ( ctx. file_id ( ) , editor) ;
103110 } ,
104111 )
105112}
0 commit comments