1 /* gbp-vim-command-provider.c
2  *
3  * Copyright 2018-2019 Christian Hergert <chergert@redhat.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * SPDX-License-Identifier: GPL-3.0-or-later
19  */
20 
21 #define G_LOG_DOMAIN "gbp-vim-command-provider"
22 
23 #include "config.h"
24 
25 #include <glib/gi18n.h>
26 #include <libide-editor.h>
27 #include <libide-gui.h>
28 #include <libide-threading.h>
29 
30 #include "gb-vim.h"
31 #include "gbp-vim-command.h"
32 #include "gbp-vim-command-provider.h"
33 
34 struct _GbpVimCommandProvider
35 {
36   GObject parent_instance;
37 };
38 
39 static void
gbp_vim_command_provider_query_async(IdeCommandProvider * provider,IdeWorkspace * workspace,const gchar * typed_text,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)40 gbp_vim_command_provider_query_async (IdeCommandProvider  *provider,
41                                       IdeWorkspace        *workspace,
42                                       const gchar         *typed_text,
43                                       GCancellable        *cancellable,
44                                       GAsyncReadyCallback  callback,
45                                       gpointer             user_data)
46 {
47   GbpVimCommandProvider *self = (GbpVimCommandProvider *)provider;
48   g_autoptr(IdeTask) task = NULL;
49   g_autoptr(GPtrArray) results = NULL;
50   g_autofree const gchar **commands = NULL;
51   g_autofree const gchar **descriptions = NULL;
52   IdePage *page;
53 
54   g_assert (GBP_IS_VIM_COMMAND_PROVIDER (self));
55   g_assert (IDE_IS_WORKSPACE (workspace));
56   g_assert (typed_text != NULL);
57   g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
58 
59   task = ide_task_new (self, cancellable, callback, user_data);
60   ide_task_set_source_tag (task, gbp_vim_command_provider_query_async);
61 
62   results = g_ptr_array_new_with_free_func (g_object_unref);
63   page = ide_workspace_get_most_recent_page (workspace);
64 
65   if (!IDE_IS_EDITOR_PAGE (page))
66     goto no_active_widget;
67 
68   commands = gb_vim_commands (typed_text, &descriptions);
69 
70   for (guint i = 0; commands[i]; i++)
71     {
72       g_autoptr(GbpVimCommand) command = NULL;
73 
74       command = gbp_vim_command_new (GTK_WIDGET (page),
75                                      typed_text,
76                                      g_dgettext (GETTEXT_PACKAGE, commands[i]),
77                                      g_dgettext (GETTEXT_PACKAGE, descriptions[i]));
78       g_ptr_array_add (results, g_steal_pointer (&command));
79     }
80 
81 no_active_widget:
82   ide_task_return_pointer (task,
83                            g_steal_pointer (&results),
84                            g_ptr_array_unref);
85 }
86 
87 static GPtrArray *
gbp_vim_command_provider_query_finish(IdeCommandProvider * provider,GAsyncResult * result,GError ** error)88 gbp_vim_command_provider_query_finish (IdeCommandProvider  *provider,
89                                        GAsyncResult        *result,
90                                        GError             **error)
91 {
92   GPtrArray *ret;
93 
94   g_assert (IDE_IS_MAIN_THREAD ());
95   g_assert (GBP_IS_VIM_COMMAND_PROVIDER (provider));
96   g_assert (G_IS_ASYNC_RESULT (result));
97 
98   ret = ide_task_propagate_pointer (IDE_TASK (result), error);
99 
100   return IDE_PTR_ARRAY_STEAL_FULL (&ret);
101 }
102 
103 static void
command_provider_iface_init(IdeCommandProviderInterface * iface)104 command_provider_iface_init (IdeCommandProviderInterface *iface)
105 {
106   iface->query_async = gbp_vim_command_provider_query_async;
107   iface->query_finish = gbp_vim_command_provider_query_finish;
108 }
109 
G_DEFINE_FINAL_TYPE_WITH_CODE(GbpVimCommandProvider,gbp_vim_command_provider,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (IDE_TYPE_COMMAND_PROVIDER,command_provider_iface_init))110 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpVimCommandProvider, gbp_vim_command_provider, G_TYPE_OBJECT,
111                          G_IMPLEMENT_INTERFACE (IDE_TYPE_COMMAND_PROVIDER, command_provider_iface_init))
112 
113 static void
114 gbp_vim_command_provider_class_init (GbpVimCommandProviderClass *klass)
115 {
116 }
117 
118 static void
gbp_vim_command_provider_init(GbpVimCommandProvider * self)119 gbp_vim_command_provider_init (GbpVimCommandProvider *self)
120 {
121 }
122