[gnome-builder/wip/commands2] commands: add a vim command provider



commit 46ce5f44f351c9ea89b5afb72172b6b3388d4f25
Author: Christian Hergert <christian hergert me>
Date:   Thu Oct 9 16:30:26 2014 -0700

    commands: add a vim command provider
    
    This adds a provider that will check to see if the command is a VIM
    command and then dispatch it to the GbEditorVim of the active tab.
    
    It should only complete if vim-mode is enabled.

 src/commands/gb-command-vim-provider.c |   95 ++++++++++++++++++++++++++++++++
 src/commands/gb-command-vim-provider.h |   56 +++++++++++++++++++
 src/commands/gb-command-vim.c          |   63 +++++++++++++++++++++
 src/commands/gb-command-vim.h          |   32 +++++++++++
 src/gnome-builder.mk                   |    4 +
 5 files changed, 250 insertions(+), 0 deletions(-)
---
diff --git a/src/commands/gb-command-vim-provider.c b/src/commands/gb-command-vim-provider.c
new file mode 100644
index 0000000..8c73739
--- /dev/null
+++ b/src/commands/gb-command-vim-provider.c
@@ -0,0 +1,95 @@
+/* gb-command-vim-provider.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define G_LOG_DOMAIN "vim-command-provider"
+
+#include "gb-command-vim.h"
+#include "gb-command-vim-provider.h"
+#include "gb-editor-tab.h"
+#include "gb-editor-vim.h"
+
+G_DEFINE_TYPE (GbCommandVimProvider, gb_command_vim_provider,
+               GB_TYPE_COMMAND_PROVIDER)
+
+GbCommandProvider *
+gb_command_vim_provider_new (GbWorkbench *workbench)
+{
+  return g_object_new (GB_TYPE_COMMAND_VIM_PROVIDER,
+                       "workbench", workbench,
+                       NULL);
+}
+
+static GAction *
+gb_command_vim_provider_lookup (GbCommandProvider  *provider,
+                                const gchar        *command_text,
+                                GVariant          **parameters)
+{
+  GbWorkbench *workbench;
+  GSettings *settings;
+  GbTab *active_tab;
+
+  g_return_val_if_fail (GB_IS_COMMAND_VIM_PROVIDER (provider), NULL);
+  g_return_val_if_fail (command_text, NULL);
+  g_return_val_if_fail (parameters, NULL);
+
+  /* Fetch our editor gsettings */
+  settings = g_object_get_data (G_OBJECT (provider), "editor-settings");
+  if (!G_IS_SETTINGS (settings))
+    return NULL;
+
+  /* Make sure vim-mode is enabled */
+  if (!g_settings_get_boolean (settings, "vim-mode"))
+    return NULL;
+  
+  /* Make sure we have a workbench */
+  workbench = gb_command_provider_get_workbench (provider);
+  if (!GB_IS_WORKBENCH (workbench))
+    return NULL;
+
+  /* Make sure we have an editor tab last focused */
+  active_tab = gb_command_provider_get_active_tab (provider);
+  if (!GB_IS_EDITOR_TAB (active_tab))
+    return NULL;
+
+  /* See if GbEditorVim recognizes this command */
+  if (gb_editor_vim_is_command (command_text))
+    {
+      *parameters = g_variant_new_string (command_text);
+      return gb_command_vim_new (GB_EDITOR_TAB (active_tab));
+    }
+
+  return NULL;
+}
+
+static void
+gb_command_vim_provider_class_init (GbCommandVimProviderClass *klass)
+{
+  GbCommandProviderClass *provider_class = GB_COMMAND_PROVIDER_CLASS (klass);
+
+  provider_class->lookup = gb_command_vim_provider_lookup;
+}
+
+static void
+gb_command_vim_provider_init (GbCommandVimProvider *self)
+{
+  GSettings *settings;
+
+  settings = g_settings_new ("org.gnome.builder.editor");
+  g_object_set_data_full (G_OBJECT (self), "editor-settings", settings,
+                          g_object_unref);
+}
diff --git a/src/commands/gb-command-vim-provider.h b/src/commands/gb-command-vim-provider.h
new file mode 100644
index 0000000..a4a0886
--- /dev/null
+++ b/src/commands/gb-command-vim-provider.h
@@ -0,0 +1,56 @@
+/* gb-command-vim-provider.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GB_COMMAND_VIM_PROVIDER_H
+#define GB_COMMAND_VIM_PROVIDER_H
+
+#include "gb-command-provider.h"
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_COMMAND_VIM_PROVIDER            (gb_command_vim_provider_get_type())
+#define GB_COMMAND_VIM_PROVIDER(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GB_TYPE_COMMAND_VIM_PROVIDER, GbCommandVimProvider))
+#define GB_COMMAND_VIM_PROVIDER_CONST(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), 
GB_TYPE_COMMAND_VIM_PROVIDER, GbCommandVimProvider const))
+#define GB_COMMAND_VIM_PROVIDER_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  
GB_TYPE_COMMAND_VIM_PROVIDER, GbCommandVimProviderClass))
+#define GB_IS_COMMAND_VIM_PROVIDER(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), 
GB_TYPE_COMMAND_VIM_PROVIDER))
+#define GB_IS_COMMAND_VIM_PROVIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  
GB_TYPE_COMMAND_VIM_PROVIDER))
+#define GB_COMMAND_VIM_PROVIDER_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  
GB_TYPE_COMMAND_VIM_PROVIDER, GbCommandVimProviderClass))
+
+typedef struct _GbCommandVimProvider        GbCommandVimProvider;
+typedef struct _GbCommandVimProviderClass   GbCommandVimProviderClass;
+typedef struct _GbCommandVimProviderPrivate GbCommandVimProviderPrivate;
+
+struct _GbCommandVimProvider
+{
+  GbCommandProvider parent;
+
+  /*< private >*/
+  GbCommandVimProviderPrivate *priv;
+};
+
+struct _GbCommandVimProviderClass
+{
+  GbCommandProviderClass parent;
+};
+
+GType              gb_command_vim_provider_get_type (void) G_GNUC_CONST;
+GbCommandProvider *gb_command_vim_provider_new      (GbWorkbench *workbench);
+
+G_END_DECLS
+
+#endif /* GB_COMMAND_VIM_PROVIDER_H */
diff --git a/src/commands/gb-command-vim.c b/src/commands/gb-command-vim.c
new file mode 100644
index 0000000..671a8a8
--- /dev/null
+++ b/src/commands/gb-command-vim.c
@@ -0,0 +1,63 @@
+/* gb-command-vim.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gb-command-vim.h"
+#include "gb-editor-tab-private.h"
+
+static void
+gb_command_vim_activate (GSimpleAction *action,
+                         GVariant      *parameter,
+                         gpointer       user_data)
+{
+  GbEditorTab *tab;
+
+  g_return_if_fail (G_IS_SIMPLE_ACTION (action));
+  g_return_if_fail (parameter);
+
+  tab = g_object_get_data (G_OBJECT (action), "GB_EDITOR_TAB");
+
+  if (!GB_IS_EDITOR_TAB (tab))
+    {
+      g_warning ("Failed to retrieve editor tab!");
+      return;
+    }
+
+  if (g_variant_is_of_type (parameter, G_VARIANT_TYPE_STRING))
+    {
+      const gchar *command_text;
+
+      command_text = g_variant_get_string (parameter, NULL);
+      gb_editor_vim_execute_command (tab->priv->vim, command_text);
+    }
+}
+
+GAction *
+gb_command_vim_new (GbEditorTab *tab)
+{
+  GSimpleAction *action;
+
+  g_return_val_if_fail (GB_IS_EDITOR_TAB (tab), NULL);
+
+  action = g_simple_action_new ("vim-command", G_VARIANT_TYPE_STRING);
+  g_object_set_data_full (G_OBJECT (action), "GB_EDITOR_TAB",
+                          g_object_ref (tab), g_object_unref);
+  g_signal_connect (action, "activate", G_CALLBACK (gb_command_vim_activate),
+                    NULL);
+
+  return action;
+}
diff --git a/src/commands/gb-command-vim.h b/src/commands/gb-command-vim.h
new file mode 100644
index 0000000..c2c7ac6
--- /dev/null
+++ b/src/commands/gb-command-vim.h
@@ -0,0 +1,32 @@
+/* gb-command-vim.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GB_COMMAND_VIM_H
+#define GB_COMMAND_VIM_H
+
+#include <gio/gio.h>
+
+#include "gb-editor-tab.h"
+
+G_BEGIN_DECLS
+
+GAction *gb_command_vim_new (GbEditorTab *tab);
+
+G_END_DECLS
+
+#endif /* GB_COMMAND_VIM_H */
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 99dc855..1b10dd1 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -15,6 +15,10 @@ libgnome_builder_la_SOURCES = \
        src/commands/gb-command-manager.h \
        src/commands/gb-command-provider.c \
        src/commands/gb-command-provider.h \
+       src/commands/gb-command-vim-provider.c \
+       src/commands/gb-command-vim-provider.h \
+       src/commands/gb-command-vim.c \
+       src/commands/gb-command-vim.h \
        src/devhelp/gb-devhelp-navigation-item.c \
        src/devhelp/gb-devhelp-navigation-item.h \
        src/devhelp/gb-devhelp-tab.c \


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