[gnome-builder] rust-analyzer/meson: activate rust-analyzer with meson buildsystems



commit 8e45e262f17558947e62d943c5c03cd9a5b10170
Author: Günther Wagner <info gunibert de>
Date:   Sun Aug 1 13:59:37 2021 +0200

    rust-analyzer/meson: activate rust-analyzer with meson buildsystems

 src/plugins/meson/gbp-meson-build-system.c         | 70 ++++++++++++++++++++++
 src/plugins/meson/gbp-meson-build-system.h         |  1 +
 .../rust-analyzer/rust-analyzer-pipeline-addin.c   | 11 +++-
 3 files changed, 81 insertions(+), 1 deletion(-)
---
diff --git a/src/plugins/meson/gbp-meson-build-system.c b/src/plugins/meson/gbp-meson-build-system.c
index bec4d92ef..e75d6ca39 100644
--- a/src/plugins/meson/gbp-meson-build-system.c
+++ b/src/plugins/meson/gbp-meson-build-system.c
@@ -34,6 +34,7 @@ struct _GbpMesonBuildSystem
   IdeCompileCommands *compile_commands;
   GFileMonitor       *monitor;
   gchar              *project_version;
+  gchar             **languages;
 };
 
 static void async_initable_iface_init (GAsyncInitableIface     *iface);
@@ -390,6 +391,7 @@ gbp_meson_build_system_finalize (GObject *object)
   g_clear_object (&self->compile_commands);
   g_clear_object (&self->monitor);
   g_clear_pointer (&self->project_version, g_free);
+  g_clear_pointer (&self->languages, g_strfreev);
 
   G_OBJECT_CLASS (gbp_meson_build_system_parent_class)->finalize (object);
 }
@@ -738,11 +740,61 @@ build_system_iface_init (IdeBuildSystemInterface *iface)
   iface->supports_toolchain = gbp_meson_build_system_supports_toolchain;
 }
 
+/**
+ * This could be 'projectname', ['c', 'rust'], ... or 'projectname', 'rust', ...
+ */
+static char **
+parse_language (gchar *language_string)
+{
+  if (strstr(language_string, "["))
+    {
+      gchar *begin = NULL;
+      gchar *end = NULL;
+      g_autofree gchar *copy = NULL;
+      GString *gstring = NULL;
+
+      if ((begin = strstr(language_string, "[")) == NULL) goto failure;
+      if ((end = strstr(language_string, "]")) == NULL) goto failure;
+      copy = g_strndup (begin + 1, end-begin - 1);
+
+      gstring = g_string_new (copy);
+      g_string_replace (gstring, "'", "", -1);
+      g_string_replace (gstring, " ", "", -1);
+      copy = g_string_free (gstring, FALSE);
+
+      return g_strsplit (copy, ",", -1);
+    }
+  else
+    {
+      gchar **list = NULL;
+      g_autofree gchar *language = NULL;
+      GString *gstring = NULL;
+      g_autoptr(GStrvBuilder) builder = NULL;
+
+      list = g_strsplit (language_string, ",", -1);
+      language = g_strstrip (list[1]);
+
+      gstring = g_string_new (language);
+      g_string_replace (gstring, "'", "", -1);
+      language = g_string_free (gstring, FALSE);
+      g_strfreev (list);
+
+      builder = g_strv_builder_new ();
+      g_strv_builder_add (builder, g_steal_pointer (&language));
+      return g_strv_builder_end (builder);
+    }
+
+failure:
+  return NULL;
+}
+
 static void
 extract_metadata (GbpMesonBuildSystem *self,
                   const gchar         *contents)
 {
   const gchar *ptr;
+  g_autoptr(GRegex) regex = NULL;
+  g_autoptr(GMatchInfo) match_info = NULL;
 
   g_assert (IDE_IS_MAIN_THREAD ());
   g_assert (GBP_IS_MESON_BUILD_SYSTEM (self));
@@ -780,6 +832,16 @@ extract_metadata (GbpMesonBuildSystem *self,
         }
     }
 
+  regex = g_regex_new ("^project\\((.*)\\)", G_REGEX_DOTALL | G_REGEX_UNGREEDY, 0, NULL);
+  g_regex_match (regex, contents, 0, &match_info);
+  while (g_match_info_matches (match_info))
+    {
+      gchar *str = g_match_info_fetch (match_info, 1);
+      self->languages = parse_language (str);
+
+      g_match_info_next (match_info, NULL);
+    }
+
 failure:
 
   return;
@@ -880,3 +942,11 @@ async_initable_iface_init (GAsyncInitableIface *iface)
   iface->init_async = gbp_meson_build_system_init_async;
   iface->init_finish = gbp_meson_build_system_init_finish;
 }
+
+const gchar * const *
+gbp_meson_build_system_get_languages (GbpMesonBuildSystem *self)
+{
+  g_return_val_if_fail (GBP_IS_MESON_BUILD_SYSTEM (self), NULL);
+
+  return (const gchar * const *)self->languages;
+}
diff --git a/src/plugins/meson/gbp-meson-build-system.h b/src/plugins/meson/gbp-meson-build-system.h
index 16a6747d5..5cb726ff3 100644
--- a/src/plugins/meson/gbp-meson-build-system.h
+++ b/src/plugins/meson/gbp-meson-build-system.h
@@ -27,5 +27,6 @@ G_BEGIN_DECLS
 #define GBP_TYPE_MESON_BUILD_SYSTEM (gbp_meson_build_system_get_type())
 
 G_DECLARE_FINAL_TYPE (GbpMesonBuildSystem, gbp_meson_build_system, GBP, MESON_BUILD_SYSTEM, IdeObject)
+const gchar * const * gbp_meson_build_system_get_languages (GbpMesonBuildSystem *self);
 
 G_END_DECLS
diff --git a/src/plugins/rust-analyzer/rust-analyzer-pipeline-addin.c 
b/src/plugins/rust-analyzer/rust-analyzer-pipeline-addin.c
index ba709ea94..4597f2ff7 100644
--- a/src/plugins/rust-analyzer/rust-analyzer-pipeline-addin.c
+++ b/src/plugins/rust-analyzer/rust-analyzer-pipeline-addin.c
@@ -26,6 +26,7 @@
 #include <libide-core.h>
 
 #include "rust-analyzer-pipeline-addin.h"
+#include <meson/gbp-meson-build-system.h>
 
 #if 0
 # define DEV_MODE
@@ -297,7 +298,15 @@ rust_analyzer_pipeline_addin_prepare (IdePipelineAddin *addin,
 
   context = ide_object_get_context (IDE_OBJECT (pipeline));
   buildsystem = ide_build_system_from_context (context);
-  if (!ide_str_equal (ide_build_system_get_id (buildsystem), "cargo"))
+
+  if (ide_str_equal (ide_build_system_get_id (buildsystem), "meson"))
+    {
+      GbpMesonBuildSystem *meson = GBP_MESON_BUILD_SYSTEM (buildsystem);
+      const gchar * const *languages = gbp_meson_build_system_get_languages (meson);
+      if (!languages || !g_strv_contains (languages, "rust"))
+        IDE_EXIT;
+    }
+  else if (!ide_str_equal (ide_build_system_get_id (buildsystem), "cargo"))
       IDE_EXIT;
 
   self->pipeline = pipeline;


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