Skip to content

Commit

Permalink
Refactor undo/redo
Browse files Browse the repository at this point in the history
  • Loading branch information
shugo committed May 28, 2024
1 parent 038cb11 commit fc2aa27
Showing 1 changed file with 24 additions and 38 deletions.
62 changes: 24 additions & 38 deletions lib/textbringer/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1057,47 +1057,11 @@ def yank_pop
end

def undo
check_read_only_flag
if @undo_stack.empty?
raise EditorError, "No further undo information"
end
action = @undo_stack.pop
@undoing = true
begin
was_modified = @modified
action.undo
if action.version == @version
@modified = false
action.version = nil
elsif !was_modified
action.version = @version
end
@redo_stack.push(action)
ensure
@undoing = false
end
undo_or_redo(:undo, @undo_stack, @redo_stack)
end

def redo
check_read_only_flag
if @redo_stack.empty?
raise EditorError, "No further redo information"
end
action = @redo_stack.pop
@undoing = true
begin
was_modified = @modified
action.redo
if action.version == @version
@modified = false
action.version = nil
elsif !was_modified
action.version = @version
end
@undo_stack.push(action)
ensure
@undoing = false
end
undo_or_redo(:redo, @redo_stack, @undo_stack)
end

def re_search_forward(s, raise_error: true, count: 1)
Expand Down Expand Up @@ -1713,6 +1677,28 @@ def push_undo(action)
end
end

def undo_or_redo(op, from_stack, to_stack)
check_read_only_flag
if from_stack.empty?
raise EditorError, "No further #{op} information"
end
action = from_stack.pop
@undoing = true
begin
was_modified = @modified
action.send(op)
if action.version == @version
@modified = false
action.version = nil
elsif !was_modified
action.version = @version
end
to_stack.push(action)
ensure
@undoing = false
end
end

def new_regexp(s)
if s.is_a?(Regexp)
s
Expand Down

0 comments on commit fc2aa27

Please sign in to comment.