@@ -3,8 +3,7 @@ use ide_db::{assists::AssistId, defs::Definition};
3
3
use stdx:: to_upper_snake_case;
4
4
use syntax:: {
5
5
AstNode ,
6
- ast:: { self , HasName , make} ,
7
- ted,
6
+ ast:: { self , HasName , syntax_factory:: SyntaxFactory } ,
8
7
} ;
9
8
10
9
use crate :: {
@@ -69,15 +68,18 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
69
68
"Promote local to constant" ,
70
69
let_stmt. syntax ( ) . text_range ( ) ,
71
70
|edit| {
71
+ let make = SyntaxFactory :: with_mappings ( ) ;
72
+ let mut editor = edit. make_editor ( let_stmt. syntax ( ) ) ;
72
73
let name = to_upper_snake_case ( & name. to_string ( ) ) ;
73
74
let usages = Definition :: Local ( local) . usages ( & ctx. sema ) . all ( ) ;
74
75
if let Some ( usages) = usages. references . get ( & ctx. file_id ( ) ) {
75
- let name_ref = make:: name_ref ( & name) ;
76
+ let name_ref = make. name_ref ( & name) ;
76
77
77
78
for usage in usages {
78
79
let Some ( usage_name) = usage. name . as_name_ref ( ) . cloned ( ) else { continue } ;
79
80
if let Some ( record_field) = ast:: RecordExprField :: for_name_ref ( & usage_name) {
80
- let name_expr = make:: expr_path ( make:: path_from_text ( & name) ) ;
81
+ let path = make. ident_path ( & name) ;
82
+ let name_expr = make. expr_path ( path) ;
81
83
utils:: replace_record_field_expr ( ctx, edit, record_field, name_expr) ;
82
84
} else {
83
85
let usage_range = usage. range ;
@@ -86,15 +88,17 @@ pub(crate) fn promote_local_to_const(acc: &mut Assists, ctx: &AssistContext<'_>)
86
88
}
87
89
}
88
90
89
- let item = make:: item_const ( None , make:: name ( & name) , make:: ty ( & ty) , initializer)
90
- . clone_for_update ( ) ;
91
- let let_stmt = edit. make_mut ( let_stmt) ;
91
+ let item = make. item_const ( None , make. name ( & name) , make. ty ( & ty) , initializer) ;
92
92
93
93
if let Some ( ( cap, name) ) = ctx. config . snippet_cap . zip ( item. name ( ) ) {
94
- edit. add_tabstop_before ( cap, name) ;
94
+ let tabstop = edit. make_tabstop_before ( cap) ;
95
+ editor. add_annotation ( name. syntax ( ) . clone ( ) , tabstop) ;
95
96
}
96
97
97
- ted:: replace ( let_stmt. syntax ( ) , item. syntax ( ) ) ;
98
+ editor. replace ( let_stmt. syntax ( ) , item. syntax ( ) ) ;
99
+
100
+ editor. add_mappings ( make. finish_with_mappings ( ) ) ;
101
+ edit. add_file_edits ( ctx. vfs_file_id ( ) , editor) ;
98
102
} ,
99
103
)
100
104
}
0 commit comments