[gtksourceview] Add a buffer_join_lines method
- From: Paolo Borelli <pborelli src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtksourceview] Add a buffer_join_lines method
- Date: Sun, 18 Jan 2015 14:00:33 +0000 (UTC)
commit baa0febd8ae5a982b7929f7cc418d5a89268566b
Author: Paolo Borelli <pborelli gnome org>
Date: Sun Jan 18 14:36:41 2015 +0100
Add a buffer_join_lines method
docs/reference/gtksourceview-3.0-sections.txt | 1 +
gtksourceview/gtksourcebuffer.c | 96 +++++++++++++++++++++++++
gtksourceview/gtksourcebuffer.h | 4 +
tests/test-buffer.c | 46 ++++++++++++
4 files changed, 147 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtksourceview-3.0-sections.txt b/docs/reference/gtksourceview-3.0-sections.txt
index de20088..8b3e2bd 100644
--- a/docs/reference/gtksourceview-3.0-sections.txt
+++ b/docs/reference/gtksourceview-3.0-sections.txt
@@ -43,6 +43,7 @@ gtk_source_buffer_get_source_marks_at_iter
gtk_source_buffer_remove_source_marks
<SUBSECTION Other>
gtk_source_buffer_change_case
+gtk_source_buffer_join_lines
gtk_source_buffer_set_implicit_trailing_newline
gtk_source_buffer_get_implicit_trailing_newline
<SUBSECTION Standard>
diff --git a/gtksourceview/gtksourcebuffer.c b/gtksourceview/gtksourcebuffer.c
index 1714548..a44b873 100644
--- a/gtksourceview/gtksourcebuffer.c
+++ b/gtksourceview/gtksourcebuffer.c
@@ -2506,6 +2506,102 @@ gtk_source_buffer_change_case (GtkSourceBuffer *buffer,
g_free (new_text);
}
+/* move to the end of the line excluding trailing spaces */
+static void
+move_to_line_text_end(GtkTextIter *iter)
+{
+ gint line;
+
+ line = gtk_text_iter_get_line (iter);
+
+ if (!gtk_text_iter_ends_line (iter))
+ {
+ gtk_text_iter_forward_to_line_end (iter);
+ }
+
+ while (gtk_text_iter_backward_char (iter) &&
+ (gtk_text_iter_get_line (iter) == line))
+ {
+ gunichar ch;
+
+ ch = gtk_text_iter_get_char (iter);
+ if (!g_unichar_isspace (ch))
+ {
+ break;
+ }
+ }
+
+ gtk_text_iter_forward_char (iter);
+}
+
+/**
+ * gtk_source_buffer_join_lines:
+ * @buffer: a #GtkSourceBuffer.
+ * @start: a #GtkTextIter.
+ * @end: a #GtkTextIter.
+ *
+ * Joins the lines of text between the specified iterators.
+ *
+ * Since: 3.16
+ **/
+void
+gtk_source_buffer_join_lines (GtkSourceBuffer *buffer,
+ GtkTextIter *start,
+ GtkTextIter *end)
+{
+ GtkTextBuffer *text_buffer;
+ GtkTextMark *end_mark;
+
+ g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+ g_return_if_fail (start != NULL);
+ g_return_if_fail (end != NULL);
+
+ if (gtk_text_iter_get_line (start) == gtk_text_iter_get_line (end))
+ {
+ return;
+ }
+
+ gtk_text_iter_order (start, end);
+
+ text_buffer = GTK_TEXT_BUFFER (buffer);
+ end_mark = gtk_text_buffer_create_mark (text_buffer, NULL, end, FALSE);
+
+ gtk_text_buffer_begin_user_action (text_buffer);
+
+ move_to_line_text_end (start);
+
+ while (gtk_text_iter_compare (start, end) < 0)
+ {
+ GtkTextIter iter;
+ gunichar ch;
+
+ iter = *start;
+
+ do
+ {
+ ch = gtk_text_iter_get_char (&iter);
+ if (!g_unichar_isspace (ch))
+ {
+ break;
+ }
+ } while (gtk_text_iter_forward_char (&iter));
+
+ if (!gtk_text_iter_is_end (&iter))
+ {
+ gtk_text_buffer_delete (text_buffer, start, &iter);
+ gtk_text_buffer_insert (text_buffer, start, " ", 1);
+ }
+
+ move_to_line_text_end (start);
+
+ gtk_text_buffer_get_iter_at_mark (text_buffer, end, end_mark);
+ }
+
+ gtk_text_buffer_end_user_action (text_buffer);
+
+ gtk_text_buffer_delete_mark (text_buffer, end_mark);
+}
+
/**
* gtk_source_buffer_set_undo_manager:
* @buffer: a #GtkSourceBuffer.
diff --git a/gtksourceview/gtksourcebuffer.h b/gtksourceview/gtksourcebuffer.h
index 449cca3..15876ca 100644
--- a/gtksourceview/gtksourcebuffer.h
+++ b/gtksourceview/gtksourcebuffer.h
@@ -198,6 +198,10 @@ void gtk_source_buffer_change_case
(GtkSourceBuffer *buffer,
GtkTextIter
*start,
GtkTextIter *end);
+void gtk_source_buffer_join_lines (GtkSourceBuffer
*buffer,
+ GtkTextIter
*start,
+ GtkTextIter *end);
+
GtkSourceUndoManager *gtk_source_buffer_get_undo_manager (GtkSourceBuffer
*buffer);
void gtk_source_buffer_set_undo_manager (GtkSourceBuffer
*buffer,
diff --git a/tests/test-buffer.c b/tests/test-buffer.c
index cba7065..6392591 100644
--- a/tests/test-buffer.c
+++ b/tests/test-buffer.c
@@ -174,6 +174,51 @@ test_change_case (void)
g_object_unref (buffer);
}
+static void
+do_test_join_lines (GtkSourceBuffer *buffer,
+ const gchar *text,
+ const gchar *expected,
+ gint start_offset,
+ gint end_offset)
+{
+ GtkTextIter start;
+ GtkTextIter end;
+ gchar *changed;
+
+ gtk_text_buffer_set_text (GTK_TEXT_BUFFER (buffer), text, -1);
+
+ gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &start, start_offset);
+ gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &end, end_offset);
+
+ gtk_source_buffer_join_lines (buffer, &start, &end);
+
+ gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (buffer), &start, &end);
+ changed = gtk_text_buffer_get_text (GTK_TEXT_BUFFER (buffer), &start, &end, TRUE);
+
+ g_assert_cmpstr (changed, ==, expected);
+
+ g_free (changed);
+}
+
+static void
+test_join_lines (void)
+{
+ GtkSourceBuffer *buffer;
+
+ buffer = gtk_source_buffer_new (NULL);
+
+ do_test_join_lines (buffer, "some text", "some text", 0, -1);
+ do_test_join_lines (buffer, "some\ntext", "some text", 0, -1);
+ do_test_join_lines (buffer, "some\t \n\t text", "some text", 0, -1);
+ do_test_join_lines (buffer, "some\n\n\ntext", "some text", 0, -1);
+ do_test_join_lines (buffer, "some\ntext", "some\ntext", 0, 1);
+ do_test_join_lines (buffer, "some\ntext", "some\ntext", 6, -1);
+ do_test_join_lines (buffer, "some\ntext\nmore", "some text\nmore", 0, 6);
+ do_test_join_lines (buffer, "some\ntext\nmore", "some\ntext more", 6, -1);
+
+ g_object_unref (buffer);
+}
+
int
main (int argc, char** argv)
{
@@ -184,6 +229,7 @@ main (int argc, char** argv)
g_test_add_func ("/Buffer/bug-634510", test_get_buffer);
g_test_add_func ("/Buffer/get-context-classes", test_get_context_classes);
g_test_add_func ("/Buffer/change-case", test_change_case);
+ g_test_add_func ("/Buffer/join-lines", test_join_lines);
return g_test_run();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]