[gtksourceview/wip/chergert/snippets] make this thing work



commit ae446da6cbb510a8999d09b3ac9e0ad0a76d4b16
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jan 24 14:55:31 2020 -0800

    make this thing work

 gtksourceview/gtksourcesnippet-private.h |   8 +-
 gtksourceview/gtksourcesnippet.c         | 137 +++++++++++++++++++++----------
 2 files changed, 99 insertions(+), 46 deletions(-)
---
diff --git a/gtksourceview/gtksourcesnippet-private.h b/gtksourceview/gtksourcesnippet-private.h
index 58ca0956..fcb456a1 100644
--- a/gtksourceview/gtksourcesnippet-private.h
+++ b/gtksourceview/gtksourcesnippet-private.h
@@ -68,12 +68,12 @@ G_GNUC_INTERNAL
 gboolean     _gtk_source_snippet_insert_set                 (GtkSourceSnippet  *self,
                                                              GtkTextMark       *mark);
 G_GNUC_INTERNAL
-GtkTextMark *_gtk_source_snippet_get_mark_begin             (GtkSourceSnippet  *self);
-G_GNUC_INTERNAL
-GtkTextMark *_gtk_source_snippet_get_mark_end               (GtkSourceSnippet  *self);
-G_GNUC_INTERNAL
 guint        _gtk_source_snippet_count_affected_chunks      (GtkSourceSnippet  *snippet,
                                                              const GtkTextIter *begin,
                                                              const GtkTextIter *end);
+G_GNUC_INTERNAL
+gboolean     _gtk_source_snippet_contains_range             (GtkSourceSnippet  *snippet,
+                                                             const GtkTextIter *begin,
+                                                             const GtkTextIter *end);
 
 G_END_DECLS
diff --git a/gtksourceview/gtksourcesnippet.c b/gtksourceview/gtksourcesnippet.c
index bc5dd5e8..35c7467d 100644
--- a/gtksourceview/gtksourcesnippet.c
+++ b/gtksourceview/gtksourcesnippet.c
@@ -400,30 +400,6 @@ gtk_source_snippet_set_description (GtkSourceSnippet *snippet,
        }
 }
 
-gboolean
-_gtk_source_snippet_insert_set (GtkSourceSnippet *snippet,
-                                GtkTextMark      *mark)
-{
-       GtkTextIter begin;
-       GtkTextIter end;
-       GtkTextIter iter;
-
-       g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (snippet), FALSE);
-       g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
-       g_return_val_if_fail (snippet->current_chunk != NULL, FALSE);
-       g_return_val_if_fail (snippet->buffer != NULL, FALSE);
-
-       gtk_text_buffer_get_iter_at_mark (snippet->buffer, &iter, mark);
-
-       if (_gtk_source_snippet_chunk_get_bounds (snippet->current_chunk, &begin, &end))
-       {
-               return gtk_text_iter_compare (&begin, &iter) <= 0 &&
-                      gtk_text_iter_compare (&end, &iter) >= 0;
-       }
-
-       return FALSE;
-}
-
 static void
 gtk_source_snippet_select_chunk (GtkSourceSnippet      *snippet,
                                  GtkSourceSnippetChunk *chunk)
@@ -433,9 +409,12 @@ gtk_source_snippet_select_chunk (GtkSourceSnippet      *snippet,
 
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET (snippet));
        g_return_if_fail (GTK_SOURCE_IS_SNIPPET_CHUNK (chunk));
+       g_return_if_fail (chunk->focus_position >= 0);
 
-       _gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end);
-       gtk_text_iter_order (&begin, &end);
+       if (!_gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end))
+       {
+               return;
+       }
 
        g_debug ("Selecting chunk with range %d:%d to %d:%d (offset %d+%d)",
                 gtk_text_iter_get_line (&begin) + 1,
@@ -446,6 +425,7 @@ gtk_source_snippet_select_chunk (GtkSourceSnippet      *snippet,
                 gtk_text_iter_get_offset (&end) - gtk_text_iter_get_offset (&begin));
 
        snippet->current_chunk = chunk;
+       snippet->focus_position = chunk->focus_position;
 
        gtk_text_buffer_select_range (snippet->buffer, &begin, &end);
 
@@ -462,6 +442,69 @@ gtk_source_snippet_select_chunk (GtkSourceSnippet      *snippet,
 #endif
 }
 
+gboolean
+_gtk_source_snippet_insert_set (GtkSourceSnippet *snippet,
+                                GtkTextMark      *mark)
+{
+       GtkTextIter begin;
+       GtkTextIter end;
+       GtkTextIter iter;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (snippet), FALSE);
+       g_return_val_if_fail (GTK_IS_TEXT_MARK (mark), FALSE);
+       g_return_val_if_fail (snippet->current_chunk != NULL, FALSE);
+       g_return_val_if_fail (snippet->buffer != NULL, FALSE);
+
+       gtk_text_buffer_get_iter_at_mark (snippet->buffer, &iter, mark);
+
+       if (_gtk_source_snippet_chunk_get_bounds (snippet->current_chunk, &begin, &end))
+       {
+               if (gtk_text_iter_compare (&begin, &iter) <= 0 &&
+                   gtk_text_iter_compare (&end, &iter) >= 0)
+               {
+                       /* No change, we're still in the current chunk */
+                       return TRUE;
+               }
+       }
+
+       /* See if the insertion position would place us in any of the other
+        * snippet chunks that are a focus position.
+        */
+       for (const GList *l = snippet->chunks.head; l; l = l->next)
+       {
+               GtkSourceSnippetChunk *chunk = l->data;
+
+               if (chunk->focus_position <= 0 || chunk == snippet->current_chunk)
+               {
+                       continue;
+               }
+
+               if (_gtk_source_snippet_chunk_get_bounds (chunk, &begin, &end))
+               {
+                       /* Ignore this chunk if it is empty. There is no way
+                        * to disambiguate between side-by-side empty chunks
+                        * to make this a meaningful movement.
+                        */
+                       if (gtk_text_iter_equal (&begin, &end))
+                       {
+                               continue;
+                       }
+
+                       /* This chunk contains the focus position, so make it
+                        * our new chunk to edit.
+                        */
+                       if (gtk_text_iter_compare (&begin, &iter) <= 0 &&
+                           gtk_text_iter_compare (&end, &iter) >= 0)
+                       {
+                               gtk_source_snippet_select_chunk (snippet, chunk);
+                               return TRUE;
+                       }
+               }
+       }
+
+       return FALSE;
+}
+
 gboolean
 _gtk_source_snippet_move_next (GtkSourceSnippet *snippet)
 {
@@ -991,6 +1034,32 @@ _gtk_source_snippet_after_delete_range (GtkSourceSnippet *snippet,
        gtk_source_snippet_restore_insert (snippet);
 }
 
+gboolean
+_gtk_source_snippet_contains_range (GtkSourceSnippet  *snippet,
+                                    const GtkTextIter *begin,
+                                    const GtkTextIter *end)
+{
+       GtkTextIter snippet_begin;
+       GtkTextIter snippet_end;
+
+       g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (snippet), FALSE);
+       g_return_val_if_fail (begin != NULL, FALSE);
+       g_return_val_if_fail (end != NULL, FALSE);
+       g_return_val_if_fail (snippet->buffer != NULL, FALSE);
+       g_return_val_if_fail (snippet->mark_begin != NULL, FALSE);
+       g_return_val_if_fail (snippet->mark_end != NULL, FALSE);
+
+       gtk_text_buffer_get_iter_at_mark (snippet->buffer,
+                                         &snippet_begin,
+                                         snippet->mark_begin);
+       gtk_text_buffer_get_iter_at_mark (snippet->buffer,
+                                         &snippet_end,
+                                         snippet->mark_end);
+
+       return gtk_text_iter_compare (begin, &snippet_begin) >= 0 &&
+              gtk_text_iter_compare (end, &snippet_end) <= 0;
+}
+
 guint
 _gtk_source_snippet_count_affected_chunks (GtkSourceSnippet  *snippet,
                                            const GtkTextIter *begin,
@@ -1046,22 +1115,6 @@ _gtk_source_snippet_count_affected_chunks (GtkSourceSnippet  *snippet,
        return count;
 }
 
-GtkTextMark *
-_gtk_source_snippet_get_mark_begin (GtkSourceSnippet *snippet)
-{
-       g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (snippet), NULL);
-
-       return snippet->mark_begin;
-}
-
-GtkTextMark *
-_gtk_source_snippet_get_mark_end (GtkSourceSnippet *snippet)
-{
-       g_return_val_if_fail (GTK_SOURCE_IS_SNIPPET (snippet), NULL);
-
-       return snippet->mark_end;
-}
-
 /**
  * gtk_source_snippet_get_context:
  * @snippet: an #GtkSourceSnippet


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