Merge pull request #348 from prculley/undo

bug 9927; fix UndoableEntry for pygobject bug
This commit is contained in:
Sam Manzi 2017-02-04 10:36:01 +11:00 committed by GitHub
commit b58ac2da8a

View File

@ -51,7 +51,7 @@ from .undoablebuffer import Stack
class UndoableInsertEntry: class UndoableInsertEntry:
"""something that has been inserted into our Gtk.editable""" """something that has been inserted into our Gtk.editable"""
def __init__(self, text, length, position, editable): def __init__(self, text, length, position):
self.offset = position self.offset = position
self.text = text self.text = text
#unicode char can have length > 1 as it points in the buffer #unicode char can have length > 1 as it points in the buffer
@ -80,7 +80,7 @@ class UndoableDeleteEntry:
else: else:
self.mergeable = True self.mergeable = True
class UndoableEntry(Gtk.Entry): class UndoableEntry(Gtk.Entry, Gtk.Editable):
""" """
The UndoableEntry is an Entry subclass with additional features. The UndoableEntry is an Entry subclass with additional features.
@ -102,7 +102,6 @@ class UndoableEntry(Gtk.Entry):
self.not_undoable_action = False self.not_undoable_action = False
self.undo_in_progress = False self.undo_in_progress = False
self.connect('insert-text', self._on_insert_text)
self.connect('delete-text', self._on_delete_text) self.connect('delete-text', self._on_delete_text)
self.connect('key-press-event', self._on_key_press_event) self.connect('key-press-event', self._on_key_press_event)
@ -134,7 +133,7 @@ class UndoableEntry(Gtk.Entry):
def __empty_redo_stack(self): def __empty_redo_stack(self):
self.redo_stack = [] self.redo_stack = []
def _on_insert_text(self, editable, text, length, positionptr): def do_insert_text(self, text, length, position):
def can_be_merged(prev, cur): def can_be_merged(prev, cur):
""" """
see if we can merge multiple inserts here see if we can merge multiple inserts here
@ -159,19 +158,17 @@ class UndoableEntry(Gtk.Entry):
if not self.undo_in_progress: if not self.undo_in_progress:
self.__empty_redo_stack() self.__empty_redo_stack()
if self.not_undoable_action: while not self.not_undoable_action:
return undo_action = self.insertclass(text, length, self.get_position())
undo_action = self.insertclass(text, length, editable.get_position(),
editable)
try: try:
prev_insert = self.undo_stack.pop() prev_insert = self.undo_stack.pop()
except IndexError: except IndexError:
self.undo_stack.append(undo_action) self.undo_stack.append(undo_action)
return break
if not isinstance(prev_insert, self.insertclass): if not isinstance(prev_insert, self.insertclass):
self.undo_stack.append(prev_insert) self.undo_stack.append(prev_insert)
self.undo_stack.append(undo_action) self.undo_stack.append(undo_action)
return break
if can_be_merged(prev_insert, undo_action): if can_be_merged(prev_insert, undo_action):
prev_insert.length += undo_action.length prev_insert.length += undo_action.length
prev_insert.text += undo_action.text prev_insert.text += undo_action.text
@ -179,6 +176,9 @@ class UndoableEntry(Gtk.Entry):
else: else:
self.undo_stack.append(prev_insert) self.undo_stack.append(prev_insert)
self.undo_stack.append(undo_action) self.undo_stack.append(undo_action)
break
self.get_buffer().insert_text(position, text, length)
return position + length
def _on_delete_text(self, editable, start, end): def _on_delete_text(self, editable, start, end):
def can_be_merged(prev, cur): def can_be_merged(prev, cur):