Add format inserts to the undo stack as single actions

Also fixes undoing list items.
github/fork/yochananmarqos/patch-1
Gonçalo Silva 2019-06-22 04:25:43 +01:00
parent 0b6e84bf8c
commit bd2d78b86a
1 changed files with 70 additions and 62 deletions

View File

@ -1,5 +1,7 @@
from gettext import gettext as _ from gettext import gettext as _
from uberwriter.helpers import user_action
class FormatInserter: class FormatInserter:
"""Manages insertion of formatting. """Manages insertion of formatting.
@ -25,6 +27,7 @@ class FormatInserter:
"""Insert horizontal rule""" """Insert horizontal rule"""
text_buffer = text_view.get_buffer() text_buffer = text_view.get_buffer()
with user_action(text_buffer):
text_buffer.insert_at_cursor("\n\n---\n") text_buffer.insert_at_cursor("\n\n---\n")
text_view.scroll_mark_onscreen(text_buffer.get_insert()) text_view.scroll_mark_onscreen(text_buffer.get_insert())
@ -35,9 +38,11 @@ class FormatInserter:
if text_buffer.get_has_selection(): if text_buffer.get_has_selection():
(start, end) = text_buffer.get_selection_bounds() (start, end) = text_buffer.get_selection_bounds()
if start.starts_line(): if start.starts_line():
with user_action(text_buffer):
text = text_buffer.get_text(start, end, False) text = text_buffer.get_text(start, end, False)
if text.startswith(("- ", "* ", "+ ")): if text.startswith(("- ", "* ", "+ ")):
delete_end = start.forward_chars(2) delete_end = start.copy()
delete_end.forward_chars(2)
text_buffer.delete(start, delete_end) text_buffer.delete(start, delete_end)
else: else:
text_buffer.insert(start, "- ") text_buffer.insert(start, "- ")
@ -53,13 +58,13 @@ class FormatInserter:
text = text_buffer.get_text(cursor_iter, start_ext, False) text = text_buffer.get_text(cursor_iter, start_ext, False)
lines = text.splitlines() lines = text.splitlines()
with user_action(text_buffer):
for line in reversed(lines): for line in reversed(lines):
if line and line.startswith(("- ", "* ", "+ ")): if line and line.startswith(("- ", "* ", "+ ")):
if cursor_iter.starts_line(): if cursor_iter.starts_line():
text_buffer.insert_at_cursor(line[:2] + helptext) text_buffer.insert_at_cursor(line[:2] + helptext)
else: else:
text_buffer.insert_at_cursor( text_buffer.insert_at_cursor("\n" + line[:2] + helptext)
"\n" + line[:2] + helptext)
break break
else: else:
if not lines[-1] and not lines[-2]: if not lines[-1] and not lines[-2]:
@ -83,6 +88,7 @@ class FormatInserter:
"""Insert header or mark a selection as a list header""" """Insert header or mark a selection as a list header"""
text_buffer = text_view.get_buffer() text_buffer = text_view.get_buffer()
with user_action(text_buffer):
if text_buffer.get_has_selection(): if text_buffer.get_has_selection():
(start, end) = text_buffer.get_selection_bounds() (start, end) = text_buffer.get_selection_bounds()
text = text_buffer.get_text(start, end, False) text = text_buffer.get_text(start, end, False)
@ -91,12 +97,14 @@ class FormatInserter:
text = _("Header") text = _("Header")
text_buffer.insert_at_cursor("#" + " " + text) text_buffer.insert_at_cursor("#" + " " + text)
self.__select_text(text_view, 0, len(text)) self.__select_text(text_view, 0, len(text))
@staticmethod @staticmethod
def __wrap(text_view, wrap, helptext=""): def __wrap(text_view, wrap, helptext=""):
"""Inserts wrap format to the selected text (helper text when nothing selected)""" """Inserts wrap format to the selected text (helper text when nothing selected)"""
text_buffer = text_view.get_buffer() text_buffer = text_view.get_buffer()
with user_action(text_buffer):
if text_buffer.get_has_selection(): if text_buffer.get_has_selection():
# Find current highlighting # Find current highlighting
(start, end) = text_buffer.get_selection_bounds() (start, end) = text_buffer.get_selection_bounds()