editor: Only add to undo history when text changes

This commit is contained in:
Andrew Hamilton 2022-04-28 22:04:52 +10:00
parent 1d37c8b147
commit 2aae3ec730

View file

@ -340,7 +340,6 @@ class Editor:
self.is_overwriting = False self.is_overwriting = False
self.previous_term_code = None self.previous_term_code = None
self.last_mouse_position = 0, 0 self.last_mouse_position = 0, 0
self.history = []
self.parts_widget = None self.parts_widget = None
def screen_x(self, x, y): def screen_x(self, x, y):
@ -440,6 +439,8 @@ class Editor:
self.view_widget.portal.is_left_aligned = False self.view_widget.portal.is_left_aligned = False
self._cursor_x, self._cursor_y = 0, 0 self._cursor_x, self._cursor_y = 0, 0
self.original_text = self.text_widget.lines.copy() self.original_text = self.text_widget.lines.copy()
self.history = []
self.add_to_history()
def load(self, path): def load(self, path):
self.path = os.path.normpath(path) self.path = os.path.normpath(path)
@ -730,6 +731,8 @@ class Editor:
def undo(self): def undo(self):
self.text_widget[:], self._cursor_x, self._cursor_y = self.history.pop() self.text_widget[:], self._cursor_x, self._cursor_y = self.history.pop()
if self.history == []:
self.add_to_history()
def toggle_overwrite(self): def toggle_overwrite(self):
self.is_overwriting = not self.is_overwriting self.is_overwriting = not self.is_overwriting
@ -797,17 +800,18 @@ class Editor:
if self.parts_widget is not None: if self.parts_widget is not None:
self.parts_widget.on_keyboard_input(term_code) self.parts_widget.on_keyboard_input(term_code)
return return
if term_code not in [terminal.CTRL_UNDERSCORE, terminal.CTRL_Z]:
self.add_to_history()
if action := (Editor.KEY_MAP.get((self.previous_term_code, term_code)) if action := (Editor.KEY_MAP.get((self.previous_term_code, term_code))
or Editor.KEY_MAP.get(term_code)): or Editor.KEY_MAP.get(term_code)):
try: try:
if action in Editor.CHANGE_ACTIONS:
self.add_to_history()
action(self) action(self)
except IndexError: except IndexError:
self.ring_bell() self.ring_bell()
elif len(term_code) == 1 and ord(term_code) < 32: elif len(term_code) == 1 and ord(term_code) < 32:
pass pass
else: else:
self.add_to_history()
self.insert_text(term_code, is_overwriting=self.is_overwriting) self.insert_text(term_code, is_overwriting=self.is_overwriting)
self.previous_term_code = term_code self.previous_term_code = term_code
self.follow_cursor() self.follow_cursor()
@ -890,6 +894,10 @@ class Editor:
terminal.CTRL_Z: undo, terminal.CTRL_G: abort_command, terminal.INSERT: toggle_overwrite, terminal.CTRL_Z: undo, terminal.CTRL_G: abort_command, terminal.INSERT: toggle_overwrite,
(terminal.CTRL_C, ">"): indent, (terminal.CTRL_C, "<"): dedent} (terminal.CTRL_C, ">"): indent, (terminal.CTRL_C, "<"): dedent}
CHANGE_ACTIONS = {open_line, enter, delete_selection, delete_character, delete_right,
paste_from_clipboard, delete_backward, join_lines, comment_lines, delete_line,
tab_align, insert_tab, indent, dedent}
def main(): def main():
editor = Editor() editor = Editor()