[gnome-builder/wip/chergert/code-index-revamp: 10/16] code-index: add phase to load build flags



commit 69d2a0cfe04a023e3a6c0c656860a6208c78476c
Author: Christian Hergert <chergert redhat com>
Date:   Fri Feb 1 13:01:31 2019 -0800

    code-index: add phase to load build flags

 .../code-index/gbp-code-index-application-addin.c  |  52 ++++++-
 src/plugins/code-index/gbp-code-index-plan.c       | 151 +++++++++++++++++++++
 src/plugins/code-index/gbp-code-index-plan.h       |   8 ++
 3 files changed, 205 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/code-index/gbp-code-index-application-addin.c 
b/src/plugins/code-index/gbp-code-index-application-addin.c
index 337dc1123..043f26ef3 100644
--- a/src/plugins/code-index/gbp-code-index-application-addin.c
+++ b/src/plugins/code-index/gbp-code-index-application-addin.c
@@ -89,16 +89,51 @@ gbp_code_index_application_addin_foreach_cb (GFile              *directory,
     {
       const GbpCodeIndexPlanItem *item = g_ptr_array_index (plan_items, i);
       const gchar *name = g_file_info_get_name (item->file_info);
+      g_autofree gchar *flags = NULL;
+
+      if (item->build_flags)
+        flags = g_strjoinv ("' '", item->build_flags);
 
       g_application_command_line_print (cmdline,
-                                        "  %s [indexer=%s]\n",
+                                        "  %s [indexer=%s] -- '%s'\n",
                                         name,
-                                        item->indexer_module_name);
+                                        item->indexer_module_name,
+                                        flags ? flags : "");
     }
 
   return FALSE;
 }
 
+static void
+gbp_code_index_application_addin_load_flags_cb (GObject      *object,
+                                                GAsyncResult *result,
+                                                gpointer      user_data)
+{
+  GbpCodeIndexPlan *plan = (GbpCodeIndexPlan *)object;
+  g_autoptr(GApplicationCommandLine) cmdline = user_data;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODE_INDEX_PLAN (plan));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (G_IS_APPLICATION_COMMAND_LINE (cmdline));
+
+  if (!gbp_code_index_plan_load_flags_finish (plan, result, &error))
+    {
+      g_application_command_line_printerr (cmdline,
+                                           _("Failed to load flags for plan: %s"),
+                                           error->message);
+      g_application_command_line_set_exit_status (cmdline, EXIT_FAILURE);
+      return;
+    }
+
+  gbp_code_index_plan_foreach (plan,
+                               gbp_code_index_application_addin_foreach_cb,
+                               cmdline);
+
+  g_application_command_line_set_exit_status (cmdline, EXIT_SUCCESS);
+}
+
 static void
 gbp_code_index_application_addin_cull_cb (GObject      *object,
                                           GAsyncResult *result,
@@ -107,6 +142,8 @@ gbp_code_index_application_addin_cull_cb (GObject      *object,
   GbpCodeIndexPlan *plan = (GbpCodeIndexPlan *)object;
   g_autoptr(GApplicationCommandLine) cmdline = user_data;
   g_autoptr(GError) error = NULL;
+  IdeWorkbench *workbench;
+  IdeContext *context;
 
   g_assert (IDE_IS_MAIN_THREAD ());
   g_assert (GBP_IS_CODE_INDEX_PLAN (plan));
@@ -122,11 +159,14 @@ gbp_code_index_application_addin_cull_cb (GObject      *object,
       return;
     }
 
-  gbp_code_index_plan_foreach (plan,
-                               gbp_code_index_application_addin_foreach_cb,
-                               cmdline);
+  workbench = g_object_get_data (G_OBJECT (cmdline), "WORKBENCH");
+  context = ide_workbench_get_context (workbench);
 
-  g_application_command_line_set_exit_status (cmdline, EXIT_SUCCESS);
+  gbp_code_index_plan_load_flags_async (plan,
+                                        context,
+                                        NULL,
+                                        gbp_code_index_application_addin_load_flags_cb,
+                                        g_steal_pointer (&cmdline));
 }
 
 static void
diff --git a/src/plugins/code-index/gbp-code-index-plan.c b/src/plugins/code-index/gbp-code-index-plan.c
index 13f80d298..fee182c5a 100644
--- a/src/plugins/code-index/gbp-code-index-plan.c
+++ b/src/plugins/code-index/gbp-code-index-plan.c
@@ -620,3 +620,154 @@ gbp_code_index_plan_populate_finish (GbpCodeIndexPlan  *self,
 
   return ide_task_propagate_boolean (IDE_TASK (result), error);
 }
+
+static gboolean
+gbp_code_index_plan_collect_files (GFile              *directory,
+                                   GPtrArray          *plan_items,
+                                   GbpCodeIndexReason  reason,
+                                   gpointer            user_data)
+{
+  GPtrArray *files = user_data;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (G_IS_FILE (directory));
+  g_assert (plan_items != NULL);
+  g_assert (files != NULL);
+
+  /* Skip if we don't care about these items */
+  if (reason == GBP_CODE_INDEX_REASON_REMOVE_INDEX)
+    return FALSE;
+
+  for (guint i = 0; i < plan_items->len; i++)
+    {
+      const GbpCodeIndexPlanItem *item = g_ptr_array_index (plan_items, i);
+      const gchar *name = g_file_info_get_name (item->file_info);
+
+      g_ptr_array_add (files, g_file_get_child (directory, name));
+    }
+
+  return FALSE;
+}
+
+static gboolean
+gbp_code_index_plan_fill_build_flags_cb (GFile              *directory,
+                                         GPtrArray          *plan_items,
+                                         GbpCodeIndexReason  reason,
+                                         gpointer            user_data)
+{
+  GHashTable *build_flags = user_data;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (G_IS_FILE (directory));
+  g_assert (plan_items != NULL);
+  g_assert (build_flags != NULL);
+
+  for (guint i = 0; i < plan_items->len; i++)
+    {
+      GbpCodeIndexPlanItem *item = g_ptr_array_index (plan_items, i);
+      const gchar *name = g_file_info_get_name (item->file_info);
+      g_autoptr(GFile) file = g_file_get_child (directory, name);
+      gchar **item_flags;
+
+      if ((item_flags = g_hash_table_lookup (build_flags, file)))
+        {
+          /* Implausible, but lets clear anyway */
+          g_clear_pointer (&item->build_flags, g_strfreev);
+          item->build_flags = g_strdupv (item_flags);
+        }
+    }
+
+  return FALSE;
+}
+
+static void
+gbp_code_index_plan_get_build_flags_cb (GObject      *object,
+                                        GAsyncResult *result,
+                                        gpointer      user_data)
+{
+  IdeBuildSystem *build_system = (IdeBuildSystem *)object;
+  g_autoptr(GHashTable) build_flags = NULL;
+  g_autoptr(IdeTask) task = user_data;
+  g_autoptr(GError) error = NULL;
+  GbpCodeIndexPlan *self;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_BUILD_SYSTEM (build_system));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_TASK (task));
+
+  if (!(build_flags = ide_build_system_get_build_flags_for_files_finish (build_system, result, &error)))
+    {
+      ide_task_return_error (task, g_steal_pointer (&error));
+      IDE_EXIT;
+    }
+
+  self = ide_task_get_source_object (task);
+
+  gbp_code_index_plan_foreach (self,
+                               gbp_code_index_plan_fill_build_flags_cb,
+                               build_flags);
+
+  ide_task_return_boolean (task, TRUE);
+
+  IDE_EXIT;
+}
+
+void
+gbp_code_index_plan_load_flags_async (GbpCodeIndexPlan    *self,
+                                      IdeContext          *context,
+                                      GCancellable        *cancellable,
+                                      GAsyncReadyCallback  callback,
+                                      gpointer             user_data)
+{
+  g_autoptr(IdeTask) task = NULL;
+  g_autoptr(GPtrArray) files = NULL;
+  IdeBuildSystem *build_system;
+
+  IDE_ENTRY;
+
+  g_return_if_fail (IDE_IS_MAIN_THREAD ());
+  g_return_if_fail (GBP_IS_CODE_INDEX_PLAN (self));
+  g_return_if_fail (IDE_IS_CONTEXT (context));
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  task = ide_task_new (self, cancellable, callback, user_data);
+  ide_task_set_source_tag (task, gbp_code_index_plan_load_flags_async);
+
+  /* Get build system to query */
+  build_system = ide_build_system_from_context (context);
+
+  /* Create array of files for every file we know about */
+  files = g_ptr_array_new_with_free_func (g_object_unref);
+  gbp_code_index_plan_foreach (self,
+                               gbp_code_index_plan_collect_files,
+                               files);
+
+  ide_build_system_get_build_flags_for_files_async (build_system,
+                                                    files,
+                                                    cancellable,
+                                                    gbp_code_index_plan_get_build_flags_cb,
+                                                    g_steal_pointer (&task));
+
+  IDE_EXIT;
+}
+
+gboolean
+gbp_code_index_plan_load_flags_finish (GbpCodeIndexPlan  *self,
+                                       GAsyncResult      *result,
+                                       GError           **error)
+{
+  gboolean ret;
+
+  IDE_ENTRY;
+
+  g_return_val_if_fail (IDE_IS_MAIN_THREAD (), FALSE);
+  g_return_val_if_fail (GBP_IS_CODE_INDEX_PLAN (self), FALSE);
+  g_return_val_if_fail (IDE_IS_TASK (result), FALSE);
+
+  ret = ide_task_propagate_boolean (IDE_TASK (result), error);
+
+  IDE_RETURN (ret);
+}
diff --git a/src/plugins/code-index/gbp-code-index-plan.h b/src/plugins/code-index/gbp-code-index-plan.h
index a754932b0..41e708194 100644
--- a/src/plugins/code-index/gbp-code-index-plan.h
+++ b/src/plugins/code-index/gbp-code-index-plan.h
@@ -73,6 +73,14 @@ void              gbp_code_index_plan_cull_indexed_async  (GbpCodeIndexPlan
 gboolean          gbp_code_index_plan_cull_indexed_finish (GbpCodeIndexPlan         *self,
                                                            GAsyncResult             *result,
                                                            GError                  **error);
+void              gbp_code_index_plan_load_flags_async    (GbpCodeIndexPlan         *self,
+                                                           IdeContext               *context,
+                                                           GCancellable             *cancellable,
+                                                           GAsyncReadyCallback       callback,
+                                                           gpointer                  user_data);
+gboolean          gbp_code_index_plan_load_flags_finish   (GbpCodeIndexPlan         *self,
+                                                           GAsyncResult             *result,
+                                                           GError                  **error);
 void              gbp_code_index_plan_foreach             (GbpCodeIndexPlan         *self,
                                                            GbpCodeIndexPlanForeach   foreach_func,
                                                            gpointer                  foreach_data);


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