[gspell] current word: improve current word handling on text deletion



commit 00f7355aba102cfb11aaa9ac1fcebdcbfa6a79b7
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Mar 5 15:06:25 2016 +0100

    current word: improve current word handling on text deletion

 gspell/gspell-inline-checker-text-buffer.c  |   67 ++++++++++++++++++++------
 testsuite/test-inline-checker-text-buffer.c |   58 ++++++++++++++++++++++-
 2 files changed, 107 insertions(+), 18 deletions(-)
---
diff --git a/gspell/gspell-inline-checker-text-buffer.c b/gspell/gspell-inline-checker-text-buffer.c
index e005e96..6508627 100644
--- a/gspell/gspell-inline-checker-text-buffer.c
+++ b/gspell/gspell-inline-checker-text-buffer.c
@@ -622,6 +622,57 @@ delete_range_before_cb (GtkTextBuffer                 *buffer,
        end_adjusted = *end;
        adjust_iters (&start_adjusted, &end_adjusted, ADJUST_MODE_INCLUDE_NEIGHBORS);
        add_subregion_to_scan (spell, &start_adjusted, &end_adjusted);
+
+       /* Check current word? */
+       if (gtk_text_buffer_get_has_selection (buffer) ||
+           gtk_text_iter_get_line (start) != gtk_text_iter_get_line (end) ||
+           (gtk_text_iter_get_line_offset (end) - gtk_text_iter_get_line_offset (start)) > 1)
+       {
+               spell->check_current_word = TRUE;
+       }
+       else
+       {
+               GtkTextIter cursor_pos;
+               gboolean is_backspace;
+               gboolean is_delete;
+
+               gtk_text_buffer_get_iter_at_mark (buffer,
+                                                 &cursor_pos,
+                                                 gtk_text_buffer_get_insert (buffer));
+
+               is_backspace = gtk_text_iter_equal (&cursor_pos, end);
+               is_delete = gtk_text_iter_equal (&cursor_pos, start);
+
+               if (is_backspace)
+               {
+                       if (gtk_text_iter_inside_word (start) ||
+                           gtk_text_iter_ends_word (start))
+                       {
+                               spell->check_current_word = FALSE;
+                       }
+                       else
+                       {
+                               spell->check_current_word = TRUE;
+                       }
+               }
+               else if (is_delete)
+               {
+                       if (gtk_text_iter_inside_word (end) ||
+                           gtk_text_iter_ends_word (end))
+                       {
+                               spell->check_current_word = FALSE;
+                       }
+                       else
+                       {
+                               spell->check_current_word = TRUE;
+                       }
+               }
+               /* Text deleted programmatically. */
+               else
+               {
+                       spell->check_current_word = TRUE;
+               }
+       }
 }
 
 static void
@@ -640,22 +691,6 @@ delete_range_after_cb (GtkTextBuffer                 *buffer,
        adjust_iters (&start_adjusted, &end_adjusted, ADJUST_MODE_INCLUDE_NEIGHBORS);
        add_subregion_to_scan (spell, &start_adjusted, &end_adjusted);
 
-       /* Check current word? */
-       if (gtk_text_buffer_get_has_selection (buffer))
-       {
-               spell->check_current_word = TRUE;
-       }
-       else
-       {
-               GtkTextIter cursor_pos;
-
-               gtk_text_buffer_get_iter_at_mark (buffer,
-                                                 &cursor_pos,
-                                                 gtk_text_buffer_get_insert (buffer));
-
-               spell->check_current_word = !gtk_text_iter_equal (start, &cursor_pos);
-       }
-
        install_timeout (spell, TIMEOUT_DURATION_BUFFER_MODIFIED);
 }
 
diff --git a/testsuite/test-inline-checker-text-buffer.c b/testsuite/test-inline-checker-text-buffer.c
index 23142f9..51db2cf 100644
--- a/testsuite/test-inline-checker-text-buffer.c
+++ b/testsuite/test-inline-checker-text-buffer.c
@@ -331,10 +331,10 @@ test_current_word (void)
                                 0, 5,
                                 -1);
 
-       gtk_text_buffer_backspace (buffer, &iter, FALSE, TRUE);
+       gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
        check_highlighted_words (buffer, inline_checker, -1);
 
-       gtk_text_buffer_backspace (buffer, &iter, FALSE, TRUE);
+       gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
        check_highlighted_words (buffer, inline_checker, -1);
 
        gtk_text_buffer_insert (buffer, &iter, "o", -1);
@@ -410,6 +410,60 @@ test_current_word (void)
                                 10, 13, /* "tkw" also highlighted */
                                 -1);
 
+       /* Delete selection.
+        * " H|el|llo " -> " H|llo "
+        */
+       gtk_text_buffer_set_text (buffer, " Helllo ", -1);
+       gtk_text_buffer_get_iter_at_offset (buffer, &start, 2);
+       gtk_text_buffer_get_iter_at_offset (buffer, &end, 4);
+       gtk_text_buffer_select_range (buffer, &start, &end);
+       gtk_text_buffer_delete (buffer, &start, &end);
+       check_highlighted_words (buffer,
+                                inline_checker,
+                                1, 5,
+                                -1);
+
+       /* Backspace at end of word.
+        * " Hllo |" -> " Hllo|"
+        */
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_place_cursor (buffer, &iter);
+       gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
+       check_highlighted_words (buffer, inline_checker, -1);
+
+       /* Backspace before a word.
+        * " |Hllo" -> "|Hllo"
+        */
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 1);
+       gtk_text_buffer_place_cursor (buffer, &iter);
+       gtk_text_buffer_backspace (buffer, &iter, TRUE, TRUE);
+       check_highlighted_words (buffer,
+                                inline_checker,
+                                0, 4,
+                                -1);
+
+       /* Delete at beginning of word.
+        * "| Hllo " -> "|Hllo "
+        */
+       gtk_text_buffer_set_text (buffer, " Hllo ", -1);
+       gtk_text_buffer_get_start_iter (buffer, &start);
+       gtk_text_buffer_get_iter_at_offset (buffer, &end, 1);
+       gtk_text_buffer_place_cursor (buffer, &start);
+       gtk_text_buffer_delete (buffer, &start, &end);
+       check_highlighted_words (buffer, inline_checker, -1);
+
+       /* Delete after a word.
+        * "Hllo| " -> "Hllo|"
+        */
+       gtk_text_buffer_get_iter_at_offset (buffer, &start, 4);
+       gtk_text_buffer_get_end_iter (buffer, &end);
+       gtk_text_buffer_place_cursor (buffer, &start);
+       gtk_text_buffer_delete (buffer, &start, &end);
+       check_highlighted_words (buffer,
+                                inline_checker,
+                                0, 4,
+                                -1);
+
        g_object_unref (inline_checker);
        g_object_unref (buffer);
 }


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