[gnome-builder] plugins/shellcmd: implement shellcmd global search



commit 68c3d731f789fbb939f9393566031d21ce24eb78
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jul 15 16:16:09 2022 -0700

    plugins/shellcmd: implement shellcmd global search

 .../shellcmd/gbp-shellcmd-search-provider.c        |  41 +++++-
 src/plugins/shellcmd/gbp-shellcmd-search-result.c  | 141 +++++++++++++++++++++
 src/plugins/shellcmd/gbp-shellcmd-search-result.h  |  36 ++++++
 src/plugins/shellcmd/meson.build                   |   1 +
 4 files changed, 218 insertions(+), 1 deletion(-)
---
diff --git a/src/plugins/shellcmd/gbp-shellcmd-search-provider.c 
b/src/plugins/shellcmd/gbp-shellcmd-search-provider.c
index 3bf0164a9..878f46358 100644
--- a/src/plugins/shellcmd/gbp-shellcmd-search-provider.c
+++ b/src/plugins/shellcmd/gbp-shellcmd-search-provider.c
@@ -24,10 +24,12 @@
 
 #include <libide-gui.h>
 #include <libide-search.h>
+#include <libide-sourceview.h>
 #include <libide-threading.h>
 
 #include "gbp-shellcmd-command-model.h"
 #include "gbp-shellcmd-search-provider.h"
+#include "gbp-shellcmd-search-result.h"
 #include "gbp-shellcmd-run-command.h"
 
 struct _GbpShellcmdSearchProvider
@@ -36,6 +38,33 @@ struct _GbpShellcmdSearchProvider
   GListModel *commands;
 };
 
+static GIcon *result_icon;
+
+static gboolean
+filter_func (gpointer item,
+             gpointer user_data)
+{
+  GbpShellcmdRunCommand *run_command = item;
+  const char *query = user_data;
+  const char *keywords;
+  guint prio = 0;
+
+  g_assert (GBP_IS_SHELLCMD_RUN_COMMAND (run_command));
+  g_assert (query != NULL);
+
+  if (!(keywords = gbp_shellcmd_run_command_get_keywords (run_command)))
+    return FALSE;
+
+  return gtk_source_completion_fuzzy_match (keywords, query, &prio);
+}
+
+static gpointer
+map_func (gpointer item,
+          gpointer user_data)
+{
+  return gbp_shellcmd_search_result_new (item, result_icon);
+}
+
 static void
 gbp_shellcmd_search_provider_search_async (IdeSearchProvider   *provider,
                                            const char          *query,
@@ -45,18 +74,27 @@ gbp_shellcmd_search_provider_search_async (IdeSearchProvider   *provider,
                                            gpointer             user_data)
 {
   GbpShellcmdSearchProvider *self = (GbpShellcmdSearchProvider *)provider;
+  g_autoptr(GtkFilterListModel) list = NULL;
+  g_autoptr(GtkMapListModel) map = NULL;
+  g_autoptr(GtkCustomFilter) filter = NULL;
   g_autoptr(IdeTask) task = NULL;
 
   IDE_ENTRY;
 
   g_assert (IDE_IS_MAIN_THREAD ());
   g_assert (GBP_IS_SHELLCMD_SEARCH_PROVIDER (self));
+  g_assert (query != NULL);
   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
 
   task = ide_task_new (self, cancellable, callback, user_data);
   ide_task_set_source_tag (task, gbp_shellcmd_search_provider_search_async);
 
-  ide_task_return_unsupported_error (task);
+  filter = gtk_custom_filter_new (filter_func, g_strdup (query), g_free);
+  list = gtk_filter_list_model_new (g_object_ref (G_LIST_MODEL (self->commands)),
+                                    g_object_ref (GTK_FILTER (filter)));
+  map = gtk_map_list_model_new (g_object_ref (G_LIST_MODEL (list)), map_func, NULL, NULL);
+
+  ide_task_return_pointer (task, g_steal_pointer (&map), g_object_unref);
 
   IDE_EXIT;
 }
@@ -142,6 +180,7 @@ G_DEFINE_FINAL_TYPE_WITH_CODE (GbpShellcmdSearchProvider, gbp_shellcmd_search_pr
 static void
 gbp_shellcmd_search_provider_class_init (GbpShellcmdSearchProviderClass *klass)
 {
+  result_icon = g_themed_icon_new ("builder-terminal-symbolic");
 }
 
 static void
diff --git a/src/plugins/shellcmd/gbp-shellcmd-search-result.c 
b/src/plugins/shellcmd/gbp-shellcmd-search-result.c
new file mode 100644
index 000000000..bd1a411d1
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-search-result.c
@@ -0,0 +1,141 @@
+/* gbp-shellcmd-search-result.c
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "gbp-shellcmd-search-result"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include <libide-editor.h>
+#include <libide-gui.h>
+#include <libide-terminal.h>
+
+#include "gbp-shellcmd-search-result.h"
+
+struct _GbpShellcmdSearchResult
+{
+  IdeSearchResult        parent_instance;
+  GbpShellcmdRunCommand *run_command;
+};
+
+G_DEFINE_FINAL_TYPE (GbpShellcmdSearchResult, gbp_shellcmd_search_result, IDE_TYPE_SEARCH_RESULT)
+
+static void
+gbp_shellcmd_search_result_activate (IdeSearchResult *result,
+                                     GtkWidget       *last_focus)
+{
+  GbpShellcmdSearchResult *self = (GbpShellcmdSearchResult *)result;
+  g_autoptr(IdeTerminalLauncher) launcher = NULL;
+  g_autoptr(IdePanelPosition) position = NULL;
+  IdeWorkspace *workspace;
+  IdeContext *context;
+  const char *title;
+  IdePage *page;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_SEARCH_RESULT (result));
+  g_assert (GTK_IS_WIDGET (last_focus));
+
+  if (!(workspace = ide_widget_get_workspace (last_focus)) ||
+      !(context = ide_workspace_get_context (workspace)))
+    IDE_EXIT;
+
+  if (!IDE_IS_PRIMARY_WORKSPACE (workspace) &&
+      !IDE_IS_EDITOR_WORKSPACE (workspace))
+    IDE_EXIT;
+
+  if (!(title = ide_run_command_get_display_name (IDE_RUN_COMMAND (self->run_command))))
+    title = _("Untitled command");
+
+  launcher = ide_terminal_launcher_new (context, IDE_RUN_COMMAND (self->run_command));
+
+  page = g_object_new (IDE_TYPE_TERMINAL_PAGE,
+                       "close-on-exit", FALSE,
+                       "icon-name", "text-x-script-symbolic",
+                       "launcher", launcher,
+                       "manage-spawn", TRUE,
+                       "respawn-on-exit", FALSE,
+                       "title", title,
+                       NULL);
+
+  position = ide_panel_position_new ();
+
+  ide_workspace_add_page (workspace, page, position);
+  panel_widget_raise (PANEL_WIDGET (page));
+  gtk_widget_grab_focus (GTK_WIDGET (page));
+
+  IDE_EXIT;
+}
+
+static void
+gbp_shellcmd_search_result_dispose (GObject *object)
+{
+  GbpShellcmdSearchResult *self = (GbpShellcmdSearchResult *)object;
+
+  g_clear_object (&self->run_command);
+
+  G_OBJECT_CLASS (gbp_shellcmd_search_result_parent_class)->dispose (object);
+}
+
+static void
+gbp_shellcmd_search_result_class_init (GbpShellcmdSearchResultClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  IdeSearchResultClass *search_result_class = IDE_SEARCH_RESULT_CLASS (klass);
+
+  object_class->dispose = gbp_shellcmd_search_result_dispose;
+
+  search_result_class->activate = gbp_shellcmd_search_result_activate;
+}
+
+static void
+gbp_shellcmd_search_result_init (GbpShellcmdSearchResult *self)
+{
+}
+
+/**
+ * gbp_shellcmd_search_result_new:
+ * @run_command: (transfer full): a #GbpShellcmdRunCommand
+ *
+ * Returns: (transfer full): a new GbpShellcmdSearchResult
+ */
+GbpShellcmdSearchResult *
+gbp_shellcmd_search_result_new (GbpShellcmdRunCommand *run_command,
+                                GIcon                 *gicon)
+{
+  GbpShellcmdSearchResult *self;
+  g_autofree char *subtitle = NULL;
+
+  g_return_val_if_fail (GBP_IS_SHELLCMD_RUN_COMMAND (run_command), NULL);
+
+  subtitle = gbp_shellcmd_run_command_dup_subtitle (run_command);
+
+  self = g_object_new (GBP_TYPE_SHELLCMD_SEARCH_RESULT,
+                       "title", ide_run_command_get_display_name (IDE_RUN_COMMAND (run_command)),
+                       "subtitle", subtitle,
+                       "gicon", gicon,
+                       NULL);
+
+  self->run_command = g_steal_pointer (&run_command);
+
+  return self;
+}
diff --git a/src/plugins/shellcmd/gbp-shellcmd-search-result.h 
b/src/plugins/shellcmd/gbp-shellcmd-search-result.h
new file mode 100644
index 000000000..67345d335
--- /dev/null
+++ b/src/plugins/shellcmd/gbp-shellcmd-search-result.h
@@ -0,0 +1,36 @@
+/* gbp-shellcmd-search-result.h
+ *
+ * Copyright 2022 Christian Hergert <chergert redhat com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libide-search.h>
+
+#include "gbp-shellcmd-run-command.h"
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SHELLCMD_SEARCH_RESULT (gbp_shellcmd_search_result_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpShellcmdSearchResult, gbp_shellcmd_search_result, GBP, SHELLCMD_SEARCH_RESULT, 
IdeSearchResult)
+
+GbpShellcmdSearchResult *gbp_shellcmd_search_result_new (GbpShellcmdRunCommand *run_command,
+                                                         GIcon                 *gicon);
+
+G_END_DECLS
diff --git a/src/plugins/shellcmd/meson.build b/src/plugins/shellcmd/meson.build
index e67207ae3..8c54dcd34 100644
--- a/src/plugins/shellcmd/meson.build
+++ b/src/plugins/shellcmd/meson.build
@@ -8,6 +8,7 @@ plugins_sources += files([
   'gbp-shellcmd-run-command.c',
   'gbp-shellcmd-run-command-provider.c',
   'gbp-shellcmd-search-provider.c',
+  'gbp-shellcmd-search-result.c',
   'gbp-shellcmd-shortcut-provider.c',
 ])
 


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