ide-code-indexer.c 8 KB
Newer Older
1
2
/* ide-code-indexer.c
 *
3
 * Copyright © 2017 Anoop Chandu <anoopchandu96@gmail.com>
4
 * Copyright © 2018 Christian Hergert <chergert@redhat.com>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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/>.
 */

#define G_LOG_DOMAIN "ide-code-indexer"

22
#include "application/ide-application.h"
23
#include "symbols/ide-code-indexer.h"
24

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * SECTION:ide-code-indexer
 * @title: IdeCodeIndexer
 * @short_description: Interface for background indexing source code
 *
 * The #IdeCodeIndexer interface is used to index source code in the project.
 * Plugins that want to provide global search features for source code should
 * implement this interface and specify which languages they support in their
 * .plugin definition, using "X-Code-Indexer-Languages". For example. to index
 * Python source code, you might use:
 *
 *   X-Code-Indexer-Languages=python,python3
 *
 * Since: 3.26
 */

41
42
G_DEFINE_INTERFACE (IdeCodeIndexer, ide_code_indexer, IDE_TYPE_OBJECT)

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
static void
ide_code_indexer_real_index_file_async (IdeCodeIndexer      *self,
                                        GFile               *file,
                                        const gchar * const *build_flags,
                                        GCancellable        *cancellable,
                                        GAsyncReadyCallback  callback,
                                        gpointer             user_data)
{
  g_assert (IDE_IS_CODE_INDEXER (self));
  g_assert (G_IS_FILE (file));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  g_task_report_new_error (self, callback, user_data,
                           ide_code_indexer_real_index_file_async,
                           G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           "Get key is not supported");
}

62
static IdeCodeIndexEntries *
63
64
65
ide_code_indexer_real_index_file_finish (IdeCodeIndexer  *self,
                                         GAsyncResult    *result,
                                         GError         **error)
66
{
67
68
69
70
  g_assert (IDE_IS_CODE_INDEXER (self));
  g_assert (G_IS_TASK (result));

  return g_task_propagate_pointer (G_TASK (result), error);
71
72
}

73
74
75
76
77
78
static void
ide_code_indexer_real_generate_key_async (IdeCodeIndexer      *self,
                                          IdeSourceLocation   *location,
                                          GCancellable        *cancellable,
                                          GAsyncReadyCallback  callback,
                                          gpointer             user_data)
79
{
80
81
82
83
84
  g_assert (IDE_IS_CODE_INDEXER (self));
  g_assert (location != NULL);
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  g_task_report_new_error (self, callback, user_data,
85
86
87
88
89
90
                           ide_code_indexer_real_generate_key_async,
                           G_IO_ERROR,
                           G_IO_ERROR_NOT_SUPPORTED,
                           "Get key is not supported");
}

91
static gchar *
92
93
94
95
ide_code_indexer_real_generate_key_finish (IdeCodeIndexer  *self,
                                           GAsyncResult    *result,
                                           GError         **error)
{
96
  g_assert (IDE_IS_CODE_INDEXER (self));
97
98
  g_assert (G_IS_TASK (result));

99
  return g_task_propagate_pointer (G_TASK (result), error);
100
101
102
103
104
}

static void
ide_code_indexer_default_init (IdeCodeIndexerInterface *iface)
{
105
106
  iface->index_file_async = ide_code_indexer_real_index_file_async;
  iface->index_file_finish = ide_code_indexer_real_index_file_finish;
107
108
109
110
111
  iface->generate_key_async = ide_code_indexer_real_generate_key_async;
  iface->generate_key_finish = ide_code_indexer_real_generate_key_finish;
}

/**
112
 * ide_code_indexer_index_file_async:
113
 * @self: An #IdeCodeIndexer instance.
114
 * @file: Source file to index.
115
 * @build_flags: (nullable) (array zero-terminated=1): array of build flags to parse @file.
116
 * @cancellable: (nullable): a #GCancellable.
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
 * @callback: a #GAsyncReadyCallback
 * @user_data: closure data for @callback
 *
 * This function will take index source file and create an array of symbols in
 * @file. @callback is called upon completion and must call
 * ide_code_indexer_index_file_finish() to complete the operation.
 *
 * Since: 3.28
 */
void
ide_code_indexer_index_file_async (IdeCodeIndexer      *self,
                                   GFile               *file,
                                   const gchar * const *build_flags,
                                   GCancellable        *cancellable,
                                   GAsyncReadyCallback  callback,
                                   gpointer             user_data)
{
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
  g_return_if_fail (IDE_IS_CODE_INDEXER (self));
  g_return_if_fail (G_IS_FILE (file));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

  return IDE_CODE_INDEXER_GET_IFACE (self)->index_file_async (self, file, build_flags, cancellable, callback, user_data);
}

/**
 * ide_code_indexer_index_file_finish:
 * @self: a #IdeCodeIndexer
 * @result: a #GAsyncResult provided to callback
 * @error: a location for a #GError, or %NULL
147
 *
148
 * Completes an asynchronous request to ide_code_indexer_index_file_async().
149
 *
150
151
 * Returns: (transfer full): an #IdeCodeIndexEntries if successful; otherwise %NULL
 *   and @error is set.
152
 *
153
 * Since: 3.28
154
155
 */
IdeCodeIndexEntries *
156
157
158
ide_code_indexer_index_file_finish (IdeCodeIndexer  *self,
                                    GAsyncResult    *result,
                                    GError         **error)
159
{
160
  g_return_val_if_fail (IDE_IS_MAIN_THREAD (), NULL);
161
  g_return_val_if_fail (IDE_IS_CODE_INDEXER (self), NULL);
162
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
163

164
  return IDE_CODE_INDEXER_GET_IFACE (self)->index_file_finish (self, result, error);
165
166
167
168
169
}

/**
 * ide_code_indexer_generate_key_async:
 * @self: An #IdeCodeIndexer instance.
170
 * @location: (not nullable): Source location of refernece.
171
 * @cancellable: (nullable): a #GCancellable.
172
173
174
175
176
 * @callback: A callback to execute upon indexing.
 * @user_data: User data to pass to @callback.
 *
 * This function will get key of reference located at #IdeSoureLocation.
 *
177
 * Since: 3.26
178
179
 */
void
180
181
182
183
184
ide_code_indexer_generate_key_async (IdeCodeIndexer      *self,
                                     IdeSourceLocation   *location,
                                     GCancellable        *cancellable,
                                     GAsyncReadyCallback  callback,
                                     gpointer             user_data)
185
{
186
  g_return_if_fail (IDE_IS_MAIN_THREAD ());
187
188
189
190
  g_return_if_fail (IDE_IS_CODE_INDEXER (self));
  g_return_if_fail (location != NULL);
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

191
  IDE_CODE_INDEXER_GET_IFACE (self)->generate_key_async (self, location, cancellable, callback, user_data);
192
193
194
195
}

/**
 * ide_code_indexer_generate_key_finish:
196
197
198
 * @self: an #IdeCodeIndexer
 * @result: a #GAsyncResult
 * @error: a location for a #GError, or %NULL
199
200
201
202
203
 *
 * Returns key for declaration of reference at a location.
 *
 * Returns: (transfer full) : A string which contains key.
 *
204
 * Since: 3.26
205
206
 */
gchar *
207
208
209
ide_code_indexer_generate_key_finish (IdeCodeIndexer  *self,
                                      GAsyncResult    *result,
                                      GError         **error)
210
{
211
  g_return_val_if_fail (IDE_IS_MAIN_THREAD (), NULL);
212
  g_return_val_if_fail (IDE_IS_CODE_INDEXER (self), NULL);
213
  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
214

215
  return IDE_CODE_INDEXER_GET_IFACE (self)->generate_key_finish (self, result, error);
216
}