[gtksourceview/wip/chergert/vim: 239/363] simplify motion for things like dd or yy




commit d5fef3b0864c276851d1eba3217760b0967bdf8a
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 3 11:52:50 2021 -0700

    simplify motion for things like dd or yy

 gtksourceview/vim/gtk-source-vim-motion.c | 72 +++++++++++++++++++++++--------
 gtksourceview/vim/gtk-source-vim-motion.h |  1 +
 gtksourceview/vim/gtk-source-vim-normal.c | 12 +++---
 3 files changed, 60 insertions(+), 25 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index cfe724b7..f16a4d06 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -64,6 +64,13 @@ struct _GtkSourceVimMotion
         */
        int apply_count;
 
+       /* If we need to alter the count of the motion by a value
+        * (typically used for things like yy dd and other things that
+        * are "this line" but can be repeated to extend). Therefore
+        * the value is generally either 0 or -1.
+        */
+       int alter_count;
+
        /* Apply the motion when leaving the state. This is useful
         * so that you can either capture a motion for future use
         * or simply apply it immediately.
@@ -96,6 +103,12 @@ struct _GtkSourceVimMotion
 
 G_DEFINE_TYPE (GtkSourceVimMotion, gtk_source_vim_motion, GTK_SOURCE_TYPE_VIM_STATE)
 
+static inline int
+get_adjusted_count (GtkSourceVimMotion *self)
+{
+       return gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self)) + self->alter_count;
+}
+
 static inline gboolean
 get_number (guint  keyval,
             int   *n)
@@ -468,7 +481,7 @@ static gboolean
 motion_forward_char_same_line (GtkTextIter        *iter,
                                GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
 
        if (self->apply_count != 1)
                return FALSE;
@@ -597,24 +610,28 @@ motion_next_line_first_char (GtkTextIter        *iter,
 
 static gboolean
 motion_next_line_visual_column (GtkTextIter        *iter,
-                                GtkSourceVimMotion *state)
+                                GtkSourceVimMotion *self)
 {
-       GtkSourceView *view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (state));
-       GtkTextIter before = *iter;
-       guint line = gtk_text_iter_get_line (iter);
-       guint column;
+       GtkTextBuffer *buffer = gtk_text_iter_get_buffer (iter);
+       GtkSourceView *view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
+       int column = gtk_source_vim_state_get_visual_column (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
+       int line = gtk_text_iter_get_line (iter);
 
-       state->invalidates_visual_column = FALSE;
+       self->invalidates_visual_column = FALSE;
 
-       column = gtk_source_vim_state_get_visual_column (GTK_SOURCE_VIM_STATE (state));
+       if (self->apply_count != 1 || count == 0)
+               return FALSE;
 
-       gtk_text_iter_set_line (iter, line + 1);
+       gtk_text_buffer_get_iter_at_line (buffer, iter, line + count);
        get_iter_at_visual_column (view, iter, column);
 
        if (!gtk_text_iter_starts_line (iter) && gtk_text_iter_ends_line (iter))
+       {
                gtk_text_iter_backward_char (iter);
+       }
 
-       return !gtk_text_iter_equal (&before, iter);
+       return TRUE;
 }
 
 static gboolean
@@ -1075,7 +1092,7 @@ static gboolean
 motion_next_scroll_page (GtkTextIter        *iter,
                          GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
        GtkTextBuffer *buffer = gtk_text_iter_get_buffer (iter);
        GtkTextMark *insert = gtk_text_buffer_get_insert (buffer);
 
@@ -1092,7 +1109,7 @@ static gboolean
 motion_prev_scroll_page (GtkTextIter        *iter,
                          GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
        GtkTextBuffer *buffer = gtk_text_iter_get_buffer (iter);
        GtkTextMark *insert = gtk_text_buffer_get_insert (buffer);
 
@@ -1108,7 +1125,7 @@ static gboolean
 motion_prev_scroll_line (GtkTextIter        *iter,
                          GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
        GtkTextBuffer *buffer = gtk_text_iter_get_buffer (iter);
        GtkTextMark *insert = gtk_text_buffer_get_insert (buffer);
        GtkSourceView *view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
@@ -1135,7 +1152,7 @@ static gboolean
 motion_next_scroll_line (GtkTextIter        *iter,
                          GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
        GtkTextBuffer *buffer = gtk_text_iter_get_buffer (iter);
        GtkTextMark *insert = gtk_text_buffer_get_insert (buffer);
        GtkSourceView *view = gtk_source_vim_state_get_view (GTK_SOURCE_VIM_STATE (self));
@@ -1171,7 +1188,7 @@ static gboolean
 motion_line_number (GtkTextIter        *iter,
                     GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
 
        if (self->apply_count != 1)
                return FALSE;
@@ -1457,7 +1474,7 @@ gtk_source_vim_motion_repeat (GtkSourceVimState *state)
        }
 
        buffer = gtk_source_vim_state_get_buffer (state, &iter, NULL);
-       count = gtk_source_vim_state_get_count (state);
+       count = get_adjusted_count (self);
 
        if (self->mark != NULL)
        {
@@ -1571,7 +1588,7 @@ gtk_source_vim_motion_apply (GtkSourceVimMotion *self,
        self->applying_inclusive = !!apply_inclusive;
 
        begin_offset = gtk_text_iter_get_offset (iter);
-       count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       count = get_adjusted_count (self);
 
        do
        {
@@ -1643,6 +1660,7 @@ gtk_source_vim_motion_new_line_end (void)
        self = g_object_new (GTK_SOURCE_TYPE_VIM_MOTION, NULL);
        self->motion = motion_line_end;
        self->inclusivity = INCLUSIVE;
+       self->wise = CHARWISE;
 
        return GTK_SOURCE_VIM_STATE (self);
 }
@@ -1720,7 +1738,7 @@ static gboolean
 motion_line_end_with_nl (GtkTextIter        *iter,
                          GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
        return do_motion_line_end_with_nl (iter, self->apply_count, count);
 }
 
@@ -1728,7 +1746,7 @@ static gboolean
 motion_next_line_end_with_nl (GtkTextIter        *iter,
                               GtkSourceVimMotion *self)
 {
-       int count = gtk_source_vim_state_get_count (GTK_SOURCE_VIM_STATE (self));
+       int count = get_adjusted_count (self);
        return do_motion_line_end_with_nl (iter, self->apply_count, count + 1);
 }
 
@@ -1763,6 +1781,8 @@ gtk_source_vim_motion_new_none (void)
 
        self = g_object_new (GTK_SOURCE_TYPE_VIM_MOTION, NULL);
        self->motion = motion_none;
+       self->inclusivity = INCLUSIVE;
+       self->wise = CHARWISE;
 
        return GTK_SOURCE_VIM_STATE (self);
 }
@@ -1863,3 +1883,17 @@ gtk_source_vim_motion_is_linewise (GtkSourceVimMotion *self)
 
        return self->wise == LINEWISE;
 }
+
+GtkSourceVimState *
+gtk_source_vim_motion_new_down (int alter_count)
+{
+       GtkSourceVimMotion *self;
+
+       self = g_object_new (GTK_SOURCE_TYPE_VIM_MOTION, NULL);
+       self->motion = motion_next_line_visual_column;
+       self->inclusivity = EXCLUSIVE;
+       self->wise = LINEWISE;
+       self->alter_count = alter_count;
+
+       return GTK_SOURCE_VIM_STATE (self);
+}
diff --git a/gtksourceview/vim/gtk-source-vim-motion.h b/gtksourceview/vim/gtk-source-vim-motion.h
index 2cc1a951..e74f5c27 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.h
+++ b/gtksourceview/vim/gtk-source-vim-motion.h
@@ -41,6 +41,7 @@ GtkSourceVimState *gtk_source_vim_motion_new_line_end_with_nl      (void);
 GtkSourceVimState *gtk_source_vim_motion_new_next_line_end_with_nl (void);
 GtkSourceVimState *gtk_source_vim_motion_new_previous_line_end     (void);
 GtkSourceVimState *gtk_source_vim_motion_new_forward_char          (void);
+GtkSourceVimState *gtk_source_vim_motion_new_down                  (int                 adjust_count);
 GtkSourceVimState *gtk_source_vim_motion_new_line_start            (void);
 void               gtk_source_vim_motion_set_apply_on_leave        (GtkSourceVimMotion *self,
                                                                     gboolean            apply_on_leave);
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index 0ffedf41..3247599e 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -452,8 +452,8 @@ key_handler_command (GtkSourceVimNormal *self,
 
                case GDK_KEY_S:
                        gtk_source_vim_normal_begin_change (self,
-                                                           gtk_source_vim_motion_new_line_end_with_nl (),
-                                                           gtk_source_vim_motion_new_line_start (),
+                                                           gtk_source_vim_motion_new_line_end (),
+                                                           gtk_source_vim_motion_new_first_char (),
                                                            GTK_SOURCE_VIM_INSERT_HERE,
                                                            NULL);
                        return TRUE;
@@ -497,8 +497,8 @@ key_handler_command (GtkSourceVimNormal *self,
 
                case GDK_KEY_Y:
                        gtk_source_vim_normal_begin_command (self,
-                                                            gtk_source_vim_motion_new_line_end_with_nl (),
-                                                            gtk_source_vim_motion_new_line_start (),
+                                                            gtk_source_vim_motion_new_down (-1),
+                                                            gtk_source_vim_motion_new_none (),
                                                             ":yank");
                        return TRUE;
 
@@ -691,8 +691,8 @@ key_handler_yank (GtkSourceVimNormal *self,
        {
                case GDK_KEY_y:
                        gtk_source_vim_normal_begin_command (self,
-                                                            gtk_source_vim_motion_new_line_end_with_nl (),
-                                                            gtk_source_vim_motion_new_line_start (),
+                                                            gtk_source_vim_motion_new_down (-1),
+                                                            gtk_source_vim_motion_new_none (),
                                                             ":yank");
                        return TRUE;
 


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