@@ -109,17 +109,22 @@ pub fn introduce_variable<'a>(file: &'a File, range: TextRange) -> Option<impl F
109
109
}
110
110
Some ( move || {
111
111
let mut buf = String :: new ( ) ;
112
+ let mut edit = EditBuilder :: new ( ) ;
113
+
112
114
buf. push_str ( "let var_name = " ) ;
113
115
expr. syntax ( ) . text ( ) . push_to ( & mut buf) ;
114
- buf. push_str ( ";" ) ;
115
- indent. text ( ) . push_to ( & mut buf) ;
116
-
117
- let mut edit = EditBuilder :: new ( ) ;
118
- edit. replace ( expr. syntax ( ) . range ( ) , "var_name" . to_string ( ) ) ;
119
- edit. insert ( anchor_stmt. syntax ( ) . range ( ) . start ( ) , buf) ;
116
+ if expr. syntax ( ) . range ( ) . start ( ) == anchor_stmt. syntax ( ) . range ( ) . start ( ) {
117
+ edit. replace ( expr. syntax ( ) . range ( ) , buf) ;
118
+ } else {
119
+ buf. push_str ( ";" ) ;
120
+ indent. text ( ) . push_to ( & mut buf) ;
121
+ edit. replace ( expr. syntax ( ) . range ( ) , "var_name" . to_string ( ) ) ;
122
+ edit. insert ( anchor_stmt. syntax ( ) . range ( ) . start ( ) , buf) ;
123
+ }
124
+ let cursor_position = anchor_stmt. syntax ( ) . range ( ) . start ( ) + TextUnit :: of_str ( "let " ) ;
120
125
LocalEdit {
121
126
edit : edit. finish ( ) ,
122
- cursor_position : Some ( anchor_stmt . syntax ( ) . range ( ) . start ( ) + TextUnit :: of_str ( "let " ) ) ,
127
+ cursor_position : Some ( cursor_position ) ,
123
128
}
124
129
} )
125
130
}
@@ -183,7 +188,7 @@ mod tests {
183
188
}
184
189
185
190
#[ test]
186
- fn test_intrdoduce_var ( ) {
191
+ fn test_intrdoduce_var_simple ( ) {
187
192
check_action_range (
188
193
"
189
194
fn foo() {
@@ -192,6 +197,19 @@ fn foo() {
192
197
fn foo() {
193
198
let <|>var_name = 1 + 1;
194
199
foo(var_name);
200
+ }" ,
201
+ |file, range| introduce_variable ( file, range) . map ( |f| f ( ) ) ,
202
+ ) ;
203
+ }
204
+ #[ test]
205
+ fn test_intrdoduce_var_expr_stmt ( ) {
206
+ check_action_range (
207
+ "
208
+ fn foo() {
209
+ <|>1 + 1<|>;
210
+ }" , "
211
+ fn foo() {
212
+ let <|>var_name = 1 + 1;
195
213
}" ,
196
214
|file, range| introduce_variable ( file, range) . map ( |f| f ( ) ) ,
197
215
) ;
0 commit comments