[gtksourceview/wip/chergert/vim] simplify code for getting text object



commit 5e80d80d76d49c8451ff5be3df4ad2056ec50308
Author: Christian Hergert <chergert redhat com>
Date:   Fri Nov 5 00:11:33 2021 -0700

    simplify code for getting text object
    
    so we can reuse it in delete too

 gtksourceview/vim/gtk-source-vim-normal.c | 184 ++++++++++++++----------------
 1 file changed, 88 insertions(+), 96 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index 6e374bb6..6747aec7 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -96,6 +96,92 @@ gtk_source_vim_normal_bail (GtkSourceVimNormal *self)
        return TRUE;
 }
 
+static GtkSourceVimState *
+get_text_object (guint keyval,
+                 guint change_modifier)
+{
+       switch (keyval)
+       {
+               case GDK_KEY_w:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_word ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_word ();
+
+               case GDK_KEY_W:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_WORD ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_WORD ();
+
+               case GDK_KEY_p:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_paragraph ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_paragraph ();
+
+               case GDK_KEY_s:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_sentence ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_sentence ();
+
+               case GDK_KEY_bracketright:
+               case GDK_KEY_bracketleft:
+                       /* TODO: this needs to use separate mechanisms for [ vs ] */
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_block_bracket ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_block_bracket ();
+
+               case GDK_KEY_braceleft:
+               case GDK_KEY_braceright:
+                       /* TODO: this needs to use separate mechanisms for { vs } */
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_block_brace ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_block_brace ();
+
+               case GDK_KEY_less:
+               case GDK_KEY_greater:
+                       /* TODO: this needs to use separate mechanisms for < vs > */
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_block_brace ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_block_brace ();
+
+               case GDK_KEY_apostrophe:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_quote_single ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_quote_single ();
+
+               case GDK_KEY_quotedbl:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_quote_double ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_quote_double ();
+
+               case GDK_KEY_grave:
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_quote_grave ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_quote_grave ();
+
+               case GDK_KEY_parenleft:
+               case GDK_KEY_parenright:
+               case GDK_KEY_b:
+                       /* TODO: this needs to use separate mechanisms for ( vs ) */
+                       if (change_modifier == CHANGE_A)
+                               return gtk_source_vim_text_object_new_a_block_paren ();
+                       else
+                               return gtk_source_vim_text_object_new_inner_block_paren ();
+
+               default:
+                       return NULL;
+       }
+}
+
 static gboolean
 gtk_source_vim_normal_replace_one (GtkSourceVimNormal *self)
 {
@@ -671,105 +757,11 @@ key_handler_c_with_modifier (GtkSourceVimNormal *self,
                              GdkModifierType     mods,
                              const char         *string)
 {
-       GtkSourceVimState *text_object = NULL;
+       GtkSourceVimState *text_object;
 
        g_assert (GTK_SOURCE_IS_VIM_NORMAL (self));
 
-       /* This should be a TextObject and we might need a "motion or
-        * text object" state.
-        */
-
-       switch (keyval)
-       {
-               case GDK_KEY_w:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_word ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_word ();
-                       break;
-
-               case GDK_KEY_W:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_WORD ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_WORD ();
-                       break;
-
-               case GDK_KEY_p:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_paragraph ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_paragraph ();
-                       break;
-
-               case GDK_KEY_s:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_sentence ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_sentence ();
-                       break;
-
-               case GDK_KEY_bracketright:
-               case GDK_KEY_bracketleft:
-                       /* TODO: this needs to use separate mechanisms for [ vs ] */
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_block_bracket ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_block_bracket ();
-                       break;
-
-               case GDK_KEY_braceleft:
-               case GDK_KEY_braceright:
-                       /* TODO: this needs to use separate mechanisms for { vs } */
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_block_brace ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_block_brace ();
-                       break;
-
-               case GDK_KEY_less:
-               case GDK_KEY_greater:
-                       /* TODO: this needs to use separate mechanisms for < vs > */
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_block_brace ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_block_brace ();
-                       break;
-
-               case GDK_KEY_apostrophe:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_quote_single ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_quote_single ();
-                       break;
-
-               case GDK_KEY_quotedbl:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_quote_double ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_quote_double ();
-                       break;
-
-               case GDK_KEY_grave:
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_quote_grave ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_quote_grave ();
-                       break;
-
-               case GDK_KEY_parenleft:
-               case GDK_KEY_parenright:
-               case GDK_KEY_b:
-                       /* TODO: this needs to use separate mechanisms for ( vs ) */
-                       if (self->change_modifier == CHANGE_A)
-                               text_object = gtk_source_vim_text_object_new_a_block_paren ();
-                       else
-                               text_object = gtk_source_vim_text_object_new_inner_block_paren ();
-                       break;
-
-               default:
-                       break;
-       }
+       text_object = get_text_object (keyval, self->change_modifier);
 
        if (text_object != NULL)
        {


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