[gtksourceview/wip/search] search: fix occurrences_count on text insertion



commit 93bd5843112ec0f5ba4ee180c3f496b2674c465a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sun Jun 23 06:58:51 2013 +0200

    search: fix occurrences_count on text insertion

 gtksourceview/gtksourcesearch.c |  140 ++++++++++++++++++++++-----------------
 1 files changed, 79 insertions(+), 61 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearch.c b/gtksourceview/gtksourcesearch.c
index 8dc8134..0aa963d 100644
--- a/gtksourceview/gtksourcesearch.c
+++ b/gtksourceview/gtksourcesearch.c
@@ -97,10 +97,10 @@
  * - Rewrite the code to implement the simpler solution explained above :-)
  */
 
-#define ENABLE_DEBUG
 /*
-#undef ENABLE_DEBUG
+#define ENABLE_DEBUG
 */
+#undef ENABLE_DEBUG
 
 #ifdef ENABLE_DEBUG
 #define DEBUG(x) (x)
@@ -460,6 +460,77 @@ adjust_subregion (GtkSourceSearch *search,
        });
 }
 
+/* Remove the occurrences in the range. @start and @end may be adjusted, if they
+ * are in a found_tag region.
+ */
+static void
+remove_occurrences_in_range (GtkSourceSearch *search,
+                            GtkTextIter     *start,
+                            GtkTextIter     *end)
+{
+       GtkTextIter iter;
+       GtkTextIter match_start;
+       GtkTextIter match_end;
+
+       if (gtk_text_iter_has_tag (start, search->priv->found_tag) &&
+           !gtk_text_iter_toggles_tag (start, search->priv->found_tag))
+       {
+               gtk_text_iter_backward_to_tag_toggle (start, search->priv->found_tag);
+       }
+
+       if (gtk_text_iter_has_tag (end, search->priv->found_tag) &&
+           !gtk_text_iter_toggles_tag (end, search->priv->found_tag))
+       {
+               gtk_text_iter_forward_to_tag_toggle (end, search->priv->found_tag);
+       }
+
+       gtk_text_buffer_remove_tag (search->priv->buffer,
+                                   search->priv->found_tag,
+                                   start,
+                                   end);
+
+       if (search->priv->text == NULL)
+       {
+               return;
+       }
+
+       iter = *start;
+
+       /* TODO optimization: search with forward_to_tag_toggle() */
+       while (gtk_text_iter_forward_search (&iter,
+                                            search->priv->text,
+                                            search->priv->flags,
+                                            &match_start,
+                                            &match_end,
+                                            end))
+       {
+               if (search->priv->region_to_scan == NULL)
+               {
+                       /* The occurrence has already been scanned, and thus
+                        * occurrence_count take it into account. */
+                       search->priv->occurrences_count--;
+               }
+               else
+               {
+                       GtkTextRegion *region = gtk_text_region_intersect (search->priv->region_to_scan,
+                                                                          &match_start,
+                                                                          &match_end);
+
+                       if (is_text_region_empty (region))
+                       {
+                               search->priv->occurrences_count--;
+                       }
+
+                       if (region != NULL)
+                       {
+                               gtk_text_region_destroy (region, TRUE);
+                       }
+               }
+
+               iter = match_end;
+       }
+}
+
 static void
 scan_subregion (GtkSourceSearch *search,
                GtkTextIter     *start,
@@ -480,11 +551,7 @@ scan_subregion (GtkSourceSearch *search,
                                       search->priv->buffer);
 
        adjust_subregion (search, start, end);
-
-       gtk_text_buffer_remove_tag (search->priv->buffer,
-                                   search->priv->found_tag,
-                                   start,
-                                   end);
+       remove_occurrences_in_range (search, start, end);
 
        if (search->priv->region_not_scanned != NULL)
        {
@@ -730,62 +797,13 @@ insert_text_before_cb (GtkSourceSearch *search,
                       gchar           *text,
                       gint             length)
 {
-       GtkTextIter start = *location;
-       GtkTextIter end = *location;
-       GtkTextIter iter;
-       GtkTextIter match_start;
-       GtkTextIter match_end;
-
-       if (search->priv->text == NULL)
-       {
-               return;
-       }
-
-       DEBUG ({
-               g_print ("insert_text_before_cb(): region to scan:\n");
-               if (search->priv->region_to_scan != NULL)
-               {
-                       gtk_text_region_debug_print (search->priv->region_to_scan);
-               }
-               else
-               {
-                       g_print ("empty\n");
-               }
-       });
-
-       adjust_subregion (search, &start, &end);
-
-       iter = start;
-
-       while (gtk_text_iter_forward_search (&iter,
-                                            search->priv->text,
-                                            search->priv->flags,
-                                            &match_start,
-                                            &match_end,
-                                            &end))
+       if (search->priv->text != NULL)
        {
-               if (search->priv->region_not_scanned == NULL)
-               {
-                       search->priv->occurrences_count--;
-               }
-               else
-               {
-                       GtkTextRegion *region = gtk_text_region_intersect (search->priv->region_not_scanned,
-                                                                          &match_start,
-                                                                          &match_end);
+               GtkTextIter start = *location;
+               GtkTextIter end = *location;
 
-                       if (is_text_region_empty (region))
-                       {
-                               search->priv->occurrences_count--;
-                       }
-
-                       if (region != NULL)
-                       {
-                               gtk_text_region_destroy (region, TRUE);
-                       }
-               }
-
-               iter = match_end;
+               remove_occurrences_in_range (search, &start, &end);
+               add_subregion_to_scan (search, &start, &end);
        }
 }
 


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