[gtksourceview] CompletionWords: fix warning messages



commit 11a2cb8c7b3c64c13be278c974d3f8c75dc8fd44
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Jan 3 18:50:00 2015 +0100

    CompletionWords: fix warning messages
    
    The checks were too strict, they were sometimes triggered on text
    deletion.
    
    For example:
    - Initial buffer contents: " world"
    - Insert "hello" at the beginning -> "hello" is added to the
    scan_region. New buffer contents: "hello world"
    - Quickly delete " world", when "hello" is still in the scan_region.
    -> this triggered the warning "'start' iter not adjusted".
    
    Another example:
    - Initial buffer contents: "hello "
    - Insert "world" at the end.
    - Quickly delete "hello ", when "world" is still in the scan_region.
    -> this triggered the warning "'text_end' iter not adjusted".
    
    Now it should be fixed.
    
    Ideally unit tests should be written... But the code needs a flush()
    function to scan all the text synchronously instead of waiting the
    timeout. Note that the examples above assume that an inserted text is
    not scanned directly, so a flush() is needed so the unit tests can call
    it whenever they want (and so the timeout would be disabled for unit
    tests).
    
    https://bugzilla.gnome.org/show_bug.cgi?id=741439

 .../words/gtksourcecompletionwordsbuffer.c         |   16 +-------
 .../words/gtksourcecompletionwordsutils.c          |   43 ++++++++++++++++++++
 .../words/gtksourcecompletionwordsutils.h          |    4 ++
 3 files changed, 48 insertions(+), 15 deletions(-)
---
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c 
b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
index 7cd26d8..e80a384 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwordsbuffer.c
@@ -164,8 +164,6 @@ scan_line (GtkSourceCompletionWordsBuffer *buffer,
 {
        GtkTextIter line_end;
        GtkTextIter text_end;
-       GtkTextIter check_start;
-       GtkTextIter check_end;
        gchar *text;
        GSList *words;
 
@@ -187,19 +185,7 @@ scan_line (GtkSourceCompletionWordsBuffer *buffer,
                text_end = line_end;
        }
 
-       check_start = *start;
-       check_end = text_end;
-       _gtk_source_completion_words_utils_adjust_region (&check_start, &check_end);
-
-       if (!gtk_text_iter_equal (start, &check_start))
-       {
-               g_warning ("words completion scan_line(): 'start' iter not adjusted.");
-       }
-
-       if (!gtk_text_iter_equal (&text_end, &check_end))
-       {
-               g_warning ("words completion scan_line(): 'text_end' iter not adjusted.");
-       }
+       _gtk_source_completion_words_utils_check_scan_region (start, &text_end);
 
        text = gtk_text_buffer_get_text (buffer->priv->buffer,
                                         start,
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.c 
b/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.c
index f8317f1..4108b1f 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.c
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.c
@@ -204,3 +204,46 @@ _gtk_source_completion_words_utils_adjust_region (GtkTextIter *start,
                gtk_text_iter_forward_char (end);
        }
 }
+
+/* @iter here is a vertical bar between two characters, not the character
+ * pointed by @iter. So "inside word" means really "inside word", not the
+ * definition used by gtk_text_iter_inside_word().
+ */
+static gboolean
+iter_inside_word (const GtkTextIter *iter)
+{
+       GtkTextIter prev;
+
+       if (gtk_text_iter_is_start (iter) || gtk_text_iter_is_end (iter))
+       {
+               return FALSE;
+       }
+
+       prev = *iter;
+       gtk_text_iter_backward_char (&prev);
+
+       return (valid_word_char (gtk_text_iter_get_char (&prev)) &&
+               valid_word_char (gtk_text_iter_get_char (iter)));
+}
+
+/* Checks if @start and @end are well placed for scanning the region between the
+ * two iters.
+ * If an iter isn't well placed, then the library of words will maybe be
+ * inconsistent with the words present in the text buffer.
+ */
+void
+_gtk_source_completion_words_utils_check_scan_region (const GtkTextIter *start,
+                                                     const GtkTextIter *end)
+{
+       g_return_if_fail (gtk_text_iter_compare (start, end) <= 0);
+
+       if (iter_inside_word (start))
+       {
+               g_warning ("Words completion: 'start' iter not well placed.");
+       }
+
+       if (iter_inside_word (end))
+       {
+               g_warning ("Words completion: 'end' iter not well placed.");
+       }
+}
diff --git a/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.h 
b/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.h
index 04b0fc5..cf8f0e1 100644
--- a/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.h
+++ b/gtksourceview/completion-providers/words/gtksourcecompletionwordsutils.h
@@ -38,6 +38,10 @@ G_GNUC_INTERNAL
 void            _gtk_source_completion_words_utils_adjust_region       (GtkTextIter *start,
                                                                         GtkTextIter *end);
 
+G_GNUC_INTERNAL
+void            _gtk_source_completion_words_utils_check_scan_region   (const GtkTextIter *start,
+                                                                        const GtkTextIter *end);
+
 G_END_DECLS
 
 #endif /* __GTK_SOURCE_COMPLETION_WORDS_UTILS_H__ */


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