[gnome-builder] file-search: add max-depth hook for indexing



commit f3f9cb6842715c8a628d5a66f29598a7e3414f16
Author: Christian Hergert <chergert redhat com>
Date:   Tue Mar 5 12:32:17 2019 -0800

    file-search: add max-depth hook for indexing
    
    This limits the indexing depth to 5 directories if we do not
    have a project and the workdir is not a child of projects_dir.

 src/plugins/file-search/gbp-file-search-index.c    | 28 ++++++++++++++++++++--
 src/plugins/file-search/gbp-file-search-provider.c | 13 ++++++++++
 2 files changed, 39 insertions(+), 2 deletions(-)
---
diff --git a/src/plugins/file-search/gbp-file-search-index.c b/src/plugins/file-search/gbp-file-search-index.c
index b2059bdb2..6ae41798e 100644
--- a/src/plugins/file-search/gbp-file-search-index.c
+++ b/src/plugins/file-search/gbp-file-search-index.c
@@ -35,6 +35,8 @@ struct _GbpFileSearchIndex
 
   GFile                *root_directory;
   DzlFuzzyMutableIndex *fuzzy;
+
+  gint                  max_depth;
 };
 
 G_DEFINE_TYPE (GbpFileSearchIndex, gbp_file_search_index, IDE_TYPE_OBJECT)
@@ -42,6 +44,7 @@ G_DEFINE_TYPE (GbpFileSearchIndex, gbp_file_search_index, IDE_TYPE_OBJECT)
 enum {
   PROP_0,
   PROP_ROOT_DIRECTORY,
+  PROP_MAX_DEPTH,
   LAST_PROP
 };
 
@@ -87,6 +90,10 @@ gbp_file_search_index_get_property (GObject    *object,
       g_value_set_object (value, self->root_directory);
       break;
 
+    case PROP_MAX_DEPTH:
+      g_value_set_int (value, self->max_depth);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -106,6 +113,10 @@ gbp_file_search_index_set_property (GObject      *object,
       gbp_file_search_index_set_root_directory (self, g_value_get_object (value));
       break;
 
+    case PROP_MAX_DEPTH:
+      self->max_depth = g_value_get_int (value);
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -127,6 +138,10 @@ gbp_file_search_index_class_init (GbpFileSearchIndexClass *klass)
                          G_TYPE_FILE,
                          (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_MAX_DEPTH] =
+    g_param_spec_int ("max-depth", NULL, NULL, 0, G_MAXINT, 0,
+                      (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, LAST_PROP, properties);
 }
 
@@ -140,6 +155,7 @@ populate_from_dir (DzlFuzzyMutableIndex *fuzzy,
                    IdeVcs               *vcs,
                    const gchar          *relpath,
                    GFile                *directory,
+                   gint                  depth,
                    GCancellable         *cancellable)
 {
   GFileEnumerator *enumerator;
@@ -150,6 +166,9 @@ populate_from_dir (DzlFuzzyMutableIndex *fuzzy,
   g_assert (G_IS_FILE (directory));
   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
 
+  if (depth <= 0)
+    return;
+
   if (ide_vcs_is_ignored (vcs, directory, NULL))
     return;
 
@@ -222,7 +241,7 @@ populate_from_dir (DzlFuzzyMutableIndex *fuzzy,
           if (relpath != NULL)
             path = g_build_filename (relpath, name, NULL);
 
-          populate_from_dir (fuzzy, vcs, path ? path : name, child, cancellable);
+          populate_from_dir (fuzzy, vcs, path ? path : name, child, depth - 1, cancellable);
         }
     }
 
@@ -242,6 +261,7 @@ gbp_file_search_index_builder (IdeTask      *task,
   GFile *directory = task_data;
   DzlFuzzyMutableIndex *fuzzy;
   gdouble elapsed;
+  gint max_depth;
 
   g_assert (IDE_IS_TASK (task));
   g_assert (GBP_IS_FILE_SEARCH_INDEX (self));
@@ -253,9 +273,13 @@ gbp_file_search_index_builder (IdeTask      *task,
 
   timer = g_timer_new ();
 
+  max_depth = self->max_depth;
+  if (max_depth <= 0)
+    max_depth = G_MAXINT;
+
   fuzzy = dzl_fuzzy_mutable_index_new (FALSE);
   dzl_fuzzy_mutable_index_begin_bulk_insert (fuzzy);
-  populate_from_dir (fuzzy, vcs, NULL, directory, cancellable);
+  populate_from_dir (fuzzy, vcs, NULL, directory, max_depth, cancellable);
   dzl_fuzzy_mutable_index_end_bulk_insert (fuzzy);
 
   self->fuzzy = fuzzy;
diff --git a/src/plugins/file-search/gbp-file-search-provider.c 
b/src/plugins/file-search/gbp-file-search-provider.c
index 92decf2ae..4f60f3c51 100644
--- a/src/plugins/file-search/gbp-file-search-provider.c
+++ b/src/plugins/file-search/gbp-file-search-provider.c
@@ -193,7 +193,9 @@ gbp_file_search_provider_vcs_changed_cb (GbpFileSearchProvider *self,
 {
   g_autoptr(GbpFileSearchIndex) index = NULL;
   g_autoptr(GFile) workdir = NULL;
+  g_autoptr(GFile) projects_dir = NULL;
   IdeContext *context;
+  gint max_depth = 0;
 
   IDE_ENTRY;
 
@@ -202,9 +204,20 @@ gbp_file_search_provider_vcs_changed_cb (GbpFileSearchProvider *self,
 
   context = ide_object_get_context (IDE_OBJECT (self));
   workdir = ide_context_ref_workdir (context);
+  projects_dir = g_file_new_for_path (ide_get_projects_dir ());
+
+  /* If the projects_dir is not a parent of workdir, then we don't want to
+   * index things, as it could end up being something way bigger than we can
+   * handle. Also ignore if workdir==projects_dir like can happen for new
+   * editor workspaces.
+   */
+  if (!ide_context_has_project (context) &&
+      !g_file_has_prefix (workdir, projects_dir))
+    max_depth = 5;
 
   index = g_object_new (GBP_TYPE_FILE_SEARCH_INDEX,
                         "root-directory", workdir,
+                        "max-depth", max_depth,
                         NULL);
 
   ide_object_append (IDE_OBJECT (self), IDE_OBJECT (index));


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