[gnome-builder: 10/17] SearchEngine: enhance search engine with custom providers



commit 774b12b5ef6c63c18d4bad87b442654e4bebab87
Author: Günther Wagner <info gunibert de>
Date:   Sun May 31 11:21:51 2020 +0200

    SearchEngine: enhance search engine with custom providers

 src/libide/lsp/libide-lsp.h           |  1 +
 src/libide/search/ide-search-engine.c | 89 ++++++++++++++++++++++++++++++++---
 src/libide/search/ide-search-engine.h | 30 +++++++-----
 3 files changed, 101 insertions(+), 19 deletions(-)
---
diff --git a/src/libide/lsp/libide-lsp.h b/src/libide/lsp/libide-lsp.h
index 7f7c713a4..c8f3e56f4 100644
--- a/src/libide/lsp/libide-lsp.h
+++ b/src/libide/lsp/libide-lsp.h
@@ -39,5 +39,6 @@
 #include "ide-lsp-symbol-node.h"
 #include "ide-lsp-symbol-resolver.h"
 #include "ide-lsp-symbol-tree.h"
+#include "ide-lsp-util.h"
 
 #undef IDE_LSP_INSIDE
diff --git a/src/libide/search/ide-search-engine.c b/src/libide/search/ide-search-engine.c
index 3e2246df4..5568fab68 100644
--- a/src/libide/search/ide-search-engine.c
+++ b/src/libide/search/ide-search-engine.c
@@ -36,6 +36,7 @@ struct _IdeSearchEngine
 {
   IdeObject         parent_instance;
   PeasExtensionSet *extensions;
+  GPtrArray        *custom_provider;
   guint             active_count;
 };
 
@@ -139,6 +140,7 @@ ide_search_engine_destroy (IdeObject *object)
   IdeSearchEngine *self = (IdeSearchEngine *)object;
 
   g_clear_object (&self->extensions);
+  g_clear_object (&self->custom_provider);
 
   IDE_OBJECT_CLASS (ide_search_engine_parent_class)->destroy (object);
 }
@@ -186,6 +188,7 @@ ide_search_engine_class_init (IdeSearchEngineClass *klass)
 static void
 ide_search_engine_init (IdeSearchEngine *self)
 {
+  self->custom_provider = g_ptr_array_new ();
 }
 
 IdeSearchEngine *
@@ -264,6 +267,27 @@ cleanup:
     ide_task_return_pointer (task, g_steal_pointer (&r->store), g_object_unref);
 }
 
+static void
+_provider_search_async (IdeSearchProvider *provider,
+                        Request           *request)
+{
+  g_assert (IDE_IS_SEARCH_PROVIDER (provider));
+  g_assert (request != NULL);
+  g_assert (IDE_IS_TASK (request->task));
+  g_assert (G_IS_LIST_STORE (request->store));
+
+  request->outstanding++;
+
+  ide_search_provider_search_async (provider,
+                                    request->query,
+                                    request->max_results,
+                                    ide_task_get_cancellable (request->task),
+                                    ide_search_engine_search_cb,
+                                    g_object_ref (request->task));
+
+
+}
+
 static void
 ide_search_engine_search_foreach (PeasExtensionSet *set,
                                   PeasPluginInfo   *plugin_info,
@@ -280,14 +304,22 @@ ide_search_engine_search_foreach (PeasExtensionSet *set,
   g_assert (IDE_IS_TASK (r->task));
   g_assert (G_IS_LIST_STORE (r->store));
 
-  r->outstanding++;
+  _provider_search_async (provider, r);
+}
 
-  ide_search_provider_search_async (provider,
-                                    r->query,
-                                    r->max_results,
-                                    ide_task_get_cancellable (r->task),
-                                    ide_search_engine_search_cb,
-                                    g_object_ref (r->task));
+static void
+ide_search_engine_search_foreach_custom_provider (gpointer data,
+                                                  gpointer user_data)
+{
+  IdeSearchProvider *provider = (IdeSearchProvider *)data;
+  Request *r = user_data;
+
+  g_assert (IDE_IS_SEARCH_PROVIDER (provider));
+  g_assert (r != NULL);
+  g_assert (IDE_IS_TASK (r->task));
+  g_assert (G_IS_LIST_STORE (r->store));
+
+  _provider_search_async (provider, r);
 }
 
 void
@@ -322,6 +354,9 @@ ide_search_engine_search_async (IdeSearchEngine     *self,
   peas_extension_set_foreach (self->extensions,
                               ide_search_engine_search_foreach,
                               r);
+  g_ptr_array_foreach (self->custom_provider,
+                       ide_search_engine_search_foreach_custom_provider,
+                       r);
 
   self->active_count += r->outstanding;
 
@@ -357,3 +392,43 @@ ide_search_engine_search_finish (IdeSearchEngine  *self,
 
   return ide_task_propagate_pointer (IDE_TASK (result), error);
 }
+
+/**
+ * ide_search_engine_add_provider:
+ * @self: a #IdeSearchEngine
+ * @provider: a #IdeSearchProvider
+ *
+ * Adds a custom search provider to the #IdeSearchEngine. This enables
+ * to bring in custom #IdeSearchProvider during the runtime.
+ *
+ * Since: 3.38
+ */
+void
+ide_search_engine_add_provider (IdeSearchEngine   *self,
+                                IdeSearchProvider *provider)
+{
+  g_return_if_fail (IDE_IS_SEARCH_ENGINE (self));
+  g_return_if_fail (IDE_IS_SEARCH_PROVIDER (provider));
+
+  g_ptr_array_add (self->custom_provider, provider);
+}
+
+/**
+ * ide_search_engine_remove_provider:
+ * @self: a #IdeSearchEngine
+ * @provider: a #IdeSearchProvider
+ *
+ * Remove a custom search provider from the #IdeSearchEngine. This removes
+ * custom #IdeSearchProvider during the runtime.
+ *
+ * Since: 3.38
+ */
+void
+ide_search_engine_remove_provider (IdeSearchEngine   *self,
+                                   IdeSearchProvider *provider)
+{
+  g_return_if_fail (IDE_IS_SEARCH_ENGINE (self));
+  g_return_if_fail (IDE_IS_SEARCH_PROVIDER (provider));
+
+  g_ptr_array_remove (self->custom_provider, provider);
+}
diff --git a/src/libide/search/ide-search-engine.h b/src/libide/search/ide-search-engine.h
index bb9ef1a73..5bbcfed2c 100644
--- a/src/libide/search/ide-search-engine.h
+++ b/src/libide/search/ide-search-engine.h
@@ -25,6 +25,7 @@
 #endif
 
 #include <libide-core.h>
+#include "ide-search-provider.h"
 
 G_BEGIN_DECLS
 
@@ -32,21 +33,26 @@ G_BEGIN_DECLS
 
 IDE_AVAILABLE_IN_3_32
 G_DECLARE_FINAL_TYPE (IdeSearchEngine, ide_search_engine, IDE, SEARCH_ENGINE, IdeObject)
-
 IDE_AVAILABLE_IN_3_32
-IdeSearchEngine *ide_search_engine_new           (void);
+IdeSearchEngine *ide_search_engine_new             (void);
 IDE_AVAILABLE_IN_3_32
-gboolean         ide_search_engine_get_busy      (IdeSearchEngine      *self);
+gboolean         ide_search_engine_get_busy        (IdeSearchEngine      *self);
 IDE_AVAILABLE_IN_3_32
-void             ide_search_engine_search_async  (IdeSearchEngine      *self,
-                                                  const gchar          *query,
-                                                  guint                 max_results,
-                                                  GCancellable         *cancellable,
-                                                  GAsyncReadyCallback   callback,
-                                                  gpointer              user_data);
+void             ide_search_engine_search_async    (IdeSearchEngine      *self,
+                                                    const gchar          *query,
+                                                    guint                 max_results,
+                                                    GCancellable         *cancellable,
+                                                    GAsyncReadyCallback   callback,
+                                                    gpointer              user_data);
 IDE_AVAILABLE_IN_3_32
-GListModel      *ide_search_engine_search_finish (IdeSearchEngine      *self,
-                                                  GAsyncResult         *result,
-                                                  GError              **error);
+GListModel      *ide_search_engine_search_finish   (IdeSearchEngine      *self,
+                                                    GAsyncResult         *result,
+                                                    GError              **error);
+IDE_AVAILABLE_IN_3_36
+void             ide_search_engine_add_provider    (IdeSearchEngine      *self,
+                                                    IdeSearchProvider    *provider);
+IDE_AVAILABLE_IN_3_36
+void             ide_search_engine_remove_provider (IdeSearchEngine      *self,
+                                                    IdeSearchProvider    *provider);
 
 G_END_DECLS


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