[gtksourceview/wip/custom-word-boundaries-2] GtkSourceIter, the come back
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview/wip/custom-word-boundaries-2] GtkSourceIter, the come back
- Date: Sun, 21 Dec 2014 12:15:41 +0000 (UTC)
commit 2f21e4cfeda1ae605be1d215692147f6eb33384f
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
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 | 607 +++++++++++++++++++++++++++++++++++++++++
gtksourceview/gtksourceiter.h | 41 +++
po/POTFILES.in | 1 +
tests/Makefile.am | 3 +
tests/test-iter.c | 83 ++++++
7 files changed, 738 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..a87856d
--- /dev/null
+++ b/gtksourceview/gtksourceiter.c
@@ -0,0 +1,607 @@
+/* -*- 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
+ */
+
+/* Private 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|"
+ */
+static void
+forward_full_word_end (GtkTextIter *iter)
+{
+ /* 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.
+ */
+
+ while (g_unichar_isspace (gtk_text_iter_get_char (iter)))
+ {
+ gtk_text_iter_forward_visible_cursor_position (iter);
+ }
+
+ while (!gtk_text_iter_is_end (iter) &&
+ !g_unichar_isspace (gtk_text_iter_get_char (iter)))
+ {
+ gtk_text_iter_forward_visible_cursor_position (iter);
+ }
+}
+
+/* Symmetric of iter_forward_full_word_end(). */
+static void
+backward_full_word_start (GtkTextIter *iter)
+{
+ GtkTextIter prev;
+
+ while (!gtk_text_iter_is_start (iter))
+ {
+ prev = *iter;
+ gtk_text_iter_backward_visible_cursor_position (&prev);
+
+ if (!g_unichar_isspace (gtk_text_iter_get_char (&prev)))
+ {
+ break;
+ }
+
+ *iter = prev;
+ }
+
+ while (!gtk_text_iter_is_start (iter))
+ {
+ prev = *iter;
+ gtk_text_iter_backward_visible_cursor_position (&prev);
+
+ if (g_unichar_isspace (gtk_text_iter_get_char (&prev)))
+ {
+ break;
+ }
+
+ *iter = prev;
+ }
+}
+
+static gboolean
+starts_full_word (const GtkTextIter *iter)
+{
+ GtkTextIter prev = *iter;
+
+ if (!gtk_text_iter_backward_visible_cursor_position (&prev))
+ {
+ return TRUE;
+ }
+
+ return (g_unichar_isspace (gtk_text_iter_get_char (&prev)) &&
+ !g_unichar_isspace (gtk_text_iter_get_char (iter)));
+}
+
+static gboolean
+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.
+ */
+static void
+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(). */
+static void
+backward_extra_natural_word_start (GtkTextIter *iter)
+{
+ if (!gtk_text_iter_backward_visible_word_start (iter))
+ {
+ gtk_text_iter_set_offset (iter, 0);
+ }
+
+ 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;
+ }
+ }
+}
+
+static gboolean
+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;
+ }
+
+ 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));
+}
+
+static gboolean
+ends_extra_natural_word (const GtkTextIter *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 definied 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"
+ */
+ forward_full_word_end (&farthest);
+
+ /* Go to the next extra-natural word end. It can be farther than 'farthest'.
+ * Example:
+ * "|---- aaaa" -> "---- aaaa|"
+ */
+ forward_extra_natural_word_end (&next_word_end);
+
+ if (gtk_text_iter_compare (&farthest, &next_word_end) < 0)
+ {
+ *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|()efgk"
+ * next_word_end: "abcd()efgk|"
+ * word_start: "abcd()|efgk" -> the good one, at the end of the word "()".
+ */
+ word_start = next_word_end;
+ 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 (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 iter_forward_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 |----"
+ */
+ backward_full_word_start (&farthest);
+
+ /* Go to the previous extra-natural word start. It can be farther than 'farthest'.
+ * Example:
+ * "aaaa ----|" -> "|aaaa ----"
+ */
+ backward_extra_natural_word_start (&prev_word_start);
+
+ if (gtk_text_iter_compare (&prev_word_start, &farthest) < 0)
+ {
+ *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;
+ 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 (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);
+}
+
+static gboolean
+starts_word (const GtkTextIter *iter)
+{
+ return FALSE;
+}
+
+static gboolean
+ends_word (const GtkTextIter *iter)
+{
+ return FALSE;
+}
+
+static gboolean
+inside_word (const GtkTextIter *iter)
+{
+ return FALSE;
+}
+
+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 (inside_word (start))
+ {
+ if (!starts_word (start))
+ {
+ _gtk_source_iter_backward_visible_word_start (start);
+ }
+
+ if (!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 (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);
+ }
+ }
+}
+
+/* Unit tests */
+
+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)
+ {
+ forward_full_word_end (&iter);
+ }
+ else
+ {
+ 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, 12);
+ 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, 0);
+ 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 (starts_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 1);
+ g_assert (!starts_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 7);
+ g_assert (starts_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 10);
+ g_assert (!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, 13);
+ g_assert (ends_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 12);
+ g_assert (!ends_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 6);
+ g_assert (ends_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 3);
+ g_assert (!ends_full_word (&iter));
+
+ gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
+ g_assert (!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)
+ {
+ forward_extra_natural_word_end (&iter);
+ }
+ else
+ {
+ 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);
+}
+
+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);
+}
+
+void
+_gtk_source_iter_run_internal_unit_tests (void)
+{
+ test_forward_full_word_end ();
+ test_backward_full_word_start ();
+ test_starts_full_word ();
+ test_ends_full_word ();
+ test_forward_extra_natural_word_end ();
+ test_backward_extra_natural_word_start ();
+}
diff --git a/gtksourceview/gtksourceiter.h b/gtksourceview/gtksourceiter.h
new file mode 100644
index 0000000..cb6544d
--- /dev/null
+++ b/gtksourceview/gtksourceiter.h
@@ -0,0 +1,41 @@
+/* -*- 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
+
+gboolean _gtk_source_iter_forward_visible_word_end (GtkTextIter *iter);
+
+gboolean _gtk_source_iter_backward_visible_word_start (GtkTextIter *iter);
+
+void _gtk_source_iter_extend_selection_word (const GtkTextIter *location,
+ GtkTextIter *start,
+ GtkTextIter *end);
+
+void _gtk_source_iter_run_internal_unit_tests (void);
+
+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..6f8c0fc
--- /dev/null
+++ b/tests/test-iter.c
@@ -0,0 +1,83 @@
+/* -*- 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_word_boundaries (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 (TRUE, "---- aaaa", 0, 4, TRUE);
+ check_word_boundaries (TRUE, "abcd", 2, 4, FALSE);
+ check_word_boundaries (TRUE, "abcd ", 2, 4, TRUE);
+ check_word_boundaries (TRUE, " abcd()", 0, 5, TRUE);
+ check_word_boundaries (TRUE, "abcd()efgk", 4, 6, TRUE);
+}
+
+static void
+test_backward_word_start (void)
+{
+ check_word_boundaries (FALSE, "aaaa ----", 9, 5, TRUE);
+ check_word_boundaries (FALSE, "abcd", 2, 0, TRUE);
+ check_word_boundaries (FALSE, "()abcd ", 7, 2, TRUE);
+ check_word_boundaries (FALSE, "abcd()", 6, 4, TRUE);
+ check_word_boundaries (FALSE, "abcd()", 0, 0, FALSE);
+}
+
+int
+main (int argc, char **argv)
+{
+ gtk_test_init (&argc, &argv);
+
+ g_test_add_func ("/Iter/internal-functions", _gtk_source_iter_run_internal_unit_tests);
+ g_test_add_func ("/Iter/forward-word-end", test_forward_word_end);
+ g_test_add_func ("/Iter/backward-word-start", test_backward_word_start);
+
+ return g_test_run ();
+}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]