[gtk/wip/chergert/fix-texthistory-insert] texthistory: fix calculation of n_chars




commit 5d02a8f5dbe285a6ff48884520a17c348bcc6774
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jul 15 19:19:57 2021 -0700

    texthistory: fix calculation of n_chars
    
    This should be the number of characters, not the end position. This fixes
    an issue where we wouldn't coalesce insert actions together.

 gtk/gtktexthistory.c        |  9 ++++-----
 testsuite/gtk/texthistory.c | 27 +++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 5 deletions(-)
---
diff --git a/gtk/gtktexthistory.c b/gtk/gtktexthistory.c
index 1d97ff31a7..61ccc33f08 100644
--- a/gtk/gtktexthistory.c
+++ b/gtk/gtktexthistory.c
@@ -989,6 +989,7 @@ gtk_text_history_text_inserted (GtkTextHistory *self,
                                 int             len)
 {
   Action *action;
+  guint n_chars;
 
   g_return_if_fail (GTK_IS_TEXT_HISTORY (self));
 
@@ -998,14 +999,12 @@ gtk_text_history_text_inserted (GtkTextHistory *self,
 
   if (len < 0)
     len = strlen (text);
+  n_chars = g_utf8_strlen (text, len);
 
   action = action_new (ACTION_KIND_INSERT);
   action->u.insert.begin = position;
-  action->u.insert.end = position + g_utf8_strlen (text, len);
-  istring_set (&action->u.insert.istr,
-               text,
-               len,
-               action->u.insert.end);
+  action->u.insert.end = position + n_chars;
+  istring_set (&action->u.insert.istr, text, len, n_chars);
 
   gtk_text_history_push (self, action);
 }
diff --git a/testsuite/gtk/texthistory.c b/testsuite/gtk/texthistory.c
index f9fe45ceb2..c64c803083 100644
--- a/testsuite/gtk/texthistory.c
+++ b/testsuite/gtk/texthistory.c
@@ -578,6 +578,32 @@ test13 (void)
   run_test (commands, G_N_ELEMENTS (commands), 3);
 }
 
+static void
+test14 (void)
+{
+  char *fill = g_strnfill (1024, 'x');
+  char *fill_after = g_strnfill (1025, 'x');
+  char *fill_after_2 = g_strdup_printf ("%s word", fill_after);
+  const Command commands[] = {
+    { BEGIN_USER, -1, -1, NULL, NULL, UNSET, UNSET, UNSET },
+    { INSERT, 0, -1, fill, fill, UNSET, UNSET, UNSET },
+    { END_USER, -1, -1, NULL, NULL, SET, UNSET, UNSET },
+    { BEGIN_USER, -1, -1, NULL, NULL, UNSET, UNSET, UNSET },
+    { INSERT, 0, -1, "x", fill_after, UNSET, UNSET, UNSET },
+    { END_USER, -1, -1, NULL, NULL, SET, UNSET, UNSET },
+    { BEGIN_USER, -1, -1, NULL, NULL, UNSET, UNSET, UNSET },
+    { INSERT_SEQ, strlen(fill_after), -1, " word", fill_after_2, UNSET, UNSET, UNSET },
+    { END_USER, -1, -1, NULL, NULL, SET, UNSET, UNSET },
+    { UNDO, -1, -1, NULL, fill_after, SET, SET, UNSET },
+    { UNDO, -1, -1, NULL, fill, SET, SET, UNSET },
+    { UNDO, -1, -1, NULL, "", UNSET, SET, UNSET },
+  };
+
+  run_test (commands, G_N_ELEMENTS (commands), 0);
+
+  g_free (fill);
+}
+
 int
 main (int   argc,
       char *argv[])
@@ -597,6 +623,7 @@ main (int   argc,
   g_test_add_func ("/Gtk/TextHistory/test11", test11);
   g_test_add_func ("/Gtk/TextHistory/test12", test12);
   g_test_add_func ("/Gtk/TextHistory/test13", test13);
+  g_test_add_func ("/Gtk/TextHistory/test14", test14);
 
   return g_test_run ();
 }


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