[gnome-builder] gui: add API to find command by identifier
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] gui: add API to find command by identifier
- Date: Wed, 7 Aug 2019 20:47:45 +0000 (UTC)
commit 233b77d52c84da78a20dee7f3fc7e73b5f68340a
Author: Christian Hergert <chergert redhat com>
Date: Wed Aug 7 13:29:42 2019 -0700
gui: add API to find command by identifier
This will be useful when executing commands via alternate means such as
shortcuts.
src/libide/gui/ide-command-manager.c | 65 +++++++++++++++++++++++++++++++++++
src/libide/gui/ide-command-manager.h | 25 ++++++++------
src/libide/gui/ide-command-provider.c | 27 +++++++++++++++
src/libide/gui/ide-command-provider.h | 43 +++++++++++++----------
src/libide/gui/ide-gui-private.h | 1 +
5 files changed, 133 insertions(+), 28 deletions(-)
---
diff --git a/src/libide/gui/ide-command-manager.c b/src/libide/gui/ide-command-manager.c
index d2d2635fa..9f438bd63 100644
--- a/src/libide/gui/ide-command-manager.c
+++ b/src/libide/gui/ide-command-manager.c
@@ -22,10 +22,13 @@
#include "config.h"
+#include <glib/gi18n.h>
#include <libpeas/peas.h>
#include <libide-plugins.h>
#include <libide-threading.h>
+#include "ide-gui-private.h"
+
#include "ide-command.h"
#include "ide-command-manager.h"
#include "ide-command-provider.h"
@@ -36,6 +39,13 @@ struct _IdeCommandManager
IdeExtensionSetAdapter *adapter;
};
+typedef struct
+{
+ IdeWorkspace *workspace;
+ const gchar *command_id;
+ IdeCommand *command;
+} FindById;
+
typedef struct
{
gchar *typed_text;
@@ -306,3 +316,58 @@ ide_command_manager_query_finish (IdeCommandManager *self,
ret = ide_task_propagate_pointer (IDE_TASK (result), error);
return IDE_PTR_ARRAY_STEAL_FULL (&ret);
}
+
+static void
+ide_command_manager_get_command_by_id_cb (IdeExtensionSetAdapter *set,
+ PeasPluginInfo *plugin_info,
+ PeasExtension *exten,
+ gpointer user_data)
+{
+ IdeCommandProvider *provider = (IdeCommandProvider *)exten;
+ FindById *state = user_data;
+
+ g_assert (IDE_IS_MAIN_THREAD ());
+ g_assert (IDE_IS_EXTENSION_SET_ADAPTER (set));
+ g_assert (plugin_info != NULL);
+ g_assert (IDE_IS_COMMAND_PROVIDER (provider));
+ g_assert (state != NULL);
+ g_assert (state->command_id != NULL);
+ g_assert (IDE_IS_WORKSPACE (state->workspace));
+ g_assert (!state->command || IDE_IS_COMMAND (state->command));
+
+ if (state->command == NULL)
+ state->command = ide_command_provider_get_command_by_id (provider,
+ state->workspace,
+ state->command_id);
+}
+
+/**
+ * ide_command_manager_get_command_by_id:
+ * @self: a #IdeCommandManager
+ * @workspace: an #IdeWorkspace
+ * @command_id: the identifier of the command
+ *
+ * Gets a command from one of the loaded command providers if any.
+ *
+ * Returns: (transfer full) (nullable): an #IdeCommand or %NULL
+ *
+ * Since: 3.34
+ */
+IdeCommand *
+ide_command_manager_get_command_by_id (IdeCommandManager *self,
+ IdeWorkspace *workspace,
+ const gchar *command_id)
+{
+ FindById state = { workspace, command_id, NULL };
+
+ g_return_val_if_fail (IDE_IS_COMMAND_MANAGER (self), NULL);
+ g_return_val_if_fail (self->adapter != NULL, NULL);
+ g_return_val_if_fail (IDE_IS_WORKSPACE (workspace), NULL);
+ g_return_val_if_fail (command_id != NULL, NULL);
+
+ ide_extension_set_adapter_foreach (self->adapter,
+ ide_command_manager_get_command_by_id_cb,
+ &state);
+
+ return g_steal_pointer (&state.command);
+}
diff --git a/src/libide/gui/ide-command-manager.h b/src/libide/gui/ide-command-manager.h
index e4fbd3d96..8c26469ac 100644
--- a/src/libide/gui/ide-command-manager.h
+++ b/src/libide/gui/ide-command-manager.h
@@ -22,6 +22,7 @@
#include <libide-core.h>
+#include "ide-command.h"
#include "ide-workspace.h"
G_BEGIN_DECLS
@@ -32,17 +33,21 @@ IDE_AVAILABLE_IN_3_34
G_DECLARE_FINAL_TYPE (IdeCommandManager, ide_command_manager, IDE, COMMAND_MANAGER, IdeObject)
IDE_AVAILABLE_IN_3_34
-IdeCommandManager *ide_command_manager_from_context (IdeContext *context);
+IdeCommandManager *ide_command_manager_from_context (IdeContext *context);
IDE_AVAILABLE_IN_3_34
-void ide_command_manager_query_async (IdeCommandManager *self,
- IdeWorkspace *workspace,
- const gchar *typed_text,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data);
+IdeCommand *ide_command_manager_get_command_by_id (IdeCommandManager *self,
+ IdeWorkspace *workspace,
+ const gchar *command_id);
IDE_AVAILABLE_IN_3_34
-GPtrArray *ide_command_manager_query_finish (IdeCommandManager *self,
- GAsyncResult *result,
- GError **error);
+void ide_command_manager_query_async (IdeCommandManager *self,
+ IdeWorkspace *workspace,
+ const gchar *typed_text,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+IDE_AVAILABLE_IN_3_34
+GPtrArray *ide_command_manager_query_finish (IdeCommandManager *self,
+ GAsyncResult *result,
+ GError **error);
G_END_DECLS
diff --git a/src/libide/gui/ide-command-provider.c b/src/libide/gui/ide-command-provider.c
index 8b0177ab9..45c625fb0 100644
--- a/src/libide/gui/ide-command-provider.c
+++ b/src/libide/gui/ide-command-provider.c
@@ -101,3 +101,30 @@ ide_command_provider_query_finish (IdeCommandProvider *self,
return IDE_COMMAND_PROVIDER_GET_IFACE (self)->query_finish (self, result, error);
}
+
+/**
+ * ide_command_provider_get_command_by_id:
+ * @self: a #IdeCommandProvider
+ * @workspace: an #IdeWorkspace
+ * @command_id: the identifier of the command
+ *
+ * Looks for a command by @command_id and returns it if found.
+ *
+ * Returns: (transfer full) (nullable): an #IdeCommand or %NULL
+ *
+ * Since: 3.34
+ */
+IdeCommand *
+ide_command_provider_get_command_by_id (IdeCommandProvider *self,
+ IdeWorkspace *workspace,
+ const gchar *command_id)
+{
+ g_return_val_if_fail (IDE_IS_COMMAND_PROVIDER (self), NULL);
+ g_return_val_if_fail (IDE_IS_WORKSPACE (workspace), NULL);
+ g_return_val_if_fail (command_id != NULL, NULL);
+
+ if (IDE_COMMAND_PROVIDER_GET_IFACE (self)->get_command_by_id)
+ return IDE_COMMAND_PROVIDER_GET_IFACE (self)->get_command_by_id (self, workspace, command_id);
+
+ return NULL;
+}
diff --git a/src/libide/gui/ide-command-provider.h b/src/libide/gui/ide-command-provider.h
index 5fc08f06b..2fe0d9b6d 100644
--- a/src/libide/gui/ide-command-provider.h
+++ b/src/libide/gui/ide-command-provider.h
@@ -40,27 +40,34 @@ struct _IdeCommandProviderInterface
{
GTypeInterface parent_iface;
- void (*query_async) (IdeCommandProvider *self,
- IdeWorkspace *workspace,
- const gchar *typed_text,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data);
- GPtrArray *(*query_finish) (IdeCommandProvider *self,
- GAsyncResult *result,
- GError **error);
+ void (*query_async) (IdeCommandProvider *self,
+ IdeWorkspace *workspace,
+ const gchar *typed_text,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+ GPtrArray *(*query_finish) (IdeCommandProvider *self,
+ GAsyncResult *result,
+ GError **error);
+ IdeCommand *(*get_command_by_id) (IdeCommandProvider *self,
+ IdeWorkspace *workspace,
+ const gchar *command_id);
};
IDE_AVAILABLE_IN_3_32
-void ide_command_provider_query_async (IdeCommandProvider *self,
- IdeWorkspace *workspace,
- const gchar *typed_text,
- GCancellable *cancellable,
- GAsyncReadyCallback callback,
- gpointer user_data);
+void ide_command_provider_query_async (IdeCommandProvider *self,
+ IdeWorkspace *workspace,
+ const gchar *typed_text,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
IDE_AVAILABLE_IN_3_32
-GPtrArray *ide_command_provider_query_finish (IdeCommandProvider *self,
- GAsyncResult *result,
- GError **error);
+GPtrArray *ide_command_provider_query_finish (IdeCommandProvider *self,
+ GAsyncResult *result,
+ GError **error);
+IDE_AVAILABLE_IN_3_34
+IdeCommand *ide_command_provider_get_command_by_id (IdeCommandProvider *self,
+ IdeWorkspace *workspace,
+ const gchar *command_id);
G_END_DECLS
diff --git a/src/libide/gui/ide-gui-private.h b/src/libide/gui/ide-gui-private.h
index 764b923c3..9567f139d 100644
--- a/src/libide/gui/ide-gui-private.h
+++ b/src/libide/gui/ide-gui-private.h
@@ -27,6 +27,7 @@
#include <libide-core.h>
#include <libide-projects.h>
+#include "ide-command-manager.h"
#include "ide-frame.h"
#include "ide-frame-header.h"
#include "ide-grid.h"
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]