[gnome-builder] source-location: add ide_source_location_new_from_variant()



commit 751b9ec3732b8aa9ef08dc10e7936afde6edd989
Author: Christian Hergert <chergert redhat com>
Date:   Tue Apr 24 16:19:07 2018 -0700

    source-location: add ide_source_location_new_from_variant()
    
    This is helpful to reconstruct a source location using the serialized form.

 src/libide/diagnostics/ide-source-location.c | 60 ++++++++++++++++++++++++++++
 src/libide/diagnostics/ide-source-location.h | 42 +++++++++----------
 2 files changed, 82 insertions(+), 20 deletions(-)
---
diff --git a/src/libide/diagnostics/ide-source-location.c b/src/libide/diagnostics/ide-source-location.c
index dfaf4ab56..e31dc5ea7 100644
--- a/src/libide/diagnostics/ide-source-location.c
+++ b/src/libide/diagnostics/ide-source-location.c
@@ -171,6 +171,66 @@ ide_source_location_new (IdeFile *file,
   return ret;
 }
 
+/**
+ * ide_source_location_new_from_variant:
+ * @self: a #IdeSourceLocation
+ *
+ * Creates a new #IdeSourceLocation using the serialized form from a
+ * previously serialized #GVariant.
+ *
+ * See also: ide_source_location_to_variant()
+ *
+ * Returns: (transfer full) (nullable): a #GVariant if succesful;
+ *   otherwise %NULL.
+ *
+ * Since: 3.30
+ */
+IdeSourceLocation *
+ide_source_location_new_from_variant (GVariant *variant)
+{
+  g_autoptr(GVariant) unboxed = NULL;
+  g_autoptr(IdeFile) ifile = NULL;
+  g_autoptr(GFile) file = NULL;
+  IdeSourceLocation *self;
+  GVariantDict dict;
+  const gchar *uri;
+  guint32 line;
+  guint32 line_offset;
+  guint32 offset;
+
+  if (variant == NULL)
+    return NULL;
+
+  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_VARIANT))
+    variant = unboxed = g_variant_get_variant (variant);
+
+  g_variant_dict_init (&dict, variant);
+
+  if (!g_variant_dict_lookup (&dict, "uri", "&s", &uri))
+    {
+      g_variant_dict_clear (&dict);
+      return NULL;
+    }
+
+  if (!g_variant_dict_lookup (&dict, "line", "u", &line))
+    line = 0;
+
+  if (!g_variant_dict_lookup (&dict, "line-offset", "u", &line_offset))
+    line_offset = 0;
+
+  if (!g_variant_dict_lookup (&dict, "offset", "u", &offset))
+    offset = 0;
+
+  file = g_file_new_for_uri (uri);
+  ifile = ide_file_new (NULL, file);
+
+  self = ide_source_location_new (ifile, line, line_offset, offset);
+
+  g_variant_dict_clear (&dict);
+
+  return self;
+}
+
 /**
  * ide_source_location_get_uri:
  * @self: (in): an #IdeSourceLocation.
diff --git a/src/libide/diagnostics/ide-source-location.h b/src/libide/diagnostics/ide-source-location.h
index bdb07c408..909db5cc5 100644
--- a/src/libide/diagnostics/ide-source-location.h
+++ b/src/libide/diagnostics/ide-source-location.h
@@ -29,38 +29,40 @@ G_BEGIN_DECLS
 #define IDE_TYPE_SOURCE_LOCATION (ide_source_location_get_type())
 
 IDE_AVAILABLE_IN_ALL
-GType              ide_source_location_get_type        (void);
+GType              ide_source_location_get_type         (void);
 IDE_AVAILABLE_IN_ALL
-IdeSourceLocation *ide_source_location_ref             (IdeSourceLocation       *self);
+IdeSourceLocation *ide_source_location_ref              (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-void               ide_source_location_unref           (IdeSourceLocation       *self);
+void               ide_source_location_unref            (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-IdeSourceLocation *ide_source_location_new             (IdeFile                 *file,
-                                                        guint                    line,
-                                                        guint                    line_offset,
-                                                        guint                    offset);
+IdeSourceLocation *ide_source_location_new              (IdeFile                 *file,
+                                                         guint                    line,
+                                                         guint                    line_offset,
+                                                         guint                    offset);
+IDE_AVAILABLE_IN_3_30
+IdeSourceLocation *ide_source_location_new_from_variant (GVariant                *variant);
 IDE_AVAILABLE_IN_ALL
-IdeSourceLocation *ide_source_location_new_for_path    (IdeContext              *context,
-                                                        const gchar             *path,
-                                                        guint                    line,
-                                                        guint                    line_offset);
+IdeSourceLocation *ide_source_location_new_for_path     (IdeContext              *context,
+                                                         const gchar             *path,
+                                                         guint                    line,
+                                                         guint                    line_offset);
 IDE_AVAILABLE_IN_ALL
-guint              ide_source_location_get_line        (IdeSourceLocation       *self);
+guint              ide_source_location_get_line         (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-guint              ide_source_location_get_line_offset (IdeSourceLocation       *self);
+guint              ide_source_location_get_line_offset  (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-guint              ide_source_location_get_offset      (IdeSourceLocation       *self);
+guint              ide_source_location_get_offset       (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-IdeFile           *ide_source_location_get_file        (IdeSourceLocation       *self);
+IdeFile           *ide_source_location_get_file         (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-IdeUri            *ide_source_location_get_uri         (IdeSourceLocation       *self);
+IdeUri            *ide_source_location_get_uri          (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_ALL
-gint               ide_source_location_compare         (const IdeSourceLocation *a,
-                                                        const IdeSourceLocation *b);
+gint               ide_source_location_compare          (const IdeSourceLocation *a,
+                                                         const IdeSourceLocation *b);
 IDE_AVAILABLE_IN_ALL
-guint              ide_source_location_hash            (IdeSourceLocation       *self);
+guint              ide_source_location_hash             (IdeSourceLocation       *self);
 IDE_AVAILABLE_IN_3_30
-GVariant          *ide_source_location_to_variant      (const IdeSourceLocation *self);
+GVariant          *ide_source_location_to_variant       (const IdeSourceLocation *self);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC (IdeSourceLocation, ide_source_location_unref)
 


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