[gtksourceview] iter: fix a bug in get_leading_spaces_end_boundary()



commit 47ac92988e7e75f4d075b92d8d36b6fad7ac7425
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Apr 21 12:59:17 2017 +0200

    iter: fix a bug in get_leading_spaces_end_boundary()
    
    _gtk_source_iter_get_leading_spaces_end_boundary() moved to the
    following lines if the line at iter doesn't contain text, because
    g_unichar_isspace() returns TRUE for \n and \r too.
    
    But after a quick look at the code calling
    _gtk_source_iter_get_leading_spaces_end_boundary(), the bug didn't have
    an impact. It had an impact in Gtef, where I've copied the function and
    used it for another purpose.

 gtksourceview/gtksourceiter.c |    2 +-
 testsuite/test-iter.c         |   40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletions(-)
---
diff --git a/gtksourceview/gtksourceiter.c b/gtksourceview/gtksourceiter.c
index 7ca7516..504e6f2 100644
--- a/gtksourceview/gtksourceiter.c
+++ b/gtksourceview/gtksourceiter.c
@@ -653,7 +653,7 @@ _gtk_source_iter_get_leading_spaces_end_boundary (const GtkTextIter *iter,
        *leading_end = *iter;
        gtk_text_iter_set_line_offset (leading_end, 0);
 
-       while (!gtk_text_iter_is_end (leading_end))
+       while (!gtk_text_iter_ends_line (leading_end))
        {
                gunichar ch = gtk_text_iter_get_char (leading_end);
 
diff --git a/testsuite/test-iter.c b/testsuite/test-iter.c
index ae4f887..f5c630d 100644
--- a/testsuite/test-iter.c
+++ b/testsuite/test-iter.c
@@ -394,6 +394,44 @@ test_backward_word_start (void)
        check_word_boundaries_movement (FALSE, "--__--", 2, 0, TRUE);
 }
 
+static void
+check_get_leading_spaces_end_boundary (const gchar *text,
+                                      gint         iter_offset,
+                                      gint         expected_leading_end_offset)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+       GtkTextIter leading_end;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, iter_offset);
+       _gtk_source_iter_get_leading_spaces_end_boundary (&iter, &leading_end);
+       g_assert_cmpint (gtk_text_iter_get_offset (&leading_end), ==, expected_leading_end_offset);
+
+       g_object_unref (buffer);
+}
+
+static void
+test_get_leading_spaces_end_boundary (void)
+{
+       check_get_leading_spaces_end_boundary ("  abc\n", 0, 2);
+       check_get_leading_spaces_end_boundary ("  \n", 0, 2);
+       check_get_leading_spaces_end_boundary ("\t\n", 0, 1);
+       check_get_leading_spaces_end_boundary ("\t\r\n", 0, 1);
+       check_get_leading_spaces_end_boundary ("\t\r", 0, 1);
+       check_get_leading_spaces_end_boundary (" \t \n", 0, 3);
+
+       /* No-Break Space U+00A0 */
+       check_get_leading_spaces_end_boundary ("\302\240abc\n", 0, 1);
+       check_get_leading_spaces_end_boundary (" \t\302\240\t\n", 0, 4);
+
+       /* Narrow No-Break Space U+202F */
+       check_get_leading_spaces_end_boundary ("\342\200\257abc\n", 0, 1);
+       check_get_leading_spaces_end_boundary ("\t \342\200\257\n", 0, 3);
+}
+
 int
 main (int argc, char **argv)
 {
@@ -413,5 +451,7 @@ main (int argc, char **argv)
        g_test_add_func ("/Iter/custom-word/forward", test_forward_word_end);
        g_test_add_func ("/Iter/custom-word/backward", test_backward_word_start);
 
+       g_test_add_func ("/Iter/get_leading_spaces_end_boundary", test_get_leading_spaces_end_boundary);
+
        return g_test_run ();
 }


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