[gnome-builder] libide-code: IdeLocation:title and GEqualFunc



commit 681f2cd46c07018833eff530272b508462e2df99
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jul 11 16:22:41 2022 -0700

    libide-code: IdeLocation:title and GEqualFunc

 src/libide/code/ide-location.c | 71 ++++++++++++++++++++++++++++++++++++++++++
 src/libide/code/ide-location.h |  5 +++
 2 files changed, 76 insertions(+)
---
diff --git a/src/libide/code/ide-location.c b/src/libide/code/ide-location.c
index 27c903af0..8c4cb5a75 100644
--- a/src/libide/code/ide-location.c
+++ b/src/libide/code/ide-location.c
@@ -38,6 +38,7 @@ enum {
   PROP_LINE,
   PROP_LINE_OFFSET,
   PROP_OFFSET,
+  PROP_TITLE,
   N_PROPS
 };
 
@@ -126,6 +127,10 @@ ide_location_get_property (GObject    *object,
       g_value_set_int (value, ide_location_get_offset (self));
       break;
 
+    case PROP_TITLE:
+      g_value_take_string (value, ide_location_dup_title (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -171,6 +176,12 @@ ide_location_class_init (IdeLocationClass *klass)
   object_class->get_property = ide_location_get_property;
   object_class->set_property = ide_location_set_property;
 
+  properties [PROP_TITLE] =
+    g_param_spec_string ("title",
+                         "Title",
+                         "The title of the location",
+                         NULL,
+                         (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
   properties [PROP_FILE] =
     g_param_spec_object ("file",
                          "File",
@@ -501,3 +512,63 @@ ide_location_hash (IdeLocation *self)
 
   return g_file_hash (priv->file) ^ g_int_hash (&priv->line) ^ g_int_hash (&priv->line_offset);
 }
+
+gboolean
+ide_location_equal (IdeLocation *a,
+                    IdeLocation *b)
+{
+  IdeLocationPrivate *a_priv = ide_location_get_instance_private (a);
+  IdeLocationPrivate *b_priv = ide_location_get_instance_private (b);
+
+  g_return_val_if_fail (!a || IDE_IS_LOCATION (a), FALSE);
+  g_return_val_if_fail (!b || IDE_IS_LOCATION (b), FALSE);
+
+  if (a == NULL || b == NULL)
+    return FALSE;
+
+  if (a_priv->file == NULL || b_priv->file == NULL)
+    return FALSE;
+
+  if (G_OBJECT_TYPE (a) != G_OBJECT_TYPE (b))
+    return FALSE;
+
+  if (!g_file_equal (a_priv->file, b_priv->file))
+    return FALSE;
+
+  return a_priv->line == b_priv->line &&
+         a_priv->line_offset == b_priv->line_offset &&
+         a_priv->offset == b_priv->offset;
+}
+
+/**
+ * ide_location_dup_title:
+ * @self: a #IdeLocation
+ *
+ * Gets a title string for the location, usually in the form of
+ *   shortname:line:column
+ *
+ * Returns: (transfer full) (nullable): A new string containing the
+ *   something suitable to be used as a title for diagnostics.
+ */
+char *
+ide_location_dup_title (IdeLocation *self)
+{
+  IdeLocationPrivate *priv = ide_location_get_instance_private (self);
+  g_autofree char *name = NULL;
+
+  g_return_val_if_fail (IDE_IS_LOCATION (self), NULL);
+
+  if (priv->file == NULL)
+    return NULL;
+
+  if (!(name = g_file_get_basename (priv->file)))
+    return NULL;
+
+  if (priv->line >= 0 && priv->line_offset >= 0)
+    return g_strdup_printf ("%s:%d:%d", name, priv->line, priv->line_offset);
+
+  if (priv->line >= 0)
+    return g_strdup_printf ("%s:%d", name, priv->line);
+
+  return g_steal_pointer (&name);
+}
diff --git a/src/libide/code/ide-location.h b/src/libide/code/ide-location.h
index 1820a8027..655cb2652 100644
--- a/src/libide/code/ide-location.h
+++ b/src/libide/code/ide-location.h
@@ -71,5 +71,10 @@ gboolean     ide_location_compare          (IdeLocation *a,
                                             IdeLocation *b);
 IDE_AVAILABLE_IN_3_32
 guint        ide_location_hash             (IdeLocation *self);
+IDE_AVAILABLE_IN_ALL
+gboolean     ide_location_equal            (IdeLocation *a,
+                                            IdeLocation *b);
+IDE_AVAILABLE_IN_ALL
+char        *ide_location_dup_title        (IdeLocation *self);
 
 G_END_DECLS


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