[gnome-builder/gnome-builder-3-30] editor: track editor search settings with session state



commit ec70b02b3acff5df29e4959a5d5029d43ef79fa5
Author: Christian Hergert <chergert redhat com>
Date:   Wed Oct 3 21:48:01 2018 -0700

    editor: track editor search settings with session state
    
    This saves various search settings with the session state so that they can
    be restored after the file has loaded.

 src/libide/editor/ide-editor-session-addin.c | 87 ++++++++++++++++++++++------
 1 file changed, 68 insertions(+), 19 deletions(-)
---
diff --git a/src/libide/editor/ide-editor-session-addin.c b/src/libide/editor/ide-editor-session-addin.c
index 1391b1360..2f4070eff 100644
--- a/src/libide/editor/ide-editor-session-addin.c
+++ b/src/libide/editor/ide-editor-session-addin.c
@@ -50,6 +50,12 @@ typedef struct
   gint   column;
   gint   row;
   gint   depth;
+  struct {
+    gchar    *keyword;
+    gboolean  case_sensitive;
+    gboolean  regex_enabled;
+    gboolean  at_word_boundaries;
+  } search;
 } Item;
 
 typedef struct
@@ -86,6 +92,7 @@ static void
 clear_item (Item *item)
 {
   g_clear_pointer (&item->uri, g_free);
+  g_clear_pointer (&item->search.keyword, g_free);
 }
 
 static void
@@ -144,6 +151,7 @@ ide_editor_session_addin_foreach_view_cb (GtkWidget *widget,
     {
       IdeBuffer *buffer = ide_editor_view_get_buffer (IDE_EDITOR_VIEW (view));
       IdeFile *file = ide_buffer_get_file (buffer);
+      IdeEditorSearch *search = ide_editor_view_get_search (IDE_EDITOR_VIEW (view));
       Item item = { 0 };
 
       if (!ide_file_get_is_temporary (file))
@@ -153,6 +161,11 @@ ide_editor_session_addin_foreach_view_cb (GtkWidget *widget,
           item.uri = g_file_get_uri (gfile);
           get_view_position (view, &item.column, &item.row, &item.depth);
 
+          item.search.keyword = g_strdup (ide_editor_search_get_search_text (search));
+          item.search.at_word_boundaries = ide_editor_search_get_at_word_boundaries (search);
+          item.search.case_sensitive = ide_editor_search_get_case_sensitive (search);
+          item.search.regex_enabled = ide_editor_search_get_regex_enabled (search);
+
           IDE_TRACE_MSG ("%u:%u:%u: %s", item.column, item.row, item.depth, item.uri);
 
           g_array_append_val (items, item);
@@ -193,18 +206,26 @@ ide_editor_session_addin_save_async (IdeSessionAddin     *addin,
 
   g_array_sort (items, compare_item);
 
-  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(siii)"));
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(siiiv)"));
 
   for (guint i = 0; i < items->len; i++)
     {
       const Item *item = &g_array_index (items, Item, i);
+      GVariantBuilder sub;
+
+      g_variant_builder_init (&sub, G_VARIANT_TYPE ("a{sv}"));
+      g_variant_builder_add_parsed (&sub, "{'search.keyword',<%s>}",item->search.keyword ?: "");
+      g_variant_builder_add_parsed (&sub, "{'search.at-word-boundaries',<%b>}", 
item->search.at_word_boundaries);
+      g_variant_builder_add_parsed (&sub, "{'search.regex-enabled',<%b>}", item->search.regex_enabled);
+      g_variant_builder_add_parsed (&sub, "{'search.case-sensitive',<%b>}", item->search.case_sensitive);
 
       g_variant_builder_add (&builder,
-                             "(siii)",
+                             "(siiiv)",
                              item->uri,
                              item->column,
                              item->row,
-                             item->depth);
+                             item->depth,
+                             g_variant_new_variant (g_variant_builder_end (&sub)));
     }
 
   ide_task_return_pointer (task,
@@ -251,6 +272,7 @@ load_state_finish (IdeEditorSessionAddin *self,
       const Item *item = &g_array_index (state->items, Item, i);
       g_autoptr(GFile) file = NULL;
       IdeLayoutGridColumn *column;
+      IdeEditorSearch *search;
       IdeLayoutStack *stack;
       IdeEditorView *view;
       IdeBuffer *buffer;
@@ -271,6 +293,13 @@ load_state_finish (IdeEditorSessionAddin *self,
                            "visible", TRUE,
                            NULL);
 
+      search = ide_editor_view_get_search (view);
+
+      ide_editor_search_set_search_text (search, item->search.keyword);
+      ide_editor_search_set_at_word_boundaries (search, item->search.at_word_boundaries);
+      ide_editor_search_set_case_sensitive (search, item->search.case_sensitive);
+      ide_editor_search_set_regex_enabled (search, item->search.regex_enabled);
+
       gtk_container_add (GTK_CONTAINER (stack), GTK_WIDGET (view));
     }
 }
@@ -390,6 +419,8 @@ ide_editor_session_addin_restore_async (IdeSessionAddin     *addin,
   IdeWorkbench *workbench;
   IdePerspective *editor;
   GVariantIter iter;
+  GVariant *extra = NULL;
+  const gchar *format = "(&siii)";
   gint column, row, depth;
 
   g_assert (IDE_IS_EDITOR_SESSION_ADDIN (addin));
@@ -426,10 +457,13 @@ ide_editor_session_addin_restore_async (IdeSessionAddin     *addin,
 
   load_state->active++;
 
-  while (g_variant_iter_next (&iter, "(&siii)", &uri, &column, &row, &depth))
+  if (g_variant_is_of_type (state, G_VARIANT_TYPE ("a(siiiv)")))
+    format = "(&siiiv)";
+
+  while (g_variant_iter_next (&iter, format, &uri, &column, &row, &depth, &extra))
     {
       g_autoptr(GFile) gfile = NULL;
-      Item item;
+      Item item = {0};
 
       IDE_TRACE_MSG ("Restore URI \"%s\" at %d:%d:%d", uri, column, row, depth);
 
@@ -437,24 +471,39 @@ ide_editor_session_addin_restore_async (IdeSessionAddin     *addin,
       item.column = column;
       item.row = row;
       item.depth = depth;
-      g_array_append_val (load_state->items, item);
 
-      /* Skip loading buffer if already loading */
-      if (g_hash_table_contains (uris, uri))
-        continue;
+      if (extra != NULL)
+        {
+          g_autoptr(GVariantDict) dict = NULL;
 
-      g_hash_table_add (uris, g_strdup (uri));
-      gfile = g_file_new_for_uri (uri);
+          dict = g_variant_dict_new (g_variant_get_variant (extra));
 
-      load_state->active++;
+          g_variant_dict_lookup (dict, "search.keyword", "s", &item.search.keyword);
+          g_variant_dict_lookup (dict, "search.at-word-boundaries", "b", &item.search.at_word_boundaries);
+          g_variant_dict_lookup (dict, "search.case-sensitive", "b", &item.search.case_sensitive);
+          g_variant_dict_lookup (dict, "search.regex-enabled", "b", &item.search.regex_enabled);
+        }
+
+      g_array_append_val (load_state->items, item);
+
+      /* Skip loading buffer if already loading */
+      if (!g_hash_table_contains (uris, uri))
+        {
+          g_hash_table_add (uris, g_strdup (uri));
+          gfile = g_file_new_for_uri (uri);
+
+          load_state->active++;
+
+          g_file_query_info_async (gfile,
+                                   G_FILE_ATTRIBUTE_STANDARD_NAME,
+                                   G_FILE_QUERY_INFO_NONE,
+                                   G_PRIORITY_LOW,
+                                   cancellable,
+                                   restore_file,
+                                   g_object_ref (task));
+        }
 
-      g_file_query_info_async (gfile,
-                               G_FILE_ATTRIBUTE_STANDARD_NAME,
-                               G_FILE_QUERY_INFO_NONE,
-                               G_PRIORITY_LOW,
-                               cancellable,
-                               restore_file,
-                               g_object_ref (task));
+      g_clear_pointer (&extra, g_variant_unref);
     }
 
   load_state->active--;


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