1 /* ide-ctags-results.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 "ide-ctags-results"
22
23 #include "ide-ctags-completion-item.h"
24 #include "ide-ctags-results.h"
25 #include "ide-ctags-util.h"
26
27 struct _IdeCtagsResults
28 {
29 GObject parent_instance;
30 const gchar * const *suffixes;
31 GCancellable *refilter_cancellable;
32 gchar *word;
33 GPtrArray *indexes;
34 GArray *items;
35 };
36
37 typedef struct
38 {
39 const IdeCtagsIndexEntry *entry;
40 guint priority;
41 } Item;
42
43 typedef struct
44 {
45 const gchar * const *suffixes;
46 gchar *word;
47 gchar *casefold;
48 GPtrArray *indexes;
49 GArray *items;
50 } Populate;
51
52 static void
populate_free(Populate * state)53 populate_free (Populate *state)
54 {
55 g_clear_pointer (&state->word, g_free);
56 g_clear_pointer (&state->casefold, g_free);
57 g_clear_pointer (&state->indexes, g_ptr_array_unref);
58 g_clear_pointer (&state->items, g_array_unref);
59 g_slice_free (Populate, state);
60 }
61
62 static GType
ide_ctags_results_get_item_type(GListModel * model)63 ide_ctags_results_get_item_type (GListModel *model)
64 {
65 return IDE_TYPE_COMPLETION_PROPOSAL;
66 }
67
68 static gpointer
ide_ctags_results_get_item(GListModel * model,guint position)69 ide_ctags_results_get_item (GListModel *model,
70 guint position)
71 {
72 IdeCtagsResults *self = (IdeCtagsResults *)model;
73 const Item *item;
74
75 g_assert (IDE_IS_CTAGS_RESULTS (self));
76 g_assert (position < self->items->len);
77
78 item = &g_array_index (self->items, Item, position);
79
80 return ide_ctags_completion_item_new (self, item->entry);
81 }
82
83 static guint
ide_ctags_results_get_n_items(GListModel * model)84 ide_ctags_results_get_n_items (GListModel *model)
85 {
86 g_assert (IDE_IS_CTAGS_RESULTS (model));
87
88 return IDE_CTAGS_RESULTS (model)->items->len;
89 }
90
91 static void
list_model_iface_init(GListModelInterface * iface)92 list_model_iface_init (GListModelInterface *iface)
93 {
94 iface->get_item_type = ide_ctags_results_get_item_type;
95 iface->get_n_items = ide_ctags_results_get_n_items;
96 iface->get_item = ide_ctags_results_get_item;
97 }
98
G_DEFINE_FINAL_TYPE_WITH_CODE(IdeCtagsResults,ide_ctags_results,G_TYPE_OBJECT,G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL,list_model_iface_init))99 G_DEFINE_FINAL_TYPE_WITH_CODE (IdeCtagsResults, ide_ctags_results, G_TYPE_OBJECT,
100 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
101
102 static void
103 ide_ctags_results_finalize (GObject *object)
104 {
105 IdeCtagsResults *self = (IdeCtagsResults *)object;
106
107 g_clear_object (&self->refilter_cancellable);
108 g_clear_pointer (&self->items, g_array_unref);
109 g_clear_pointer (&self->indexes, g_ptr_array_unref);
110 g_clear_pointer (&self->word, g_free);
111
112 G_OBJECT_CLASS (ide_ctags_results_parent_class)->finalize (object);
113 }
114
115 static void
ide_ctags_results_class_init(IdeCtagsResultsClass * klass)116 ide_ctags_results_class_init (IdeCtagsResultsClass *klass)
117 {
118 GObjectClass *object_class = G_OBJECT_CLASS (klass);
119
120 object_class->finalize = ide_ctags_results_finalize;
121 }
122
123 static void
ide_ctags_results_init(IdeCtagsResults * self)124 ide_ctags_results_init (IdeCtagsResults *self)
125 {
126 self->indexes = g_ptr_array_new_with_free_func (g_object_unref);
127 self->items = g_array_new (FALSE, FALSE, sizeof (Item));
128 }
129
130 static gint
sort_by_priority(gconstpointer a,gconstpointer b)131 sort_by_priority (gconstpointer a,
132 gconstpointer b)
133 {
134 return (gint)((const Item *)a)->priority -
135 (gint)((const Item *)b)->priority;
136 }
137
138 static void
ide_ctags_results_populate_worker(IdeTask * task,gpointer source_object,gpointer task_data,GCancellable * cancellable)139 ide_ctags_results_populate_worker (IdeTask *task,
140 gpointer source_object,
141 gpointer task_data,
142 GCancellable *cancellable)
143 {
144 Populate *p = task_data;
145 g_autoptr(GHashTable) completions = NULL;
146 guint word_len;
147
148 g_assert (IDE_IS_TASK (task));
149 g_assert (IDE_IS_CTAGS_RESULTS (source_object));
150 g_assert (p != NULL);
151 g_assert (p->word != NULL);
152 g_assert (p->casefold != NULL);
153 g_assert (p->indexes != NULL);
154 g_assert (p->items != NULL);
155 g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
156
157 completions = g_hash_table_new (g_str_hash, g_str_equal);
158 word_len = strlen (p->word);
159
160 for (guint i = 0; i < p->indexes->len; i++)
161 {
162 g_autofree gchar *copy = g_strdup (p->word);
163 IdeCtagsIndex *index = g_ptr_array_index (p->indexes, i);
164 const IdeCtagsIndexEntry *entries = NULL;
165 guint tmp_len = word_len;
166 gsize n_entries = 0;
167
168 while (!entries && *copy)
169 {
170 if (!(entries = ide_ctags_index_lookup_prefix (index, copy, &n_entries)))
171 copy [--tmp_len] = '\0';
172 }
173
174 if (!entries || !n_entries)
175 continue;
176
177 for (guint j = 0; j < n_entries; j++)
178 {
179 const IdeCtagsIndexEntry *entry = &entries [j];
180 guint priority;
181
182 if (g_hash_table_contains (completions, entry->name))
183 continue;
184
185 g_hash_table_add (completions, (gchar *)entry->name);
186
187 if (!ide_ctags_is_allowed (entry, p->suffixes))
188 continue;
189
190 if (ide_completion_fuzzy_match (entry->name, p->casefold, &priority))
191 {
192 Item item;
193
194 item.entry = entry;
195 item.priority = priority;
196
197 g_array_append_val (p->items, item);
198 }
199 }
200 }
201
202 g_array_sort (p->items, sort_by_priority);
203
204 ide_task_return_pointer (task,
205 g_array_ref (p->items),
206 g_array_unref);
207 }
208
209 #if 0
210
211 casefold = g_utf8_casefold (word, -1);
212 store = g_list_store_new (IDE_TYPE_COMPLETION_PROPOSAL);
213
214 *results = g_object_ref (G_LIST_MODEL (store));
215
216 ide_task_return_pointer (task, g_steal_pointer (&store), g_object_unref);
217
218 #endif
219
220 void
ide_ctags_results_populate_async(IdeCtagsResults * self,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)221 ide_ctags_results_populate_async (IdeCtagsResults *self,
222 GCancellable *cancellable,
223 GAsyncReadyCallback callback,
224 gpointer user_data)
225 {
226 g_autoptr(IdeTask) task = NULL;
227 Populate *p;
228
229 g_return_if_fail (IDE_IS_CTAGS_RESULTS (self));
230 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
231
232 task = ide_task_new (self, cancellable, callback, user_data);
233 ide_task_set_source_tag (task, ide_ctags_results_populate_async);
234 ide_task_set_priority (task, G_PRIORITY_HIGH);
235 ide_task_set_complete_priority (task, G_PRIORITY_LOW);
236
237 if (self->word == NULL)
238 {
239 ide_task_return_new_error (task,
240 G_IO_ERROR,
241 G_IO_ERROR_INVAL,
242 "No word set to query");
243 return;
244 }
245
246 p = g_slice_new (Populate);
247 p->word = g_strdup (self->word);
248 p->casefold = g_utf8_casefold (self->word, -1);
249 p->indexes = g_ptr_array_new_full (self->indexes->len, g_object_unref);
250 p->items = g_array_new (FALSE, FALSE, sizeof (GArray));
251 p->suffixes = self->suffixes;
252
253 for (guint i = 0; i < self->indexes->len; i++)
254 g_ptr_array_add (p->indexes, g_object_ref (g_ptr_array_index (self->indexes, i)));
255
256 ide_task_set_task_data (task, p, populate_free);
257 ide_task_run_in_thread (task, ide_ctags_results_populate_worker);
258 }
259
260 gboolean
ide_ctags_results_populate_finish(IdeCtagsResults * self,GAsyncResult * result,GError ** error)261 ide_ctags_results_populate_finish (IdeCtagsResults *self,
262 GAsyncResult *result,
263 GError **error)
264 {
265 GArray *items;
266
267 g_return_val_if_fail (IDE_IS_CTAGS_RESULTS (self), FALSE);
268 g_return_val_if_fail (IDE_IS_TASK (result), FALSE);
269
270 if ((items = ide_task_propagate_pointer (IDE_TASK (result), error)))
271 {
272 guint old_len = self->items->len;
273 guint new_len = items->len;
274
275 g_array_unref (self->items);
276 self->items = g_steal_pointer (&items);
277
278 g_list_model_items_changed (G_LIST_MODEL (self), 0, old_len, new_len);
279
280 return TRUE;
281 }
282
283 return FALSE;
284 }
285
286 void
ide_ctags_results_set_suffixes(IdeCtagsResults * self,const gchar * const * suffixes)287 ide_ctags_results_set_suffixes (IdeCtagsResults *self,
288 const gchar * const *suffixes)
289 {
290 /* @suffixes is an interned string array */
291 self->suffixes = suffixes;
292 }
293
294 void
ide_ctags_results_set_word(IdeCtagsResults * self,const gchar * word)295 ide_ctags_results_set_word (IdeCtagsResults *self,
296 const gchar *word)
297 {
298 g_assert (IDE_IS_CTAGS_RESULTS (self));
299
300 if (word != self->word)
301 {
302 g_free (self->word);
303 self->word = g_strdup (word);
304 }
305 }
306
307 IdeCtagsResults *
ide_ctags_results_new(void)308 ide_ctags_results_new (void)
309 {
310 return g_object_new (IDE_TYPE_CTAGS_RESULTS, NULL);
311 }
312
313 void
ide_ctags_results_add_index(IdeCtagsResults * self,IdeCtagsIndex * index)314 ide_ctags_results_add_index (IdeCtagsResults *self,
315 IdeCtagsIndex *index)
316 {
317 g_assert (IDE_IS_CTAGS_RESULTS (self));
318 g_assert (IDE_IS_CTAGS_INDEX (index));
319
320 g_ptr_array_add (self->indexes, g_object_ref (index));
321 }
322
323 static void
ide_ctags_results_refilter_cb(GObject * object,GAsyncResult * result,gpointer user_data)324 ide_ctags_results_refilter_cb (GObject *object,
325 GAsyncResult *result,
326 gpointer user_data)
327 {
328 IdeCtagsResults *self = (IdeCtagsResults *)object;
329
330 g_assert (IDE_IS_CTAGS_RESULTS (self));
331 g_assert (G_IS_ASYNC_RESULT (result));
332 g_assert (user_data == NULL);
333
334 if (ide_ctags_results_populate_finish (self, result, NULL))
335 g_clear_object (&self->refilter_cancellable);
336 }
337
338 void
ide_ctags_results_refilter(IdeCtagsResults * self)339 ide_ctags_results_refilter (IdeCtagsResults *self)
340 {
341 g_return_if_fail (IDE_IS_CTAGS_RESULTS (self));
342
343 g_cancellable_cancel (self->refilter_cancellable);
344 self->refilter_cancellable = g_cancellable_new();
345
346 ide_ctags_results_populate_async (self,
347 self->refilter_cancellable,
348 ide_ctags_results_refilter_cb,
349 NULL);
350 }
351