[gtksourceview/wip/chergert/vim] start tracking command text



commit 5b56f200b68417003c0638c7eaa769beef05ff29
Author: Christian Hergert <chergert redhat com>
Date:   Tue Oct 26 17:13:02 2021 -0700

    start tracking command text

 gtksourceview/vim/gtk-source-vim-normal.c | 39 +++++++++++++++++++++++++++++++
 gtksourceview/vim/gtk-source-vim-state.c  | 21 +++++++++++++++++
 gtksourceview/vim/gtk-source-vim-state.h  |  4 ++++
 gtksourceview/vim/gtk-source-vim.c        | 37 +++++++++++++++++++----------
 tests/test-vim.c                          |  2 ++
 5 files changed, 91 insertions(+), 12 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-normal.c b/gtksourceview/vim/gtk-source-vim-normal.c
index 5125229f..7361c089 100644
--- a/gtksourceview/vim/gtk-source-vim-normal.c
+++ b/gtksourceview/vim/gtk-source-vim-normal.c
@@ -37,6 +37,8 @@ struct _GtkSourceVimNormal
 {
        GtkSourceVimState parent_instance;
 
+       GString *command_text;
+
        KeyHandler handler;
 
        int repeat;
@@ -628,6 +630,8 @@ gtk_source_vim_normal_handle_keypress (GtkSourceVimState *state,
 
        g_assert (GTK_SOURCE_IS_VIM_STATE (self));
 
+       g_string_append (self->command_text, string);
+
        if (gtk_source_vim_state_is_escape (keyval, mods))
        {
                gtk_source_vim_normal_clear (self);
@@ -690,11 +694,43 @@ gtk_source_vim_normal_enter (GtkSourceVimState *state)
        gtk_source_vim_state_set_overwrite (state, TRUE);
 }
 
+static void
+gtk_source_vim_normal_append_command (GtkSourceVimState *state,
+                                      GString           *string)
+{
+       GtkSourceVimNormal *self = (GtkSourceVimNormal *)state;
+
+       g_assert (GTK_SOURCE_IS_VIM_STATE (state));
+       g_assert (string != NULL);
+
+       if (self->command_text->len > 0)
+       {
+               g_string_append_len (string,
+                                    self->command_text->str,
+                                    self->command_text->len);
+       }
+}
+
+static void
+gtk_source_vim_normal_finalize (GObject *object)
+{
+       GtkSourceVimNormal *self = (GtkSourceVimNormal *)object;
+
+       g_string_free (self->command_text, TRUE);
+       self->command_text = NULL;
+
+       G_OBJECT_CLASS (gtk_source_vim_normal_parent_class)->finalize (object);
+}
+
 static void
 gtk_source_vim_normal_class_init (GtkSourceVimNormalClass *klass)
 {
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
 
+       object_class->finalize = gtk_source_vim_normal_finalize;
+
+       state_class->append_command = gtk_source_vim_normal_append_command;
        state_class->handle_keypress = gtk_source_vim_normal_handle_keypress;
        state_class->enter = gtk_source_vim_normal_enter;
        state_class->resume = gtk_source_vim_normal_resume;
@@ -704,6 +740,7 @@ static void
 gtk_source_vim_normal_init (GtkSourceVimNormal *self)
 {
        self->handler = key_handler_initial;
+       self->command_text = g_string_new (NULL);
 }
 
 GtkSourceVimState *
@@ -720,4 +757,6 @@ gtk_source_vim_normal_clear (GtkSourceVimNormal *self)
        self->handler = key_handler_initial;
        self->repeat = 0;
        self->has_repeat = FALSE;
+
+       g_string_truncate (self->command_text, 0);
 }
diff --git a/gtksourceview/vim/gtk-source-vim-state.c b/gtksourceview/vim/gtk-source-vim-state.c
index 538dcc5c..59b6ad22 100644
--- a/gtksourceview/vim/gtk-source-vim-state.c
+++ b/gtksourceview/vim/gtk-source-vim-state.c
@@ -719,3 +719,24 @@ gtk_source_vim_state_z_scroll (GtkSourceVimState *self,
 
        gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (view), &iter, 0, TRUE, 1.0, yalign);
 }
+
+void
+gtk_source_vim_state_append_command (GtkSourceVimState *self,
+                                     GString           *string)
+{
+       GtkSourceVimState *child;
+
+       g_return_if_fail (GTK_SOURCE_IS_VIM_STATE (self));
+
+       if (GTK_SOURCE_VIM_STATE_GET_CLASS (self)->append_command)
+       {
+               GTK_SOURCE_VIM_STATE_GET_CLASS (self)->append_command (self, string);
+       }
+
+       child = gtk_source_vim_state_get_child (self);
+
+       if (child != NULL)
+       {
+               gtk_source_vim_state_append_command (child, string);
+       }
+}
diff --git a/gtksourceview/vim/gtk-source-vim-state.h b/gtksourceview/vim/gtk-source-vim-state.h
index 04ec22be..45e0fabc 100644
--- a/gtksourceview/vim/gtk-source-vim-state.h
+++ b/gtksourceview/vim/gtk-source-vim-state.h
@@ -52,11 +52,15 @@ struct _GtkSourceVimStateClass
        gboolean (*get_can_repeat)  (GtkSourceVimState *state);
        void     (*repeat)          (GtkSourceVimState *state,
                                     int                repeat);
+       void     (*append_command)  (GtkSourceVimState *state,
+                                    GString           *string);
 };
 
 void               gtk_source_vim_state_push                  (GtkSourceVimState *self,
                                                                GtkSourceVimState *new_state);
 void               gtk_source_vim_state_pop                   (GtkSourceVimState *self);
+void               gtk_source_vim_state_append_command        (GtkSourceVimState *self,
+                                                               GString           *string);
 void               gtk_source_vim_state_beep                  (GtkSourceVimState *self);
 GtkSourceVimState *gtk_source_vim_state_get_child             (GtkSourceVimState *self);
 GtkSourceVimState *gtk_source_vim_state_get_current           (GtkSourceVimState *self);
diff --git a/gtksourceview/vim/gtk-source-vim.c b/gtksourceview/vim/gtk-source-vim.c
index c024ec0d..7d55642e 100644
--- a/gtksourceview/vim/gtk-source-vim.c
+++ b/gtksourceview/vim/gtk-source-vim.c
@@ -30,6 +30,7 @@
 struct _GtkSourceVim
 {
        GtkSourceVimState parent_instance;
+       GString *command_text;
 };
 
 G_DEFINE_TYPE (GtkSourceVim, gtk_source_vim, GTK_SOURCE_TYPE_VIM_STATE)
@@ -71,9 +72,11 @@ static gboolean
 gtk_source_vim_handle_event (GtkSourceVimState *state,
                              GdkEvent          *event)
 {
+       GtkSourceVim *self = (GtkSourceVim *)state;
        GtkSourceVimState *current;
+       gboolean ret;
 
-       g_assert (GTK_SOURCE_IS_VIM (state));
+       g_assert (GTK_SOURCE_IS_VIM (self));
        g_assert (event != NULL);
 
        current = gtk_source_vim_state_get_current (state);
@@ -83,7 +86,13 @@ gtk_source_vim_handle_event (GtkSourceVimState *state,
                return FALSE;
        }
 
-       return gtk_source_vim_state_handle_event (current, event);
+       ret = gtk_source_vim_state_handle_event (current, event);
+
+       g_string_truncate (self->command_text, 0);
+       gtk_source_vim_state_append_command (state, self->command_text);
+       g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COMMAND_TEXT]);
+
+       return ret;
 }
 
 static void
@@ -95,6 +104,17 @@ gtk_source_vim_view_set (GtkSourceVimState *state)
        gtk_source_vim_state_push (state, gtk_source_vim_normal_new ());
 }
 
+static void
+gtk_source_vim_finalize (GObject *object)
+{
+       GtkSourceVim *self = (GtkSourceVim *)object;
+
+       g_string_free (self->command_text, TRUE);
+       self->command_text = 0;
+
+       G_OBJECT_CLASS (gtk_source_vim_parent_class)->finalize (object);
+}
+
 static void
 gtk_source_vim_get_property (GObject    *object,
                              guint       prop_id,
@@ -124,6 +144,7 @@ gtk_source_vim_class_init (GtkSourceVimClass *klass)
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
 
+       object_class->finalize = gtk_source_vim_finalize;
        object_class->get_property = gtk_source_vim_get_property;
 
        state_class->handle_event = gtk_source_vim_handle_event;
@@ -198,23 +219,15 @@ gtk_source_vim_class_init (GtkSourceVimClass *klass)
 static void
 gtk_source_vim_init (GtkSourceVim *self)
 {
+       self->command_text = g_string_new (NULL);
 }
 
 const char *
 gtk_source_vim_get_command_text (GtkSourceVim *self)
 {
-       GtkSourceVimState *current;
-
        g_return_val_if_fail (GTK_SOURCE_IS_VIM (self), NULL);
 
-       current = gtk_source_vim_state_get_current (GTK_SOURCE_VIM_STATE (self));
-
-       if (GTK_SOURCE_IS_VIM_NORMAL (current))
-       {
-               /* TODO */
-       }
-
-       return "";
+       return self->command_text->str;
 }
 
 const char *
diff --git a/tests/test-vim.c b/tests/test-vim.c
index fd3518f2..0abf4e4a 100644
--- a/tests/test-vim.c
+++ b/tests/test-vim.c
@@ -124,11 +124,13 @@ main (int argc,
                                    "xalign", 0.0f,
                                    "margin-top", 6,
                                    "margin-bottom", 6,
+                                   "margin-end", 12,
                                    NULL);
        command = g_object_new (GTK_TYPE_LABEL,
                                "xalign", 0.0f,
                                "margin-top", 6,
                                "margin-bottom", 6,
+                               "margin-end", 12,
                                NULL);
        gtk_box_append (box, GTK_WIDGET (command_bar));
        gtk_box_append (box, GTK_WIDGET (command));


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