[gnome-builder] snippets: get full name and email from IdeVcsConfig



commit febc8e74857bc3dad6de10f6aee8f40463804a0b
Author: Akshaya Kakkilaya <akshaya kakkilaya gmail com>
Date:   Sun Jun 5 22:01:22 2016 +0530

    snippets: get full name and email from IdeVcsConfig
    
    https://bugzilla.gnome.org/show_bug.cgi?id=767272

 libide/ide-source-snippet-context.c |   41 ++++++++--------------------
 libide/ide-source-snippet-context.h |   37 ++++++++++++++------------
 libide/ide-source-view.c            |   49 ++++++++++++++++++++++++++++++----
 libide/ide-vcs.c                    |   23 ++++++++++++++++
 libide/ide-vcs.h                    |    3 ++
 plugins/git/ide-git-vcs-config.h    |    2 +
 plugins/git/ide-git-vcs.c           |   10 +++++++
 7 files changed, 113 insertions(+), 52 deletions(-)
---
diff --git a/libide/ide-source-snippet-context.c b/libide/ide-source-snippet-context.c
index 09aeda4..9dee2f9 100644
--- a/libide/ide-source-snippet-context.c
+++ b/libide/ide-source-snippet-context.c
@@ -103,6 +103,17 @@ ide_source_snippet_context_add_variable (IdeSourceSnippetContext *context,
   g_hash_table_replace (context->variables, g_strdup (key), g_strdup (value));
 }
 
+void
+ide_source_snippet_context_add_shared_variable (IdeSourceSnippetContext *context,
+                                                const gchar             *key,
+                                                const gchar             *value)
+{
+  g_return_if_fail (IDE_IS_SOURCE_SNIPPET_CONTEXT (context));
+  g_return_if_fail (key);
+
+  g_hash_table_replace (context->shared, g_strdup (key), g_strdup (value));
+}
+
 const gchar *
 ide_source_snippet_context_get_variable (IdeSourceSnippetContext *context,
                                         const gchar            *key)
@@ -601,32 +612,6 @@ ide_source_snippet_context_emit_changed (IdeSourceSnippetContext *context)
   g_signal_emit (context, signals[CHANGED], 0);
 }
 
-static gchar *
-run_command (const gchar *command)
-{
-  GError *error = NULL;
-  gchar *output = NULL;
-  gchar **argv = NULL;
-  gint argc = 0;
-
-  if (!g_shell_parse_argv (command, &argc, &argv, NULL))
-    return NULL;
-
-  /*
-   * TODO: Run in project directory.
-   */
-
-  if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &output, NULL, NULL, &error))
-    {
-      g_printerr ("%s\n", error->message);
-      g_error_free (error);
-    }
-
-  g_strfreev (argv);
-
-  return g_strstrip (output);
-}
-
 static void
 ide_source_snippet_context_finalize (GObject *object)
 {
@@ -702,9 +687,7 @@ ide_source_snippet_context_init (IdeSourceSnippetContext *context)
   g_free (str);
   g_date_time_unref (dt);
 
-  str = run_command ("git config user.email");
-  ADD_VARIABLE ("email", str);
-  g_free (str);
+  ADD_VARIABLE ("email", "unknown domain org");
 
 #undef ADD_VARIABLE
 }
diff --git a/libide/ide-source-snippet-context.h b/libide/ide-source-snippet-context.h
index fe9752f..ad2b617 100644
--- a/libide/ide-source-snippet-context.h
+++ b/libide/ide-source-snippet-context.h
@@ -28,23 +28,26 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (IdeSourceSnippetContext, ide_source_snippet_context,
                       IDE, SOURCE_SNIPPET_CONTEXT, GObject)
 
-IdeSourceSnippetContext *ide_source_snippet_context_new             (void);
-void                     ide_source_snippet_context_emit_changed    (IdeSourceSnippetContext *context);
-void                     ide_source_snippet_context_clear_variables (IdeSourceSnippetContext *context);
-void                     ide_source_snippet_context_add_variable    (IdeSourceSnippetContext *context,
-                                                                     const gchar             *key,
-                                                                     const gchar             *value);
-const gchar             *ide_source_snippet_context_get_variable    (IdeSourceSnippetContext *context,
-                                                                     const gchar             *key);
-gchar                   *ide_source_snippet_context_expand          (IdeSourceSnippetContext *context,
-                                                                     const gchar             *input);
-void                     ide_source_snippet_context_set_tab_width   (IdeSourceSnippetContext *context,
-                                                                     gint                     tab_size);
-void                     ide_source_snippet_context_set_use_spaces  (IdeSourceSnippetContext *context,
-                                                                     gboolean                 use_spaces);
-void                     ide_source_snippet_context_set_line_prefix (IdeSourceSnippetContext *context,
-                                                                     const gchar             *line_prefix);
-void                     ide_source_snippet_context_dump            (IdeSourceSnippetContext *context);
+IdeSourceSnippetContext *ide_source_snippet_context_new                 (void);
+void                     ide_source_snippet_context_emit_changed        (IdeSourceSnippetContext *context);
+void                     ide_source_snippet_context_clear_variables     (IdeSourceSnippetContext *context);
+void                     ide_source_snippet_context_add_variable        (IdeSourceSnippetContext *context,
+                                                                         const gchar             *key,
+                                                                         const gchar             *value);
+void                     ide_source_snippet_context_add_shared_variable (IdeSourceSnippetContext *context,
+                                                                         const gchar             *key,
+                                                                         const gchar             *value);
+const gchar             *ide_source_snippet_context_get_variable        (IdeSourceSnippetContext *context,
+                                                                         const gchar             *key);
+gchar                   *ide_source_snippet_context_expand              (IdeSourceSnippetContext *context,
+                                                                         const gchar             *input);
+void                     ide_source_snippet_context_set_tab_width       (IdeSourceSnippetContext *context,
+                                                                         gint                     tab_size);
+void                     ide_source_snippet_context_set_use_spaces      (IdeSourceSnippetContext *context,
+                                                                         gboolean                 
use_spaces);
+void                     ide_source_snippet_context_set_line_prefix     (IdeSourceSnippetContext *context,
+                                                                         const gchar             
*line_prefix);
+void                     ide_source_snippet_context_dump                (IdeSourceSnippetContext *context);
 
 G_END_DECLS
 
diff --git a/libide/ide-source-view.c b/libide/ide-source-view.c
index 1f17b79..ecfaba6 100644
--- a/libide/ide-source-view.c
+++ b/libide/ide-source-view.c
@@ -67,6 +67,7 @@
 #include "ide-source-view-movements.h"
 #include "ide-symbol.h"
 #include "ide-text-util.h"
+#include "ide-vcs.h"
 #include "ide-workbench-private.h"
 
 #define INCLUDE_STATEMENTS "^#include[\\s]+[\\\"\\<][^\\s\\\"\\\'\\<\\>[:cntrl:]]+[\\\"\\>]"
@@ -4006,6 +4007,7 @@ ide_source_view_real_push_snippet (IdeSourceView           *self,
                                    const GtkTextIter       *location)
 {
   IdeSourceViewPrivate *priv = ide_source_view_get_instance_private (self);
+  IdeContext *ide_context;
   IdeSourceSnippetContext *context;
   IdeFile *file;
   GFile *gfile;
@@ -4016,14 +4018,49 @@ ide_source_view_real_push_snippet (IdeSourceView           *self,
 
   context = ide_source_snippet_get_context (snippet);
 
-  if ((priv->buffer != NULL) &&
-      (file = ide_buffer_get_file (priv->buffer)) &&
-      (gfile = ide_file_get_file (file)))
+  if (priv->buffer != NULL)
     {
-      g_autofree gchar *name = NULL;
+      if ((file = ide_buffer_get_file (priv->buffer)) &&
+          (gfile = ide_file_get_file (file)))
+        {
+          g_autofree gchar *name = NULL;
+
+          name = g_file_get_basename (gfile);
+          ide_source_snippet_context_add_variable (context, "filename", name);
+        }
+
+      if ((ide_context = ide_buffer_get_context (priv->buffer)))
+        {
+          IdeVcs *vcs;
+          IdeVcsConfig *vcs_config;
+
+          vcs = ide_context_get_vcs (ide_context);
+          if ((vcs_config = ide_vcs_get_config (vcs)))
+            {
+              GValue value = G_VALUE_INIT;
+
+              g_value_init (&value, G_TYPE_STRING);
 
-      name = g_file_get_basename (gfile);
-      ide_source_snippet_context_add_variable (context, "filename", name);
+              ide_vcs_config_get_config (vcs_config, IDE_VCS_CONFIG_FULL_NAME, &value);
+
+              if (!ide_str_empty0 (g_value_get_string (&value)))
+                {
+                  ide_source_snippet_context_add_shared_variable (context, "author", g_value_get_string 
(&value));
+                  ide_source_snippet_context_add_shared_variable (context, "fullname", g_value_get_string 
(&value));
+                  ide_source_snippet_context_add_shared_variable (context, "username", g_value_get_string 
(&value));
+                }
+
+              g_value_reset (&value);
+
+              ide_vcs_config_get_config (vcs_config, IDE_VCS_CONFIG_EMAIL, &value);
+
+              if (!ide_str_empty0 (g_value_get_string (&value)))
+                ide_source_snippet_context_add_shared_variable (context, "email", g_value_get_string 
(&value));
+
+              g_value_unset (&value);
+              g_object_unref (vcs_config);
+            }
+        }
     }
 }
 
diff --git a/libide/ide-vcs.c b/libide/ide-vcs.c
index d32b637..98f0450 100644
--- a/libide/ide-vcs.c
+++ b/libide/ide-vcs.c
@@ -182,3 +182,26 @@ ide_vcs_emit_changed (IdeVcs *self)
 
   g_signal_emit (self, signals [CHANGED], 0);
 }
+
+/**
+ * ide_vcs_get_config:
+ *
+ * Retrieves an #IdeVcsConfig for the #IdeVcs provided. If the #IdeVcs implementation does not
+ * support access to configuration, then %NULL is returned.
+ *
+ * Returns: (transfer full) (nullable): An #IdeVcsConfig or %NULL.
+ */
+IdeVcsConfig *
+ide_vcs_get_config (IdeVcs *self)
+{
+  IdeVcsConfig *ret = NULL;
+
+  g_return_val_if_fail (IDE_IS_VCS (self), NULL);
+
+  if (IDE_VCS_GET_IFACE (self)->get_config)
+    ret = IDE_VCS_GET_IFACE (self)->get_config (self);
+
+  g_return_val_if_fail (!ret || IDE_IS_VCS_CONFIG (ret), NULL);
+
+  return  ret;
+}
diff --git a/libide/ide-vcs.h b/libide/ide-vcs.h
index 76ae359..3b76850 100644
--- a/libide/ide-vcs.h
+++ b/libide/ide-vcs.h
@@ -22,6 +22,7 @@
 #include <gio/gio.h>
 
 #include "ide-object.h"
+#include "ide-vcs-config.h"
 
 G_BEGIN_DECLS
 
@@ -41,6 +42,7 @@ struct _IdeVcsInterface
                                                         GError    **error);
   gint                    (*get_priority)              (IdeVcs     *self);
   void                    (*changed)                   (IdeVcs     *self);
+  IdeVcsConfig           *(*get_config)                (IdeVcs     *self);
 };
 
 IdeBufferChangeMonitor *ide_vcs_get_buffer_change_monitor (IdeVcs               *self,
@@ -58,6 +60,7 @@ gboolean                ide_vcs_is_ignored                (IdeVcs
                                                            GError              **error);
 gint                    ide_vcs_get_priority              (IdeVcs               *self);
 void                    ide_vcs_emit_changed              (IdeVcs               *self);
+IdeVcsConfig           *ide_vcs_get_config                (IdeVcs               *self);
 
 G_END_DECLS
 
diff --git a/plugins/git/ide-git-vcs-config.h b/plugins/git/ide-git-vcs-config.h
index 9181a50..2fc69e8 100644
--- a/plugins/git/ide-git-vcs-config.h
+++ b/plugins/git/ide-git-vcs-config.h
@@ -27,6 +27,8 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (IdeGitVcsConfig, ide_git_vcs_config, IDE, GIT_VCS_CONFIG, GObject)
 
+IdeGitVcsConfig *ide_git_vcs_config_new (void);
+
 G_END_DECLS
 
 #endif /* IDE_GIT_VCS_CONFIG_H */
diff --git a/plugins/git/ide-git-vcs.c b/plugins/git/ide-git-vcs.c
index 95eca51..8df40a7 100644
--- a/plugins/git/ide-git-vcs.c
+++ b/plugins/git/ide-git-vcs.c
@@ -27,6 +27,7 @@
 #include "ide-debug.h"
 #include "ide-git-buffer-change-monitor.h"
 #include "ide-git-vcs.h"
+#include "ide-git-vcs-config.h"
 #include "ide-project.h"
 #include "ide-project-file.h"
 #include "ide-project-files.h"
@@ -103,6 +104,14 @@ ide_git_vcs_get_working_directory (IdeVcs *vcs)
   return self->working_directory;
 }
 
+static IdeVcsConfig *
+ide_git_vcs_get_config (IdeVcs *vcs)
+{
+  g_return_val_if_fail (IDE_IS_GIT_VCS (vcs), NULL);
+
+  return (IdeVcsConfig *)ide_git_vcs_config_new ();
+}
+
 static IdeBufferChangeMonitor *
 ide_git_vcs_get_buffer_change_monitor (IdeVcs    *vcs,
                                        IdeBuffer *buffer)
@@ -371,6 +380,7 @@ ide_git_vcs_init_iface (IdeVcsInterface *iface)
   iface->get_working_directory = ide_git_vcs_get_working_directory;
   iface->get_buffer_change_monitor = ide_git_vcs_get_buffer_change_monitor;
   iface->is_ignored = ide_git_vcs_is_ignored;
+  iface->get_config = ide_git_vcs_get_config;
 }
 
 static void


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