[gnome-builder] highlight: add helper to serialize a highlight index



commit 2855b6e834e209a6228a6e731b36b485b5b96dc1
Author: Christian Hergert <chergert redhat com>
Date:   Sun Apr 22 00:43:12 2018 -0700

    highlight: add helper to serialize a highlight index
    
    This is useful if you need to pass data across the IPC boundary like we
    will do with the clang plugin.

 src/libide/highlighting/ide-highlight-index.c | 49 +++++++++++++++++++++++++++
 src/libide/highlighting/ide-highlight-index.h | 22 ++++++------
 2 files changed, 61 insertions(+), 10 deletions(-)
---
diff --git a/src/libide/highlighting/ide-highlight-index.c b/src/libide/highlighting/ide-highlight-index.c
index e188ae815..898941aa6 100644
--- a/src/libide/highlighting/ide-highlight-index.c
+++ b/src/libide/highlighting/ide-highlight-index.c
@@ -149,3 +149,52 @@ ide_highlight_index_dump (IdeHighlightIndex *self)
   g_debug ("IdeHighlightIndex (%p) contains %u items and consumes %s.",
            self, self->count, format);
 }
+
+/**
+ * ide_highlight_index_to_variant:
+ * @self: a #IdeHighlightIndex
+ *
+ * Creates a variant to represent the index. Useful to transport across IPC boundaries.
+ *
+ * Returns: (transfer full): a #GVariant
+ */
+GVariant *
+ide_highlight_index_to_variant (IdeHighlightIndex *self)
+{
+  g_autoptr(GHashTable) arrays = NULL;
+  GHashTableIter iter;
+  const gchar *k, *v;
+  GPtrArray *ar;
+  GVariantDict dict;
+
+  g_return_val_if_fail (self != NULL, NULL);
+
+  arrays = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, (GDestroyNotify)g_ptr_array_unref);
+
+  g_hash_table_iter_init (&iter, self->index);
+  while (g_hash_table_iter_next (&iter, (gpointer *)&k, (gpointer *)&v))
+    {
+      if G_UNLIKELY (!(ar = g_hash_table_lookup (arrays, v)))
+        {
+          ar = g_ptr_array_new ();
+          g_hash_table_insert (arrays, (gchar *)v, ar);
+        }
+
+      g_ptr_array_add (ar, (gchar *)k);
+    }
+
+  g_variant_dict_init (&dict, NULL);
+
+  g_hash_table_iter_init (&iter, arrays);
+  while (g_hash_table_iter_next (&iter, (gpointer *)&k, (gpointer *)&ar))
+    {
+      GVariant *keys;
+
+      g_ptr_array_add (ar, NULL);
+
+      keys = g_variant_new_strv ((const gchar * const *)ar->pdata, ar->len - 1);
+      g_variant_dict_insert_value (&dict, k, keys);
+    }
+
+  return g_variant_ref_sink (g_variant_dict_end (&dict));
+}
diff --git a/src/libide/highlighting/ide-highlight-index.h b/src/libide/highlighting/ide-highlight-index.h
index ff39c41d0..548cc6807 100644
--- a/src/libide/highlighting/ide-highlight-index.h
+++ b/src/libide/highlighting/ide-highlight-index.h
@@ -29,22 +29,24 @@ G_BEGIN_DECLS
 typedef struct _IdeHighlightIndex IdeHighlightIndex;
 
 IDE_AVAILABLE_IN_ALL
-GType              ide_highlight_index_get_type (void);
+GType              ide_highlight_index_get_type   (void);
 IDE_AVAILABLE_IN_ALL
-IdeHighlightIndex *ide_highlight_index_new      (void);
+IdeHighlightIndex *ide_highlight_index_new        (void);
 IDE_AVAILABLE_IN_ALL
-IdeHighlightIndex *ide_highlight_index_ref      (IdeHighlightIndex *self);
+IdeHighlightIndex *ide_highlight_index_ref        (IdeHighlightIndex *self);
 IDE_AVAILABLE_IN_ALL
-void               ide_highlight_index_unref    (IdeHighlightIndex *self);
+void               ide_highlight_index_unref      (IdeHighlightIndex *self);
 IDE_AVAILABLE_IN_ALL
-void               ide_highlight_index_insert   (IdeHighlightIndex *self,
-                                                 const gchar       *word,
-                                                 gpointer           tag);
+void               ide_highlight_index_insert     (IdeHighlightIndex *self,
+                                                   const gchar       *word,
+                                                   gpointer           tag);
 IDE_AVAILABLE_IN_ALL
-gpointer           ide_highlight_index_lookup   (IdeHighlightIndex *self,
-                                                 const gchar       *word);
+gpointer           ide_highlight_index_lookup     (IdeHighlightIndex *self,
+                                                   const gchar       *word);
 IDE_AVAILABLE_IN_ALL
-void               ide_highlight_index_dump     (IdeHighlightIndex *self);
+void               ide_highlight_index_dump       (IdeHighlightIndex *self);
+IDE_AVAILABLE_IN_3_30
+GVariant          *ide_highlight_index_to_variant (IdeHighlightIndex *self);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeHighlightIndex, ide_highlight_index_unref)
 


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