[gtksourceview] iter: fix bug with extra-natural word containing only underscores



commit b1d2e3eb3cf6cc8b211b4bdca0718e7468972147
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Wed Feb 4 19:53:24 2015 +0100

    iter: fix bug with extra-natural word containing only underscores
    
    gtk_text_iter_{forward,backward}_search() are used because we need the
    VISIBLE_ONLY flag, otherwise
    gtk_text_iter_{forward,backward}_find_char() would be easier to use,
    especially if we need to search several characters (if we want to make
    the definition of an extra-natural word more flexible).

 gtksourceview/gtksourceiter.c |   52 +++++++++++++++++++++++++++++++++++++++-
 tests/test-iter.c             |   16 ++++++++++++
 2 files changed, 66 insertions(+), 2 deletions(-)
---
diff --git a/gtksourceview/gtksourceiter.c b/gtksourceview/gtksourceiter.c
index 76f79ab..807e8e4 100644
--- a/gtksourceview/gtksourceiter.c
+++ b/gtksourceview/gtksourceiter.c
@@ -158,7 +158,31 @@ _gtk_source_iter_ends_full_word (const GtkTextIter *iter)
 void
 _gtk_source_iter_forward_extra_natural_word_end (GtkTextIter *iter)
 {
-       gtk_text_iter_forward_visible_word_end (iter);
+       GtkTextIter next_word_end = *iter;
+       GtkTextIter next_underscore_end = *iter;
+       GtkTextIter *limit = NULL;
+       gboolean found;
+
+       if (gtk_text_iter_forward_visible_word_end (&next_word_end))
+       {
+               limit = &next_word_end;
+       }
+
+       found = gtk_text_iter_forward_search (iter,
+                                             "_",
+                                             GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY,
+                                             NULL,
+                                             &next_underscore_end,
+                                             limit);
+
+       if (found)
+       {
+               *iter = next_underscore_end;
+       }
+       else
+       {
+               *iter = next_word_end;
+       }
 
        while (TRUE)
        {
@@ -181,7 +205,31 @@ _gtk_source_iter_forward_extra_natural_word_end (GtkTextIter *iter)
 void
 _gtk_source_iter_backward_extra_natural_word_start (GtkTextIter *iter)
 {
-       gtk_text_iter_backward_visible_word_start (iter);
+       GtkTextIter prev_word_start = *iter;
+       GtkTextIter prev_underscore_start = *iter;
+       GtkTextIter *limit = NULL;
+       gboolean found;
+
+       if (gtk_text_iter_backward_visible_word_start (&prev_word_start))
+       {
+               limit = &prev_word_start;
+       }
+
+       found = gtk_text_iter_backward_search (iter,
+                                              "_",
+                                              GTK_TEXT_SEARCH_VISIBLE_ONLY | GTK_TEXT_SEARCH_TEXT_ONLY,
+                                              &prev_underscore_start,
+                                              NULL,
+                                              limit);
+
+       if (found)
+       {
+               *iter = prev_underscore_start;
+       }
+       else
+       {
+               *iter = prev_word_start;
+       }
 
        while (!gtk_text_iter_is_start (iter))
        {
diff --git a/tests/test-iter.c b/tests/test-iter.c
index 9532515..cbbdd0b 100644
--- a/tests/test-iter.c
+++ b/tests/test-iter.c
@@ -176,6 +176,10 @@ test_forward_extra_natural_word_end (void)
        check_extra_natural_word_boundaries (TRUE, "a_ ", 2, 2);
        check_extra_natural_word_boundaries (TRUE, "ab \ncd", 2, 6);
        check_extra_natural_word_boundaries (TRUE, "a_ \n_d", 2, 6);
+
+       check_extra_natural_word_boundaries (TRUE, "__ ab", 0, 2);
+       check_extra_natural_word_boundaries (TRUE, "--__--", 0, 4);
+       check_extra_natural_word_boundaries (TRUE, "--__-- ab", 0, 4);
 }
 
 static void
@@ -195,6 +199,10 @@ test_backward_extra_natural_word_start (void)
        check_extra_natural_word_boundaries (FALSE, " _d", 1, 1);
        check_extra_natural_word_boundaries (FALSE, "ab\n cd", 4, 0);
        check_extra_natural_word_boundaries (FALSE, "_b\n c_", 4, 0);
+
+       check_extra_natural_word_boundaries (FALSE, "ab __", 5, 3);
+       check_extra_natural_word_boundaries (FALSE, "--__--", 6, 2);
+       check_extra_natural_word_boundaries (FALSE, "ab --__--", 9, 5);
 }
 
 static void
@@ -353,6 +361,10 @@ test_forward_word_end (void)
        check_word_boundaries_movement (TRUE, "ab ", 2, 2, FALSE);
        check_word_boundaries_movement (TRUE, "ab \n", 2, 2, FALSE);
        check_word_boundaries_movement (TRUE, "ab \ncd", 2, 6, FALSE);
+
+       check_word_boundaries_movement (TRUE, "--__--", 0, 2, TRUE);
+       check_word_boundaries_movement (TRUE, "--__--", 2, 4, TRUE);
+       check_word_boundaries_movement (TRUE, "--__--", 4, 6, FALSE);
 }
 
 static void
@@ -377,6 +389,10 @@ test_backward_word_start (void)
        check_word_boundaries_movement (FALSE, " cd", 1, 1, FALSE);
        check_word_boundaries_movement (FALSE, "\n cd", 2, 2, FALSE);
        check_word_boundaries_movement (FALSE, "ab\n cd", 4, 0, TRUE);
+
+       check_word_boundaries_movement (FALSE, "--__--", 6, 4, TRUE);
+       check_word_boundaries_movement (FALSE, "--__--", 4, 2, TRUE);
+       check_word_boundaries_movement (FALSE, "--__--", 2, 0, TRUE);
 }
 
 int


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