Skip to content

Commit 300be2b

Browse files
tompngmatzbot
authored andcommitted
[ruby/reline] Undo and redo should restore indentation
(ruby/reline#793) * Undo and redo should restore indentation Undo and redo should not perform auto indentation. It should not change the indentation. Instead, it should restore previous indentation. * Rename ivar undoing(undoing or redoing) to restoring ruby/reline@6355a6e0b2
1 parent 776ec52 commit 300be2b

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

lib/reline/line_editor.rb

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def reset_variables(prompt = '')
252252
@rendered_screen = RenderedScreen.new(base_y: 0, lines: [], cursor_y: 0)
253253
@input_lines = [[[""], 0, 0]]
254254
@input_lines_position = 0
255-
@undoing = false
255+
@restoring = false
256256
@prev_action_state = NullActionState
257257
@next_action_state = NullActionState
258258
reset_line
@@ -1070,8 +1070,8 @@ def input_key(key)
10701070
@completion_journey_state = nil
10711071
end
10721072

1073-
push_input_lines unless @undoing
1074-
@undoing = false
1073+
push_input_lines unless @restoring
1074+
@restoring = false
10751075

10761076
if @in_pasting
10771077
clear_dialogs
@@ -1185,18 +1185,6 @@ def set_current_line(line, byte_pointer = nil)
11851185
process_auto_indent
11861186
end
11871187

1188-
def set_current_lines(lines, byte_pointer = nil, line_index = 0)
1189-
cursor = current_byte_pointer_cursor
1190-
@buffer_of_lines = lines
1191-
@line_index = line_index
1192-
if byte_pointer
1193-
@byte_pointer = byte_pointer
1194-
else
1195-
calculate_nearest_cursor(cursor)
1196-
end
1197-
process_auto_indent
1198-
end
1199-
12001188
def retrieve_completion_block
12011189
quote_characters = Reline.completer_quote_characters
12021190
before = current_line.byteslice(0, @byte_pointer).grapheme_clusters
@@ -2368,24 +2356,23 @@ def finish
23682356
@config.editing_mode = :vi_insert
23692357
end
23702358

2371-
private def undo(_key)
2372-
@undoing = true
2359+
private def move_undo_redo(direction)
2360+
@restoring = true
2361+
return unless (0..@input_lines.size - 1).cover?(@input_lines_position + direction)
23732362

2374-
return if @input_lines_position <= 0
2363+
@input_lines_position += direction
2364+
buffer_of_lines, byte_pointer, line_index = @input_lines[@input_lines_position]
2365+
@buffer_of_lines = buffer_of_lines.dup
2366+
@line_index = line_index
2367+
@byte_pointer = byte_pointer
2368+
end
23752369

2376-
@input_lines_position -= 1
2377-
target_lines, target_cursor_x, target_cursor_y = @input_lines[@input_lines_position]
2378-
set_current_lines(target_lines.dup, target_cursor_x, target_cursor_y)
2370+
private def undo(_key)
2371+
move_undo_redo(-1)
23792372
end
23802373

23812374
private def redo(_key)
2382-
@undoing = true
2383-
2384-
return if @input_lines_position >= @input_lines.size - 1
2385-
2386-
@input_lines_position += 1
2387-
target_lines, target_cursor_x, target_cursor_y = @input_lines[@input_lines_position]
2388-
set_current_lines(target_lines.dup, target_cursor_x, target_cursor_y)
2375+
move_undo_redo(+1)
23892376
end
23902377

23912378
private def prev_action_state_value(type)

test/reline/test_key_actor_emacs.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,20 @@ def test_redo_with_multiline
16761676
assert_line_around_cursor('3', '')
16771677
end
16781678

1679+
def test_undo_redo_restores_indentation
1680+
@line_editor.multiline_on
1681+
@line_editor.confirm_multiline_termination_proc = proc {}
1682+
input_keys(" 1", false)
1683+
assert_whole_lines([' 1'])
1684+
input_keys("2", false)
1685+
assert_whole_lines([' 12'])
1686+
@line_editor.auto_indent_proc = proc { 2 }
1687+
input_keys("\C-_", false)
1688+
assert_whole_lines([' 1'])
1689+
input_keys("\M-\C-_", false)
1690+
assert_whole_lines([' 12'])
1691+
end
1692+
16791693
def test_redo_with_many_times
16801694
str = "a" + "b" * 98 + "c"
16811695
input_keys(str, false)

0 commit comments

Comments
 (0)