@@ -250,8 +250,8 @@ def reset_variables(prompt = '')
250250 @resized = false
251251 @cache = { }
252252 @rendered_screen = RenderedScreen . new ( base_y : 0 , lines : [ ] , cursor_y : 0 )
253- @input_lines = [ [ [ "" ] , 0 , 0 ] ]
254- @input_lines_position = 0
253+ @undo_redo_history = [ [ [ "" ] , 0 , 0 ] ]
254+ @undo_redo_index = 0
255255 @restoring = false
256256 @prev_action_state = NullActionState
257257 @next_action_state = NullActionState
@@ -1012,7 +1012,7 @@ def update(key)
10121012 end
10131013
10141014 def input_key ( key )
1015- save_old_buffer
1015+ old_buffer_of_lines = @buffer_of_lines . dup
10161016 @config . reset_oneshot_key_bindings
10171017 if key . char . nil?
10181018 process_insert ( force : true )
@@ -1037,15 +1037,16 @@ def input_key(key)
10371037 @completion_journey_state = nil
10381038 end
10391039
1040- push_input_lines unless @restoring
1040+ modified = old_buffer_of_lines != @buffer_of_lines
1041+
1042+ push_undo_redo ( modified ) unless @restoring
10411043 @restoring = false
10421044
10431045 if @in_pasting
10441046 clear_dialogs
10451047 return
10461048 end
10471049
1048- modified = @old_buffer_of_lines != @buffer_of_lines
10491050 if !@completion_occurs && modified && !@config . disable_completion && @config . autocompletion
10501051 # Auto complete starts only when edited
10511052 process_insert ( force : true )
@@ -1054,26 +1055,17 @@ def input_key(key)
10541055 modified
10551056 end
10561057
1057- def save_old_buffer
1058- @old_buffer_of_lines = @buffer_of_lines . dup
1059- end
1060-
1061- def push_input_lines
1062- if @old_buffer_of_lines == @buffer_of_lines
1063- @input_lines [ @input_lines_position ] = [ @buffer_of_lines . dup , @byte_pointer , @line_index ]
1058+ MAX_UNDO_REDO_HISTORY_SIZE = 100
1059+ def push_undo_redo ( modified )
1060+ if modified
1061+ @undo_redo_history = @undo_redo_history [ 0 ..@undo_redo_index ]
1062+ @undo_redo_history . push ( [ @buffer_of_lines . dup , @byte_pointer , @line_index ] )
1063+ if @undo_redo_history . size > MAX_UNDO_REDO_HISTORY_SIZE
1064+ @undo_redo_history . shift
1065+ end
1066+ @undo_redo_index = @undo_redo_history . size - 1
10641067 else
1065- @input_lines = @input_lines [ 0 ..@input_lines_position ]
1066- @input_lines_position += 1
1067- @input_lines . push ( [ @buffer_of_lines . dup , @byte_pointer , @line_index ] )
1068- end
1069- trim_input_lines
1070- end
1071-
1072- MAX_INPUT_LINES = 100
1073- def trim_input_lines
1074- if @input_lines . size > MAX_INPUT_LINES
1075- @input_lines . shift
1076- @input_lines_position -= 1
1068+ @undo_redo_history [ @undo_redo_index ] = [ @buffer_of_lines . dup , @byte_pointer , @line_index ]
10771069 end
10781070 end
10791071
@@ -2331,10 +2323,10 @@ def finish
23312323
23322324 private def move_undo_redo ( direction )
23332325 @restoring = true
2334- return unless ( 0 ..@input_lines . size - 1 ) . cover? ( @input_lines_position + direction )
2326+ return unless ( 0 ..@undo_redo_history . size - 1 ) . cover? ( @undo_redo_index + direction )
23352327
2336- @input_lines_position += direction
2337- buffer_of_lines , byte_pointer , line_index = @input_lines [ @input_lines_position ]
2328+ @undo_redo_index += direction
2329+ buffer_of_lines , byte_pointer , line_index = @undo_redo_history [ @undo_redo_index ]
23382330 @buffer_of_lines = buffer_of_lines . dup
23392331 @line_index = line_index
23402332 @byte_pointer = byte_pointer
0 commit comments