[gtksourceview/wip/chergert/vim: 84/363] add helpers to add prefix/suffix text when entering insert




commit 2036b140e3df311a5ef659ded3d9b86c1785b2a5
Author: Christian Hergert <chergert redhat com>
Date:   Wed Oct 27 11:34:54 2021 -0700

    add helpers to add prefix/suffix text when entering insert

 gtksourceview/vim/gtk-source-vim-insert.c | 178 +++++++++++++++++++++++++++++-
 gtksourceview/vim/gtk-source-vim-insert.h |   9 +-
 2 files changed, 184 insertions(+), 3 deletions(-)
---
diff --git a/gtksourceview/vim/gtk-source-vim-insert.c b/gtksourceview/vim/gtk-source-vim-insert.c
index 4ab28908..67bbea7d 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.c
+++ b/gtksourceview/vim/gtk-source-vim-insert.c
@@ -21,6 +21,9 @@
 
 #include "config.h"
 
+#include "gtksourceindenter.h"
+#include "gtksourceview.h"
+
 #include "gtk-source-vim-insert.h"
 #include "gtk-source-vim-insert-literal.h"
 #include "gtk-source-vim-replace.h"
@@ -28,10 +31,23 @@
 struct _GtkSourceVimInsert
 {
        GtkSourceVimState parent_instance;
+       char *prefix;
+       char *suffix;
+       guint indent : 1;
 };
 
 G_DEFINE_TYPE (GtkSourceVimInsert, gtk_source_vim_insert, GTK_SOURCE_TYPE_VIM_STATE)
 
+enum {
+       PROP_0,
+       PROP_INDENT,
+       PROP_PREFIX,
+       PROP_SUFFIX,
+       N_PROPS
+};
+
+static GParamSpec *properties[N_PROPS];
+
 GtkSourceVimState *
 gtk_source_vim_insert_new (void)
 {
@@ -152,14 +168,46 @@ gtk_source_vim_insert_resume (GtkSourceVimState *state,
 static void
 gtk_source_vim_insert_enter (GtkSourceVimState *state)
 {
+       GtkSourceVimInsert *self = (GtkSourceVimInsert *)state;
        GtkSourceBuffer *buffer;
+       GtkSourceView *view;
+       GtkTextIter iter;
 
-       g_assert (GTK_SOURCE_IS_VIM_INSERT (state));
+       g_assert (GTK_SOURCE_IS_VIM_INSERT (self));
 
        gtk_source_vim_state_set_overwrite (state, FALSE);
 
-       buffer = gtk_source_vim_state_get_buffer (state, NULL, NULL);
+       view = gtk_source_vim_state_get_view (state);
+       buffer = gtk_source_vim_state_get_buffer (state, &iter, NULL);
        gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
+
+       if (self->suffix)
+       {
+               gsize len = g_utf8_strlen (self->suffix, -1);
+
+               if (len > 0)
+               {
+                       gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter, self->suffix, -1);
+                       gtk_text_iter_backward_chars (&iter, len);
+                       gtk_source_vim_state_select (state, &iter, &iter);
+               }
+       }
+
+       if (self->prefix)
+       {
+               gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter, self->prefix, -1);
+               gtk_source_vim_state_select (state, &iter, &iter);
+       }
+
+       if (self->indent)
+       {
+               GtkSourceIndenter *indenter = gtk_source_view_get_indenter (view);
+
+               if (indenter != NULL)
+               {
+                       gtk_source_indenter_indent (indenter, view, &iter);
+               }
+       }
 }
 
 static void
@@ -181,9 +229,67 @@ gtk_source_vim_insert_append_command (GtkSourceVimState *state,
        g_string_truncate (string, 0);
 }
 
+static void
+gtk_source_vim_insert_get_property (GObject    *object,
+                                    guint       prop_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+       GtkSourceVimInsert *self = GTK_SOURCE_VIM_INSERT (object);
+
+       switch (prop_id)
+       {
+               case PROP_INDENT:
+                       g_value_set_boolean (value, self->indent);
+                       break;
+
+               case PROP_PREFIX:
+                       g_value_set_string (value, self->prefix);
+                       break;
+
+               case PROP_SUFFIX:
+                       g_value_set_string (value, self->suffix);
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       }
+}
+
+static void
+gtk_source_vim_insert_set_property (GObject      *object,
+                                    guint         prop_id,
+                                    const GValue *value,
+                                    GParamSpec   *pspec)
+{
+       GtkSourceVimInsert *self = GTK_SOURCE_VIM_INSERT (object);
+
+       switch (prop_id)
+       {
+               case PROP_INDENT:
+                       gtk_source_vim_insert_set_indent (self, g_value_get_boolean (value));
+                       break;
+
+               case PROP_PREFIX:
+                       gtk_source_vim_insert_set_prefix (self, g_value_get_string (value));
+                       break;
+
+               case PROP_SUFFIX:
+                       gtk_source_vim_insert_set_suffix (self, g_value_get_string (value));
+                       break;
+
+               default:
+                       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+       }
+}
+
 static void
 gtk_source_vim_insert_dispose (GObject *object)
 {
+       GtkSourceVimInsert *self = (GtkSourceVimInsert *)object;
+
+       g_clear_pointer (&self->prefix, g_free);
+
        G_OBJECT_CLASS (gtk_source_vim_insert_parent_class)->dispose (object);
 }
 
@@ -194,15 +300,83 @@ gtk_source_vim_insert_class_init (GtkSourceVimInsertClass *klass)
        GtkSourceVimStateClass *state_class = GTK_SOURCE_VIM_STATE_CLASS (klass);
 
        object_class->dispose = gtk_source_vim_insert_dispose;
+       object_class->get_property = gtk_source_vim_insert_get_property;
+       object_class->set_property = gtk_source_vim_insert_set_property;
 
        state_class->append_command = gtk_source_vim_insert_append_command;
        state_class->handle_event = gtk_source_vim_insert_handle_event;
        state_class->resume = gtk_source_vim_insert_resume;
        state_class->enter = gtk_source_vim_insert_enter;
        state_class->leave = gtk_source_vim_insert_leave;
+
+       properties [PROP_INDENT] =
+               g_param_spec_boolean ("indent",
+                                     "Indent",
+                                     "Indent after the prefix text",
+                                     FALSE,
+                                     (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+       properties [PROP_PREFIX] =
+               g_param_spec_string ("prefix",
+                                    "Prefix",
+                                    "Text to insert at the insertion cursor before entering insert mode",
+                                    NULL,
+                                    (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+       properties [PROP_SUFFIX] =
+               g_param_spec_string ("suffix",
+                                    "suffix",
+                                    "Text to insert after the insertion cursor before entering insert mode",
+                                    NULL,
+                                    (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+       g_object_class_install_properties (object_class, N_PROPS, properties);
 }
 
 static void
 gtk_source_vim_insert_init (GtkSourceVimInsert *self)
 {
 }
+
+void
+gtk_source_vim_insert_set_prefix (GtkSourceVimInsert *self,
+                                  const char         *prefix)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_INSERT (self));
+
+       if (g_strcmp0 (self->prefix, prefix) != 0)
+       {
+               g_free (self->prefix);
+               self->prefix = g_strdup (prefix);
+               g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_PREFIX]);
+       }
+}
+
+void
+gtk_source_vim_insert_set_suffix (GtkSourceVimInsert *self,
+                                  const char         *suffix)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_INSERT (self));
+
+       if (g_strcmp0 (self->suffix, suffix) != 0)
+       {
+               g_free (self->suffix);
+               self->suffix = g_strdup (suffix);
+               g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SUFFIX]);
+       }
+}
+
+void
+gtk_source_vim_insert_set_indent (GtkSourceVimInsert *self,
+                                  gboolean            indent)
+{
+       g_return_if_fail (GTK_SOURCE_IS_VIM_INSERT (self));
+
+       indent = !!indent;
+
+       if (self->indent != indent)
+       {
+               self->indent = indent;
+               g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_INDENT]);
+       }
+}
diff --git a/gtksourceview/vim/gtk-source-vim-insert.h b/gtksourceview/vim/gtk-source-vim-insert.h
index 66749541..ff1d5744 100644
--- a/gtksourceview/vim/gtk-source-vim-insert.h
+++ b/gtksourceview/vim/gtk-source-vim-insert.h
@@ -29,6 +29,13 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (GtkSourceVimInsert, gtk_source_vim_insert, GTK_SOURCE, VIM_INSERT, GtkSourceVimState)
 
-GtkSourceVimState *gtk_source_vim_insert_new (void);
+GtkSourceVimState *gtk_source_vim_insert_new        (void);
+void               gtk_source_vim_insert_set_indent (GtkSourceVimInsert *self,
+                                                     gboolean            indent);
+void               gtk_source_vim_insert_set_prefix (GtkSourceVimInsert *self,
+                                                     const char         *prefix);
+void               gtk_source_vim_insert_set_suffix (GtkSourceVimInsert *self,
+                                                     const char         *suffix);
+
 
 G_END_DECLS


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