[gtksourceview/wip/undo-redo: 5/6] UndoManager: another batch of code clean-up



commit bedc921816eda60b72339aba59d4c3d7f01825aa
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Aug 22 19:03:08 2014 +0200

    UndoManager: another batch of code clean-up

 gtksourceview/gtksourceundomanagerdefault.c |   96 +++++++++++++++++++--------
 1 files changed, 67 insertions(+), 29 deletions(-)
---
diff --git a/gtksourceview/gtksourceundomanagerdefault.c b/gtksourceview/gtksourceundomanagerdefault.c
index 79d9a16..5935b16 100644
--- a/gtksourceview/gtksourceundomanagerdefault.c
+++ b/gtksourceview/gtksourceundomanagerdefault.c
@@ -27,11 +27,8 @@
 #endif
 
 #include "gtksourceundomanagerdefault.h"
-
 #include <string.h>
-
 #include "gtksourceundomanager.h"
-#include "gtksourceview-i18n.h"
 
 #define DEFAULT_MAX_UNDO_LEVELS -1
 
@@ -103,13 +100,6 @@ struct _GtkSourceUndoAction
        guint modified : 1;
 };
 
-enum
-{
-       PROP_0,
-       PROP_BUFFER,
-       PROP_MAX_UNDO_LEVELS
-};
-
 struct _GtkSourceUndoManagerDefaultPrivate
 {
        /* Weak ref to the buffer. */
@@ -151,6 +141,13 @@ struct _GtkSourceUndoManagerDefaultPrivate
        GtkSourceUndoAction *modified_action;
 };
 
+enum
+{
+       PROP_0,
+       PROP_BUFFER,
+       PROP_MAX_UNDO_LEVELS
+};
+
 static void gtk_source_undo_manager_iface_init (GtkSourceUndoManagerIface *iface);
 
 G_DEFINE_TYPE_WITH_CODE (GtkSourceUndoManagerDefault, gtk_source_undo_manager_default, G_TYPE_OBJECT,
@@ -237,10 +234,14 @@ static GtkSourceUndoAction *
 action_list_nth_data (GPtrArray *array,
                       gint       n)
 {
-       if (n < 0 || n >= (gint)array->len)
+       if (n < 0 || (gint)array->len <= n)
+       {
                return NULL;
+       }
        else
+       {
                return array->pdata[array->len - 1 - n];
+       }
 }
 
 static void
@@ -297,6 +298,32 @@ free_first_n_actions (GtkSourceUndoManagerDefault *um,
        }
 }
 
+static GtkSourceUndoAction *
+get_next_undo_action (GtkSourceUndoManagerDefault *manager)
+{
+       return action_list_nth_data (manager->priv->actions,
+                                    manager->priv->next_redo + 1);
+}
+
+static GtkSourceUndoAction *
+get_next_redo_action (GtkSourceUndoManagerDefault *manager)
+{
+       return action_list_nth_data (manager->priv->actions,
+                                    manager->priv->next_redo);
+}
+
+static void
+remove_redo_actions (GtkSourceUndoManagerDefault *manager)
+{
+       if (manager->priv->next_redo >= 0)
+       {
+               free_first_n_actions (manager, manager->priv->next_redo + 1);
+       }
+
+       manager->priv->next_redo = -1;
+       update_can_undo_can_redo (manager);
+}
+
 static void
 check_list_size (GtkSourceUndoManagerDefault *um)
 {
@@ -465,12 +492,7 @@ add_action (GtkSourceUndoManagerDefault *um,
 {
        GtkSourceUndoAction* action;
 
-       if (um->priv->next_redo >= 0)
-       {
-               free_first_n_actions (um, um->priv->next_redo + 1);
-       }
-
-       um->priv->next_redo = -1;
+       remove_redo_actions (um);
 
        if (!merge_action (um, undo_action))
        {
@@ -478,9 +500,14 @@ add_action (GtkSourceUndoManagerDefault *um,
                *action = *undo_action;
 
                if (action->action_type == GTK_SOURCE_UNDO_ACTION_INSERT)
-                       action->action.insert.text = g_strndup (undo_action->action.insert.text, 
undo_action->action.insert.length);
+               {
+                       action->action.insert.text = g_strndup (undo_action->action.insert.text,
+                                                               undo_action->action.insert.length);
+               }
                else if (action->action_type == GTK_SOURCE_UNDO_ACTION_DELETE)
+               {
                        action->action.delete.text = g_strdup (undo_action->action.delete.text);
+               }
                else
                {
                        g_free (action);
@@ -491,7 +518,9 @@ add_action (GtkSourceUndoManagerDefault *um,
                action->order_in_group = um->priv->actions_in_current_group;
 
                if (action->order_in_group == 1)
+               {
                        ++um->priv->num_of_groups;
+               }
 
                action_list_prepend (um->priv->actions, action);
        }
@@ -520,7 +549,9 @@ set_max_undo_levels (GtkSourceUndoManagerDefault *manager,
        manager->priv->max_undo_levels = max_undo_levels;
 
        if (max_undo_levels < 1)
+       {
                return;
+       }
 
        if (old_levels > max_undo_levels)
        {
@@ -726,8 +757,7 @@ gtk_source_undo_manager_undo_impl (GtkSourceUndoManager *manager)
 
        do
        {
-               undo_action = action_list_nth_data (manager_default->priv->actions,
-                                                   manager_default->priv->next_redo + 1);
+               undo_action = get_next_undo_action (manager_default);
 
                g_return_if_fail (undo_action != NULL);
 
@@ -815,8 +845,7 @@ gtk_source_undo_manager_redo_impl (GtkSourceUndoManager *manager)
                                  &selection_insert,
                                  &selection_bound);
 
-       undo_action = action_list_nth_data (manager_default->priv->actions,
-                                           manager_default->priv->next_redo);
+       undo_action = get_next_redo_action (manager_default);
        g_return_if_fail (undo_action != NULL);
 
        gtk_source_undo_manager_begin_not_undoable_action (manager);
@@ -862,10 +891,13 @@ gtk_source_undo_manager_redo_impl (GtkSourceUndoManager *manager)
                }
 
                if (manager_default->priv->next_redo < 0)
+               {
                        undo_action = NULL;
+               }
                else
-                       undo_action = action_list_nth_data (manager_default->priv->actions,
-                                                           manager_default->priv->next_redo);
+               {
+                       undo_action = get_next_redo_action (manager_default);
+               }
 
        } while ((undo_action != NULL) && (undo_action->order_in_group > 1));
 
@@ -987,15 +1019,19 @@ modified_changed_cb (GtkTextBuffer               *buffer,
        gint idx;
 
        if (manager->priv->actions->len == 0)
+       {
                return;
+       }
 
-       idx = manager->priv->next_redo + 1;
-       action = action_list_nth_data (manager->priv->actions, idx);
+       action = get_next_undo_action (manager);
 
-       if (gtk_text_buffer_get_modified (buffer) == FALSE)
+       /* The buffer has been saved. */
+       if (!gtk_text_buffer_get_modified (buffer))
        {
                if (action != NULL)
+               {
                        action->mergeable = FALSE;
+               }
 
                if (manager->priv->modified_action != NULL)
                {
@@ -1009,7 +1045,6 @@ modified_changed_cb (GtkTextBuffer               *buffer,
        if (action == NULL)
        {
                g_return_if_fail (manager->priv->running_not_undoable_actions > 0);
-
                return;
        }
 
@@ -1020,8 +1055,11 @@ modified_changed_cb (GtkTextBuffer               *buffer,
        }
 
        if (action->order_in_group > 1)
-               manager->priv->modified_undoing_group  = TRUE;
+       {
+               manager->priv->modified_undoing_group = TRUE;
+       }
 
+       idx = manager->priv->next_redo + 1;
        while (action->order_in_group > 1)
        {
                action = action_list_nth_data (manager->priv->actions, ++idx);


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