[gnome-builder/wip/chergert/clang] highlight-index: allow recreating index from variant



commit 4a927ef0ac8302b3a5d1fd4d23d084abcb4cbc2e
Author: Christian Hergert <chergert redhat com>
Date:   Wed Apr 25 02:40:20 2018 -0700

    highlight-index: allow recreating index from variant

 src/libide/highlighting/ide-highlight-index.c | 60 +++++++++++++++++++++++++++
 src/libide/highlighting/ide-highlight-index.h | 24 ++++++-----
 2 files changed, 73 insertions(+), 11 deletions(-)
---
diff --git a/src/libide/highlighting/ide-highlight-index.c b/src/libide/highlighting/ide-highlight-index.c
index 898941aa6..d7d8bfb7c 100644
--- a/src/libide/highlighting/ide-highlight-index.c
+++ b/src/libide/highlighting/ide-highlight-index.c
@@ -43,6 +43,7 @@ struct _IdeHighlightIndex
 
   GStringChunk  *strings;
   GHashTable    *index;
+  GVariant      *variant;
 };
 
 IdeHighlightIndex *
@@ -60,6 +61,65 @@ ide_highlight_index_new (void)
   return ret;
 }
 
+IdeHighlightIndex *
+ide_highlight_index_new_from_variant (GVariant *variant)
+{
+  IdeHighlightIndex *self;
+
+  self = ide_highlight_index_new ();
+
+  if (variant != NULL)
+    {
+      g_autoptr(GVariant) unboxed = NULL;
+
+      self->variant = g_variant_ref_sink (variant);
+
+      if (g_variant_is_of_type (variant, G_VARIANT_TYPE_VARIANT))
+        variant = unboxed = g_variant_get_variant (variant);
+
+      if (g_variant_is_of_type (variant, G_VARIANT_TYPE_VARDICT))
+        {
+          GVariantIter iter;
+          GVariant *value;
+          gchar *key;
+
+          g_variant_iter_init (&iter, variant);
+
+          while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
+            {
+              g_autoptr(GVariant) ar = g_variant_get_variant (value);
+
+              if (g_variant_is_of_type (ar, G_VARIANT_TYPE_STRING_ARRAY))
+                {
+                  g_autofree const gchar **strv = NULL;
+                  const gchar *tag;
+                  gsize len;
+
+                  tag = g_string_chunk_insert (self->strings, key);
+                  strv = g_variant_get_strv (ar, &len);
+
+                  for (gsize i = 0; i < len; i++)
+                    {
+                      const gchar *word = strv[i];
+
+                      if (g_hash_table_contains (self->index, word))
+                        continue;
+
+                      /* word is guaranteed to be alive and valid inside our
+                       * variant that we are storing. No need to add to the
+                       * string chunk too.
+                       */
+                      g_hash_table_insert (self->index, (gchar *)word, (gchar *)tag);
+                      self->count++;
+                    }
+                }
+            }
+        }
+    }
+
+  return self;
+}
+
 void
 ide_highlight_index_insert (IdeHighlightIndex *self,
                             const gchar       *word,
diff --git a/src/libide/highlighting/ide-highlight-index.h b/src/libide/highlighting/ide-highlight-index.h
index 548cc6807..51a95165a 100644
--- a/src/libide/highlighting/ide-highlight-index.h
+++ b/src/libide/highlighting/ide-highlight-index.h
@@ -29,24 +29,26 @@ 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_3_30
+IdeHighlightIndex *ide_highlight_index_new_from_variant (GVariant          *variant);
 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);
+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]