[gtksourceview/wip/chergert/snippets] more cleanup on queue design



commit c9338d0038f3fb35a04e527eaad9268d56875edb
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jan 24 14:00:10 2020 -0800

    more cleanup on queue design

 gtksourceview/gtksourcesnippet.c | 254 ++++++++++++---------------------------
 1 file changed, 75 insertions(+), 179 deletions(-)
---
diff --git a/gtksourceview/gtksourcesnippet.c b/gtksourceview/gtksourcesnippet.c
index eebc5c27..187916c2 100644
--- a/gtksourceview/gtksourcesnippet.c
+++ b/gtksourceview/gtksourcesnippet.c
@@ -102,37 +102,6 @@ print_chunk_positions (GtkSourceSnippet *self)
        }
 }
 
-static GtkSourceSnippetChunk *
-get_chunk_at_iter (GtkSourceSnippet *self,
-                   GtkTextIter      *iter)
-{
-       g_assert (GTK_SOURCE_IS_SNIPPET (self));
-       g_assert (iter != NULL);
-
-       /* If our current chunk (as known from focus tracking) contains
-        * the iter that is requested, then we will use that. Otherwise,
-        * we'll scan the chunks (starting from 0) to see which one matches
-        * the requested location.
-        */
-       if (self->current_chunk != NULL &&
-           _gtk_source_snippet_chunk_contains (self->current_chunk, iter))
-       {
-               return self->current_chunk;
-       }
-
-       for (const GList *l = self->chunks.head; l; l = l->next)
-       {
-               GtkSourceSnippetChunk *chunk = l->data;
-
-               if (_gtk_source_snippet_chunk_contains (chunk, iter))
-               {
-                       return chunk;
-               }
-       }
-
-       g_return_val_if_reached (NULL);
-}
-
 static void
 gtk_source_snippet_save_insert (GtkSourceSnippet *snippet)
 {
@@ -430,47 +399,28 @@ gtk_source_snippet_set_description (GtkSourceSnippet *self,
        }
 }
 
-static gboolean
-gtk_source_snippet_contains (GtkSourceSnippet *self,
-                             GtkTextIter      *iter)
-{
-       GtkTextIter begin;
-       GtkTextIter end;
-       gboolean ret;
-
-       g_assert (GTK_SOURCE_IS_SNIPPET (self));
-       g_assert (iter != NULL);
-
-       gtk_text_buffer_get_iter_at_mark (self->buffer, &begin, self->mark_begin);
-       gtk_text_buffer_get_iter_at_mark (self->buffer, &end, self->mark_end);
-
-       ret = ((gtk_text_iter_compare (&begin, iter) <= 0) &&
-              (gtk_text_iter_compare (&end, iter) >= 0));
-
-       return ret;
-}
-
 gboolean
 _gtk_source_snippet_insert_set (GtkSourceSnippet *self,
                                 GtkTextMark      *mark)
 {
+       GtkTextIter begin;
+       GtkTextIter end;
        GtkTextIter iter;
 
        g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (self), FALSE);
        g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
+       g_return_val_if_fail (self->current_chunk != NULL, FALSE);
+       g_return_val_if_fail (self->buffer != NULL, FALSE);
 
        gtk_text_buffer_get_iter_at_mark (self->buffer, &iter, mark);
 
-       if (!gtk_source_snippet_contains (self, &iter))
+       if (_gtk_source_snippet_chunk_get_bounds (self->current_chunk, &begin, &end))
        {
-               self->current_chunk = NULL;
-               return FALSE;
-       }
-       else
-       {
-               self->current_chunk = get_chunk_at_iter (self, &iter);
-               return TRUE;
+               return gtk_text_iter_compare (&begin, &iter) <= 0 &&
+                      gtk_text_iter_compare (&end, &iter) >= 0;
        }
+
+       return FALSE;
 }
 
 static void
@@ -483,11 +433,6 @@ gtk_source_snippet_select_chunk (GtkSourceSnippet      *self,
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET (self));
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET_CHUNK (chunk));
 
-       if (self->current_chunk == chunk)
-       {
-               return;
-       }
-
        _gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end);
        gtk_text_iter_order (&begin, &end);
 
@@ -523,11 +468,6 @@ _gtk_source_snippet_move_next (GtkSourceSnippet *self)
 
        g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (self), FALSE);
 
-       if (self->focus_position > self->max_focus_position)
-       {
-               return FALSE;
-       }
-
        self->focus_position++;
 
        for (const GList *l = self->chunks.head; l; l = l->next)
@@ -541,7 +481,7 @@ _gtk_source_snippet_move_next (GtkSourceSnippet *self)
                }
        }
 
-       for (const GList *l = self->chunks.head; l; l = l->next)
+       for (const GList *l = self->chunks.tail; l; l = l->prev)
        {
                GtkSourceSnippetChunk *chunk = l->data;
 
@@ -566,7 +506,7 @@ _gtk_source_snippet_move_previous (GtkSourceSnippet *self)
 {
        g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (self), FALSE);
 
-       if (self->focus_position == 1)
+       if (self->focus_position <= 1)
        {
                GtkTextIter iter;
 
@@ -581,7 +521,7 @@ _gtk_source_snippet_move_previous (GtkSourceSnippet *self)
                return FALSE;
        }
 
-       self->focus_position = MAX (1, self->focus_position - 1);
+       self->focus_position--;
 
        for (const GList *l = self->chunks.head; l; l = l->next)
        {
@@ -796,10 +736,22 @@ gtk_source_snippet_add_chunk (GtkSourceSnippet      *self,
 static void
 gtk_source_snippet_update_marks (GtkSourceSnippet *snippet)
 {
+       GtkSourceSnippetChunk *current;
        GtkTextBuffer *buffer;
+       GtkTextIter current_begin;
+       GtkTextIter current_end;
 
        g_assert (GTK_SOURCE_IS_SNIPPET (snippet));
 
+       buffer = GTK_TEXT_BUFFER (snippet->buffer);
+       current = snippet->current_chunk;
+
+       if (current == NULL ||
+           !_gtk_source_snippet_chunk_get_bounds (current, &current_begin, &current_end))
+       {
+               return;
+       }
+
        /* If the begin of this chunk has come before the end
         * of the last chunk, then that mights we are empty and
         * the right gravity of the begin mark was greedily taken
@@ -834,84 +786,55 @@ gtk_source_snippet_update_marks (GtkSourceSnippet *snippet)
         *   [t][t][[]]
         */
 
-#if 1
-       g_printerr ("Before:\n");
-       print_chunk_positions (snippet);
-#endif
-
-       if (snippet->chunks.length == 0)
-       {
-               return;
-       }
-
-       buffer = GTK_TEXT_BUFFER (snippet->buffer);
-
-       /* For all marks before the current_chunk we want to ensure that their
-        * end_mark has not moved past the begin_mark for the chunk after it.
-        * That can happen when text is inserted at the beginning of a chunk as
-        * the right gravity mark from the adjacent chunk will be moved.
-        */
-       for (const GList *l = snippet->chunks.head; l && l->next; l = l->next)
+       for (const GList *l = current->link.prev; l; l = l->prev)
        {
                GtkSourceSnippetChunk *chunk = l->data;
-               GtkSourceSnippetChunk *next = l->next->data;
-               GtkTextIter begin, end;
-               GtkTextIter next_begin, next_end;
-
-               if (!_gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end) ||
-                   !_gtk_source_snippet_chunk_get_bounds (next, &next_begin, &next_end))
-               {
-                       break;
-               }
+               GtkTextMark *mark;
+               GtkTextIter begin;
+               GtkTextIter end;
 
-               if (gtk_text_iter_compare (&end, &next_begin) > 0)
+               if (_gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end))
                {
-                       gtk_text_buffer_move_mark (buffer,
-                                                  _gtk_source_snippet_chunk_get_end_mark (chunk),
-                                                  &next_begin);
-               }
+                       if (gtk_text_iter_compare (&end, &current_begin) > 0)
+                       {
+                               mark = _gtk_source_snippet_chunk_get_end_mark (chunk);
+                               gtk_text_buffer_move_mark (buffer, mark, &current_begin);
+                               end = current_begin;
+                       }
 
-               if (chunk == snippet->current_chunk)
-               {
-                       break;
+                       if (gtk_text_iter_compare (&begin, &end) > 0)
+                       {
+                               mark = _gtk_source_snippet_chunk_get_begin_mark (chunk);
+                               gtk_text_buffer_move_mark (buffer, mark, &end);
+                               begin = end;
+                       }
                }
        }
 
-#if 1
-       g_printerr ("After move up to current_chunk:\n");
-       print_chunk_positions (snippet);
-#endif
-
-       /* Now for all of the snippets, if their start position becomes before
-        * the previous end position, move it forward. This can happen when you
-        * have adjacent chunks and the gravity keeps the mark in the wrong
-        * position relative to other marks.
-        */
-       for (const GList *l = g_queue_peek_nth_link (&snippet->chunks, 1); l; l = l->next)
+       for (const GList *l = current->link.next; l; l = l->prev)
        {
                GtkSourceSnippetChunk *chunk = l->data;
-               GtkSourceSnippetChunk *prev = l->prev->data;
-               GtkTextIter begin, end;
-               GtkTextIter prev_begin, prev_end;
+               GtkTextMark *mark;
+               GtkTextIter begin;
+               GtkTextIter end;
 
-               if (!_gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end) ||
-                   !_gtk_source_snippet_chunk_get_bounds (prev, &prev_begin, &prev_end))
+               if (_gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end))
                {
-                       break;
-               }
+                       if (gtk_text_iter_compare (&begin, &current_end) < 0)
+                       {
+                               mark = _gtk_source_snippet_chunk_get_begin_mark (chunk);
+                               gtk_text_buffer_move_mark (buffer, mark, &current_end);
+                               begin = current_end;
+                       }
 
-               if (gtk_text_iter_compare (&begin, &prev_end) < 0)
-               {
-                       gtk_text_buffer_move_mark (buffer,
-                                                  _gtk_source_snippet_chunk_get_begin_mark (chunk),
-                                                  &end);
+                       if (gtk_text_iter_compare (&end, &begin) < 0)
+                       {
+                               mark = _gtk_source_snippet_chunk_get_end_mark (chunk);
+                               gtk_text_buffer_move_mark (buffer, mark, &begin);
+                               end = begin;
+                       }
                }
        }
-
-#if 1
-       g_printerr ("After move of all:\n");
-       print_chunk_positions (snippet);
-#endif
 }
 
 static void
@@ -937,15 +860,13 @@ gtk_source_snippet_rewrite_updated_chunks (GtkSourceSnippet *self)
                self->current_chunk = chunk;
 
                _gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end);
+               real_text = gtk_text_iter_get_slice (&begin, &end);
 
                text = gtk_source_snippet_chunk_get_text (chunk);
-               real_text = gtk_text_iter_get_slice (&begin, &end);
 
                if (g_strcmp0 (text, real_text) != 0)
                {
                        gtk_text_buffer_delete (self->buffer, &begin, &end);
-                       gtk_source_snippet_update_marks (self);
-
                        gtk_text_buffer_insert (self->buffer, &begin, real_text, -1);
                        gtk_source_snippet_update_marks (self);
                }
@@ -953,12 +874,6 @@ gtk_source_snippet_rewrite_updated_chunks (GtkSourceSnippet *self)
                g_free (real_text);
        }
 
-       if (current)
-               g_print ("!!!! reseting current chunk to %d\n",
-                        g_list_position (self->chunks.head, &current->link));
-       else
-               g_print ("!!!! clearing current chunk\n");
-
        self->current_chunk = current;
 }
 
@@ -973,11 +888,6 @@ _gtk_source_snippet_before_insert_text (GtkSourceSnippet *self,
        g_return_if_fail (self->current_chunk != NULL);
        g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
        g_return_if_fail (iter != NULL);
-
-       g_printerr ("Insert: %u:%u text=%s\n",
-                   gtk_text_iter_get_line (iter),
-                   gtk_text_iter_get_line_offset (iter),
-                   text);
 }
 
 void
@@ -987,14 +897,11 @@ _gtk_source_snippet_after_insert_text (GtkSourceSnippet *self,
                                        const gchar      *text,
                                        gint              len)
 {
-       GtkSourceSnippetChunk *chunk;
-
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET (self));
        g_return_if_fail (self->current_chunk != NULL);
        g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
        g_return_if_fail (iter != NULL);
-
-       g_printerr ("+++++ AFTER INSERT\n");
+       g_return_if_fail (self->current_chunk != NULL);
 
        /* This function is guaranteed to only be called once for the
         * actual insert by gtksourceview-snippets.c. That allows us
@@ -1002,15 +909,17 @@ _gtk_source_snippet_after_insert_text (GtkSourceSnippet *self,
         * delete/insert text in linked chunks.
         */
 
+       /* Save our insert position so we can restore it after updating
+        * linked chunks (which could be rewritten).
+        */
        gtk_source_snippet_save_insert (self);
 
+       /* Now save the modified text for the iter in question */
+       _gtk_source_snippet_chunk_save_text (self->current_chunk);
+
        /* First we want to update marks from the inserted text */
        gtk_source_snippet_update_marks (self);
 
-       /* Now save the modified text for the iter in question */
-       chunk = get_chunk_at_iter (self, iter);
-       _gtk_source_snippet_chunk_save_text (chunk);
-
        /* Update the context (two passes to ensure that we handle chunks
         * referencing chunks which come after themselves in the array).
         */
@@ -1025,6 +934,7 @@ _gtk_source_snippet_after_insert_text (GtkSourceSnippet *self,
        /* Now we can apply tags for the given chunks */
        gtk_source_snippet_update_tags (self);
 
+       /* Place the insertion cursor back where the user expects it */
        gtk_source_snippet_restore_insert (self);
 }
 
@@ -1034,21 +944,11 @@ _gtk_source_snippet_before_delete_range (GtkSourceSnippet *self,
                                          GtkTextIter      *begin,
                                          GtkTextIter      *end)
 {
-       gchar *text;
-
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET (self));
        g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
        g_return_if_fail (begin != NULL);
        g_return_if_fail (end != NULL);
 
-       text = gtk_text_iter_get_slice (begin, end);
-       g_printerr ("----- DELETE: %u:%u to %u:%u text=%s\n",
-                   gtk_text_iter_get_line (begin),
-                   gtk_text_iter_get_line_offset (begin),
-                   gtk_text_iter_get_line (end),
-                   gtk_text_iter_get_line_offset (end),
-                   text);
-       g_free (text);
 }
 
 void
@@ -1057,30 +957,24 @@ _gtk_source_snippet_after_delete_range (GtkSourceSnippet *self,
                                         GtkTextIter      *begin,
                                         GtkTextIter      *end)
 {
-       GtkSourceSnippetChunk *chunk;
-
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET (self));
        g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
-       g_return_if_fail (begin);
-       g_return_if_fail (end);
+       g_return_if_fail (begin != NULL);
+       g_return_if_fail (end != NULL);
+       g_return_if_fail (self->current_chunk != NULL);
 
-       g_printerr ("----- AFTER DELETE\n");
+       /* Now save the modified text for the iter in question */
+       _gtk_source_snippet_chunk_save_text (self->current_chunk);
 
+       /* Stash our cursor position so we can restore it after changes */
        gtk_source_snippet_save_insert (self);
 
        /* First update mark positions based on the deletions */
        gtk_source_snippet_update_marks (self);
 
-       /* Now save the modified text for the iter in question */
-       chunk = get_chunk_at_iter (self, begin);
-       g_print ("updating text for chunk at focus: %d\n",
-                gtk_source_snippet_chunk_get_focus_position (chunk));
-       _gtk_source_snippet_chunk_save_text (chunk);
-
        /* Update the context (two passes to ensure that we handle chunks
         * referencing chunks which come after themselves in the array).
         */
-       g_print ("updating context variables\n");
        gtk_source_snippet_update_context (self);
        gtk_source_snippet_update_context (self);
 
@@ -1089,8 +983,10 @@ _gtk_source_snippet_after_delete_range (GtkSourceSnippet *self,
         */
        gtk_source_snippet_rewrite_updated_chunks (self);
 
+       /* Now update any scheme styling for focus positions */
        gtk_source_snippet_update_tags (self);
 
+       /* Place the insertion cursor back where the user expects it */
        gtk_source_snippet_restore_insert (self);
 }
 


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