[gnome-builder] search: Fix issue with searching and restore to position before search



commit 096e30e62d6b0357fd8975e339d3562fb7bc5cd6
Author: Dimitris Zenios <dimitris zenios gmail com>
Date:   Sun May 17 23:02:31 2015 +0300

    search: Fix issue with searching and restore to position before search
    
    https://bugzilla.gnome.org/show_bug.cgi?id=746995

 libide/ide-source-view.c             |    1 +
 src/editor/gb-editor-frame-actions.c |   16 +++++++---
 src/editor/gb-editor-frame-private.h |    1 +
 src/editor/gb-editor-frame.c         |   56 ++++++++++++++++++++++------------
 4 files changed, 49 insertions(+), 25 deletions(-)
---
diff --git a/libide/ide-source-view.c b/libide/ide-source-view.c
index 7991d84..623e408 100644
--- a/libide/ide-source-view.c
+++ b/libide/ide-source-view.c
@@ -1091,6 +1091,7 @@ ide_source_view__search_settings_notify_search_text (IdeSourceView           *se
       if (gtk_source_search_context_forward (priv->search_context, &begin_iter,
                                              &match_begin, &match_end))
         {
+          gtk_text_buffer_select_range (buffer, &match_begin, &match_end);
           gtk_text_buffer_move_mark (buffer, priv->rubberband_mark, &match_begin);
           ide_source_view_scroll_mark_onscreen (self, priv->rubberband_mark, TRUE, 0.5, 0.5);
         }
diff --git a/src/editor/gb-editor-frame-actions.c b/src/editor/gb-editor-frame-actions.c
index e890816..cf9c263 100644
--- a/src/editor/gb-editor-frame-actions.c
+++ b/src/editor/gb-editor-frame-actions.c
@@ -28,13 +28,17 @@ gb_editor_frame_actions_find (GSimpleAction *action,
 {
   GbEditorFrame *self = user_data;
   GtkTextBuffer *buffer;
-  GtkTextIter start_sel;
-  GtkTextIter end_sel;
+  GtkTextMark *mark;
+  GtkTextIter iter;
 
   g_assert (GB_IS_EDITOR_FRAME (self));
 
   buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
 
+  /* Get the position of the insert mark */
+  mark = gtk_text_buffer_get_insert (buffer);
+  gtk_text_buffer_get_iter_at_mark (buffer, &iter, mark);
+
   /*
    * If the buffer currently has a selection, we prime the search entry with the
    * selected text. If not, we use our previous search text in the case that it was
@@ -43,6 +47,9 @@ gb_editor_frame_actions_find (GSimpleAction *action,
 
   if (gtk_text_buffer_get_has_selection (buffer))
     {
+      GtkTextIter start_sel;
+      GtkTextIter end_sel;
+
       gtk_text_buffer_get_selection_bounds (buffer, &start_sel, &end_sel);
 
       if (gtk_text_iter_get_line (&start_sel) == gtk_text_iter_get_line (&end_sel))
@@ -54,10 +61,9 @@ gb_editor_frame_actions_find (GSimpleAction *action,
         }
     }
   else if (self->previous_search_string != NULL)
-    {
-      gtk_entry_set_text (GTK_ENTRY (self->search_entry), self->previous_search_string);
-    }
+    gtk_entry_set_text (GTK_ENTRY (self->search_entry), self->previous_search_string);
 
+  gtk_text_buffer_move_mark (buffer, self->search_mark, &iter);
   gtk_revealer_set_reveal_child (self->search_revealer, TRUE);
   gtk_widget_grab_focus (GTK_WIDGET (self->search_entry));
 }
diff --git a/src/editor/gb-editor-frame-private.h b/src/editor/gb-editor-frame-private.h
index 0d7c633..db404df 100644
--- a/src/editor/gb-editor-frame-private.h
+++ b/src/editor/gb-editor-frame-private.h
@@ -46,6 +46,7 @@ struct _GbEditorFrame
   GbEditorMapBin      *source_map_container;
   IdeSourceMap        *source_map;
   GtkOverlay          *source_overlay;
+  GtkTextMark         *search_mark;
 
   IdeAnimation        *map_animation;
 
diff --git a/src/editor/gb-editor-frame.c b/src/editor/gb-editor-frame.c
index 874fd34..4a7a29b 100644
--- a/src/editor/gb-editor-frame.c
+++ b/src/editor/gb-editor-frame.c
@@ -292,6 +292,7 @@ gb_editor_frame_set_document (GbEditorFrame    *self,
 
   mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (document));
   gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (document), &iter, mark);
+  self->search_mark = gtk_text_buffer_create_mark (GTK_TEXT_BUFFER (document), NULL, &iter, FALSE);
   on_cursor_moved (document, &iter, self);
 
   /*
@@ -406,35 +407,43 @@ gb_editor_frame__search_key_press_event (GbEditorFrame *self,
                                          GdkEventKey   *event,
                                          GdTaggedEntry *entry)
 {
-  GtkTextBuffer *buffer;
-
   g_assert (GB_IS_EDITOR_FRAME (self));
   g_assert (GD_IS_TAGGED_ENTRY (entry));
 
   switch (event->keyval)
     {
     case GDK_KEY_Escape:
-      /* stash the search string for later */
-      g_free (self->previous_search_string);
-      g_object_get (self->search_entry, "text", &self->previous_search_string, NULL);
+      {
+        GtkTextIter iter;
+        GtkTextBuffer *buffer;
 
-      /* clear the highlights in the source view */
-      ide_source_view_clear_search (self->source_view);
+        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
 
-      /* disable rubberbanding and ensure insert mark is on screen */
-      buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
-      ide_source_view_set_rubberband_search (self->source_view, FALSE);
-      ide_source_view_scroll_mark_onscreen (self->source_view,
-                                            gtk_text_buffer_get_insert (buffer),
-                                            TRUE,
-                                            0.5,
-                                            0.5);
+        /* stash the search string for later */
+        g_free (self->previous_search_string);
+        g_object_get (self->search_entry, "text", &self->previous_search_string, NULL);
 
-      /* finally we can focus the source view */
-      gtk_widget_grab_focus (GTK_WIDGET (self->source_view));
+        /* clear the highlights in the source view */
+        ide_source_view_clear_search (self->source_view);
 
-      return GDK_EVENT_STOP;
+        /* disable rubberbanding and ensure insert mark is on screen */
+        ide_source_view_set_rubberband_search (self->source_view, FALSE);
+        gtk_text_buffer_get_iter_at_mark (buffer, &iter, self->search_mark);
+        ide_source_view_scroll_to_iter (self->source_view, &iter, 0.0, TRUE, 0.5, 0.5, TRUE);
+
+        /* finally we can focus the source view */
+        gtk_widget_grab_focus (GTK_WIDGET (self->source_view));
 
+        /*
+         * Unfortunately We have to place the cursor after grabbing focus
+         * because ide-source-view has its own handler when focused which
+         * sets the cursor back to the insert mark which at that time points
+         * to the last search found
+         */
+        gtk_text_buffer_place_cursor (buffer, &iter);
+
+        return GDK_EVENT_STOP;
+      }
     case GDK_KEY_KP_Enter:
     case GDK_KEY_Return:
       if ((event->state & GDK_SHIFT_MASK) == 0)
@@ -711,12 +720,19 @@ gb_editor_frame_dispose (GObject *object)
 
   ide_clear_weak_pointer (&self->map_animation);
 
-  if (self->source_view && self->cursor_moved_handler)
+  if (self->source_view)
     {
       GtkTextBuffer *buffer;
 
       buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (self->source_view));
-      ide_clear_signal_handler (buffer, &self->cursor_moved_handler);
+      if (self->cursor_moved_handler)
+        ide_clear_signal_handler (buffer, &self->cursor_moved_handler);
+
+      if (self->search_mark != NULL)
+        {
+          gtk_text_buffer_delete_mark (buffer, self->search_mark);
+          self->search_mark = NULL;
+        }
     }
 
   g_clear_object (&self->search_entry_tag);


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