[gnome-builder] source-snippet: insert text before deleting previous text
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] source-snippet: insert text before deleting previous text
- Date: Fri, 11 Nov 2016 03:30:33 +0000 (UTC)
commit 88f3b92bd98fa1690a6b3846c5ee3348953b69ad
Author: Christian Hergert <chergert redhat com>
Date: Thu Nov 10 19:20:28 2016 -0800
source-snippet: insert text before deleting previous text
This allows us to always have non-zero lengths with replacing text. It
also allows us to avoid the delete() when there is nothing to delete.
libide/snippets/ide-source-snippet.c | 36 +++++++++++++++++++++++++++++----
1 files changed, 31 insertions(+), 5 deletions(-)
---
diff --git a/libide/snippets/ide-source-snippet.c b/libide/snippets/ide-source-snippet.c
index 7674f06..1680858 100644
--- a/libide/snippets/ide-source-snippet.c
+++ b/libide/snippets/ide-source-snippet.c
@@ -721,15 +721,41 @@ ide_source_snippet_replace_chunk_text (IdeSourceSnippet *self,
{
GtkTextIter begin;
GtkTextIter end;
+ gint diff = 0;
g_return_if_fail (IDE_IS_SOURCE_SNIPPET (self));
g_return_if_fail (n >= 0);
g_return_if_fail (text);
+ /*
+ * This replaces the text for the snippet. We insert new text before
+ * we delete the old text to ensure things are more stable as we
+ * manipulate the runs. Avoiding zero-length runs, even temporarily
+ * can be helpful.
+ */
+
ide_source_snippet_get_nth_chunk_range (self, n, &begin, &end);
- gtk_text_buffer_delete (self->buffer, &begin, &end);
- gtk_text_buffer_insert (self->buffer, &end, text, -1);
- g_array_index (self->runs, gint, n) = g_utf8_strlen (text, -1);
+
+ if (!gtk_text_iter_equal (&begin, &end))
+ {
+ gtk_text_iter_order (&begin, &end);
+ diff = gtk_text_iter_get_offset (&end) - gtk_text_iter_get_offset (&begin);
+ }
+
+ g_array_index (self->runs, gint, n) += g_utf8_strlen (text, -1);
+ gtk_text_buffer_insert (self->buffer, &begin, text, -1);
+
+ /* At this point, begin should be updated to the end of where we inserted
+ * our new text. If `diff` is non-zero, then we need to remove those
+ * characters immediately after `begin`.
+ */
+ if (diff != 0)
+ {
+ end = begin;
+ gtk_text_iter_forward_chars (&end, diff);
+ g_array_index (self->runs, gint, n) -= diff;
+ gtk_text_buffer_delete (self->buffer, &begin, &end);
+ }
}
static void
@@ -819,7 +845,7 @@ ide_source_snippet_after_insert_text (IdeSourceSnippet *self,
ide_source_snippet_update_tags (self);
-#if 0
+#if 1
ide_source_snippet_context_dump (self->snippet_context);
#endif
@@ -919,7 +945,7 @@ ide_source_snippet_after_delete_range (IdeSourceSnippet *self,
ide_source_snippet_update_tags (self);
-#if 0
+#if 1
ide_source_snippet_context_dump (self->snippet_context);
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]