[gnome-builder] GbSourceView: special insert workaround for double quotes.



commit 417ce19a7f57ea230d992481dcbb601b82c99424
Author: Christian Hergert <christian hergert me>
Date:   Fri Dec 19 17:21:54 2014 -0800

    GbSourceView: special insert workaround for double quotes.
    
    Just be a bit smarter about inserting double quotes by determining if we
    are inserting the closing quote here.

 src/editor/gb-source-view.c |   45 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 45 insertions(+), 0 deletions(-)
---
diff --git a/src/editor/gb-source-view.c b/src/editor/gb-source-view.c
index 0c7e335..a98d9e5 100644
--- a/src/editor/gb-source-view.c
+++ b/src/editor/gb-source-view.c
@@ -1352,6 +1352,33 @@ is_closing_char (gunichar ch)
     }
 }
 
+static guint
+count_chars_on_line (GbSourceView      *view,
+                     gunichar           expected_char,
+                     const GtkTextIter *iter)
+{
+  GtkTextIter cur;
+  guint count = 0;
+
+  g_return_val_if_fail (GB_IS_SOURCE_VIEW (view), 0);
+  g_return_val_if_fail (iter, 0);
+
+  cur = *iter;
+
+  gtk_text_iter_set_line_offset (&cur, 0);
+
+  while (!gtk_text_iter_ends_line (&cur))
+    {
+      gunichar ch;
+
+      ch = gtk_text_iter_get_char (&cur);
+      count += (ch == expected_char);
+      gtk_text_iter_forward_char (&cur);
+    }
+
+  return count;
+}
+
 static gboolean
 gb_source_view_maybe_insert_match (GbSourceView *view,
                                    GdkEventKey  *event)
@@ -1419,6 +1446,8 @@ gb_source_view_maybe_insert_match (GbSourceView *view,
    *  - We are at EOF
    *  - The next character is whitespace
    *  - The next character is a closing brace.
+   *  - If the char is ", then there must be an even number already on
+   *    the current line.
    */
 
   next_iter = iter;
@@ -1427,6 +1456,22 @@ gb_source_view_maybe_insert_match (GbSourceView *view,
 
   if (!next_ch || g_unichar_isspace (next_ch) || is_closing_char (next_ch))
     {
+      /*
+       * Special case for working with double quotes.
+       *
+       * Ignore double quote if we just added enough to make there be an
+       * even number on this line. However, if it was the first quote on
+       * the line, we still need to include a second.
+       */
+      if (ch == '"')
+        {
+          guint count;
+
+          count = count_chars_on_line (view, '"', &iter);
+          if ((count > 1) && ((count % 2) == 0))
+            return FALSE;
+        }
+
       gtk_text_buffer_insert_at_cursor (buffer, &ch, 1);
       gtk_text_buffer_get_iter_at_mark (buffer, &iter, insert);
       gtk_text_iter_backward_char (&iter);


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