[gtksourceview] iter: add leading/trailing spaces utils functions



commit a83bc705e29f21e900f8e953a9429d342ec1350a
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Jul 22 21:52:24 2016 +0200

    iter: add leading/trailing spaces utils functions
    
    Have two separate functions, to have clearer names.

 gtksourceview/gtksourceiter.c        |   60 +++++++++++++++++++++++++++++++
 gtksourceview/gtksourceiter.h        |    8 ++++
 gtksourceview/gtksourcespacedrawer.c |   60 +++-----------------------------
 gtksourceview/gtksourceview.c        |   64 +++------------------------------
 4 files changed, 79 insertions(+), 113 deletions(-)
---
diff --git a/gtksourceview/gtksourceiter.c b/gtksourceview/gtksourceiter.c
index b38db4d..c7ac287 100644
--- a/gtksourceview/gtksourceiter.c
+++ b/gtksourceview/gtksourceiter.c
@@ -636,3 +636,63 @@ _gtk_source_iter_extend_selection_word (const GtkTextIter *location,
                }
        }
 }
+
+/* Get the boundary, on @iter's line, between leading spaces (indentation) and
+ * the text.
+ */
+void
+_gtk_source_iter_get_leading_spaces_end_boundary (const GtkTextIter *iter,
+                                                 GtkTextIter       *leading_end)
+{
+       g_return_if_fail (iter != NULL);
+       g_return_if_fail (leading_end != NULL);
+
+       *leading_end = *iter;
+       gtk_text_iter_set_line_offset (leading_end, 0);
+
+       while (!gtk_text_iter_is_end (leading_end))
+       {
+               gunichar ch = gtk_text_iter_get_char (leading_end);
+
+               if (!g_unichar_isspace (ch))
+               {
+                       break;
+               }
+
+               gtk_text_iter_forward_char (leading_end);
+       }
+}
+
+/* Get the boundary, on @iter's line, between the end of the text and trailing
+ * spaces.
+ */
+void
+_gtk_source_iter_get_trailing_spaces_start_boundary (const GtkTextIter *iter,
+                                                    GtkTextIter       *trailing_start)
+{
+       g_return_if_fail (iter != NULL);
+       g_return_if_fail (trailing_start != NULL);
+
+       *trailing_start = *iter;
+       if (!gtk_text_iter_ends_line (trailing_start))
+       {
+               gtk_text_iter_forward_to_line_end (trailing_start);
+       }
+
+       while (!gtk_text_iter_starts_line (trailing_start))
+       {
+               GtkTextIter prev;
+               gunichar ch;
+
+               prev = *trailing_start;
+               gtk_text_iter_backward_char (&prev);
+
+               ch = gtk_text_iter_get_char (&prev);
+               if (!g_unichar_isspace (ch))
+               {
+                       break;
+               }
+
+               *trailing_start = prev;
+       }
+}
diff --git a/gtksourceview/gtksourceiter.h b/gtksourceview/gtksourceiter.h
index fe824f7..6ce804d 100644
--- a/gtksourceview/gtksourceiter.h
+++ b/gtksourceview/gtksourceiter.h
@@ -56,6 +56,14 @@ GTK_SOURCE_INTERNAL
 gboolean       _gtk_source_iter_ends_extra_natural_word                (const GtkTextIter *iter,
                                                                         gboolean           visible);
 
+GTK_SOURCE_INTERNAL
+void           _gtk_source_iter_get_leading_spaces_end_boundary        (const GtkTextIter *iter,
+                                                                        GtkTextIter       *leading_end);
+
+GTK_SOURCE_INTERNAL
+void           _gtk_source_iter_get_trailing_spaces_start_boundary     (const GtkTextIter *iter,
+                                                                        GtkTextIter       *trailing_start);
+
 /* Internal functions, in the header for unit tests. */
 
 GTK_SOURCE_INTERNAL
diff --git a/gtksourceview/gtksourcespacedrawer.c b/gtksourceview/gtksourcespacedrawer.c
index 6c4aed8..afd6be2 100644
--- a/gtksourceview/gtksourcespacedrawer.c
+++ b/gtksourceview/gtksourcespacedrawer.c
@@ -26,6 +26,7 @@
 
 #include "gtksourcespacedrawer.h"
 #include "gtksourcebuffer.h"
+#include "gtksourceiter.h"
 #include "gtksourcestylescheme.h"
 #include "gtksourcetag.h"
 
@@ -469,59 +470,6 @@ space_needs_drawing (GtkSourceSpaceDrawer *drawer,
                 gtk_text_iter_ends_line (iter) && !gtk_text_iter_is_end (iter)));
 }
 
-/* TODO share with gtksourceview.c -> private utils function. */
-static void
-get_leading_trailing (const GtkTextIter *iter,
-                     GtkTextIter       *leading,
-                     GtkTextIter       *trailing)
-{
-       /* Find end of leading whitespaces */
-       if (leading != NULL)
-       {
-               *leading = *iter;
-               gtk_text_iter_set_line_offset (leading, 0);
-
-               while (!gtk_text_iter_is_end (leading))
-               {
-                       gunichar ch = gtk_text_iter_get_char (leading);
-
-                       if (!g_unichar_isspace (ch))
-                       {
-                               break;
-                       }
-
-                       gtk_text_iter_forward_char (leading);
-               }
-       }
-
-       /* Find start of trailing whitespaces */
-       if (trailing != NULL)
-       {
-               *trailing = *iter;
-               if (!gtk_text_iter_ends_line (trailing))
-               {
-                       gtk_text_iter_forward_to_line_end (trailing);
-               }
-
-               while (!gtk_text_iter_starts_line (trailing))
-               {
-                       GtkTextIter prev;
-                       gunichar ch;
-
-                       prev = *trailing;
-                       gtk_text_iter_backward_char (&prev);
-
-                       ch = gtk_text_iter_get_char (&prev);
-                       if (!g_unichar_isspace (ch))
-                       {
-                               break;
-                       }
-
-                       *trailing = prev;
-               }
-       }
-}
-
 static void
 get_end_iter (GtkTextView *text_view,
              GtkTextIter *start_iter,
@@ -647,7 +595,8 @@ _gtk_source_space_drawer_draw (GtkSourceSpaceDrawer *drawer,
        cairo_set_line_width (cr, 0.8);
        cairo_translate (cr, -0.5, -0.5);
 
-       get_leading_trailing (&s, &leading, &trailing);
+       _gtk_source_iter_get_leading_spaces_end_boundary (&s, &leading);
+       _gtk_source_iter_get_trailing_spaces_start_boundary (&s, &trailing);
        get_end_iter (text_view, &s, &lineend, x2, y2, is_wrapping);
 
        while (TRUE)
@@ -692,7 +641,8 @@ _gtk_source_space_drawer_draw (GtkSourceSpaceDrawer *drawer,
                                gtk_text_iter_backward_char (&s);
                        }
 
-                       get_leading_trailing (&s, &leading, &trailing);
+                       _gtk_source_iter_get_leading_spaces_end_boundary (&s, &leading);
+                       _gtk_source_iter_get_trailing_spaces_start_boundary (&s, &trailing);
                        get_end_iter (text_view, &s, &lineend,
                                      x2, y2, is_wrapping);
                }
diff --git a/gtksourceview/gtksourceview.c b/gtksourceview/gtksourceview.c
index cf9f517..96a9a64 100644
--- a/gtksourceview/gtksourceview.c
+++ b/gtksourceview/gtksourceview.c
@@ -2393,58 +2393,6 @@ gtk_source_view_paint_marks_background (GtkSourceView *view,
 }
 
 static void
-get_leading_trailing (const GtkTextIter *iter,
-                     GtkTextIter       *leading,
-                     GtkTextIter       *trailing)
-{
-       /* Find end of leading whitespaces */
-       if (leading != NULL)
-       {
-               *leading = *iter;
-               gtk_text_iter_set_line_offset (leading, 0);
-
-               while (!gtk_text_iter_is_end (leading))
-               {
-                       gunichar ch = gtk_text_iter_get_char (leading);
-
-                       if (!g_unichar_isspace (ch))
-                       {
-                               break;
-                       }
-
-                       gtk_text_iter_forward_char (leading);
-               }
-       }
-
-       /* Find start of trailing whitespaces */
-       if (trailing != NULL)
-       {
-               *trailing = *iter;
-               if (!gtk_text_iter_ends_line (trailing))
-               {
-                       gtk_text_iter_forward_to_line_end (trailing);
-               }
-
-               while (!gtk_text_iter_starts_line (trailing))
-               {
-                       GtkTextIter prev;
-                       gunichar ch;
-
-                       prev = *trailing;
-                       gtk_text_iter_backward_char (&prev);
-
-                       ch = gtk_text_iter_get_char (&prev);
-                       if (!g_unichar_isspace (ch))
-                       {
-                               break;
-                       }
-
-                       *trailing = prev;
-               }
-       }
-}
-
-static void
 gtk_source_view_paint_right_margin (GtkSourceView *view,
                                    cairo_t       *cr)
 {
@@ -3774,7 +3722,7 @@ do_smart_backspace (GtkSourceView *view)
        gboolean default_editable;
        GtkTextIter insert;
        GtkTextIter end;
-       GtkTextIter leading;
+       GtkTextIter leading_end;
        guint visual_column;
        gint indent_width;
 
@@ -3787,8 +3735,8 @@ do_smart_backspace (GtkSourceView *view)
        }
 
        /* If the line isn't empty up to our cursor, ignore. */
-       get_leading_trailing (&insert, &leading, NULL);
-       if (gtk_text_iter_compare (&leading, &insert) < 0)
+       _gtk_source_iter_get_leading_spaces_end_boundary (&insert, &leading_end);
+       if (gtk_text_iter_compare (&leading_end, &insert) < 0)
        {
                return FALSE;
        }
@@ -3845,7 +3793,7 @@ do_ctrl_backspace (GtkSourceView *view)
        GtkTextBuffer *buffer;
        GtkTextIter insert;
        GtkTextIter end;
-       GtkTextIter leading;
+       GtkTextIter leading_end;
        gboolean default_editable;
 
        buffer = GTK_TEXT_BUFFER (view->priv->source_buffer);
@@ -3871,8 +3819,8 @@ do_ctrl_backspace (GtkSourceView *view)
        /* If only leading whitespaces are on the left of the cursor, delete up
         * to the zero position.
         */
-       get_leading_trailing (&insert, &leading, NULL);
-       if (gtk_text_iter_compare (&insert, &leading) <= 0)
+       _gtk_source_iter_get_leading_spaces_end_boundary (&insert, &leading_end);
+       if (gtk_text_iter_compare (&insert, &leading_end) <= 0)
        {
                gtk_text_iter_set_line_offset (&insert, 0);
                gtk_text_buffer_delete_interactive (buffer, &insert, &end, default_editable);


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