[gtksourceview/wip/public-region: 2/3] region: modify debug_print() to to_string()



commit 082c6f5e234e19e2c5ce4e73b36d87b029addefa
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Apr 3 17:17:07 2016 +0200

    region: modify debug_print() to to_string()
    
    And make to_string() public. It is useful when first using
    GtkSourceRegion, so make life easier for new developers.

 gtksourceview/gtksourceregion.c        |   56 ++++++++++++++++++++++++--------
 gtksourceview/gtksourceregion.h        |    4 +-
 gtksourceview/gtksourcesearchcontext.c |   20 +++++++++--
 testsuite/test-region.c                |   12 ++++++-
 4 files changed, 70 insertions(+), 22 deletions(-)
---
diff --git a/gtksourceview/gtksourceregion.c b/gtksourceview/gtksourceregion.c
index f2ae41e..dacfb8a 100644
--- a/gtksourceview/gtksourceregion.c
+++ b/gtksourceview/gtksourceregion.c
@@ -102,6 +102,18 @@ static GParamSpec *properties[LAST_PROP];
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceRegion, gtk_source_region, G_TYPE_OBJECT)
 
+#ifdef ENABLE_DEBUG
+static void
+print_region (GtkSourceRegion *region)
+{
+       gchar *str;
+
+       str = gtk_source_region_to_string (region);
+       g_print ("%s\n", str);
+       g_free (str);
+}
+#endif
+
 /* Find and return a subregion node which contains the given text
  * iter.  If left_side is TRUE, return the subregion which contains
  * the text iter or which is the leftmost; else return the rightmost
@@ -387,7 +399,7 @@ gtk_source_region_add (GtkSourceRegion   *region,
        end = *_end;
 
        DEBUG (g_print ("---\n"));
-       DEBUG (_gtk_source_region_debug_print (region));
+       DEBUG (print_region (region));
        DEBUG (g_message ("region_add (%d, %d)",
                          gtk_text_iter_get_offset (&start),
                          gtk_text_iter_get_offset (&end)));
@@ -472,7 +484,7 @@ gtk_source_region_add (GtkSourceRegion   *region,
 
        priv->timestamp++;
 
-       DEBUG (_gtk_source_region_debug_print (region));
+       DEBUG (print_region (region));
 }
 
 /**
@@ -518,7 +530,7 @@ gtk_source_region_subtract (GtkSourceRegion   *region,
        end = *_end;
 
        DEBUG (g_print ("---\n"));
-       DEBUG (_gtk_source_region_debug_print (region));
+       DEBUG (print_region (region));
        DEBUG (g_message ("region_substract (%d, %d)",
                          gtk_text_iter_get_offset (&start),
                          gtk_text_iter_get_offset (&end)));
@@ -641,12 +653,12 @@ gtk_source_region_subtract (GtkSourceRegion   *region,
 
        priv->timestamp++;
 
-       DEBUG (_gtk_source_region_debug_print (region));
+       DEBUG (print_region (region));
 
        /* Now get rid of empty subregions. */
        gtk_source_region_clear_zero_length_subregions (region);
 
-       DEBUG (_gtk_source_region_debug_print (region));
+       DEBUG (print_region (region));
 }
 
 /**
@@ -1079,22 +1091,36 @@ gtk_source_region_iter_get_subregion (GtkSourceRegionIter *iter,
        return TRUE;
 }
 
-void
-_gtk_source_region_debug_print (GtkSourceRegion *region)
+/**
+ * gtk_source_region_to_string:
+ * @region: a #GtkSourceRegion.
+ *
+ * Gets a string represention of @region, for debugging purposes.
+ *
+ * The returned string contains the character offsets of the subregions. It
+ * doesn't include a newline character.
+ *
+ * Returns: (transfer full) (nullable): a string represention of @region. Free
+ *   with g_free() when no longer needed.
+ * Since: 3.22
+ */
+gchar *
+gtk_source_region_to_string (GtkSourceRegion *region)
 {
        GtkSourceRegionPrivate *priv;
+       GString *string;
        GList *l;
 
-       g_return_if_fail (GTK_SOURCE_IS_REGION (region));
+       g_return_val_if_fail (GTK_SOURCE_IS_REGION (region), NULL);
 
        priv = gtk_source_region_get_instance_private (region);
 
        if (priv->buffer == NULL)
        {
-               return;
+               return NULL;
        }
 
-       g_print ("Subregions: ");
+       string = g_string_new ("Subregions:");
 
        for (l = priv->subregions; l != NULL; l = l->next)
        {
@@ -1105,9 +1131,11 @@ _gtk_source_region_debug_print (GtkSourceRegion *region)
                gtk_text_buffer_get_iter_at_mark (priv->buffer, &start, sr->start);
                gtk_text_buffer_get_iter_at_mark (priv->buffer, &end, sr->end);
 
-               g_print ("%d-%d ",
-                        gtk_text_iter_get_offset (&start),
-                        gtk_text_iter_get_offset (&end));
+               g_string_append_printf (string,
+                                       " %d-%d",
+                                       gtk_text_iter_get_offset (&start),
+                                       gtk_text_iter_get_offset (&end));
        }
-       g_print ("\n");
+
+       return g_string_free (string, FALSE);
 }
diff --git a/gtksourceview/gtksourceregion.h b/gtksourceview/gtksourceregion.h
index ea42ad9..de1b143 100644
--- a/gtksourceview/gtksourceregion.h
+++ b/gtksourceview/gtksourceregion.h
@@ -100,8 +100,8 @@ gboolean            gtk_source_region_iter_get_subregion    (GtkSourceRegionIter *iter,
                                                                 GtkTextIter         *start,
                                                                 GtkTextIter         *end);
 
-G_GNUC_INTERNAL
-void                   _gtk_source_region_debug_print          (GtkSourceRegion *region);
+GTK_SOURCE_AVAILABLE_IN_3_22
+gchar *                        gtk_source_region_to_string             (GtkSourceRegion *region);
 
 G_END_DECLS
 
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 0835397..5aa996c 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -370,6 +370,18 @@ G_DEFINE_TYPE_WITH_PRIVATE (GtkSourceSearchContext, gtk_source_search_context, G
 
 static void            install_idle_scan               (GtkSourceSearchContext *search);
 
+#ifdef ENABLE_DEBUG
+static void
+print_region (GtkSourceRegion *region)
+{
+       gchar *str;
+
+       str = gtk_source_region_to_string (region);
+       g_print ("%s\n", str);
+       g_free (str);
+}
+#endif
+
 static void
 sync_found_tag (GtkSourceSearchContext *search)
 {
@@ -1483,14 +1495,14 @@ scan_subregion (GtkSourceSearchContext *search,
        {
                DEBUG ({
                        g_print ("Region to scan, before:\n");
-                       _gtk_source_region_debug_print (search->priv->scan_region);
+                       print_region (search->priv->scan_region);
                });
 
                gtk_source_region_subtract (search->priv->scan_region, start, end);
 
                DEBUG ({
                        g_print ("Region to scan, after:\n");
-                       _gtk_source_region_debug_print (search->priv->scan_region);
+                       print_region (search->priv->scan_region);
                });
        }
 
@@ -2239,14 +2251,14 @@ add_subregion_to_scan (GtkSourceSearchContext *search,
 
        DEBUG ({
                g_print ("add_subregion_to_scan(): region to scan, before:\n");
-               _gtk_source_region_debug_print (search->priv->scan_region);
+               print_region (search->priv->scan_region);
        });
 
        gtk_source_region_add (search->priv->scan_region, &start, &end);
 
        DEBUG ({
                g_print ("add_subregion_to_scan(): region to scan, after:\n");
-               _gtk_source_region_debug_print (search->priv->scan_region);
+               print_region (search->priv->scan_region);
        });
 
        install_idle_scan (search);
diff --git a/testsuite/test-region.c b/testsuite/test-region.c
index a2c178a..8440959 100644
--- a/testsuite/test-region.c
+++ b/testsuite/test-region.c
@@ -32,6 +32,7 @@ test_region (void)
        GtkSourceRegionIter reg_iter;
        GtkTextIter iter1, iter2;
        guint i;
+       gchar *region_str = NULL;
 
 #define NUM_OPS 23
 
@@ -117,7 +118,10 @@ test_region (void)
                }
                g_print ("%s %d-%d\n", op_name, ops [i][1], ops [i][2]);
 
-               _gtk_source_region_debug_print (region);
+               region_str = gtk_source_region_to_string (region);
+               g_print ("%s\n", region_str);
+               g_free (region_str);
+               region_str = NULL;
        }
 
        for (i = 0; i < NUM_INTERSECTS; i++) {
@@ -127,7 +131,11 @@ test_region (void)
                g_print ("intersect %d-%d\n", inter [i][0], inter [i][1]);
                intersection = gtk_source_region_intersect (region, &iter1, &iter2);
                if (intersection) {
-                       _gtk_source_region_debug_print (intersection);
+                       region_str = gtk_source_region_to_string (region);
+                       g_print ("%s\n", region_str);
+                       g_free (region_str);
+                       region_str = NULL;
+
                        g_clear_object (&intersection);
                } else {
                        g_print ("no intersection\n");


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