[gtksourceview/wip/search] search: -1 for search-occurrences-count if not yet known



commit 87900487812085245acc0aec8bdb0f09462b55c3
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jul 13 17:43:48 2013 +0200

    search: -1 for search-occurrences-count if not yet known
    
    It is useful for display purposes. -1 is already used for
    get_search_occurrence_position().

 gtksourceview/gtksourcebuffer.c |   27 +++++++++-------
 gtksourceview/gtksourcebuffer.h |    2 +-
 gtksourceview/gtksourcesearch.c |   13 ++++++--
 gtksourceview/gtksourcesearch.h |    2 +-
 tests/test-search-ui.c          |   12 +++++--
 tests/test-search.c             |   62 +++++++++++++++++++-------------------
 6 files changed, 66 insertions(+), 52 deletions(-)
---
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 9031614..430cf71 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -420,19 +420,20 @@ gtk_source_buffer_class_init (GtkSourceBufferClass *klass)
         * GtkSourceBuffer:search-occurrences-count:
         *
         * The total number of search occurrences. If the search is disabled,
-        * the value is 0.
+        * the value is 0. If the buffer is not already fully scanned, the value
+        * is -1.
         *
         * Since: 3.10
         */
        g_object_class_install_property (object_class,
                                         PROP_SEARCH_OCCURRENCES_COUNT,
-                                        g_param_spec_uint ("search-occurrences-count",
-                                                           _("Search occurrences count"),
-                                                           _("Total number of search occurrences"),
-                                                           0,
-                                                           G_MAXUINT,
-                                                           0,
-                                                           G_PARAM_READABLE));
+                                        g_param_spec_int ("search-occurrences-count",
+                                                          _("Search occurrences count"),
+                                                          _("Total number of search occurrences"),
+                                                          -1,
+                                                          G_MAXINT,
+                                                          0,
+                                                          G_PARAM_READABLE));
 
        /**
         * GtkSourceBuffer:case-sensitive-search:
@@ -800,7 +801,7 @@ gtk_source_buffer_get_property (GObject    *object,
                        break;
 
                case PROP_SEARCH_OCCURRENCES_COUNT:
-                       g_value_set_uint (value, _gtk_source_search_get_occurrences_count 
(source_buffer->priv->search));
+                       g_value_set_int (value, _gtk_source_search_get_occurrences_count 
(source_buffer->priv->search));
                        break;
 
                case PROP_CASE_SENSITIVE_SEARCH:
@@ -2924,12 +2925,14 @@ gtk_source_buffer_get_highlight_search (GtkSourceBuffer *buffer)
  * gtk_source_buffer_get_search_occurrences_count:
  * @buffer: a #GtkSourceBuffer.
  *
- * Gets the total number of search occurrences.
+ * Gets the total number of search occurrences. If the buffer is not already
+ * fully scanned, the total number of occurrences is unknown, and -1 is
+ * returned.
  *
- * Returns: the total number of search occurrences.
+ * Returns: the total number of search occurrences, or -1 if unknown.
  * Since: 3.10
  */
-guint
+gint
 gtk_source_buffer_get_search_occurrences_count (GtkSourceBuffer *buffer)
 {
        g_return_val_if_fail (GTK_SOURCE_IS_BUFFER (buffer), 0);
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index 632a80d..c95b1d2 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -205,7 +205,7 @@ void                         gtk_source_buffer_set_highlight_search                 
(GtkSourceBuffer        *buffer,
 
 gboolean                gtk_source_buffer_get_highlight_search                 (GtkSourceBuffer        
*buffer);
 
-guint                   gtk_source_buffer_get_search_occurrences_count         (GtkSourceBuffer        
*buffer);
+gint                    gtk_source_buffer_get_search_occurrences_count         (GtkSourceBuffer        
*buffer);
 
 gint                    gtk_source_buffer_get_search_occurrence_position       (GtkSourceBuffer        
*buffer,
                                                                                 const GtkTextIter      
*match_start,
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index d087c63..22d93ba 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -135,7 +135,7 @@ struct _GtkSourceSearchPrivate
 
        gulong idle_scan_id;
 
-       guint occurrences_count;
+       gint occurrences_count;
 
        GtkTextTag *found_tag;
 
@@ -1786,12 +1786,19 @@ _gtk_source_search_get_highlight (GtkSourceSearch *search)
        return search->priv->highlight;
 }
 
-guint
+gint
 _gtk_source_search_get_occurrences_count (GtkSourceSearch *search)
 {
        g_return_val_if_fail (GTK_SOURCE_IS_SEARCH (search), 0);
 
-       return search->priv->occurrences_count;
+       if (is_text_region_empty (search->priv->scan_region))
+       {
+               return search->priv->occurrences_count;
+       }
+       else
+       {
+               return -1;
+       }
 }
 
 gint
diff --git a/gtksourceview/gtksourcesearch.h b/gtksourceview/gtksourcesearch.h
index f745a4f..d8b10d0 100644
--- a/gtksourceview/gtksourcesearch.h
+++ b/gtksourceview/gtksourcesearch.h
@@ -93,7 +93,7 @@ G_GNUC_INTERNAL
 gboolean               _gtk_source_search_get_highlight                (GtkSourceSearch        *search);
 
 G_GNUC_INTERNAL
-guint                  _gtk_source_search_get_occurrences_count        (GtkSourceSearch        *search);
+gint                   _gtk_source_search_get_occurrences_count        (GtkSourceSearch        *search);
 
 G_GNUC_INTERNAL
 gint                   _gtk_source_search_get_occurrence_position      (GtkSourceSearch        *search,
diff --git a/tests/test-search-ui.c b/tests/test-search-ui.c
index c953845..86f3b66 100644
--- a/tests/test-search-ui.c
+++ b/tests/test-search-ui.c
@@ -94,7 +94,7 @@ open_file (TestSearchUI *search,
 static void
 update_label (TestSearchUI *search)
 {
-       guint occurrences_count;
+       gint occurrences_count;
        GtkTextIter select_start;
        GtkTextIter select_end;
        gint occurrence_pos;
@@ -110,14 +110,18 @@ update_label (TestSearchUI *search)
                                                                           &select_start,
                                                                           &select_end);
 
-       if (occurrence_pos > 0)
+       if (occurrences_count == -1)
        {
-               text = g_strdup_printf ("%d of %u", occurrence_pos, occurrences_count);
+               text = g_strdup ("");
        }
-       else
+       else if (occurrence_pos == -1)
        {
                text = g_strdup_printf ("%u occurrences", occurrences_count);
        }
+       else
+       {
+               text = g_strdup_printf ("%d of %u", occurrence_pos, occurrences_count);
+       }
 
        gtk_label_set_text (search->priv->label_occurrences, text);
        g_free (text);
diff --git a/tests/test-search.c b/tests/test-search.c
index 3e725e5..1ef5fa8 100644
--- a/tests/test-search.c
+++ b/tests/test-search.c
@@ -38,33 +38,33 @@ test_occurrences_count_simple (void)
 {
        GtkSourceBuffer *buffer = gtk_source_buffer_new (NULL);
        GtkTextIter iter;
-       guint occurrences_count;
+       gint occurrences_count;
 
        gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);
        gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &iter, "Some foo\nSome bar\n", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
-       g_assert_cmpuint (occurrences_count, ==, 0);
+       g_assert_cmpint (occurrences_count, ==, 0);
 
        gtk_source_buffer_set_search_text (buffer, "world");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
-       g_assert_cmpuint (occurrences_count, ==, 0);
+       g_assert_cmpint (occurrences_count, ==, 0);
 
        gtk_source_buffer_set_search_text (buffer, "Some");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        gtk_source_buffer_set_search_text (buffer, "foo");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        gtk_source_buffer_set_search_text (buffer, "world");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (buffer);
-       g_assert_cmpuint (occurrences_count, ==, 0);
+       g_assert_cmpint (occurrences_count, ==, 0);
 
        g_object_unref (buffer);
 }
@@ -75,7 +75,7 @@ test_occurrences_count_with_insert (void)
        GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
        GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
        GtkTextIter iter;
-       guint occurrences_count;
+       gint occurrences_count;
 
        /* Contents: "foobar" */
        gtk_text_buffer_get_start_iter (text_buffer, &iter);
@@ -84,56 +84,56 @@ test_occurrences_count_with_insert (void)
        gtk_source_buffer_set_search_text (source_buffer, "foo");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        /* Contents: "foobar " */
        gtk_text_buffer_get_end_iter (text_buffer, &iter);
        gtk_text_buffer_insert (text_buffer, &iter, " ", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        /* Contents: "foobar foobeer" */
        gtk_text_buffer_get_end_iter (text_buffer, &iter);
        gtk_text_buffer_insert (text_buffer, &iter, "foobeer", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        /* Contents: "foo bar foobeer" */
        gtk_text_buffer_get_iter_at_offset (text_buffer, &iter, 3);
        gtk_text_buffer_insert (text_buffer, &iter, " ", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        /* Contents: "foto bar foobeer" */
        gtk_text_buffer_get_iter_at_offset (text_buffer, &iter, 2);
        gtk_text_buffer_insert (text_buffer, &iter, "t", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        /* Contents: "footo bar foobeer" */
        gtk_text_buffer_get_iter_at_offset (text_buffer, &iter, 2);
        gtk_text_buffer_insert (text_buffer, &iter, "o", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        /* Contents: "foofooto bar foobeer" */
        gtk_text_buffer_get_start_iter (text_buffer, &iter);
        gtk_text_buffer_insert (text_buffer, &iter, "foo", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 3);
+       g_assert_cmpint (occurrences_count, ==, 3);
 
        /* Contents: "fooTfooto bar foobeer" */
        gtk_text_buffer_get_iter_at_offset (text_buffer, &iter, 3);
        gtk_text_buffer_insert (text_buffer, &iter, "T", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 3);
+       g_assert_cmpint (occurrences_count, ==, 3);
 
        g_object_unref (source_buffer);
 }
@@ -145,7 +145,7 @@ test_occurrences_count_with_delete (void)
        GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
        GtkTextIter start;
        GtkTextIter end;
-       guint occurrences_count;
+       gint occurrences_count;
 
        gtk_source_buffer_set_search_text (source_buffer, "foo");
 
@@ -153,52 +153,52 @@ test_occurrences_count_with_delete (void)
        gtk_text_buffer_set_text (text_buffer, "foo", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        gtk_text_buffer_get_bounds (text_buffer, &start, &end);
        gtk_text_buffer_delete (text_buffer, &start, &end);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 0);
+       g_assert_cmpint (occurrences_count, ==, 0);
 
        /* Contents: "foo" -> "oo" */
        gtk_text_buffer_set_text (text_buffer, "foo", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        gtk_text_buffer_get_start_iter (text_buffer, &start);
        gtk_text_buffer_get_iter_at_offset (text_buffer, &end, 1);
        gtk_text_buffer_delete (text_buffer, &start, &end);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 0);
+       g_assert_cmpint (occurrences_count, ==, 0);
 
        /* Contents: "foobar foobeer" -> "foobar" */
        gtk_text_buffer_set_text (text_buffer, "foobar foobeer", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        gtk_text_buffer_get_iter_at_offset (text_buffer, &start, 6);
        gtk_text_buffer_get_end_iter (text_buffer, &end);
        gtk_text_buffer_delete (text_buffer, &start, &end);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        /* Contents: "foo[foo]foo" -> "foofoo" */
        gtk_text_buffer_set_text (text_buffer, "foofoofoo", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 3);
+       g_assert_cmpint (occurrences_count, ==, 3);
 
        gtk_text_buffer_get_iter_at_offset (text_buffer, &start, 3);
        gtk_text_buffer_get_iter_at_offset (text_buffer, &end, 6);
        gtk_text_buffer_delete (text_buffer, &start, &end);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        /* Contents: "fo[of]oo" -> "fooo" */
        gtk_text_buffer_get_iter_at_offset (text_buffer, &start, 2);
@@ -206,20 +206,20 @@ test_occurrences_count_with_delete (void)
        gtk_text_buffer_delete (text_buffer, &start, &end);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        /* Contents: "foto" -> "foo" */
        gtk_text_buffer_set_text (text_buffer, "foto", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 0);
+       g_assert_cmpint (occurrences_count, ==, 0);
 
        gtk_text_buffer_get_iter_at_offset (text_buffer, &start, 2);
        gtk_text_buffer_get_iter_at_offset (text_buffer, &end, 3);
        gtk_text_buffer_delete (text_buffer, &start, &end);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        g_object_unref (source_buffer);
 }
@@ -229,24 +229,24 @@ test_occurrences_count_multiple_lines (void)
 {
        GtkSourceBuffer *source_buffer = gtk_source_buffer_new (NULL);
        GtkTextBuffer *text_buffer = GTK_TEXT_BUFFER (source_buffer);
-       guint occurrences_count;
+       gint occurrences_count;
 
        gtk_source_buffer_set_search_text (source_buffer, "world\nhello");
 
        gtk_text_buffer_set_text (text_buffer, "hello world\nhello world\nhello world\n", -1);
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 2);
+       g_assert_cmpint (occurrences_count, ==, 2);
 
        gtk_source_buffer_set_search_text (source_buffer, "world\n");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 3);
+       g_assert_cmpint (occurrences_count, ==, 3);
 
        gtk_source_buffer_set_search_text (source_buffer, "\nhello world\n");
        flush_queue ();
        occurrences_count = gtk_source_buffer_get_search_occurrences_count (source_buffer);
-       g_assert_cmpuint (occurrences_count, ==, 1);
+       g_assert_cmpint (occurrences_count, ==, 1);
 
        g_object_unref (source_buffer);
 }


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