[gtksourceview] UndoManager: bug fix for empty user actions



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

    UndoManager: bug fix for empty user actions
    
    Empty user actions can happen in gnome-builder for example.

 gtksourceview/gtksourceundomanagerdefault.c |   37 +++++++++++--------
 tests/test-undo-manager.c                   |   51 +++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 16 deletions(-)
---
diff --git a/gtksourceview/gtksourceundomanagerdefault.c b/gtksourceview/gtksourceundomanagerdefault.c
index 9ba622d..619ba73 100644
--- a/gtksourceview/gtksourceundomanagerdefault.c
+++ b/gtksourceview/gtksourceundomanagerdefault.c
@@ -419,6 +419,14 @@ action_group_merge (ActionGroup *group,
        Action *action;
        Action *new_action;
 
+       g_assert (group != NULL);
+       g_assert (new_group != NULL);
+
+       if (new_group->actions->length == 0)
+       {
+               return TRUE;
+       }
+
        if (group->force_not_mergeable ||
            new_group->force_not_mergeable ||
            group->actions->length > 1 ||
@@ -427,11 +435,6 @@ action_group_merge (ActionGroup *group,
                return FALSE;
        }
 
-       if (new_group->actions->length == 0)
-       {
-               return TRUE;
-       }
-
        action = g_queue_peek_head (group->actions);
        new_action = g_queue_peek_head (new_group->actions);
 
@@ -459,21 +462,19 @@ try_merge_current_action_group (GtkSourceUndoManagerDefault *manager)
        }
 
        g_assert (new_node != NULL);
+       new_group = new_node->data;
 
        prev_node = new_node->prev;
 
-       if (prev_node == NULL)
+       if (prev_node != NULL)
        {
-               goto end;
-       }
-
-       new_group = new_node->data;
-       prev_group = prev_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 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 +486,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)
@@ -500,6 +502,9 @@ try_merge_current_action_group (GtkSourceUndoManagerDefault *manager)
 
                action_group_free (new_group);
                g_queue_delete_link (manager->priv->action_groups, new_node);
+
+               check_history_size (manager);
+               update_can_undo_can_redo (manager);
                return;
        }
 
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]