[gnome-builder] snippets: use gtk_text_iter_backward_find_char()



commit 54119a4d44706c64c327fc0b3907b70e516552ec
Author: Christian Hergert <christian hergert me>
Date:   Thu Sep 11 14:31:10 2014 -0700

    snippets: use gtk_text_iter_backward_find_char()
    
    This clean's up our traversal backwards to find a stop character.
    Much simpler to follow. It also fixes a bug where if inserting a
    snippet like `gpl' on the second line, it would destructively destroy
    the first line.

 .../gb-source-snippet-completion-provider.c        |   29 +++++++-------------
 1 files changed, 10 insertions(+), 19 deletions(-)
---
diff --git a/src/snippets/gb-source-snippet-completion-provider.c 
b/src/snippets/gb-source-snippet-completion-provider.c
index bfbdc96..a6a7601 100644
--- a/src/snippets/gb-source-snippet-completion-provider.c
+++ b/src/snippets/gb-source-snippet-completion-provider.c
@@ -192,9 +192,10 @@ gb_source_snippet_completion_provider_init (GbSourceSnippetCompletionProvider *p
 }
 
 static gboolean
-is_stop_char (gunichar c)
+stop_on_predicate (gunichar ch,
+                   gpointer data)
 {
-  switch (c)
+  switch (ch)
     {
     case '_':
       return FALSE;
@@ -215,7 +216,7 @@ is_stop_char (gunichar c)
       return TRUE;
 
     default:
-      return !g_unichar_isalnum (c);
+      return !g_unichar_isalnum (ch);
     }
 }
 
@@ -223,28 +224,18 @@ static gchar *
 get_word (GtkSourceCompletionProvider *provider,
           GtkTextIter                 *iter)
 {
+  GtkTextBuffer *buffer;
   GtkTextIter end;
-  gboolean moved = FALSE;
-  gunichar c;
-  gchar *word;
 
   gtk_text_iter_assign (&end, iter);
+  buffer = gtk_text_iter_get_buffer (iter);
 
-  do
-    {
-      if (!gtk_text_iter_backward_char (iter))
-        break;
-      c = gtk_text_iter_get_char (iter);
-      moved = TRUE;
-    }
-  while (!is_stop_char (c));
-
-  if (moved && !gtk_text_iter_is_start (iter))
-    gtk_text_iter_forward_char (iter);
+  if (!gtk_text_iter_backward_find_char (iter, stop_on_predicate, NULL, NULL))
+    return gtk_text_buffer_get_text (buffer, iter, &end, TRUE);
 
-  word = g_strstrip (gtk_text_iter_get_text (iter, &end));
+  gtk_text_iter_forward_char (iter);
 
-  return word;
+  return gtk_text_iter_get_text (iter, &end);
 }
 
 static GdkPixbuf *


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