@@ -3,7 +3,7 @@ use ide_db::{FxHashMap, RootDatabase};
3
3
use itertools:: Itertools ;
4
4
use syntax:: {
5
5
ast:: { self , HasName } ,
6
- ted , AstNode ,
6
+ AstNode , SyntaxElement ,
7
7
} ;
8
8
9
9
use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
@@ -46,6 +46,11 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
46
46
let impl_ast = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?;
47
47
let items = impl_ast. assoc_item_list ( ) ?;
48
48
49
+ let parent_node = match ctx. covering_element ( ) {
50
+ SyntaxElement :: Node ( n) => n,
51
+ SyntaxElement :: Token ( t) => t. parent ( ) ?,
52
+ } ;
53
+
49
54
// restrict the range
50
55
// if cursor is in assoc_items, abort
51
56
let assoc_range = items. syntax ( ) . text_range ( ) ;
@@ -94,12 +99,14 @@ pub(crate) fn reorder_impl_items(acc: &mut Assists, ctx: &AssistContext<'_>) ->
94
99
"Sort items by trait definition" ,
95
100
target,
96
101
|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) ;
103
110
} ,
104
111
)
105
112
}
0 commit comments