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