[gtksourceview/wip/chergert/vim: 192/293] fix replaying x with linewise




commit e67b251a664ad8b5ee6b78e605a59cb764bfdf51
Author: Christian Hergert <chergert redhat com>
Date:   Sun Oct 31 18:27:58 2021 -0700

    fix replaying x with linewise

 gtksourceview/vim/gtk-source-vim-motion.c | 93 +++++++++++++++----------------
 1 file changed, 46 insertions(+), 47 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index ea4c828d..d8a5f0a7 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -1756,12 +1756,47 @@ motion_chained (GtkTextIter        *iter,
        {
                GtkSourceVimMotion *motion = g_ptr_array_index (self->chained, i);
 
+               gtk_source_vim_motion_set_mark (motion, self->mark);
                gtk_source_vim_motion_apply (motion, iter, self->applying_inclusive);
+               gtk_source_vim_motion_set_mark (motion, NULL);
        }
 
        return !gtk_text_iter_equal (&before, iter);
 }
 
+static void
+gtk_source_vim_motion_add (GtkSourceVimMotion *self,
+                           GtkSourceVimMotion *other)
+{
+       g_assert (GTK_SOURCE_IS_VIM_MOTION (self));
+       g_assert (self->motion == motion_chained);
+       g_assert (GTK_SOURCE_IS_VIM_MOTION (other));
+       g_assert (self != other);
+
+       if (self->chained->len > 0)
+       {
+               GtkSourceVimMotion *last;
+
+               last = g_ptr_array_index (self->chained, self->chained->len - 1);
+
+               if (last->motion == other->motion &&
+                   last->inclusivity == other->inclusivity &&
+                   last->f_char == other->f_char)
+               {
+                       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (last))
+                                 + gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (other));
+
+                       gtk_source_vim_state_set_count (GTK_SOURCE_VIM_STATE (last), count);
+                       return;
+               }
+       }
+
+       gtk_source_vim_motion_set_mark (other, NULL);
+       g_ptr_array_add (self->chained, g_object_ref (other));
+       gtk_source_vim_state_set_parent (GTK_SOURCE_VIM_STATE (other),
+                                        GTK_SOURCE_VIM_STATE (self));
+}
+
 GtkSourceVimState *
 gtk_source_vim_motion_chain (GtkSourceVimMotion *self,
                              GtkSourceVimMotion *other)
@@ -1771,59 +1806,23 @@ gtk_source_vim_motion_chain (GtkSourceVimMotion *self,
        g_return_val_if_fail (!self || GTK_SOURCE_IS_VIM_MOTION (self), NULL);
        g_return_val_if_fail (!other || GTK_SOURCE_IS_VIM_MOTION (other), NULL);
 
-       if (self)
-               gtk_source_vim_motion_set_mark (self, NULL);
-
-       if (other)
-               gtk_source_vim_motion_set_mark (other, NULL);
-
-       if (!self && other)
-               return g_object_ref (GTK_SOURCE_VIM_STATE (other));
-
-       if (!other && self)
-               return g_object_ref (GTK_SOURCE_VIM_STATE (other));
-
-       g_assert (self != NULL);
-       g_assert (other != NULL);
-
-       if (self->motion == motion_chained)
+       if (self == NULL || self->motion != motion_chained)
        {
-               g_assert (self->chained != NULL);
-               g_assert (self->chained->len > 0);
-
-               g_ptr_array_add (self->chained, g_object_ref (other));
-
-               gtk_source_vim_state_set_parent (GTK_SOURCE_VIM_STATE (other),
-                                                GTK_SOURCE_VIM_STATE (self));
-
-               return g_object_ref (GTK_SOURCE_VIM_STATE (self));
+               chained = GTK_SOURCE_VIM_MOTION (gtk_source_vim_motion_new ());
+               chained->motion = motion_chained;
+               chained->inclusivity = INCLUSIVE;
+               chained->chained = g_ptr_array_new_with_free_func (g_object_unref);
        }
-
-       if (self->motion == other->motion &&
-           self->inclusivity == other->inclusivity &&
-           self->f_char == other->f_char)
+       else
        {
-               int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self))
-                         + gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (other));
-
-               gtk_source_vim_state_set_count (GTK_SOURCE_VIM_STATE (self), count);
-
-               return g_object_ref (GTK_SOURCE_VIM_STATE (self));
+               chained = g_object_ref (self);
        }
 
+       if (self != chained && self != NULL)
+               gtk_source_vim_motion_add (chained, self);
 
-       chained = GTK_SOURCE_VIM_MOTION (gtk_source_vim_motion_new ());
-       chained->motion = motion_chained;
-       chained->inclusivity = INCLUSIVE;
-       chained->chained = g_ptr_array_new_with_free_func (g_object_unref);
-
-       g_ptr_array_add (chained->chained, g_object_ref (self));
-       g_ptr_array_add (chained->chained, g_object_ref (other));
-
-       gtk_source_vim_state_set_parent (GTK_SOURCE_VIM_STATE (self),
-                                        GTK_SOURCE_VIM_STATE (chained));
-       gtk_source_vim_state_set_parent (GTK_SOURCE_VIM_STATE (other),
-                                        GTK_SOURCE_VIM_STATE (chained));
+       if (other != NULL)
+               gtk_source_vim_motion_add (chained, other);
 
        return GTK_SOURCE_VIM_STATE (chained);
 }


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