[gnome-builder] code-index: add indexer info helper module



commit 1c609d421128a61e9427e3afd7bcc8995725e665
Author: Christian Hergert <chergert redhat com>
Date:   Mon Feb 4 19:34:50 2019 -0800

    code-index: add indexer info helper module

 src/plugins/code-index/gbp-code-index-plan.c | 123 +---------------------
 src/plugins/code-index/indexer-info.c        | 149 +++++++++++++++++++++++++++
 src/plugins/code-index/indexer-info.h        |  42 ++++++++
 src/plugins/code-index/meson.build           |   1 +
 4 files changed, 193 insertions(+), 122 deletions(-)
---
diff --git a/src/plugins/code-index/gbp-code-index-plan.c b/src/plugins/code-index/gbp-code-index-plan.c
index 78d50a05f..02ab57ae8 100644
--- a/src/plugins/code-index/gbp-code-index-plan.c
+++ b/src/plugins/code-index/gbp-code-index-plan.c
@@ -30,6 +30,7 @@
 
 #include "gbp-code-index-plan.h"
 #include "ide-code-index-index.h"
+#include "indexer-info.h"
 
 struct _GbpCodeIndexPlan
 {
@@ -54,13 +55,6 @@ typedef struct
   IdeBuildSystem *build_system;
 } PopulateData;
 
-typedef struct
-{
-  const gchar *module_name;
-  GPtrArray   *specs;
-  GPtrArray   *mime_types;
-} IndexerInfo;
-
 typedef struct
 {
   GFile *cachedir;
@@ -77,14 +71,6 @@ directory_info_free (DirectoryInfo *info)
   g_slice_free (DirectoryInfo, info);
 }
 
-static void
-indexer_info_free (IndexerInfo *info)
-{
-  g_clear_pointer (&info->specs, g_ptr_array_unref);
-  g_clear_pointer (&info->mime_types, g_ptr_array_unref);
-  g_slice_free (IndexerInfo, info);
-}
-
 static void
 populate_data_free (PopulateData *data)
 {
@@ -371,113 +357,6 @@ gbp_code_index_plan_foreach (GbpCodeIndexPlan        *self,
   g_mutex_unlock (&self->mutex);
 }
 
-static GPtrArray *
-collect_indexer_info (void)
-{
-  GtkSourceLanguageManager *manager;
-  g_autoptr(GPtrArray) indexers = NULL;
-  const GList *plugins;
-  PeasEngine *engine;
-
-  g_assert (IDE_IS_MAIN_THREAD ());
-
-  manager = gtk_source_language_manager_get_default ();
-  engine = peas_engine_get_default ();
-  plugins = peas_engine_get_plugin_list (engine);
-  indexers = g_ptr_array_new_with_free_func ((GDestroyNotify)indexer_info_free);
-
-  for (; plugins != NULL; plugins = plugins->next)
-    {
-      const PeasPluginInfo *plugin_info = plugins->data;
-      const gchar *module_name;
-      g_autofree gchar *str = NULL;
-      g_auto(GStrv) split = NULL;
-      IndexerInfo *info;
-
-      if (!peas_plugin_info_is_loaded (plugin_info) ||
-          !(str = g_strdup (peas_plugin_info_get_external_data (plugin_info, "Code-Indexer-Languages"))))
-        continue;
-
-      module_name = peas_plugin_info_get_module_name (plugin_info);
-      split = g_strsplit (g_strdelimit (str, ",", ';'), ";", 0);
-
-      info = g_slice_new0 (IndexerInfo);
-      info->module_name = g_intern_string (module_name);
-      info->mime_types = g_ptr_array_new ();
-      info->specs = g_ptr_array_new_with_free_func ((GDestroyNotify)g_pattern_spec_free);
-
-      for (guint i = 0; split[i]; i++)
-        {
-          GtkSourceLanguage *lang;
-          const gchar *name = split[i];
-          g_auto(GStrv) globs = NULL;
-          g_auto(GStrv) mime_types = NULL;
-
-          if (!(lang = gtk_source_language_manager_get_language (manager, name)))
-            {
-              g_warning ("No such language \"%s\" in %s plugin description",
-                         name, module_name);
-              continue;
-            }
-
-          globs = gtk_source_language_get_globs (lang);
-          mime_types = gtk_source_language_get_mime_types (lang);
-
-          for (guint j = 0; globs[j] != NULL; j++)
-            {
-              g_autoptr(GPatternSpec) spec = g_pattern_spec_new (globs[j]);
-              g_ptr_array_add (info->specs, g_steal_pointer (&spec));
-            }
-
-          for (guint j = 0; mime_types[j]; j++)
-            g_ptr_array_add (info->mime_types, (gchar *)g_intern_string (mime_types[j]));
-        }
-
-      g_ptr_array_add (indexers, g_steal_pointer (&info));
-    }
-
-  return g_steal_pointer (&indexers);
-}
-
-static gboolean
-indexer_info_matches (const IndexerInfo *info,
-                      const gchar       *filename,
-                      const gchar       *filename_reversed,
-                      const gchar       *mime_type)
-{
-  gsize len;
-
-  g_assert (info != NULL);
-  g_assert (filename != NULL);
-  g_assert (filename_reversed != NULL);
-
-  if (mime_type != NULL)
-    {
-      mime_type = g_intern_string (mime_type);
-
-      for (guint i = 0; i < info->mime_types->len; i++)
-        {
-          const gchar *mt = g_ptr_array_index (info->mime_types, i);
-
-          /* interned strings can-use pointer comparison */
-          if (mt == mime_type)
-            return TRUE;
-        }
-    }
-
-  len = strlen (filename);
-
-  for (guint i = 0; i < info->specs->len; i++)
-    {
-      GPatternSpec *spec = g_ptr_array_index (info->specs, i);
-
-      if (g_pattern_match (spec, len, filename, filename_reversed))
-        return TRUE;
-    }
-
-  return FALSE;
-}
-
 static void
 gbp_code_index_plan_populate_cb (GFile     *directory,
                                  GPtrArray *file_infos,
diff --git a/src/plugins/code-index/indexer-info.c b/src/plugins/code-index/indexer-info.c
new file mode 100644
index 000000000..432b00513
--- /dev/null
+++ b/src/plugins/code-index/indexer-info.c
@@ -0,0 +1,149 @@
+/* indexer-info.c
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "indexer-info"
+
+#include "config.h"
+
+#include <libide-core.h>
+#include <libpeas/peas.h>
+#include <gtksourceview/gtksource.h>
+#include <string.h>
+
+#include "indexer-info.h"
+
+void
+indexer_info_free (IndexerInfo *info)
+{
+  g_clear_pointer (&info->specs, g_ptr_array_unref);
+  g_clear_pointer (&info->mime_types, g_ptr_array_unref);
+  g_clear_pointer (&info->lang_ids, g_strfreev);
+  g_slice_free (IndexerInfo, info);
+}
+
+GPtrArray *
+collect_indexer_info (void)
+{
+  GtkSourceLanguageManager *manager;
+  g_autoptr(GPtrArray) indexers = NULL;
+  const GList *plugins;
+  PeasEngine *engine;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+
+  manager = gtk_source_language_manager_get_default ();
+  engine = peas_engine_get_default ();
+  plugins = peas_engine_get_plugin_list (engine);
+  indexers = g_ptr_array_new_with_free_func ((GDestroyNotify)indexer_info_free);
+
+  for (; plugins != NULL; plugins = plugins->next)
+    {
+      const PeasPluginInfo *plugin_info = plugins->data;
+      const gchar *module_name;
+      g_autofree gchar *str = NULL;
+      g_auto(GStrv) split = NULL;
+      IndexerInfo *info;
+
+      if (!peas_plugin_info_is_loaded (plugin_info) ||
+          !(str = g_strdup (peas_plugin_info_get_external_data (plugin_info, "Code-Indexer-Languages"))))
+        continue;
+
+      module_name = peas_plugin_info_get_module_name (plugin_info);
+      split = g_strsplit (g_strdelimit (str, ",", ';'), ";", 0);
+
+      info = g_slice_new0 (IndexerInfo);
+      info->module_name = g_intern_string (module_name);
+      info->mime_types = g_ptr_array_new ();
+      info->specs = g_ptr_array_new_with_free_func ((GDestroyNotify)g_pattern_spec_free);
+      info->lang_ids = g_strdupv (split);
+
+      for (guint i = 0; split[i]; i++)
+        {
+          GtkSourceLanguage *lang;
+          const gchar *name = split[i];
+          g_auto(GStrv) globs = NULL;
+          g_auto(GStrv) mime_types = NULL;
+
+          if (!(lang = gtk_source_language_manager_get_language (manager, name)))
+            {
+              g_warning ("No such language \"%s\" in %s plugin description",
+                         name, module_name);
+              continue;
+            }
+
+          globs = gtk_source_language_get_globs (lang);
+          mime_types = gtk_source_language_get_mime_types (lang);
+
+          for (guint j = 0; globs[j] != NULL; j++)
+            {
+              g_autoptr(GPatternSpec) spec = g_pattern_spec_new (globs[j]);
+              g_ptr_array_add (info->specs, g_steal_pointer (&spec));
+            }
+
+          for (guint j = 0; mime_types[j]; j++)
+            g_ptr_array_add (info->mime_types, (gchar *)g_intern_string (mime_types[j]));
+        }
+
+      g_ptr_array_add (indexers, g_steal_pointer (&info));
+    }
+
+  return g_steal_pointer (&indexers);
+}
+
+gboolean
+indexer_info_matches (const IndexerInfo *info,
+                      const gchar       *filename,
+                      const gchar       *filename_reversed,
+                      const gchar       *mime_type)
+{
+  gsize len;
+
+  g_assert (info != NULL);
+  g_assert (filename != NULL);
+  g_assert (filename_reversed != NULL);
+
+  if (mime_type != NULL)
+    {
+      mime_type = g_intern_string (mime_type);
+
+      for (guint i = 0; i < info->mime_types->len; i++)
+        {
+          const gchar *mt = g_ptr_array_index (info->mime_types, i);
+
+          /* interned strings can-use pointer comparison */
+          if (mt == mime_type)
+            return TRUE;
+        }
+    }
+
+  len = strlen (filename);
+
+  for (guint i = 0; i < info->specs->len; i++)
+    {
+      GPatternSpec *spec = g_ptr_array_index (info->specs, i);
+
+      if (g_pattern_match (spec, len, filename, filename_reversed))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+
diff --git a/src/plugins/code-index/indexer-info.h b/src/plugins/code-index/indexer-info.h
new file mode 100644
index 000000000..31693d71f
--- /dev/null
+++ b/src/plugins/code-index/indexer-info.h
@@ -0,0 +1,42 @@
+/* indexer-info.h
+ *
+ * Copyright 2019 Christian Hergert <chergert redhat 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#include <libpeas/peas.h>
+
+G_BEGIN_DECLS
+
+typedef struct
+{
+  const gchar  *module_name;
+  GPtrArray    *specs;
+  GPtrArray    *mime_types;
+  gchar       **lang_ids;
+} IndexerInfo;
+
+void       indexer_info_free    (IndexerInfo       *info);
+GPtrArray *collect_indexer_info (void);
+gboolean   indexer_info_matches (const IndexerInfo *info,
+                                 const gchar       *filename,
+                                 const gchar       *filename_reversed,
+                                 const gchar       *mime_type);
+
+G_END_DECLS
diff --git a/src/plugins/code-index/meson.build b/src/plugins/code-index/meson.build
index 8c3e9d688..1917c28af 100644
--- a/src/plugins/code-index/meson.build
+++ b/src/plugins/code-index/meson.build
@@ -13,6 +13,7 @@ plugins_sources += files([
   'ide-code-index-search-provider.c',
   'ide-code-index-search-result.c',
   'ide-code-index-symbol-resolver.c',
+  'indexer-info.c',
 ])
 
 plugin_code_index_resources = gnome.compile_resources(


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