[gtksourceview/cherry-pick-410fb1fd] Merge branch 'wip/chergert/fix-190' into 'master'




commit 26eb751b27d9c81d53bccddbf5cd9ebb3cf6b464
Author: Christian Hergert <christian hergert me>
Date:   Wed Jul 28 22:12:04 2021 +0000

    Merge branch 'wip/chergert/fix-190' into 'master'
    
    fix move-words
    
    Closes #190
    
    See merge request GNOME/gtksourceview!201
    
    (cherry picked from commit 410fb1fdd227d70014351bc8094fc1f6e2fd19bc)
    
    be351c4c testsuite: add failing test for move-words
    1ee1c620 view: fix move-words when boundaries are juxtapose

 gtksourceview/gtksourceview.c |  7 +++---
 testsuite/test-buffer.c       | 51 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 3 deletions(-)
---
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index e10584aa..5db18ea7 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -3748,7 +3748,8 @@ gtk_source_view_move_words (GtkSourceView *view,
        }
 
        if (gtk_text_iter_in_range (&ns, &s, &e) ||
-           gtk_text_iter_in_range (&ne, &s, &e))
+           (!gtk_text_iter_equal (&s, &ne) &&
+            gtk_text_iter_in_range (&ne, &s, &e)))
        {
                return;
        }
@@ -3758,8 +3759,8 @@ gtk_source_view_move_words (GtkSourceView *view,
 
        gtk_text_buffer_begin_user_action (buf);
 
-       nsmark = gtk_text_buffer_create_mark (buf, NULL, &ns, TRUE);
-       nemark = gtk_text_buffer_create_mark (buf, NULL, &ne, FALSE);
+       nsmark = gtk_text_buffer_create_mark (buf, NULL, &ns, step < 0);
+       nemark = gtk_text_buffer_create_mark (buf, NULL, &ne, step < 0);
 
        gtk_text_buffer_delete (buf, &s, &e);
        gtk_text_buffer_insert (buf, &s, new_text, -1);
diff --git a/testsuite/test-buffer.c b/testsuite/test-buffer.c
index 618077ad..204f6356 100644
--- a/testsuite/test-buffer.c
+++ b/testsuite/test-buffer.c
@@ -293,6 +293,56 @@ test_sort_lines (void)
        g_object_unref (buffer);
 }
 
+static void
+do_test_move_words (GtkSourceView      *view,
+                    GtkSourceBuffer    *buffer,
+                    const gchar        *text,
+                    const gchar        *expected,
+                    gint                start_offset,
+                    gint                end_offset,
+                    gint                step)
+{
+       GtkTextIter start;
+       GtkTextIter end;
+       gchar *changed;
+
+       gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &start, start_offset);
+       gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &end, end_offset);
+       gtk_text_buffer_select_range (GTK_TEXT_BUFFER (buffer), &start, &end);
+
+       g_signal_emit_by_name (view, "move-words", step);
+
+       gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
+       changed = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer), &start, &end, TRUE);
+
+       g_assert_cmpstr (changed, ==, expected);
+
+       g_free (changed);
+}
+
+static void
+test_move_words (void)
+{
+       GtkSourceView *view;
+       GtkSourceBuffer *buffer;
+
+       buffer = gtk_source_buffer_new (NULL);
+       view = g_object_ref_sink (GTK_SOURCE_VIEW (gtk_source_view_new ()));
+
+       gtk_text_view_set_buffer (GTK_TEXT_VIEW (view), GTK_TEXT_BUFFER (buffer));
+
+       do_test_move_words (view, buffer, "a > b", "a b >", 2, 3, 1);
+       do_test_move_words (view, buffer, "a>b", "ab>", 1, 2, 1);
+       do_test_move_words (view, buffer, "a>b", ">ab", 1, 2, -1);
+       do_test_move_words (view, buffer, "what is this word.", "what word this is.", 13, 17, -2);
+       do_test_move_words (view, buffer, "what word this is.", "what is this word.", 5, 9, 2);
+
+       g_object_unref (buffer);
+       g_object_unref (view);
+}
+
 static void
 do_test_bracket_matching (GtkSourceBuffer           *source_buffer,
                          const gchar               *text,
@@ -448,6 +498,7 @@ main (int argc, char** argv)
        g_test_add_func ("/Buffer/change-case", test_change_case);
        g_test_add_func ("/Buffer/join-lines", test_join_lines);
        g_test_add_func ("/Buffer/sort-lines", test_sort_lines);
+       g_test_add_func ("/Buffer/move-words", test_move_words);
        g_test_add_func ("/Buffer/bracket-matching", test_bracket_matching);
 
        return g_test_run();


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