@@ -83,10 +83,11 @@ impl Prompt {
83
83
}
84
84
}
85
85
86
- pub fn with_line ( mut self , line : String ) -> Self {
86
+ pub fn with_line ( mut self , line : String , editor : & Editor ) -> Self {
87
87
let cursor = line. len ( ) ;
88
88
self . line = line;
89
89
self . cursor = cursor;
90
+ self . recalculate_completion ( editor) ;
90
91
self
91
92
}
92
93
@@ -95,6 +96,7 @@ impl Prompt {
95
96
}
96
97
97
98
pub fn recalculate_completion ( & mut self , editor : & Editor ) {
99
+ self . exit_selection ( ) ;
98
100
self . completion = ( self . completion_fn ) ( editor, & self . line ) ;
99
101
}
100
102
@@ -213,12 +215,12 @@ impl Prompt {
213
215
self . cursor = pos;
214
216
}
215
217
self . recalculate_completion ( cx. editor ) ;
216
- self . exit_selection ( ) ;
217
218
}
218
219
219
- pub fn insert_str ( & mut self , s : & str ) {
220
+ pub fn insert_str ( & mut self , s : & str , editor : & Editor ) {
220
221
self . line . insert_str ( self . cursor , s) ;
221
222
self . cursor += s. len ( ) ;
223
+ self . recalculate_completion ( editor) ;
222
224
}
223
225
224
226
pub fn move_cursor ( & mut self , movement : Movement ) {
@@ -234,65 +236,65 @@ impl Prompt {
234
236
self . cursor = self . line . len ( ) ;
235
237
}
236
238
237
- pub fn delete_char_backwards ( & mut self , cx : & Context ) {
239
+ pub fn delete_char_backwards ( & mut self , editor : & Editor ) {
238
240
let pos = self . eval_movement ( Movement :: BackwardChar ( 1 ) ) ;
239
241
self . line . replace_range ( pos..self . cursor , "" ) ;
240
242
self . cursor = pos;
241
243
242
- self . exit_selection ( ) ;
243
- self . recalculate_completion ( cx. editor ) ;
244
+ self . recalculate_completion ( editor) ;
244
245
}
245
246
246
- pub fn delete_char_forwards ( & mut self , cx : & Context ) {
247
+ pub fn delete_char_forwards ( & mut self , editor : & Editor ) {
247
248
let pos = self . eval_movement ( Movement :: ForwardChar ( 1 ) ) ;
248
249
self . line . replace_range ( self . cursor ..pos, "" ) ;
249
250
250
- self . exit_selection ( ) ;
251
- self . recalculate_completion ( cx. editor ) ;
251
+ self . recalculate_completion ( editor) ;
252
252
}
253
253
254
- pub fn delete_word_backwards ( & mut self , cx : & Context ) {
254
+ pub fn delete_word_backwards ( & mut self , editor : & Editor ) {
255
255
let pos = self . eval_movement ( Movement :: BackwardWord ( 1 ) ) ;
256
256
self . line . replace_range ( pos..self . cursor , "" ) ;
257
257
self . cursor = pos;
258
258
259
- self . exit_selection ( ) ;
260
- self . recalculate_completion ( cx. editor ) ;
259
+ self . recalculate_completion ( editor) ;
261
260
}
262
261
263
- pub fn delete_word_forwards ( & mut self , cx : & Context ) {
262
+ pub fn delete_word_forwards ( & mut self , editor : & Editor ) {
264
263
let pos = self . eval_movement ( Movement :: ForwardWord ( 1 ) ) ;
265
264
self . line . replace_range ( self . cursor ..pos, "" ) ;
266
265
267
- self . exit_selection ( ) ;
268
- self . recalculate_completion ( cx. editor ) ;
266
+ self . recalculate_completion ( editor) ;
269
267
}
270
268
271
- pub fn kill_to_start_of_line ( & mut self , cx : & Context ) {
269
+ pub fn kill_to_start_of_line ( & mut self , editor : & Editor ) {
272
270
let pos = self . eval_movement ( Movement :: StartOfLine ) ;
273
271
self . line . replace_range ( pos..self . cursor , "" ) ;
274
272
self . cursor = pos;
275
273
276
- self . exit_selection ( ) ;
277
- self . recalculate_completion ( cx. editor ) ;
274
+ self . recalculate_completion ( editor) ;
278
275
}
279
276
280
- pub fn kill_to_end_of_line ( & mut self , cx : & Context ) {
277
+ pub fn kill_to_end_of_line ( & mut self , editor : & Editor ) {
281
278
let pos = self . eval_movement ( Movement :: EndOfLine ) ;
282
279
self . line . replace_range ( self . cursor ..pos, "" ) ;
283
280
284
- self . exit_selection ( ) ;
285
- self . recalculate_completion ( cx. editor ) ;
281
+ self . recalculate_completion ( editor) ;
286
282
}
287
283
288
- pub fn clear ( & mut self , cx : & Context ) {
284
+ pub fn clear ( & mut self , editor : & Editor ) {
289
285
self . line . clear ( ) ;
290
286
self . cursor = 0 ;
291
- self . recalculate_completion ( cx. editor ) ;
292
- self . exit_selection ( ) ;
287
+ self . recalculate_completion ( editor) ;
293
288
}
294
289
295
- pub fn change_history ( & mut self , register : & [ String ] , direction : CompletionDirection ) {
290
+ pub fn change_history (
291
+ & mut self ,
292
+ cx : & mut Context ,
293
+ register : char ,
294
+ direction : CompletionDirection ,
295
+ ) {
296
+ let register = cx. editor . registers . get_mut ( register) . read ( ) ;
297
+
296
298
if register. is_empty ( ) {
297
299
return ;
298
300
}
@@ -312,6 +314,7 @@ impl Prompt {
312
314
self . history_pos = Some ( index) ;
313
315
314
316
self . move_end ( ) ;
317
+ self . recalculate_completion ( cx. editor ) ;
315
318
}
316
319
317
320
pub fn change_completion_selection ( & mut self , direction : CompletionDirection ) {
@@ -494,16 +497,18 @@ impl Component for Prompt {
494
497
ctrl ! ( 'f' ) | key ! ( Right ) => self . move_cursor ( Movement :: ForwardChar ( 1 ) ) ,
495
498
ctrl ! ( 'e' ) | key ! ( End ) => self . move_end ( ) ,
496
499
ctrl ! ( 'a' ) | key ! ( Home ) => self . move_start ( ) ,
497
- ctrl ! ( 'w' ) | alt ! ( Backspace ) | ctrl ! ( Backspace ) => self . delete_word_backwards ( cx) ,
498
- alt ! ( 'd' ) | alt ! ( Delete ) | ctrl ! ( Delete ) => self . delete_word_forwards ( cx) ,
499
- ctrl ! ( 'k' ) => self . kill_to_end_of_line ( cx) ,
500
- ctrl ! ( 'u' ) => self . kill_to_start_of_line ( cx) ,
500
+ ctrl ! ( 'w' ) | alt ! ( Backspace ) | ctrl ! ( Backspace ) => {
501
+ self . delete_word_backwards ( cx. editor )
502
+ }
503
+ alt ! ( 'd' ) | alt ! ( Delete ) | ctrl ! ( Delete ) => self . delete_word_forwards ( cx. editor ) ,
504
+ ctrl ! ( 'k' ) => self . kill_to_end_of_line ( cx. editor ) ,
505
+ ctrl ! ( 'u' ) => self . kill_to_start_of_line ( cx. editor ) ,
501
506
ctrl ! ( 'h' ) | key ! ( Backspace ) => {
502
- self . delete_char_backwards ( cx) ;
507
+ self . delete_char_backwards ( cx. editor ) ;
503
508
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update ) ;
504
509
}
505
510
ctrl ! ( 'd' ) | key ! ( Delete ) => {
506
- self . delete_char_forwards ( cx) ;
511
+ self . delete_char_forwards ( cx. editor ) ;
507
512
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update ) ;
508
513
}
509
514
ctrl ! ( 's' ) => {
@@ -520,14 +525,13 @@ impl Component for Prompt {
520
525
) ;
521
526
let line = text. slice ( range. from ( ) ..range. to ( ) ) . to_string ( ) ;
522
527
if !line. is_empty ( ) {
523
- self . insert_str ( line. as_str ( ) ) ;
528
+ self . insert_str ( line. as_str ( ) , cx . editor ) ;
524
529
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update ) ;
525
530
}
526
531
}
527
532
key ! ( Enter ) => {
528
533
if self . selection . is_some ( ) && self . line . ends_with ( std:: path:: MAIN_SEPARATOR ) {
529
534
self . recalculate_completion ( cx. editor ) ;
530
- self . exit_selection ( ) ;
531
535
} else {
532
536
// handle executing with last command in history if nothing entered
533
537
let input: Cow < str > = if self . line . is_empty ( ) {
@@ -553,15 +557,13 @@ impl Component for Prompt {
553
557
}
554
558
ctrl ! ( 'p' ) | key ! ( Up ) => {
555
559
if let Some ( register) = self . history_register {
556
- let register = cx. editor . registers . get_mut ( register) ;
557
- self . change_history ( register. read ( ) , CompletionDirection :: Backward ) ;
560
+ self . change_history ( cx, register, CompletionDirection :: Backward ) ;
558
561
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update ) ;
559
562
}
560
563
}
561
564
ctrl ! ( 'n' ) | key ! ( Down ) => {
562
565
if let Some ( register) = self . history_register {
563
- let register = cx. editor . registers . get_mut ( register) ;
564
- self . change_history ( register. read ( ) , CompletionDirection :: Forward ) ;
566
+ self . change_history ( cx, register, CompletionDirection :: Forward ) ;
565
567
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update ) ;
566
568
}
567
569
}
@@ -570,7 +572,6 @@ impl Component for Prompt {
570
572
// if single completion candidate is a directory list content in completion
571
573
if self . completion . len ( ) == 1 && self . line . ends_with ( std:: path:: MAIN_SEPARATOR ) {
572
574
self . recalculate_completion ( cx. editor ) ;
573
- self . exit_selection ( ) ;
574
575
}
575
576
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update )
576
577
}
@@ -602,8 +603,8 @@ impl Component for Prompt {
602
603
. read ( c)
603
604
. and_then ( |r| r. first ( ) )
604
605
. map_or ( "" , |r| r. as_str ( ) ) ,
606
+ context. editor ,
605
607
) ;
606
- prompt. recalculate_completion ( context. editor ) ;
607
608
} ) ) ;
608
609
( self . callback_fn ) ( cx, & self . line , PromptEvent :: Update ) ;
609
610
return EventResult :: Consumed ( None ) ;
0 commit comments