[gtksourceview: 2/2] SearchContext: use extra-natural word for non-regex word boundaries



commit 64e248ab54ed76c6be2f1805328b5b22f1c6141a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jun 11 18:49:31 2016 +0200

    SearchContext: use extra-natural word for non-regex word boundaries
    
    So that a word can contain underscores.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=745790

 gtksourceview/gtksourcesearchcontext.c |   16 +++++++++-------
 testsuite/test-search-context.c        |   15 +++++++++++++++
 2 files changed, 24 insertions(+), 7 deletions(-)
---
diff --git a/gtksourceview/gtksourcesearchcontext.c b/gtksourceview/gtksourcesearchcontext.c
index 378ddd3..61dbfe0 100644
--- a/gtksourceview/gtksourcesearchcontext.c
+++ b/gtksourceview/gtksourcesearchcontext.c
@@ -31,6 +31,7 @@
 #include "gtksourcestylescheme.h"
 #include "gtksourceutils.h"
 #include "gtksourceregion.h"
+#include "gtksourceiter.h"
 #include "gtksourceview-i18n.h"
 #include "gtksourceview-typebuiltins.h"
 
@@ -264,13 +265,14 @@
  *
  * To search at word boundaries, \b is added at the beginning and at the
  * end of the pattern. But \b is not the same as
- * gtk_text_iter_starts_word() and gtk_text_iter_ends_word(). \b for
+ * _gtk_source_iter_starts_extra_natural_word() and
+ * _gtk_source_iter_ends_extra_natural_word(). \b for
  * example doesn't take the underscore as a word boundary.
- * Using gtk_text_iter_starts_word() and ends_word() for regex searches
+ * Using _gtk_source_iter_starts_extra_natural_word() and ends_word() for regex searches
  * is not easily possible: if the GRegex returns a match, but doesn't
  * start and end a word, maybe a shorter match (for a greedy pattern)
  * start and end a word, or a longer match (for an ungreedy pattern). To
- * be able to use the gtk_text_iter_starts_word() and ends_word()
+ * be able to use the _gtk_source_iter_starts_extra_natural_word() and ends_word()
  * functions for regex search, g_regex_match_all_full() must be used, to
  * retrieve _all_ matches, and test the word boundaries until a match is
  * found at word boundaries.
@@ -839,8 +841,8 @@ basic_forward_search (GtkSourceSearchContext *search,
                        return found;
                }
 
-               if (gtk_text_iter_starts_word (match_start) &&
-                   gtk_text_iter_ends_word (match_end))
+               if (_gtk_source_iter_starts_extra_natural_word (match_start, FALSE) &&
+                   _gtk_source_iter_ends_extra_natural_word (match_end, FALSE))
                {
                        return TRUE;
                }
@@ -940,8 +942,8 @@ basic_backward_search (GtkSourceSearchContext *search,
                        return found;
                }
 
-               if (gtk_text_iter_starts_word (match_start) &&
-                   gtk_text_iter_ends_word (match_end))
+               if (_gtk_source_iter_starts_extra_natural_word (match_start, FALSE) &&
+                   _gtk_source_iter_ends_extra_natural_word (match_end, FALSE))
                {
                        return TRUE;
                }
diff --git a/testsuite/test-search-context.c b/testsuite/test-search-context.c
index 377d0c2..fd7586e 100644
--- a/testsuite/test-search-context.c
+++ b/testsuite/test-search-context.c
@@ -389,6 +389,21 @@ test_search_at_word_boundaries (void)
        occurrences_count = gtk_source_search_context_get_occurrences_count (context);
        g_assert_cmpint (occurrences_count, ==, 2);
 
+       /* Word with underscores */
+
+       gtk_text_buffer_set_text (text_buffer, "_hello_world_ _hello_", -1);
+       gtk_source_search_settings_set_search_text (settings, "_hello_");
+
+       gtk_source_search_settings_set_at_word_boundaries (settings, TRUE);
+       flush_queue ();
+       occurrences_count = gtk_source_search_context_get_occurrences_count (context);
+       g_assert_cmpint (occurrences_count, ==, 1);
+
+       gtk_source_search_settings_set_at_word_boundaries (settings, FALSE);
+       flush_queue ();
+       occurrences_count = gtk_source_search_context_get_occurrences_count (context);
+       g_assert_cmpint (occurrences_count, ==, 2);
+
        g_object_unref (source_buffer);
        g_object_unref (settings);
        g_object_unref (context);


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