[gspell/wip/tune-current-word] current word: don't check current word if only one alnum char inserted



commit 6e7679e0ec7932b7d4135f9c13f1a0b10634b9de
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Mar 5 13:16:17 2016 +0100

    current word: don't check current word if only one alnum char inserted

 gspell/gspell-inline-checker-text-buffer.c  |   23 ++++++++++-
 testsuite/test-inline-checker-text-buffer.c |   54 ++++++++++++++++++++++++--
 2 files changed, 70 insertions(+), 7 deletions(-)
---
diff --git a/gspell/gspell-inline-checker-text-buffer.c b/gspell/gspell-inline-checker-text-buffer.c
index 82c87b9..061f608 100644
--- a/gspell/gspell-inline-checker-text-buffer.c
+++ b/gspell/gspell-inline-checker-text-buffer.c
@@ -559,18 +559,37 @@ insert_text_after_cb (GtkTextBuffer                 *buffer,
                      gint                           length,
                      GspellInlineCheckerTextBuffer *spell)
 {
+       glong n_chars;
        GtkTextIter start;
        GtkTextIter end;
 
+       n_chars = g_utf8_strlen (text, length);
+
        start = *location;
        end = *location;
-       gtk_text_iter_backward_chars (&start, g_utf8_strlen (text, length));
+       gtk_text_iter_backward_chars (&start, n_chars);
 
        adjust_iters (&start, &end, ADJUST_MODE_INCLUDE_NEIGHBORS);
        add_subregion_to_scan (spell, &start, &end);
 
        /* Check current word? */
-       if (gtk_text_buffer_get_has_selection (buffer))
+
+       /* If more than one character is inserted, it's probably not a normal
+        * keypress, e.g. a clipboard paste or DND. So it's better to check the
+        * current word in that case, to know ASAP if the word is correctly
+        * spelled.
+        * The same if e.g. a space or punctuation is inserted: in that case we
+        * are not editing the current word. Maybe a word has been split in two,
+        * in which case the word on the left will anyway be checked, so it's
+        * better to know directly whether the word on the right is correctly
+        * spelled as well, so we know if we need to edit it or not.
+        * And if there is a selection, it means that the text was inserted
+        * programmatically, so the user is not editing the current word
+        * manually.
+        */
+       if (n_chars > 1 ||
+           !g_unichar_isalnum (g_utf8_get_char (text)) ||
+           gtk_text_buffer_get_has_selection (buffer))
        {
                spell->check_current_word = TRUE;
        }
diff --git a/testsuite/test-inline-checker-text-buffer.c b/testsuite/test-inline-checker-text-buffer.c
index 417bb31..23142f9 100644
--- a/testsuite/test-inline-checker-text-buffer.c
+++ b/testsuite/test-inline-checker-text-buffer.c
@@ -39,6 +39,20 @@ create_buffer (void)
        return buffer;
 }
 
+static void
+insert_text_one_char_at_a_time (GtkTextBuffer *buffer,
+                               GtkTextIter   *iter,
+                               const gchar   *text)
+{
+       const gchar *p;
+
+       /* Assumes it's only ASCII characters. */
+       for (p = text; p != NULL && *p != '\0'; p++)
+       {
+               gtk_text_buffer_insert (buffer, iter, p, 1);
+       }
+}
+
 /*
  * check_highlighted_words:
  * @buffer:
@@ -308,7 +322,7 @@ test_current_word (void)
        _gspell_inline_checker_text_buffer_set_unit_test_mode (inline_checker, TRUE);
 
        gtk_text_buffer_get_start_iter (buffer, &iter);
-       gtk_text_buffer_insert (buffer, &iter, "Hella", -1);
+       insert_text_one_char_at_a_time (buffer, &iter, "Hella");
        check_highlighted_words (buffer, inline_checker, -1);
 
        gtk_text_buffer_insert (buffer, &iter, " ", -1);
@@ -329,14 +343,14 @@ test_current_word (void)
        gtk_text_buffer_insert (buffer, &iter, " ", -1);
        check_highlighted_words (buffer, inline_checker, -1);
 
-       gtk_text_buffer_insert (buffer, &iter, "nrst", -1);
+       insert_text_one_char_at_a_time (buffer, &iter, "nrst");
        check_highlighted_words (buffer, inline_checker, -1);
 
        /* Cursor movement -> misspelled word highlighted. */
        gtk_text_iter_backward_cursor_position (&iter);
        gtk_text_buffer_place_cursor (buffer, &iter);
 
-       /* Buffer content: "Hello nrst".
+       /* Buffer content: "Hello nrs|t".
         * Cursor position: between 's' and 't'.
         */
        check_highlighted_words (buffer,
@@ -345,7 +359,7 @@ test_current_word (void)
                                 -1);
 
        /* Delete the 'e' programmatically, not at the cursor position.
-        * Hello -> Hllo
+        * "Hello nrs|t" -> "Hllo nrs|t"
         */
        gtk_text_buffer_get_iter_at_offset (buffer, &start, 1);
        gtk_text_buffer_get_iter_at_offset (buffer, &end, 2);
@@ -357,7 +371,7 @@ test_current_word (void)
                                 -1);
 
        /* Insert 'e' programmatically, not at the cursor position.
-        * Hllo -> Hello
+        * "Hllo nrs|t" -> "Hello nrs|t"
         */
        gtk_text_buffer_get_iter_at_offset (buffer, &iter, 1);
        gtk_text_buffer_insert (buffer, &iter, "e", -1);
@@ -366,6 +380,36 @@ test_current_word (void)
                                 6, 10, /* "nrst" still highlighted */
                                 -1);
 
+       /* Delete "nrst" programmatically.
+        * "Hello nrs|t" -> "Hello |"
+        */
+       gtk_text_buffer_get_iter_at_offset (buffer, &start, 6);
+       gtk_text_buffer_get_iter_at_offset (buffer, &end, 10);
+       gtk_text_buffer_delete (buffer, &start, &end);
+       check_highlighted_words (buffer, inline_checker, -1);
+
+       /* Insert several chars at once at the cursor position (e.g. a paste).
+        * "Hello |" -> "Hello nrstkw|"
+        */
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert (buffer, &iter, "nrstkw", -1);
+       check_highlighted_words (buffer,
+                                inline_checker,
+                                6, 12,
+                                -1);
+
+       /* Insert a comma to split a word in two.
+        * "Hello nrs|tkw" -> "Hello nrs,|tkw"
+        */
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 9);
+       gtk_text_buffer_place_cursor (buffer, &iter);
+       gtk_text_buffer_insert (buffer, &iter, ",", -1);
+       check_highlighted_words (buffer,
+                                inline_checker,
+                                6, 9,
+                                10, 13, /* "tkw" also highlighted */
+                                -1);
+
        g_object_unref (inline_checker);
        g_object_unref (buffer);
 }


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