[gnome-text-editor] spellcheck: improve spell cursor movements



commit 8a97c0711f649fd3542ff3877d89673e38db081b
Author: Christian Hergert <chergert redhat com>
Date:   Mon Jun 28 15:45:07 2021 -0700

    spellcheck: improve spell cursor movements
    
    We need to handle boundaries better where the GtkTextIter might give us
    an answer that isn't quite what we want. We can consider end of buffer a
    break for all of our cases, and can ignore the FALSE value there.

 src/editor-spell-cursor.c              | 82 ++++++++++++++++++++--------------
 src/editor-text-buffer-spell-adapter.c | 13 ++++--
 2 files changed, 58 insertions(+), 37 deletions(-)
---
diff --git a/src/editor-spell-cursor.c b/src/editor-spell-cursor.c
index e536cfc..0800c87 100644
--- a/src/editor-spell-cursor.c
+++ b/src/editor-spell-cursor.c
@@ -46,10 +46,52 @@ editor_spell_cursor_init (EditorSpellCursor *cursor,
   cursor->misspelled_tag = misspelled_tag;
   cursor->begin = *begin;
   cursor->end = *end;
+  cursor->exhausted = FALSE;
+
   gtk_text_iter_order (&cursor->begin, &cursor->end);
+  gtk_text_iter_backward_word_start (&cursor->begin);
+  gtk_text_iter_forward_word_end (&cursor->end);
+
   cursor->word_begin = cursor->begin;
   cursor->word_end = cursor->begin;
-  cursor->exhausted = FALSE;
+
+  /* Clear the tag for the (possibly extended) region */
+  gtk_text_buffer_remove_tag (cursor->buffer,
+                              cursor->misspelled_tag,
+                              &cursor->begin,
+                              &cursor->end);
+}
+
+static gboolean
+forward_word_end (GtkTextIter *iter)
+{
+  GtkTextIter tmp = *iter;
+
+  if (gtk_text_iter_forward_word_end (iter))
+    return TRUE;
+
+  if (gtk_text_iter_is_end (iter) &&
+      gtk_text_iter_ends_word (iter) &&
+      !gtk_text_iter_equal (&tmp, iter))
+    return TRUE;
+
+  return FALSE;
+}
+
+static gboolean
+backward_word_start (GtkTextIter *iter)
+{
+  GtkTextIter tmp = *iter;
+
+  if (gtk_text_iter_backward_word_start (iter))
+    return TRUE;
+
+  if (gtk_text_iter_is_start (iter) &&
+      gtk_text_iter_starts_word (iter) &&
+      !gtk_text_iter_equal (&tmp, iter))
+    return TRUE;
+
+  return FALSE;
 }
 
 char *
@@ -60,43 +102,15 @@ editor_spell_cursor_next_word (EditorSpellCursor *cursor)
   if (cursor->exhausted)
     return NULL;
 
-  /* If this is the initial movement, then we need to handle the
-   * case where the first word overlaps the boundary.
-   */
-  if (gtk_text_iter_equal (&cursor->word_begin, &cursor->word_end))
-    {
-      if (!gtk_text_iter_starts_word (&cursor->word_begin))
-        {
-          if (gtk_text_iter_backward_word_start (&cursor->word_begin))
-            {
-              cursor->word_end = cursor->word_begin;
-              gtk_text_iter_forward_word_end (&cursor->word_end);
-            }
-           else
-            {
-              if (!gtk_text_iter_forward_word_end (&cursor->word_end))
-                goto exhausted;
-
-              cursor->word_begin = cursor->word_end;
-              gtk_text_iter_backward_word_start (&cursor->word_begin);
-            }
-        }
-
-      /* If the word position overlaps the region, then we can return it.
-       * Otherwise we need to try to move forward (the regular flow).
-       */
-      if (gtk_text_iter_compare (&cursor->word_end, &cursor->begin) > 0)
-        return editor_spell_cursor_word (cursor);
-    }
-
-  if (!gtk_text_iter_forward_word_end (&cursor->word_end))
+  if (!forward_word_end (&cursor->word_end))
     goto exhausted;
 
   cursor->word_begin = cursor->word_end;
-  gtk_text_iter_backward_word_start (&cursor->word_begin);
 
-  if (gtk_text_iter_compare (&cursor->word_begin, &cursor->end) <= 0)
-    return editor_spell_cursor_word (cursor);
+  if (!backward_word_start (&cursor->word_begin))
+    goto exhausted;
+
+  return editor_spell_cursor_word (cursor);
 
 exhausted:
   cursor->exhausted = TRUE;
diff --git a/src/editor-text-buffer-spell-adapter.c b/src/editor-text-buffer-spell-adapter.c
index 05b5d54..8644ca3 100644
--- a/src/editor-text-buffer-spell-adapter.c
+++ b/src/editor-text-buffer-spell-adapter.c
@@ -125,9 +125,6 @@ editor_text_buffer_spell_adapter_update_range (EditorTextBufferSpellAdapter *sel
 
   g_assert (EDITOR_IS_TEXT_BUFFER_SPELL_ADAPTER (self));
 
-  if (begin_offset == end_offset)
-    return FALSE;
-
   if (!scan_for_next_unchecked (self->region, begin_offset, end_offset, &position))
     return FALSE;
 
@@ -472,6 +469,8 @@ editor_text_buffer_spell_adapter_insert_text (EditorTextBufferSpellAdapter *self
   g_return_if_fail (length > 0);
 
   _cjh_text_region_insert (self->region, offset, length, UNCHECKED);
+
+  editor_text_buffer_spell_adapter_queue_update (self);
 }
 
 void
@@ -484,6 +483,14 @@ editor_text_buffer_spell_adapter_delete_range (EditorTextBufferSpellAdapter *sel
   g_return_if_fail (length > 0);
 
   _cjh_text_region_remove (self->region, offset, length);
+
+  /* Invalidate surrounding characters */
+  if (offset)
+    _cjh_text_region_replace (self->region, offset - 1, 1, UNCHECKED);
+  if (offset + length < _cjh_text_region_get_length (self->region))
+    _cjh_text_region_replace (self->region, offset, 1, UNCHECKED);
+
+  editor_text_buffer_spell_adapter_queue_update (self);
 }
 
 void


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