[gtksourceview/wip/chergert/vim: 242/293] track what motions are considered linewise




commit ba30654cdaf07c7fc742dc64c6c50a50f1a99874
Author: Christian Hergert <chergert redhat com>
Date:   Wed Nov 3 10:12:43 2021 -0700

    track what motions are considered linewise

 gtksourceview/vim/gtk-source-vim-motion.c | 99 ++++++++++++++++++++-----------
 gtksourceview/vim/gtk-source-vim-motion.h |  1 +
 2 files changed, 65 insertions(+), 35 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-motion.c b/gtksourceview/vim/gtk-source-vim-motion.c
index 7bb3f6c6..cfe724b7 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.c
+++ b/gtksourceview/vim/gtk-source-vim-motion.c
@@ -33,6 +33,11 @@ typedef enum {
        EXCLUSIVE = 1,
 } Inclusivity;
 
+typedef enum {
+       CHARWISE = 0,
+       LINEWISE = 1,
+} MotionWise;
+
 struct _GtkSourceVimMotion
 {
        GtkSourceVimState parent_instance;
@@ -81,6 +86,12 @@ struct _GtkSourceVimMotion
        guint applying_inclusive : 1;
 
        guint invalidates_visual_column : 1;
+
+       /* Some motions are considered linewise when applying commands,
+        * generally when they land on a new line. Not all are, however, such
+        * as paragraph or sentence movements.
+        */
+       MotionWise wise : 1;
 };
 
 G_DEFINE_TYPE (GtkSourceVimMotion, gtk_source_vim_motion, GTK_SOURCE_TYPE_VIM_STATE)
@@ -1202,12 +1213,14 @@ gtk_source_vim_motion_bail (GtkSourceVimMotion *self)
 static gboolean
 gtk_source_vim_motion_complete (GtkSourceVimMotion *self,
                                 Motion              motion,
-                                Inclusivity         inclusivity)
+                                Inclusivity         inclusivity,
+                                MotionWise          wise)
 {
        g_assert (GTK_SOURCE_IS_VIM_MOTION (self));
 
        self->motion = motion;
        self->inclusivity = inclusivity;
+       self->wise = wise;
 
        g_string_truncate (self->command_text, 0);
 
@@ -1248,13 +1261,13 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                switch (keyval)
                {
                        case GDK_KEY_g:
-                               return gtk_source_vim_motion_complete (self, motion_buffer_start_first_char, 
INCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_buffer_start_first_char, 
INCLUSIVE, LINEWISE);
 
                        case GDK_KEY_e:
-                               return gtk_source_vim_motion_complete (self, motion_backward_word_end, 
EXCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_backward_word_end, 
INCLUSIVE, CHARWISE);
 
                        case GDK_KEY_E:
-                               return gtk_source_vim_motion_complete (self, motion_backward_WORD_end, 
EXCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_backward_WORD_end, 
INCLUSIVE, CHARWISE);
 
                        default:
                                return gtk_source_vim_motion_bail (self);
@@ -1274,18 +1287,21 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
        {
                switch (keyval)
                {
-
+                       /* Technically, none of these are usable with commands
+                        * like d{motion} and therefore may require some extra
+                        * tweaking to see how we use them.
+                        */
                        case GDK_KEY_f:
-                               return gtk_source_vim_motion_complete (self, motion_next_scroll_page, 
INCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_next_scroll_page, 
INCLUSIVE, LINEWISE);
 
                        case GDK_KEY_b:
-                               return gtk_source_vim_motion_complete (self, motion_prev_scroll_page, 
INCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_prev_scroll_page, 
INCLUSIVE, LINEWISE);
 
                        case GDK_KEY_e:
-                               return gtk_source_vim_motion_complete (self, motion_next_scroll_line, 
INCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_next_scroll_line, 
INCLUSIVE, LINEWISE);
 
                        case GDK_KEY_y:
-                               return gtk_source_vim_motion_complete (self, motion_prev_scroll_line, 
INCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_prev_scroll_line, 
INCLUSIVE, LINEWISE);
 
                        default:
                                break;
@@ -1298,7 +1314,7 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
                case GDK_KEY_KP_0:
                case GDK_KEY_Home:
                case GDK_KEY_bar:
-                       return gtk_source_vim_motion_complete (self, motion_line_start, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_line_start, INCLUSIVE, CHARWISE);
 
                case GDK_KEY_1: case GDK_KEY_KP_1:
                case GDK_KEY_2: case GDK_KEY_KP_2:
@@ -1315,103 +1331,106 @@ gtk_source_vim_motion_handle_keypress (GtkSourceVimState *state,
 
                case GDK_KEY_asciicircum:
                case GDK_KEY_underscore:
-                       return gtk_source_vim_motion_complete (self, motion_line_first_char, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_line_first_char, INCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_space:
-                       return gtk_source_vim_motion_complete (self, motion_forward_char, EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_char, EXCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_BackSpace:
-                       return gtk_source_vim_motion_complete (self, motion_backward_char, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_backward_char, INCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_Left:
                case GDK_KEY_h:
-                       return gtk_source_vim_motion_complete (self, motion_backward_char_same_line, 
INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_backward_char_same_line, 
INCLUSIVE, CHARWISE);
 
                case GDK_KEY_Right:
                case GDK_KEY_l:
-                       return gtk_source_vim_motion_complete (self, motion_forward_char_same_line, 
EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_char_same_line, 
EXCLUSIVE, CHARWISE);
 
                case GDK_KEY_ISO_Enter:
                case GDK_KEY_KP_Enter:
                case GDK_KEY_Return:
-                       return gtk_source_vim_motion_complete (self, motion_next_line_first_char, EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_next_line_first_char, EXCLUSIVE, 
LINEWISE);
 
                case GDK_KEY_End:
                case GDK_KEY_dollar:
-                       return gtk_source_vim_motion_complete (self, motion_line_end, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_line_end, INCLUSIVE, CHARWISE);
 
                case GDK_KEY_Down:
                case GDK_KEY_j:
-                       return gtk_source_vim_motion_complete (self, motion_next_line_visual_column, 
EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_next_line_visual_column, 
EXCLUSIVE, LINEWISE);
 
                case GDK_KEY_Up:
                case GDK_KEY_k:
-                       return gtk_source_vim_motion_complete (self, motion_prev_line_visual_column, 
INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_prev_line_visual_column, 
INCLUSIVE, LINEWISE);
 
                case GDK_KEY_G:
                        if (gtk_source_vim_state_get_count_set (state))
-                               return gtk_source_vim_motion_complete (self, motion_line_number, INCLUSIVE);
-                       /* TODO: this needs to be inclusive of the whole line */
-                       return gtk_source_vim_motion_complete (self, motion_last_line_first_char, INCLUSIVE);
+                               return gtk_source_vim_motion_complete (self, motion_line_number, INCLUSIVE, 
LINEWISE);
+                       return gtk_source_vim_motion_complete (self, motion_last_line_first_char, INCLUSIVE, 
LINEWISE);
 
                case GDK_KEY_g:
                        self->g_command = TRUE;
                        return TRUE;
 
                case GDK_KEY_H:
-                       return gtk_source_vim_motion_complete (self, motion_screen_top, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_screen_top, INCLUSIVE, LINEWISE);
 
                case GDK_KEY_M:
-                       return gtk_source_vim_motion_complete (self, motion_screen_middle, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_screen_middle, INCLUSIVE, 
LINEWISE);
 
                case GDK_KEY_L:
-                       return gtk_source_vim_motion_complete (self, motion_screen_bottom, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_screen_bottom, INCLUSIVE, 
LINEWISE);
 
                case GDK_KEY_w:
-                       return gtk_source_vim_motion_complete (self, motion_forward_word_start, EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_word_start, EXCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_W:
-                       return gtk_source_vim_motion_complete (self, motion_forward_WORD_start, EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_WORD_start, EXCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_b:
-                       return gtk_source_vim_motion_complete (self, motion_backward_word_start, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_backward_word_start, INCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_B:
-                       return gtk_source_vim_motion_complete (self, motion_backward_WORD_start, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_backward_WORD_start, INCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_e:
-                       return gtk_source_vim_motion_complete (self, motion_forward_word_end, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_word_end, INCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_E:
-                       return gtk_source_vim_motion_complete (self, motion_forward_WORD_end, INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_WORD_end, INCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_f:
                        self->waiting_for_f_char = TRUE;
                        self->motion = motion_f_char;
                        self->inclusivity = EXCLUSIVE;
+                       self->wise = CHARWISE;
                        return TRUE;
 
                case GDK_KEY_F:
                        self->waiting_for_f_char = TRUE;
                        self->motion = motion_F_char;
                        self->inclusivity = INCLUSIVE;
+                       self->wise = CHARWISE;
                        return TRUE;
 
                case GDK_KEY_parenleft:
-                       return gtk_source_vim_motion_complete (self, motion_backward_sentence_start, 
INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_backward_sentence_start, 
INCLUSIVE, CHARWISE);
 
                case GDK_KEY_parenright:
-                       return gtk_source_vim_motion_complete (self, motion_forward_sentence_start, 
EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_sentence_start, 
EXCLUSIVE, CHARWISE);
 
                case GDK_KEY_braceleft:
-                       return gtk_source_vim_motion_complete (self, motion_backward_paragraph_start, 
INCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_backward_paragraph_start, 
INCLUSIVE, CHARWISE);
 
                case GDK_KEY_braceright:
-                       return gtk_source_vim_motion_complete (self, motion_forward_paragraph_end, EXCLUSIVE);
+                       return gtk_source_vim_motion_complete (self, motion_forward_paragraph_end, EXCLUSIVE, 
CHARWISE);
 
                case GDK_KEY_n:
                case GDK_KEY_N:
                case GDK_KEY_percent:
+                       /* exclusive */
+                       /* charwise */
                        /* TODO */
                        G_GNUC_FALLTHROUGH;
 
@@ -1529,6 +1548,8 @@ gtk_source_vim_motion_init (GtkSourceVimMotion *self)
        self->apply_on_leave = TRUE;
        self->command_text = g_string_new (NULL);
        self->invalidates_visual_column = TRUE;
+       self->wise = CHARWISE;
+       self->inclusivity = INCLUSIVE;
 }
 
 gboolean
@@ -1834,3 +1855,11 @@ gtk_source_vim_motion_invalidates_visual_column (GtkSourceVimMotion *self)
 
        return self->invalidates_visual_column;
 }
+
+gboolean
+gtk_source_vim_motion_is_linewise (GtkSourceVimMotion *self)
+{
+       g_return_val_if_fail (GTK_SOURCE_IS_VIM_MOTION (self), FALSE);
+
+       return self->wise == LINEWISE;
+}
diff --git a/gtksourceview/vim/gtk-source-vim-motion.h b/gtksourceview/vim/gtk-source-vim-motion.h
index 07b1cdcf..2cc1a951 100644
--- a/gtksourceview/vim/gtk-source-vim-motion.h
+++ b/gtksourceview/vim/gtk-source-vim-motion.h
@@ -50,5 +50,6 @@ gboolean           gtk_source_vim_motion_apply                     (GtkSourceVim
                                                                     GtkTextIter        *iter,
                                                                     gboolean            apply_inclusive);
 gboolean           gtk_source_vim_motion_invalidates_visual_column (GtkSourceVimMotion *self);
+gboolean           gtk_source_vim_motion_is_linewise               (GtkSourceVimMotion *self);
 
 G_END_DECLS


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