[gnome-builder] symbols: Adding IdeCodeIndexer & IdeCodeIndexEntry



commit a8595350214192f0cf6218d16df934597d28bed0
Author: Anoop Chandu <anoopchandu96 gmail com>
Date:   Tue Aug 29 20:58:35 2017 +0530

    symbols: Adding IdeCodeIndexer & IdeCodeIndexEntry
    
    IdeCodeIndexEntry represents an entry in code index.
    IdeCodeIndexer interface has to be implemented by plugins
    which want to provide indexing service for a language
    source file. This interface will take file as input and
    generates IdeCodeIndexEntries using which index entries
    can be retrieved for that file.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786700

 libide/ide.h                          |    2 +
 libide/meson.build                    |    6 +
 libide/symbols/ide-code-index-entry.c |  289 +++++++++++++++++++++++++++++++++
 libide/symbols/ide-code-index-entry.h |   48 ++++++
 libide/symbols/ide-code-indexer.c     |  160 ++++++++++++++++++
 libide/symbols/ide-code-indexer.h     |   66 ++++++++
 6 files changed, 571 insertions(+), 0 deletions(-)
---
diff --git a/libide/ide.h b/libide/ide.h
index 0ecdd45..86e0819 100644
--- a/libide/ide.h
+++ b/libide/ide.h
@@ -146,6 +146,8 @@ G_BEGIN_DECLS
 #include "sourceview/ide-source-view.h"
 #include "subprocess/ide-subprocess.h"
 #include "subprocess/ide-subprocess-launcher.h"
+#include "symbols/ide-code-indexer.h"
+#include "symbols/ide-code-index-entry.h"
 #include "symbols/ide-symbol-resolver.h"
 #include "symbols/ide-symbol.h"
 #include "symbols/ide-tags-builder.h"
diff --git a/libide/meson.build b/libide/meson.build
index 0c656a5..5a880e9 100644
--- a/libide/meson.build
+++ b/libide/meson.build
@@ -185,6 +185,9 @@ libide_public_headers = [
   'subprocess/ide-subprocess.h',
   'subprocess/ide-subprocess-launcher.h',
   'subprocess/ide-subprocess-supervisor.h',
+  'symbols/ide-code-index-entries.h',
+  'symbols/ide-code-index-entry.h',
+  'symbols/ide-code-indexer.h',
   'symbols/ide-symbol-node.h',
   'symbols/ide-symbol-resolver.h',
   'symbols/ide-symbol-tree.h',
@@ -395,6 +398,9 @@ libide_public_sources = [
   'subprocess/ide-subprocess.c',
   'subprocess/ide-subprocess-launcher.c',
   'subprocess/ide-subprocess-supervisor.c',
+  'symbols/ide-code-index-entries.c',
+  'symbols/ide-code-index-entry.c',
+  'symbols/ide-code-indexer.c',
   'symbols/ide-symbol-node.c',
   'symbols/ide-symbol-resolver.c',
   'symbols/ide-symbol-tree.c',
diff --git a/libide/symbols/ide-code-index-entry.c b/libide/symbols/ide-code-index-entry.c
new file mode 100644
index 0000000..84340f8
--- /dev/null
+++ b/libide/symbols/ide-code-index-entry.c
@@ -0,0 +1,289 @@
+/* ide-code-index-entry.c
+ *
+ * Copyright (C) 2017 Anoop Chandu <anoopchandu96 gmail 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-code-index-entry"
+
+#include "ide-code-index-entry.h"
+
+typedef struct
+{
+  GObject                 parent;
+
+  gchar                  *key;
+  gchar                  *name;
+
+  IdeSymbolKind           kind;
+  IdeSymbolFlags          flags;
+
+  guint                   begin_line;
+  guint                   begin_line_offset;
+  guint                   end_line;
+  guint                   end_line_offset;
+} IdeCodeIndexEntryPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeCodeIndexEntry, ide_code_index_entry, G_TYPE_OBJECT)
+
+enum {
+  PROP_0,
+  PROP_KEY,
+  PROP_NAME,
+  PROP_KIND,
+  PROP_FLAGS,
+  PROP_BEGIN_LINE,
+  PROP_BEGIN_LINE_OFFSET,
+  PROP_END_LINE,
+  PROP_END_LINE_OFFSET,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_code_index_entry_finalize (GObject *object)
+{
+  IdeCodeIndexEntry *self = (IdeCodeIndexEntry *)object;
+  IdeCodeIndexEntryPrivate *priv = ide_code_index_entry_get_instance_private (self);
+
+  g_clear_pointer (&priv->name, g_free);
+  g_clear_pointer (&priv->key, g_free);
+
+  G_OBJECT_CLASS (ide_code_index_entry_parent_class)->finalize (object);
+}
+
+static void
+ide_code_index_entry_set_property (GObject       *object,
+                                   guint          prop_id,
+                                   const GValue  *value,
+                                   GParamSpec    *pspec)
+{
+  IdeCodeIndexEntry *self = (IdeCodeIndexEntry *)object;
+  IdeCodeIndexEntryPrivate *priv = ide_code_index_entry_get_instance_private (self);
+
+  switch (prop_id)
+    {
+    case PROP_KEY:
+      priv->key = g_strdup (g_value_get_string (value));
+      break;
+    case PROP_NAME:
+      priv->name = g_strdup (g_value_get_string (value));
+      break;
+    case PROP_KIND:
+      priv->kind = g_value_get_int (value);
+      break;
+    case PROP_FLAGS:
+      priv->flags = g_value_get_int (value);
+      break;
+    case PROP_BEGIN_LINE:
+      priv->begin_line = g_value_get_uint (value);
+      break;
+    case PROP_BEGIN_LINE_OFFSET:
+      priv->begin_line_offset = g_value_get_uint (value);
+      break;
+    case PROP_END_LINE:
+      priv->end_line = g_value_get_uint (value);
+      break;
+    case PROP_END_LINE_OFFSET:
+      priv->end_line_offset = g_value_get_uint (value);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_code_index_entry_get_property (GObject    *object,
+                                   guint       prop_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+  IdeCodeIndexEntry *self = (IdeCodeIndexEntry *)object;
+  IdeCodeIndexEntryPrivate *priv = ide_code_index_entry_get_instance_private (self);
+
+  switch (prop_id)
+    {
+    case PROP_KEY:
+      g_value_set_string (value, priv->key);
+      break;
+    case PROP_NAME:
+      g_value_set_string (value, priv->name);
+      break;
+    case PROP_KIND:
+      g_value_set_int (value, priv->kind);
+      break;
+    case PROP_FLAGS:
+      g_value_set_int (value, priv->flags);
+      break;
+    case PROP_BEGIN_LINE:
+      g_value_set_uint (value, priv->begin_line);
+      break;
+    case PROP_BEGIN_LINE_OFFSET:
+      g_value_set_uint (value, priv->begin_line_offset);
+      break;
+    case PROP_END_LINE:
+      g_value_set_uint (value, priv->end_line);
+      break;
+    case PROP_END_LINE_OFFSET:
+      g_value_set_uint (value, priv->end_line_offset);
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_code_index_entry_class_init (IdeCodeIndexEntryClass *klass)
+{
+  GObjectClass *object_class = (GObjectClass *)klass;
+
+  object_class->finalize = ide_code_index_entry_finalize;
+  object_class->set_property = ide_code_index_entry_set_property;
+  object_class->get_property = ide_code_index_entry_get_property;
+
+  properties [PROP_KEY] =
+    g_param_spec_string ("key",
+                         "Key",
+                         "A key unique to declaration.",
+                         NULL,
+                         (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_NAME] =
+    g_param_spec_string ("name",
+                         "Name",
+                         "Name of declaration.",
+                         NULL,
+                         (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_KIND] =
+    g_param_spec_int ("kind",
+                      "Kind",
+                      "Kind of declaration.",
+                       G_MININT, G_MAXINT, IDE_SYMBOL_NONE,
+                       (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_FLAGS] =
+    g_param_spec_int ("flags",
+                      "Flags",
+                      "Flags of declaration.",
+                       G_MININT, G_MAXINT, IDE_SYMBOL_FLAGS_NONE,
+                       (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_BEGIN_LINE] =
+    g_param_spec_uint ("begin-line",
+                       "Begin Line",
+                       "Begin Line of declaration.",
+                       0, G_MAXUINT, 0,
+                       (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_BEGIN_LINE_OFFSET] =
+    g_param_spec_uint ("begin-line-offset",
+                       "Begin Line Offset",
+                       "Begin Line Offset of declaration.",
+                       0, G_MAXUINT, 0,
+                       (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_END_LINE] =
+    g_param_spec_uint ("end-line",
+                       "End Line",
+                       "End Line of declaration.",
+                       0, G_MAXUINT, 0,
+                       (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  properties [PROP_END_LINE_OFFSET] =
+    g_param_spec_uint ("end-line-offset",
+                       "End Line Offset",
+                       "End Line Offset of declaration.",
+                       0, G_MAXUINT, 0,
+                       (G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_code_index_entry_init (IdeCodeIndexEntry *self)
+{
+}
+
+gchar *
+ide_code_index_entry_get_key (IdeCodeIndexEntry *self)
+{
+  IdeCodeIndexEntryPrivate *priv;
+
+  g_return_val_if_fail (IDE_IS_CODE_INDEX_ENTRY (self), NULL);
+
+  priv = ide_code_index_entry_get_instance_private (self);
+  return priv->key;
+}
+
+gchar *
+ide_code_index_entry_get_name (IdeCodeIndexEntry *self)
+{
+  IdeCodeIndexEntryPrivate *priv;
+
+  g_return_val_if_fail (IDE_IS_CODE_INDEX_ENTRY (self), NULL);
+
+  priv = ide_code_index_entry_get_instance_private (self);
+  return priv->name;
+}
+
+IdeSymbolKind
+ide_code_index_entry_get_kind (IdeCodeIndexEntry *self)
+{
+  IdeCodeIndexEntryPrivate *priv;
+
+  g_return_val_if_fail (IDE_IS_CODE_INDEX_ENTRY (self), IDE_SYMBOL_NONE);
+
+  priv = ide_code_index_entry_get_instance_private (self);
+  return priv->kind;
+}
+
+IdeSymbolFlags
+ide_code_index_entry_get_flags (IdeCodeIndexEntry *self)
+{
+  IdeCodeIndexEntryPrivate *priv;
+
+  g_return_val_if_fail (IDE_IS_CODE_INDEX_ENTRY (self), IDE_SYMBOL_FLAGS_NONE);
+
+  priv = ide_code_index_entry_get_instance_private (self);
+  return priv->flags;
+}
+
+void
+ide_code_index_entry_get_range (IdeCodeIndexEntry *self,
+                                guint             *begin_line,
+                                guint             *begin_line_offset,
+                                guint             *end_line,
+                                guint             *end_line_offset)
+{
+  IdeCodeIndexEntryPrivate *priv;
+
+  g_return_if_fail (IDE_IS_CODE_INDEX_ENTRY (self));
+
+  priv = ide_code_index_entry_get_instance_private (self);
+
+  if (begin_line != NULL)
+    *begin_line = priv->begin_line;
+
+  if (begin_line_offset != NULL)
+    *begin_line_offset = priv->begin_line_offset;
+
+  if (end_line != NULL)
+    *end_line = priv->end_line;
+
+  if (end_line_offset != NULL)
+    *end_line_offset = priv->end_line_offset;
+}
diff --git a/libide/symbols/ide-code-index-entry.h b/libide/symbols/ide-code-index-entry.h
new file mode 100644
index 0000000..c214955
--- /dev/null
+++ b/libide/symbols/ide-code-index-entry.h
@@ -0,0 +1,48 @@
+/* ide-code-index-entry.h
+ *
+ * Copyright (C) 2017 Anoop Chandu <anoopchandu96 gmail 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/>.
+ */
+
+#ifndef IDE_CODE_INDEX_ENTRY_H
+#define IDE_CODE_INDEX_ENTRY_H
+
+#include "ide-object.h"
+#include "ide-symbol.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_CODE_INDEX_ENTRY (ide_code_index_entry_get_type ())
+
+G_DECLARE_DERIVABLE_TYPE (IdeCodeIndexEntry, ide_code_index_entry, IDE, CODE_INDEX_ENTRY, GObject)
+
+struct _IdeCodeIndexEntryClass
+{
+  GObjectClass parent;
+};
+
+gchar           *ide_code_index_entry_get_key   (IdeCodeIndexEntry *self);
+gchar           *ide_code_index_entry_get_name  (IdeCodeIndexEntry *self);
+IdeSymbolKind    ide_code_index_entry_get_kind  (IdeCodeIndexEntry *self);
+IdeSymbolFlags   ide_code_index_entry_get_flags (IdeCodeIndexEntry *self);
+void             ide_code_index_entry_get_range (IdeCodeIndexEntry *self,
+                                                 guint             *begin_line,
+                                                 guint             *begin_line_offset,
+                                                 guint             *end_line,
+                                                 guint             *end_line_offset);
+
+G_END_DECLS
+
+#endif /* IDE_CODE_INDEX_ENTRY_H */
diff --git a/libide/symbols/ide-code-indexer.c b/libide/symbols/ide-code-indexer.c
new file mode 100644
index 0000000..807cadf
--- /dev/null
+++ b/libide/symbols/ide-code-indexer.c
@@ -0,0 +1,160 @@
+/* ide-code-indexer.c
+ *
+ * Copyright (C) 2017 Anoop Chandu <anoopchandu96 gmail 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-code-indexer"
+
+#include "ide-code-indexer.h"
+
+G_DEFINE_INTERFACE (IdeCodeIndexer, ide_code_indexer, IDE_TYPE_OBJECT)
+
+static IdeCodeIndexEntries *
+ide_code_indexer_real_index_file (IdeCodeIndexer      *self,
+                                  GFile               *file,
+                                  gchar              **build_flags,
+                                  GCancellable        *cancellable,
+                                  GError             **error)
+{
+  g_set_error (error,
+               G_IO_ERROR,
+               G_IO_ERROR_NOT_SUPPORTED,
+               "Indexing is not supported");
+  return NULL;
+}
+
+void
+ide_code_indexer_real_generate_key_async (IdeCodeIndexer       *self,
+                                          IdeSourceLocation    *location,
+                                          GCancellable         *cancellable,
+                                          GAsyncReadyCallback   callback,
+                                          gpointer              user_data)
+{
+  g_task_report_new_error (self,
+                           callback,
+                           user_data,
+                           ide_code_indexer_real_generate_key_async,
+                           G_IO_ERROR,
+                           G_IO_ERROR_NOT_SUPPORTED,
+                           "Get key is not supported");
+}
+
+gchar *
+ide_code_indexer_real_generate_key_finish (IdeCodeIndexer  *self,
+                                           GAsyncResult    *result,
+                                           GError         **error)
+{
+  GTask *task = (GTask *)result;
+
+  g_assert (G_IS_TASK (result));
+
+  return g_task_propagate_pointer (task, error);
+}
+
+static void
+ide_code_indexer_default_init (IdeCodeIndexerInterface *iface)
+{
+  iface->index_file = ide_code_indexer_real_index_file;
+  iface->generate_key_async = ide_code_indexer_real_generate_key_async;
+  iface->generate_key_finish = ide_code_indexer_real_generate_key_finish;
+}
+
+/**
+ * ide_code_indexer_index_file:
+ * @self: An #IdeCodeIndexer instance.
+ * @file: Source file to index.
+ * @build_flags: (nullable): array of build flags to parse @file.
+ * @cancellable: (nullable): A #GCancellable.
+ * @error: A #GError.
+ *
+ * This function will take index source file and create an array
+ * of symbols in @file.
+ *
+ * Returns: (transfer full) : A #IdeCodeIndexEntries contains list
+ *    of #IdeCodeIndexEntry.
+ *
+ * Since: 3.26
+ */
+IdeCodeIndexEntries *
+ide_code_indexer_index_file (IdeCodeIndexer      *self,
+                             GFile               *file,
+                             gchar              **build_flags,
+                             GCancellable        *cancellable,
+                             GError             **error)
+{
+  IdeCodeIndexerInterface *iface;
+
+  g_return_val_if_fail (IDE_IS_CODE_INDEXER (self), NULL);
+  g_return_val_if_fail (G_IS_FILE (file), NULL);
+  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), NULL);
+
+  iface = IDE_CODE_INDEXER_GET_IFACE (self);
+
+  return iface->index_file (self, file, build_flags, cancellable, error);
+}
+
+/**
+ * ide_code_indexer_generate_key_async:
+ * @self: An #IdeCodeIndexer instance.
+ * @location: Source location of refernece.
+ * @cancellable: (nullable): A #GCancellable.
+ * @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.
+ *
+ * since : 3.26
+ */
+void
+ide_code_indexer_generate_key_async (IdeCodeIndexer       *self,
+                                     IdeSourceLocation    *location,
+                                     GCancellable         *cancellable,
+                                     GAsyncReadyCallback   callback,
+                                     gpointer              user_data)
+{
+  IdeCodeIndexerInterface *iface;
+
+  g_return_if_fail (IDE_IS_CODE_INDEXER (self));
+  g_return_if_fail (location != NULL);
+  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+  iface = IDE_CODE_INDEXER_GET_IFACE (self);
+
+  iface->generate_key_async (self, location, cancellable, callback, user_data);
+}
+
+/**
+ * ide_code_indexer_generate_key_finish:
+ *
+ * Returns key for declaration of reference at a location.
+ *
+ * Returns: (transfer full) : A string which contains key.
+ *
+ * since 3.26
+ */
+gchar *
+ide_code_indexer_generate_key_finish (IdeCodeIndexer       *self,
+                                      GAsyncResult         *result,
+                                      GError              **error)
+{
+  IdeCodeIndexerInterface *iface;
+
+  g_return_val_if_fail (IDE_IS_CODE_INDEXER (self), NULL);
+
+  iface  = IDE_CODE_INDEXER_GET_IFACE (self);
+
+  return iface->generate_key_finish (self, result, error);
+}
diff --git a/libide/symbols/ide-code-indexer.h b/libide/symbols/ide-code-indexer.h
new file mode 100644
index 0000000..1801e03
--- /dev/null
+++ b/libide/symbols/ide-code-indexer.h
@@ -0,0 +1,66 @@
+/* ide-code-indexer.h
+ *
+ * Copyright (C) 2017 Anoop Chandu <anoopchandu96 gmail 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/>.
+ */
+
+#ifndef IDE_CODE_INDEXER_H
+#define IDE_CODE_INDEXER_H
+
+#include "ide-object.h"
+#include "ide-code-index-entries.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_CODE_INDEXER (ide_code_indexer_get_type())
+
+G_DECLARE_INTERFACE (IdeCodeIndexer, ide_code_indexer, IDE, CODE_INDEXER, IdeObject)
+
+struct _IdeCodeIndexerInterface
+{
+  GTypeInterface parent_iface;
+
+  IdeCodeIndexEntries *(*index_file)             (IdeCodeIndexer       *self,
+                                                  GFile                *file,
+                                                  gchar               **build_flags,
+                                                  GCancellable         *cancellable,
+                                                  GError              **error);
+  void                 (*generate_key_async)     (IdeCodeIndexer       *self,
+                                                  IdeSourceLocation    *location,
+                                                  GCancellable         *cancellable,
+                                                  GAsyncReadyCallback   callback,
+                                                  gpointer              user_data);
+  gchar               *(*generate_key_finish)    (IdeCodeIndexer       *self,
+                                                  GAsyncResult         *result,
+                                                  GError              **error);
+};
+
+IdeCodeIndexEntries  *ide_code_indexer_index_file           (IdeCodeIndexer       *self,
+                                                             GFile                *file,
+                                                             gchar               **build_flags,
+                                                             GCancellable         *cancellable,
+                                                             GError              **error);
+void                  ide_code_indexer_generate_key_async   (IdeCodeIndexer       *self,
+                                                             IdeSourceLocation    *location,
+                                                             GCancellable         *cancellable,
+                                                             GAsyncReadyCallback   callback,
+                                                             gpointer              user_data);
+gchar                *ide_code_indexer_generate_key_finish  (IdeCodeIndexer       *self,
+                                                             GAsyncResult         *result,
+                                                             GError              **error);
+
+G_END_DECLS
+
+#endif /* IDE_CODE_INDEXER_H */


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