[gtksourceview/wip/undo-manager-fix] UndoManager: bug fix for empty user actions



commit 0bbb0e021247be9869ed6698439c8bdc3fbf485f
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Tue Oct 7 14:19:01 2014 +0200

    UndoManager: bug fix for empty user actions

 gtksourceview/gtksourceundomanagerdefault.c |   21 ++++++-----
 tests/test-undo-manager.c                   |   51 +++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 9 deletions(-)
---
diff --git a/gtksourceview/gtksourceundomanagerdefault.c b/gtksourceview/gtksourceundomanagerdefault.c
index 9ba622d..0c3c63b 100644
--- a/gtksourceview/gtksourceundomanagerdefault.c
+++ b/gtksourceview/gtksourceundomanagerdefault.c
@@ -419,6 +419,9 @@ action_group_merge (ActionGroup *group,
        Action *action;
        Action *new_action;
 
+       g_assert (group != NULL);
+       g_assert (new_group != NULL);
+
        if (group->force_not_mergeable ||
            new_group->force_not_mergeable ||
            group->actions->length > 1 ||
@@ -462,18 +465,17 @@ try_merge_current_action_group (GtkSourceUndoManagerDefault *manager)
 
        prev_node = new_node->prev;
 
-       if (prev_node == NULL)
+       if (prev_node != NULL)
        {
-               goto end;
+               prev_group = prev_node->data;
+
+               /* If the previous group is empty, it means that it was not correctly
+                * merged.
+                */
+               g_assert_cmpuint (prev_group->actions->length, >, 0);
        }
 
        new_group = new_node->data;
-       prev_group = prev_node->data;
-
-       /* If the previous group is empty, it means that it was not correctly
-        * merged.
-        */
-       g_assert_cmpuint (prev_group->actions->length, >, 0);
 
        /* If the saved_location is between the two nodes, the two nodes cannot
         * be merged. Except if the new node is empty.
@@ -485,7 +487,8 @@ try_merge_current_action_group (GtkSourceUndoManagerDefault *manager)
                goto end;
        }
 
-       if (action_group_merge (prev_group, new_group))
+       if ((prev_group == NULL && new_group->actions->length == 0) ||
+           (prev_group != NULL && action_group_merge (prev_group, new_group)))
        {
                if (manager->priv->has_saved_location &&
                    manager->priv->saved_location == new_node)
diff --git a/tests/test-undo-manager.c b/tests/test-undo-manager.c
index d79d514..46a7db2 100644
--- a/tests/test-undo-manager.c
+++ b/tests/test-undo-manager.c
@@ -616,6 +616,54 @@ test_modified (void)
        g_object_unref (source_buffer);
 }
 
+static void
+empty_user_actions (GtkTextBuffer *text_buffer,
+                   gint           count)
+{
+       gint i;
+
+       for (i = 0; i < count; i++)
+       {
+               gtk_text_buffer_begin_user_action (text_buffer);
+               gtk_text_buffer_end_user_action (text_buffer);
+       }
+}
+
+static void
+test_empty_user_actions (void)
+{
+       GtkSourceBuffer *source_buffer;
+       GtkTextBuffer *text_buffer;
+       GList *contents_history = NULL;
+
+       source_buffer = gtk_source_buffer_new (NULL);
+       text_buffer = GTK_TEXT_BUFFER (source_buffer);
+       gtk_source_buffer_set_max_undo_levels (source_buffer, -1);
+
+       contents_history = g_list_append (contents_history, get_contents (source_buffer));
+
+       empty_user_actions (text_buffer, 3);
+       check_contents_history (source_buffer, contents_history);
+
+       insert_text (source_buffer, "foo\n");
+       contents_history = g_list_append (contents_history, get_contents (source_buffer));
+       check_contents_history (source_buffer, contents_history);
+
+       empty_user_actions (text_buffer, 1);
+       check_contents_history (source_buffer, contents_history);
+
+       insert_text (source_buffer, "bar\n");
+       contents_history = g_list_append (contents_history, get_contents (source_buffer));
+       check_contents_history (source_buffer, contents_history);
+
+       gtk_source_buffer_undo (source_buffer);
+       empty_user_actions (text_buffer, 1);
+       check_contents_history (source_buffer, contents_history);
+
+       g_object_unref (source_buffer);
+       g_list_free_full (contents_history, g_free);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -648,5 +696,8 @@ main (int argc, char **argv)
        g_test_add_func ("/UndoManager/test-modified",
                         test_modified);
 
+       g_test_add_func ("/UndoManager/test-empty-user-actions",
+                        test_empty_user_actions);
+
        return g_test_run ();
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]