[gnome-builder] workbench: make gb_workbench_reveal_file() work with lazy tree building



commit 87c6c0a6f732c236f5d9da8f9e78b179c7604ea0
Author: Christian Hergert <christian hergert me>
Date:   Sun Jun 14 16:59:31 2015 -0700

    workbench: make gb_workbench_reveal_file() work with lazy tree building
    
    Now that not all the nodes have been built, we need to be more precise
    with our lookups. This uses the new gb_tree_find_child_node() helper.
    
    As a side-effect, we don't have to scan the entire tree anymore either.
    
    The gb_tree_find_child_node() function will ensure the node is built
    before scaning the children. So tree paths following the lookup request
    will be opportunistically built.

 src/workbench/gb-workbench.c |   78 ++++++++++++++++++++++++++++++++----------
 1 files changed, 60 insertions(+), 18 deletions(-)
---
diff --git a/src/workbench/gb-workbench.c b/src/workbench/gb-workbench.c
index a24c5c5..243f3ec 100644
--- a/src/workbench/gb-workbench.c
+++ b/src/workbench/gb-workbench.c
@@ -983,22 +983,43 @@ gb_workbench_get_view_grid (GbWorkbench *self)
 }
 
 static gboolean
-gb_workbench_reveal_file_cb (gconstpointer a,
-                             gconstpointer b)
+find_files_node (GbTree     *tree,
+                 GbTreeNode *node,
+                 GbTreeNode *child,
+                 gpointer    user_data)
 {
-  GFile *file = (GFile *)a;
-  GObject *object = (GObject *)b;
+  GObject *item;
 
-  g_assert (G_IS_FILE (file));
-  g_assert (G_IS_OBJECT (object));
+  g_assert (GB_IS_TREE (tree));
+  g_assert (GB_IS_TREE_NODE (node));
+  g_assert (GB_IS_TREE_NODE (child));
 
-  if (IDE_IS_PROJECT_FILE (object))
+  item = gb_tree_node_get_item (child);
+
+  return IDE_IS_PROJECT_FILES (item);
+}
+
+static gboolean
+find_child_node (GbTree     *tree,
+                 GbTreeNode *node,
+                 GbTreeNode *child,
+                 gpointer    user_data)
+{
+  const gchar *name = user_data;
+  GObject *item;
+
+  g_assert (GB_IS_TREE (tree));
+  g_assert (GB_IS_TREE_NODE (node));
+  g_assert (GB_IS_TREE_NODE (child));
+
+  item = gb_tree_node_get_item (child);
+
+  if (IDE_IS_PROJECT_FILE (item))
     {
-      IdeProjectFile *pf = (IdeProjectFile *)object;
-      GFile *pf_file;
+      const gchar *item_name;
 
-      pf_file = ide_project_file_get_file (pf);
-      return g_file_equal (pf_file, file);
+      item_name = ide_project_file_get_name (IDE_PROJECT_FILE (item));
+      return ide_str_equal0 (item_name, name);
     }
 
   return FALSE;
@@ -1008,19 +1029,40 @@ void
 gb_workbench_reveal_file (GbWorkbench *self,
                           GFile       *file)
 {
+  g_autofree gchar *relpath = NULL;
+  g_auto(GStrv) parts = NULL;
   GbTreeNode *node;
+  GbTree *tree;
+  IdeVcs *vcs;
+  GFile *workdir;
+  gsize i;
 
   g_return_if_fail (GB_IS_WORKBENCH (self));
   g_return_if_fail (G_IS_FILE (file));
+  g_return_if_fail (self->context != NULL);
+
+  vcs = ide_context_get_vcs (self->context);
+  workdir = ide_vcs_get_working_directory (vcs);
+  relpath = g_file_get_relative_path (workdir, file);
+  tree = GB_TREE (self->project_tree);
 
-  node = gb_tree_find_custom (GB_TREE (self->project_tree),
-                              gb_workbench_reveal_file_cb,
-                              file);
+  if (relpath == NULL)
+    return;
 
-  if (node != NULL)
+  node = gb_tree_find_child_node (tree, NULL, find_files_node, NULL);
+  if (node == NULL)
+    return;
+
+  parts = g_strsplit (relpath, G_DIR_SEPARATOR_S, 0);
+
+  for (i = 0; parts [i]; i++)
     {
-      gb_tree_expand_to_node (GB_TREE (self->project_tree), node);
-      gb_tree_scroll_to_node (GB_TREE (self->project_tree), node);
-      gb_tree_node_select (node);
+      node = gb_tree_find_child_node (tree, node, find_child_node, parts [i]);
+      if (node == NULL)
+        return;
     }
+
+  gb_tree_expand_to_node (tree, node);
+  gb_tree_scroll_to_node (tree, node);
+  gb_tree_node_select (node);
 }


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