[gtksourceview] GtkSourceIter, the come back



commit 93f42228976286e2ecea01866d65c662af921732
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Dec 20 15:01:07 2014 +0100

    GtkSourceIter, the come back
    
    Add GtkTextIter functions to implement custom word boundaries. It'll be
    used in gtksourceview.c. It's better to have a separate file with those
    functions so the file can easily be copied in applications, for example
    in gnome-builder (to implement word movements for the Vim mode). It
    maybe makes sense to publish those functions as public, but as a first
    step let's keep that private.

 docs/reference/Makefile.am    |    1 +
 gtksourceview/Makefile.am     |    2 +
 gtksourceview/gtksourceiter.c |  576 +++++++++++++++++++++++++++++++++++++++++
 gtksourceview/gtksourceiter.h |   87 ++++++
 po/POTFILES.in                |    1 +
 tests/Makefile.am             |    3 +
 tests/test-iter.c             |  402 ++++++++++++++++++++++++++++
 7 files changed, 1072 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 6af4fd9..0bdc48a 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -38,6 +38,7 @@ IGNORE_HFILES =                                       \
        gtksourcegutterrendererlines.h          \
        gtksourcegutterrenderermarks.h          \
        gtksourcegutterrenderer-private.h       \
+       gtksourceiter.h                         \
        gtksourcelanguage-private.h             \
        gtksourcemarkssequence.h                \
        gtksourcepixbufhelper.h                 \
diff --git a/gtksourceview/Makefile.am b/gtksourceview/Makefile.am
index 5dd626f..8df429f 100644
--- a/gtksourceview/Makefile.am
+++ b/gtksourceview/Makefile.am
@@ -65,6 +65,7 @@ libgtksourceview_private_headers =            \
        gtksourcegutterrendererlines.h          \
        gtksourcegutterrenderermarks.h          \
        gtksourcegutterrenderer-private.h       \
+       gtksourceiter.h                         \
        gtksourcelanguage-private.h             \
        gtksourcemarkssequence.h                \
        gtksourcepixbufhelper.h                 \
@@ -85,6 +86,7 @@ libgtksourceview_private_c_files =    \
        gtksourceengine.c               \
        gtksourcegutterrendererlines.c  \
        gtksourcegutterrenderermarks.c  \
+       gtksourceiter.c                 \
        gtksourcelanguage-parser-1.c    \
        gtksourcelanguage-parser-2.c    \
        gtksourcemarkssequence.c        \
diff --git a/gtksourceview/gtksourceiter.c b/gtksourceview/gtksourceiter.c
new file mode 100644
index 0000000..76f79ab
--- /dev/null
+++ b/gtksourceview/gtksourceiter.c
@@ -0,0 +1,576 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* gtksourceiter.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+/* GtkTextIter functions. Contains forward/backward functions for word
+ * movements, with custom word boundaries that are used for word selection
+ * (double-click) and cursor movements (Ctrl+left, Ctrl+right, etc).  The
+ * initial idea was to use those word boundaries directly in GTK+, for all text
+ * widgets. But in the end only the GtkTextView::extend-selection signal has
+ * been added to be able to customize the boundaries for double- and
+ * triple-click (the ::move-cursor and ::delete-from-cursor signals were already
+ * present to customize boundaries for cursor movements). The GTK+ developers
+ * didn't want to change the word boundaries for text widgets. More information:
+ * https://mail.gnome.org/archives/gtk-devel-list/2014-September/msg00019.html
+ * https://bugzilla.gnome.org/show_bug.cgi?id=111503
+ */
+
+#include "gtksourceiter.h"
+
+/* Go to the end of the next or current "full word". A full word is a group of
+ * non-blank chars.
+ * In other words, this function is the same as the 'E' Vim command.
+ *
+ * Examples ('|' is the iter position):
+ * "|---- abcd"   -> "----| abcd"
+ * "|  ---- abcd" -> "  ----| abcd"
+ * "--|-- abcd"   -> "----| abcd"
+ * "---- a|bcd"   -> "---- abcd|"
+ */
+void
+_gtk_source_iter_forward_full_word_end (GtkTextIter *iter)
+{
+       GtkTextIter pos;
+       gboolean non_blank_found = FALSE;
+
+       /* It would be better to use gtk_text_iter_forward_visible_char(), but
+        * it doesn't exist. So move by cursor position instead, it should be
+        * equivalent here.
+        */
+
+       pos = *iter;
+
+       while (g_unichar_isspace (gtk_text_iter_get_char (&pos)))
+       {
+               gtk_text_iter_forward_visible_cursor_position (&pos);
+       }
+
+       while (!gtk_text_iter_is_end (&pos) &&
+              !g_unichar_isspace (gtk_text_iter_get_char (&pos)))
+       {
+               non_blank_found = TRUE;
+               gtk_text_iter_forward_visible_cursor_position (&pos);
+       }
+
+       if (non_blank_found)
+       {
+               *iter = pos;
+       }
+}
+
+/* Symmetric of iter_forward_full_word_end(). */
+void
+_gtk_source_iter_backward_full_word_start (GtkTextIter *iter)
+{
+       GtkTextIter pos;
+       GtkTextIter prev;
+       gboolean non_blank_found = FALSE;
+
+       pos = *iter;
+
+       while (!gtk_text_iter_is_start (&pos))
+       {
+               prev = pos;
+               gtk_text_iter_backward_visible_cursor_position (&prev);
+
+               if (!g_unichar_isspace (gtk_text_iter_get_char (&prev)))
+               {
+                       break;
+               }
+
+               pos = prev;
+       }
+
+       while (!gtk_text_iter_is_start (&pos))
+       {
+               prev = pos;
+               gtk_text_iter_backward_visible_cursor_position (&prev);
+
+               if (g_unichar_isspace (gtk_text_iter_get_char (&prev)))
+               {
+                       break;
+               }
+
+               non_blank_found = TRUE;
+               pos = prev;
+       }
+
+       if (non_blank_found)
+       {
+               *iter = pos;
+       }
+}
+
+gboolean
+_gtk_source_iter_starts_full_word (const GtkTextIter *iter)
+{
+       GtkTextIter prev = *iter;
+
+       if (gtk_text_iter_is_end (iter))
+       {
+               return FALSE;
+       }
+
+       if (!gtk_text_iter_backward_visible_cursor_position (&prev))
+       {
+               return !g_unichar_isspace (gtk_text_iter_get_char (iter));
+       }
+
+       return (g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
+               !g_unichar_isspace (gtk_text_iter_get_char (iter)));
+}
+
+gboolean
+_gtk_source_iter_ends_full_word (const GtkTextIter *iter)
+{
+       GtkTextIter prev = *iter;
+
+       if (!gtk_text_iter_backward_visible_cursor_position (&prev))
+       {
+               return FALSE;
+       }
+
+       return (!g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
+               (gtk_text_iter_is_end (iter) ||
+                g_unichar_isspace (gtk_text_iter_get_char (iter))));
+}
+
+/* Extends the definition of a natural-language word used by Pango. The
+ * underscore is added to the possible characters of a natural-language word.
+ */
+void
+_gtk_source_iter_forward_extra_natural_word_end (GtkTextIter *iter)
+{
+       gtk_text_iter_forward_visible_word_end (iter);
+
+       while (TRUE)
+       {
+               if (gtk_text_iter_get_char (iter) == '_')
+               {
+                       gtk_text_iter_forward_visible_cursor_position (iter);
+               }
+               else if (gtk_text_iter_starts_word (iter))
+               {
+                       gtk_text_iter_forward_visible_word_end (iter);
+               }
+               else
+               {
+                       break;
+               }
+       }
+}
+
+/* Symmetric of iter_forward_extra_natural_word_end(). */
+void
+_gtk_source_iter_backward_extra_natural_word_start (GtkTextIter *iter)
+{
+       gtk_text_iter_backward_visible_word_start (iter);
+
+       while (!gtk_text_iter_is_start (iter))
+       {
+               GtkTextIter prev = *iter;
+               gtk_text_iter_backward_visible_cursor_position (&prev);
+
+               if (gtk_text_iter_get_char (&prev) == '_')
+               {
+                       *iter = prev;
+               }
+               else if (gtk_text_iter_ends_word (iter))
+               {
+                       gtk_text_iter_backward_visible_word_start (iter);
+               }
+               else
+               {
+                       break;
+               }
+       }
+}
+
+gboolean
+_gtk_source_iter_starts_extra_natural_word (const GtkTextIter *iter)
+{
+       gboolean starts_word;
+       GtkTextIter prev;
+
+       starts_word = gtk_text_iter_starts_word (iter);
+
+       prev = *iter;
+       if (!gtk_text_iter_backward_visible_cursor_position (&prev))
+       {
+               return starts_word || gtk_text_iter_get_char (iter) == '_';
+       }
+
+       if (starts_word)
+       {
+               return gtk_text_iter_get_char (&prev) != '_';
+       }
+
+       return (gtk_text_iter_get_char (iter) == '_' &&
+               gtk_text_iter_get_char (&prev) != '_' &&
+               !gtk_text_iter_ends_word (iter));
+}
+
+gboolean
+_gtk_source_iter_ends_extra_natural_word (const GtkTextIter *iter)
+{
+       GtkTextIter prev;
+       gboolean ends_word;
+
+       prev = *iter;
+       if (!gtk_text_iter_backward_visible_cursor_position (&prev))
+       {
+               return FALSE;
+       }
+
+       ends_word = gtk_text_iter_ends_word (iter);
+
+       if (gtk_text_iter_is_end (iter))
+       {
+               return ends_word || gtk_text_iter_get_char (&prev) == '_';
+       }
+
+       if (ends_word)
+       {
+               return gtk_text_iter_get_char (iter) != '_';
+       }
+
+       return (gtk_text_iter_get_char (&prev) == '_' &&
+               gtk_text_iter_get_char (iter) != '_' &&
+               !gtk_text_iter_starts_word (iter));
+}
+
+/* Similar to gtk_text_iter_forward_visible_word_end, but with a custom
+ * definition of "word".
+ *
+ * It is normally the same word boundaries as in Vim. This function is the same
+ * as the 'e' command.
+ *
+ * With the custom word definition, a word can be:
+ * - a natural-language word as defined by Pango, plus the underscore. The
+ *   underscore is added because it is often used in programming languages.
+ * - a group of contiguous non-blank characters.
+ */
+gboolean
+_gtk_source_iter_forward_visible_word_end (GtkTextIter *iter)
+{
+       GtkTextIter orig = *iter;
+       GtkTextIter farthest = *iter;
+       GtkTextIter next_word_end = *iter;
+       GtkTextIter word_start;
+
+       /* 'farthest' is the farthest position that this function can return. Example:
+        * "|---- aaaa"  ->  "----| aaaa"
+        */
+       _gtk_source_iter_forward_full_word_end (&farthest);
+
+       /* Go to the next extra-natural word end. It can be farther than
+        * 'farthest':
+        * "|---- aaaa"  ->  "---- aaaa|"
+        *
+        * Or it can remain at the same place:
+        * "aaaa| ----"  ->  "aaaa| ----"
+        */
+       _gtk_source_iter_forward_extra_natural_word_end (&next_word_end);
+
+       if (gtk_text_iter_compare (&farthest, &next_word_end) < 0 ||
+           gtk_text_iter_equal (iter, &next_word_end))
+       {
+               *iter = farthest;
+               goto end;
+       }
+
+       /* From 'next_word_end', go to the previous extra-natural word start.
+        *
+        * Example 1:
+        * iter:          "ab|cd"
+        * next_word_end: "abcd|" -> the good one
+        * word_start:    "|abcd"
+        *
+        * Example 2:
+        * iter:          "| abcd()"
+        * next_word_end: " abcd|()" -> the good one
+        * word_start:    " |abcd()"
+        *
+        * Example 3:
+        * iter:          "abcd|()efgh"
+        * next_word_end: "abcd()efgh|"
+        * word_start:    "abcd()|efgh" -> the good one, at the end of the word "()".
+        */
+       word_start = next_word_end;
+       _gtk_source_iter_backward_extra_natural_word_start (&word_start);
+
+       /* Example 1 */
+       if (gtk_text_iter_compare (&word_start, iter) <= 0)
+       {
+               *iter = next_word_end;
+       }
+
+       /* Example 2 */
+       else if (_gtk_source_iter_starts_full_word (&word_start))
+       {
+               *iter = next_word_end;
+       }
+
+       /* Example 3 */
+       else
+       {
+               *iter = word_start;
+       }
+
+end:
+       return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
+}
+
+/* Symmetric of _gtk_source_iter_forward_visible_word_end(). */
+gboolean
+_gtk_source_iter_backward_visible_word_start (GtkTextIter *iter)
+{
+       GtkTextIter orig = *iter;
+       GtkTextIter farthest = *iter;
+       GtkTextIter prev_word_start = *iter;
+       GtkTextIter word_end;
+
+       /* 'farthest' is the farthest position that this function can return. Example:
+        * "aaaa ----|"  ->  "aaaa |----"
+        */
+       _gtk_source_iter_backward_full_word_start (&farthest);
+
+       /* Go to the previous extra-natural word start. It can be farther than
+        * 'farthest':
+        * "aaaa ----|"  ->  "|aaaa ----"
+        *
+        * Or it can remain at the same place:
+        * "---- |aaaa"  ->  "---- |aaaa"
+        */
+       _gtk_source_iter_backward_extra_natural_word_start (&prev_word_start);
+
+       if (gtk_text_iter_compare (&prev_word_start, &farthest) < 0 ||
+           gtk_text_iter_equal (iter, &prev_word_start))
+       {
+               *iter = farthest;
+               goto end;
+       }
+
+       /* From 'prev_word_start', go to the next extra-natural word end.
+        *
+        * Example 1:
+        * iter:            "ab|cd"
+        * prev_word_start: "|abcd" -> the good one
+        * word_end:        "abcd|"
+        *
+        * Example 2:
+        * iter:            "()abcd |"
+        * prev_word_start: "()|abcd " -> the good one
+        * word_end:        "()abcd| "
+        *
+        * Example 3:
+        * iter:            "abcd()|"
+        * prev_word_start: "|abcd()"
+        * word_end:        "abcd|()" -> the good one, at the start of the word "()".
+        */
+       word_end = prev_word_start;
+       _gtk_source_iter_forward_extra_natural_word_end (&word_end);
+
+       /* Example 1 */
+       if (gtk_text_iter_compare (iter, &word_end) <= 0)
+       {
+               *iter = prev_word_start;
+       }
+
+       /* Example 2 */
+       else if (_gtk_source_iter_ends_full_word (&word_end))
+       {
+               *iter = prev_word_start;
+       }
+
+       /* Example 3 */
+       else
+       {
+               *iter = word_end;
+       }
+
+end:
+       return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
+}
+
+/* Similar to gtk_text_iter_forward_visible_word_ends(). */
+gboolean
+_gtk_source_iter_forward_visible_word_ends (GtkTextIter *iter,
+                                           gint         count)
+{
+       GtkTextIter orig = *iter;
+       gint i;
+
+       if (count < 0)
+       {
+               return _gtk_source_iter_backward_visible_word_starts (iter, -count);
+       }
+
+       for (i = 0; i < count; i++)
+       {
+               if (!_gtk_source_iter_forward_visible_word_end (iter))
+               {
+                       break;
+               }
+       }
+
+       return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
+}
+
+/* Similar to gtk_text_iter_backward_visible_word_starts(). */
+gboolean
+_gtk_source_iter_backward_visible_word_starts (GtkTextIter *iter,
+                                              gint         count)
+{
+       GtkTextIter orig = *iter;
+       gint i;
+
+       if (count < 0)
+       {
+               return _gtk_source_iter_forward_visible_word_ends (iter, -count);
+       }
+
+       for (i = 0; i < count; i++)
+       {
+               if (!_gtk_source_iter_backward_visible_word_start (iter))
+               {
+                       break;
+               }
+       }
+
+       return !gtk_text_iter_equal (&orig, iter) && !gtk_text_iter_is_end (iter);
+}
+
+gboolean
+_gtk_source_iter_starts_word (const GtkTextIter *iter)
+{
+       if (_gtk_source_iter_starts_full_word (iter) ||
+           _gtk_source_iter_starts_extra_natural_word (iter))
+       {
+               return TRUE;
+       }
+
+       /* Example: "abcd|()", at the start of the word "()". */
+       return (!_gtk_source_iter_ends_full_word (iter) &&
+               _gtk_source_iter_ends_extra_natural_word (iter));
+}
+
+gboolean
+_gtk_source_iter_ends_word (const GtkTextIter *iter)
+{
+       if (_gtk_source_iter_ends_full_word (iter) ||
+           _gtk_source_iter_ends_extra_natural_word (iter))
+       {
+               return TRUE;
+       }
+
+       /* Example: "abcd()|efgh", at the end of the word "()". */
+       return (!_gtk_source_iter_starts_full_word (iter) &&
+               _gtk_source_iter_starts_extra_natural_word (iter));
+}
+
+gboolean
+_gtk_source_iter_inside_word (const GtkTextIter *iter)
+{
+       GtkTextIter prev_word_start;
+       GtkTextIter word_end;
+
+       if (_gtk_source_iter_starts_word (iter))
+       {
+               return TRUE;
+       }
+
+       prev_word_start = *iter;
+       if (!_gtk_source_iter_backward_visible_word_start (&prev_word_start))
+       {
+               return FALSE;
+       }
+
+       word_end = prev_word_start;
+       _gtk_source_iter_forward_visible_word_end (&word_end);
+
+       return (gtk_text_iter_compare (&prev_word_start, iter) <= 0 &&
+               gtk_text_iter_compare (iter, &word_end) < 0);
+}
+
+/* Used for the GtkTextView::extend-selection signal. */
+void
+_gtk_source_iter_extend_selection_word (const GtkTextIter *location,
+                                       GtkTextIter       *start,
+                                       GtkTextIter       *end)
+{
+       /* Exactly the same algorithm as in GTK+, but with our custom word
+        * boundaries.
+        */
+       *start = *location;
+       *end = *location;
+
+       if (_gtk_source_iter_inside_word (start))
+       {
+               if (!_gtk_source_iter_starts_word (start))
+               {
+                       _gtk_source_iter_backward_visible_word_start (start);
+               }
+
+               if (!_gtk_source_iter_ends_word (end))
+               {
+                       _gtk_source_iter_forward_visible_word_end (end);
+               }
+       }
+       else
+       {
+               GtkTextIter tmp;
+
+               tmp = *start;
+               if (_gtk_source_iter_backward_visible_word_start (&tmp))
+               {
+                       _gtk_source_iter_forward_visible_word_end (&tmp);
+               }
+
+               if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (start))
+               {
+                       *start = tmp;
+               }
+               else
+               {
+                       gtk_text_iter_set_line_offset (start, 0);
+               }
+
+               tmp = *end;
+               if (!_gtk_source_iter_forward_visible_word_end (&tmp))
+               {
+                       gtk_text_iter_forward_to_end (&tmp);
+               }
+
+               if (_gtk_source_iter_ends_word (&tmp))
+               {
+                       _gtk_source_iter_backward_visible_word_start (&tmp);
+               }
+
+               if (gtk_text_iter_get_line (&tmp) == gtk_text_iter_get_line (end))
+               {
+                       *end = tmp;
+               }
+               else
+               {
+                       gtk_text_iter_forward_to_line_end (end);
+               }
+       }
+}
diff --git a/gtksourceview/gtksourceiter.h b/gtksourceview/gtksourceiter.h
new file mode 100644
index 0000000..df48c5e
--- /dev/null
+++ b/gtksourceview/gtksourceiter.h
@@ -0,0 +1,87 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* gtksourceiter.h
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef __GTK_SOURCE_ITER_H__
+#define __GTK_SOURCE_ITER_H__
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+/* Semi-public functions. */
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_forward_visible_word_end               (GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_forward_visible_word_ends              (GtkTextIter *iter,
+                                                                        gint         count);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_backward_visible_word_start            (GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_backward_visible_word_starts           (GtkTextIter *iter,
+                                                                        gint         count);
+
+G_GNUC_INTERNAL
+void           _gtk_source_iter_extend_selection_word                  (const GtkTextIter *location,
+                                                                        GtkTextIter       *start,
+                                                                        GtkTextIter       *end);
+
+/* Internal functions, in the header for unit tests. */
+
+G_GNUC_INTERNAL
+void           _gtk_source_iter_forward_full_word_end                  (GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+void           _gtk_source_iter_backward_full_word_start               (GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_starts_full_word                       (const GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_ends_full_word                         (const GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+void           _gtk_source_iter_forward_extra_natural_word_end         (GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+void           _gtk_source_iter_backward_extra_natural_word_start      (GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_starts_extra_natural_word              (const GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_ends_extra_natural_word                (const GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_starts_word                            (const GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_ends_word                              (const GtkTextIter *iter);
+
+G_GNUC_INTERNAL
+gboolean       _gtk_source_iter_inside_word                            (const GtkTextIter *iter);
+
+G_END_DECLS
+
+#endif /* __GTK_SOURCE_ITER_H__ */
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ecd97de..b30df07 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -139,6 +139,7 @@ gtksourceview/gtksourcegutter.c
 gtksourceview/gtksourcegutterrenderer.c
 gtksourceview/gtksourcegutterrendererpixbuf.c
 gtksourceview/gtksourcegutterrenderertext.c
+gtksourceview/gtksourceiter.c
 gtksourceview/gtksourcelanguage.c
 gtksourceview/gtksourcelanguagemanager.c
 gtksourceview/gtksourcelanguage-parser-2.c
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a7532a0..e6be5fb 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -87,6 +87,9 @@ test_file_loader_SOURCES = test-file-loader.c
 UNIT_TEST_PROGS += test-file-saver
 test_file_saver_SOURCES = test-file-saver.c
 
+UNIT_TEST_PROGS += test-iter
+test_iter_SOURCES = test-iter.c
+
 UNIT_TEST_PROGS += test-language
 test_language_SOURCES = test-language.c
 
diff --git a/tests/test-iter.c b/tests/test-iter.c
new file mode 100644
index 0000000..9532515
--- /dev/null
+++ b/tests/test-iter.c
@@ -0,0 +1,402 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- */
+/* test-iter.c
+ * This file is part of GtkSourceView
+ *
+ * Copyright (C) 2014 - Sébastien Wilmet <swilmet gnome org>
+ *
+ * GtkSourceView is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * GtkSourceView is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "gtksourceview/gtksourceiter.h"
+
+static void
+check_full_word_boundaries (gboolean     forward,
+                           const gchar *buffer_text,
+                           gint         initial_offset,
+                           gint         result_offset)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset);
+
+       if (forward)
+       {
+               _gtk_source_iter_forward_full_word_end (&iter);
+       }
+       else
+       {
+               _gtk_source_iter_backward_full_word_start (&iter);
+       }
+
+       g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_forward_full_word_end (void)
+{
+       check_full_word_boundaries (TRUE, "  ---- abcd ", 2, 6);
+       check_full_word_boundaries (TRUE, "  ---- abcd ", 0, 6);
+       check_full_word_boundaries (TRUE, "  ---- abcd ", 4, 6);
+       check_full_word_boundaries (TRUE, "  ---- abcd ", 8, 11);
+       check_full_word_boundaries (TRUE, "  ---- abcd ", 11, 11);
+       check_full_word_boundaries (TRUE, "  ---- abcd \n  ----", 11, 19);
+}
+
+static void
+test_backward_full_word_start (void)
+{
+       check_full_word_boundaries (FALSE, "---- abcd  ", 9, 5);
+       check_full_word_boundaries (FALSE, "---- abcd  ", 11, 5);
+       check_full_word_boundaries (FALSE, "---- abcd  ", 7, 5);
+       check_full_word_boundaries (FALSE, "---- abcd  ", 3, 0);
+       check_full_word_boundaries (FALSE, " ---- abcd  ", 1, 1);
+       check_full_word_boundaries (FALSE, "abcd \n ---- abcd  ", 7, 0);
+}
+
+static void
+test_starts_full_word (void)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, "foo--- ---bar", -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
+       g_assert (_gtk_source_iter_starts_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 1);
+       g_assert (!_gtk_source_iter_starts_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 7);
+       g_assert (_gtk_source_iter_starts_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 10);
+       g_assert (!_gtk_source_iter_starts_full_word (&iter));
+
+       gtk_text_buffer_set_text (buffer, " ab ", -1);
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
+       g_assert (!_gtk_source_iter_starts_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 4);
+       g_assert (!_gtk_source_iter_starts_full_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_ends_full_word (void)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, "foo--- ---bar ", -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 14);
+       g_assert (!_gtk_source_iter_ends_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 13);
+       g_assert (_gtk_source_iter_ends_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 12);
+       g_assert (!_gtk_source_iter_ends_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 6);
+       g_assert (_gtk_source_iter_ends_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 3);
+       g_assert (!_gtk_source_iter_ends_full_word (&iter));
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
+       g_assert (!_gtk_source_iter_ends_full_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+check_extra_natural_word_boundaries (gboolean     forward,
+                                    const gchar *buffer_text,
+                                    gint         initial_offset,
+                                    gint         result_offset)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset);
+
+       if (forward)
+       {
+               _gtk_source_iter_forward_extra_natural_word_end (&iter);
+       }
+       else
+       {
+               _gtk_source_iter_backward_extra_natural_word_start (&iter);
+       }
+
+       g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_forward_extra_natural_word_end (void)
+{
+       const gchar *str = "hello_world ---- blah";
+
+       check_extra_natural_word_boundaries (TRUE, str, 0, 11);
+       check_extra_natural_word_boundaries (TRUE, str, 1, 11);
+       check_extra_natural_word_boundaries (TRUE, str, 5, 11);
+       check_extra_natural_word_boundaries (TRUE, str, 6, 11);
+       check_extra_natural_word_boundaries (TRUE, str, 11, 21);
+       check_extra_natural_word_boundaries (TRUE, str, 21, 21);
+
+       check_extra_natural_word_boundaries (TRUE, "ab ", 2, 2);
+       check_extra_natural_word_boundaries (TRUE, "a_ ", 2, 2);
+       check_extra_natural_word_boundaries (TRUE, "ab \ncd", 2, 6);
+       check_extra_natural_word_boundaries (TRUE, "a_ \n_d", 2, 6);
+}
+
+static void
+test_backward_extra_natural_word_start (void)
+{
+       const gchar *str = "hello_world ---- blah";
+
+       check_extra_natural_word_boundaries (FALSE, str, 21, 17);
+       check_extra_natural_word_boundaries (FALSE, str, 20, 17);
+       check_extra_natural_word_boundaries (FALSE, str, 17, 0);
+       check_extra_natural_word_boundaries (FALSE, str, 11, 0);
+       check_extra_natural_word_boundaries (FALSE, str, 6, 0);
+       check_extra_natural_word_boundaries (FALSE, str, 5, 0);
+       check_extra_natural_word_boundaries (FALSE, str, 0, 0);
+
+       check_extra_natural_word_boundaries (FALSE, " cd", 1, 1);
+       check_extra_natural_word_boundaries (FALSE, " _d", 1, 1);
+       check_extra_natural_word_boundaries (FALSE, "ab\n cd", 4, 0);
+       check_extra_natural_word_boundaries (FALSE, "_b\n c_", 4, 0);
+}
+
+static void
+check_starts_extra_natural_word (const gchar *buffer_text,
+                                gint         offset,
+                                gboolean     starts)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
+       g_assert_cmpint (starts, ==, _gtk_source_iter_starts_extra_natural_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_starts_extra_natural_word (void)
+{
+       check_starts_extra_natural_word ("ab", 2, FALSE);
+       check_starts_extra_natural_word ("hello", 0, TRUE);
+       check_starts_extra_natural_word ("__", 0, TRUE);
+       check_starts_extra_natural_word (" hello", 0, FALSE);
+       check_starts_extra_natural_word (" hello", 1, TRUE);
+       check_starts_extra_natural_word ("_hello", 1, FALSE);
+       check_starts_extra_natural_word ("()", 1, FALSE);
+       check_starts_extra_natural_word ("__", 1, FALSE);
+       check_starts_extra_natural_word (" __", 1, TRUE);
+       check_starts_extra_natural_word (" __hello", 1, TRUE);
+       check_starts_extra_natural_word ("hello_", 5, FALSE);
+}
+
+static void
+check_ends_extra_natural_word (const gchar *buffer_text,
+                              gint         offset,
+                              gboolean     ends)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
+       g_assert_cmpint (ends, ==, _gtk_source_iter_ends_extra_natural_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_ends_extra_natural_word (void)
+{
+       check_ends_extra_natural_word ("ab", 0, FALSE);
+       check_ends_extra_natural_word ("ab", 2, TRUE);
+       check_ends_extra_natural_word ("__", 2, TRUE);
+       check_ends_extra_natural_word ("ab ", 3, FALSE);
+       check_ends_extra_natural_word ("ab ", 2, TRUE);
+       check_ends_extra_natural_word ("ab_", 2, FALSE);
+       check_ends_extra_natural_word ("()", 1, FALSE);
+       check_ends_extra_natural_word ("__ ", 1, FALSE);
+       check_ends_extra_natural_word ("__ab ", 2, FALSE);
+       check_ends_extra_natural_word ("__ ", 2, TRUE);
+}
+
+static void
+check_word_boundaries (const gchar *buffer_text,
+                      gint         offset,
+                      gboolean     starts_word_result,
+                      gboolean     ends_word_result,
+                      gboolean     inside_word_result)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, offset);
+
+       g_assert_cmpint (starts_word_result, ==, _gtk_source_iter_starts_word (&iter));
+       g_assert_cmpint (ends_word_result, ==, _gtk_source_iter_ends_word (&iter));
+       g_assert_cmpint (inside_word_result, ==, _gtk_source_iter_inside_word (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_word_boundaries (void)
+{
+       check_word_boundaries ("ab()cd", 0, TRUE, FALSE, TRUE);
+       check_word_boundaries ("ab()cd", 1, FALSE, FALSE, TRUE);
+       check_word_boundaries ("ab()cd", 2, TRUE, TRUE, TRUE);
+       check_word_boundaries ("ab()cd", 3, FALSE, FALSE, TRUE);
+       check_word_boundaries ("ab()cd", 4, TRUE, TRUE, TRUE);
+       check_word_boundaries ("ab()cd", 5, FALSE, FALSE, TRUE);
+       check_word_boundaries ("ab()cd", 6, FALSE, TRUE, FALSE);
+
+       check_word_boundaries (" ab", 0, FALSE, FALSE, FALSE);
+       check_word_boundaries ("ab ", 3, FALSE, FALSE, FALSE);
+
+       check_word_boundaries (" () ", 1, TRUE, FALSE, TRUE);
+       check_word_boundaries (" () ", 3, FALSE, TRUE, FALSE);
+}
+
+static void
+check_word_boundaries_movement (gboolean     forward,
+                               const gchar *buffer_text,
+                               gint         initial_offset,
+                               gint         result_offset,
+                               gboolean     ret)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       buffer = gtk_text_buffer_new (NULL);
+       gtk_text_buffer_set_text (buffer, buffer_text, -1);
+
+       gtk_text_buffer_get_iter_at_offset (buffer, &iter, initial_offset);
+
+       if (forward)
+       {
+               g_assert_cmpint (ret, ==, _gtk_source_iter_forward_visible_word_end (&iter));
+       }
+       else
+       {
+               g_assert_cmpint (ret, ==, _gtk_source_iter_backward_visible_word_start (&iter));
+       }
+
+       g_assert_cmpint (result_offset, ==, gtk_text_iter_get_offset (&iter));
+
+       g_object_unref (buffer);
+}
+
+static void
+test_forward_word_end (void)
+{
+       check_word_boundaries_movement (TRUE, "---- aaaa", 0, 4, TRUE);
+       check_word_boundaries_movement (TRUE, "---- aaaa", 1, 4, TRUE);
+       check_word_boundaries_movement (TRUE, "---- aaaa", 4, 9, FALSE);
+       check_word_boundaries_movement (TRUE, "---- aaaa", 5, 9, FALSE);
+       check_word_boundaries_movement (TRUE, "---- aaaa", 6, 9, FALSE);
+       check_word_boundaries_movement (TRUE, "aaaa ----", 0, 4, TRUE);
+       check_word_boundaries_movement (TRUE, "aaaa ----", 1, 4, TRUE);
+       check_word_boundaries_movement (TRUE, "aaaa ----", 4, 9, FALSE);
+       check_word_boundaries_movement (TRUE, "aaaa ----", 5, 9, FALSE);
+       check_word_boundaries_movement (TRUE, "aaaa ----", 6, 9, FALSE);
+
+       check_word_boundaries_movement (TRUE, "abcd", 2, 4, FALSE);
+       check_word_boundaries_movement (TRUE, "abcd ", 2, 4, TRUE);
+       check_word_boundaries_movement (TRUE, " abcd()", 0, 5, TRUE);
+       check_word_boundaries_movement (TRUE, "abcd()efgh", 4, 6, TRUE);
+
+       check_word_boundaries_movement (TRUE, "ab ", 2, 2, FALSE);
+       check_word_boundaries_movement (TRUE, "ab \n", 2, 2, FALSE);
+       check_word_boundaries_movement (TRUE, "ab \ncd", 2, 6, FALSE);
+}
+
+static void
+test_backward_word_start (void)
+{
+       check_word_boundaries_movement (FALSE, "aaaa ----", 9, 5, TRUE);
+       check_word_boundaries_movement (FALSE, "aaaa ----", 8, 5, TRUE);
+       check_word_boundaries_movement (FALSE, "aaaa ----", 5, 0, TRUE);
+       check_word_boundaries_movement (FALSE, "aaaa ----", 4, 0, TRUE);
+       check_word_boundaries_movement (FALSE, "aaaa ----", 3, 0, TRUE);
+       check_word_boundaries_movement (FALSE, "---- aaaa", 9, 5, TRUE);
+       check_word_boundaries_movement (FALSE, "---- aaaa", 8, 5, TRUE);
+       check_word_boundaries_movement (FALSE, "---- aaaa", 5, 0, TRUE);
+       check_word_boundaries_movement (FALSE, "---- aaaa", 4, 0, TRUE);
+       check_word_boundaries_movement (FALSE, "---- aaaa", 3, 0, TRUE);
+
+       check_word_boundaries_movement (FALSE, "abcd", 2, 0, TRUE);
+       check_word_boundaries_movement (FALSE, "()abcd ", 7, 2, TRUE);
+       check_word_boundaries_movement (FALSE, "abcd()", 6, 4, TRUE);
+       check_word_boundaries_movement (FALSE, "abcd()", 0, 0, FALSE);
+
+       check_word_boundaries_movement (FALSE, " cd", 1, 1, FALSE);
+       check_word_boundaries_movement (FALSE, "\n cd", 2, 2, FALSE);
+       check_word_boundaries_movement (FALSE, "ab\n cd", 4, 0, TRUE);
+}
+
+int
+main (int argc, char **argv)
+{
+       gtk_test_init (&argc, &argv);
+
+       g_test_add_func ("/Iter/full-word/forward", test_forward_full_word_end);
+       g_test_add_func ("/Iter/full-word/backward", test_backward_full_word_start);
+       g_test_add_func ("/Iter/full-word/starts", test_starts_full_word);
+       g_test_add_func ("/Iter/full-word/ends", test_ends_full_word);
+
+       g_test_add_func ("/Iter/extra-natural-word/forward", test_forward_extra_natural_word_end);
+       g_test_add_func ("/Iter/extra-natural-word/backward", test_backward_extra_natural_word_start);
+       g_test_add_func ("/Iter/extra-natural-word/starts", test_starts_extra_natural_word);
+       g_test_add_func ("/Iter/extra-natural-word/ends", test_ends_extra_natural_word);
+
+       g_test_add_func ("/Iter/custom-word/boundaries", test_word_boundaries);
+       g_test_add_func ("/Iter/custom-word/forward", test_forward_word_end);
+       g_test_add_func ("/Iter/custom-word/backward", test_backward_word_start);
+
+       return g_test_run ();
+}


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