8000 Batch undo for overwrite mode by shugo · Pull Request #97 · shugo/textbringer · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Batch undo for overwrite mode #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged

Batch undo for overwrite mode #97

merged 1 commit into from
Jun 3, 2025

Conversation

shugo
Copy link
Owner
@shugo shugo commented Jun 3, 2025

No description provided.

@shugo shugo requested a review from Copilot June 3, 2025 02:31
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds batch undo support for overwrite mode by introducing a new OverwriteAction and refactoring buffer undo/redo management to handle composite edits.

  • Merge consecutive delete/insert actions into a single OverwriteAction in overwrite mode
  • Extend Buffer with undo_stack, redo_stack, undo_limit, push_undo, and undo_or_redo
  • Add string readers to InsertAction and DeleteAction for merging support

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
lib/textbringer/modes/overwrite_mode.rb Implements batch undo hook and defines OverwriteAction
lib/textbringer/buffer.rb Introduces undo/redo stacks, limit, methods, and action readers
Comments suppressed due to low confidence (3)

lib/textbringer/modes/overwrite_mode.rb:65

  • There are no tests covering the new OverwriteAction.merge behavior or the overwrite-mode batching. Adding unit tests for consecutive overwrite operations will ensure merging works correctly and prevent regressions.
def merge(inserted_string, deleted_string)

lib/textbringer/buffer.rb:1795

  • [nitpick] Using a generic string reader in DeleteAction can be ambiguous. Renaming it to deleted_string would clarify its purpose when accessed in merge logic.
attr_reader :string

lib/textbringer/modes/overwrite_mode.rb:66

  • [nitpick] String#concat mutates the original string in place. If these strings are shared or frozen elsewhere, consider using non-mutating concatenation (+) or duplicating the string before merging to avoid side effects.
@inserted_string.concat(inserted_string)

@@ -14,6 +14,23 @@ class OverwriteMode < MinorMode
buffer.delete_region(pos, buffer.point)
end
end
if !buffer.undoing? && buffer.undo_limit >= 2
Copy link
Preview
Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The guard checks undo_limit but not the current undo_stack size; if undo_limit >= 2 but the stack has fewer than two entries, accessing undo_stack[-2] returns nil and skips the merge silently. Consider also checking buffer.undo_stack.size >= 2.

Copilot uses AI. Check for mistakes.

Comment on lines 17 to 41
if !buffer.undoing? && buffer.undo_limit >= 2
if !buffer.undo_stack[-1].is_a?(DeleteAction) ||
!buffer.undo_stack[-2].is_a?(InsertAction)
next
end
delete_action = buffer.undo_stack.pop
insert_action = buffer.undo_stack.pop
action = buffer.undo_stack.last
if action.is_a?(OverwriteAction)
action.merge(insert_action.string, delete_action.string)
buffer.redo_stack.clear
else
new_action = OverwriteAction.new(buffer, insert_action.location,
insert_action.string, delete_action.string)
buffer.push_undo(new_action)
end
end
}
Copy link
Preview
Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The batch-undo logic in this hook is nested and hard to follow. Extracting it into a named helper method (e.g., handle_overwrite_merge) would improve readability and make it easier to test in isolation.

Suggested change
if !buffer.undoing? && buffer.undo_limit >= 2
if !buffer.undo_stack[-1].is_a?(DeleteAction) ||
!buffer.undo_stack[-2].is_a?(InsertAction)
next
end
delete_action = buffer.undo_stack.pop
insert_action = buffer.undo_stack.pop
action = buffer.undo_stack.last
if action.is_a?(OverwriteAction)
action.merge(insert_action.string, delete_action.string)
buffer.redo_stack.clear
else
new_action = OverwriteAction.new(buffer, insert_action.location,
insert_action.string, delete_action.string)
buffer.push_undo(new_action)
end
end
}
handle_overwrite_merge(buffer)
}
def handle_overwrite_merge(buffer)
return if buffer.undoing? || buffer.undo_limit < 2
if !buffer.undo_stack[-1].is_a?(DeleteAction) ||
!buffer.undo_stack[-2].is_a?(InsertAction)
return
end
delete_action = buffer.undo_stack.pop
insert_action = buffer.undo_stack.pop
action = buffer.undo_stack.last
if action.is_a?(OverwriteAction)
action.merge(insert_action.string, delete_action.string)
buffer.redo_stack.clear
else
new_action = OverwriteAction.new(buffer, insert_action.location,
insert_action.string, delete_action.string)
buffer.push_undo(new_action)
end
end

Copilot uses AI. Check for mistakes.

@@ -24,4 +41,30 @@ def disable
remove_hook(:post_self_insert_hook, POST_INSERT_HOOK, local: true)
end
end

class OverwriteAction < UndoableAction
Copy link
Preview
Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider adding a class-level comment for OverwriteAction explaining its role in batch undo and how its merge method combines multiple actions.

Copilot uses AI. Check for mistakes.

@shugo shugo force-pushed the undo_overwrite branch 2 times, most recently from 9a82ca2 to 8660a0e Compare June 3, 2025 02:53
@shugo shugo requested a review from Copilot June 3, 2025 02:54
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds batch undo support for overwrite mode by merging consecutive insert and delete actions into a single undoable action.

  • Introduce merge_overwrite_action refinement on Buffer and invoke it in the overwrite mode hook
  • Define a new OverwriteAction class with undo, redo, and merge methods
  • Expose string readers on InsertAction and DeleteAction to enable merging

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/textbringer/modes/overwrite_mode.rb Add merge_overwrite_action, update hook call, and implement OverwriteAction
lib/textbringer/buffer.rb Add attr_reader :string to InsertAction and DeleteAction
Comments suppressed due to low confidence (2)

lib/textbringer/modes/overwrite_mode.rb:12

  • [nitpick] Consider renaming the local variable action to last_action or previous_action for clearer intent in the merge logic.
action = @undo_stack.last

lib/textbringer/modes/overwrite_mode.rb:52

  • Add unit tests for OverwriteAction, particularly around its merge behavior and integration via merge_overwrite_action, to ensure batch undo works as intended.
class OverwriteAction < UndoableAction

@@ -1,4 +1,27 @@
module Textbringer
using Module.new {
refine Buffer do
def merge_overwrite_action
Copy link
Preview
Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment or docstring to merge_overwrite_action explaining its behavior and when it should be triggered, to improve maintainability and clarity.

Copilot uses AI. Check for mistakes.

@@ -1,4 +1,27 @@
module Textbringer
using Module.new {
Copy link
Preview
Copilot AI Jun 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Extract the anonymous refinement module into a named module (e.g., BufferOverwriteRefinement) to improve discoverability and readability.

Suggested change
using Module.new {
module BufferOverwriteRefinement

Copilot uses AI. Check for mistakes.

@shugo shugo force-pushed the undo_overwrite branch from 8660a0e to 45cec22 Compare June 3, 2025 03:05
@shugo shugo enabled auto-merge June 3, 2025 03:05
@shugo shugo merged commit ec72549 into main Jun 3, 2025
7 of 8 checks passed
@shugo shugo deleted the undo_overwrite branch June 3, 2025 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0