[gnome-builder: 14/17] rust-analyzer: cargo check command preference



commit 49613311a477243c78bced15d71c1f12e23df1ac
Author: Günther Wagner <info gunibert de>
Date:   Mon Jun 1 12:07:06 2020 +0200

    rust-analyzer: cargo check command preference

 src/plugins/rust-analyzer/meson.build              |  3 +
 .../org.gnome.builder.rust-analyzer.gschema.xml    | 12 +++
 .../rust-analyzer-preferences-addin.c              | 98 ++++++++++++++++++++++
 .../rust-analyzer-preferences-addin.h              | 31 +++++++
 src/plugins/rust-analyzer/rust-analyzer-service.c  | 79 +++++++++++++++++
 src/plugins/rust-analyzer/rust-analyzer-service.h  | 17 ++--
 src/plugins/rust-analyzer/rust-analyzer.c          |  4 +
 7 files changed, 237 insertions(+), 7 deletions(-)
---
diff --git a/src/plugins/rust-analyzer/meson.build b/src/plugins/rust-analyzer/meson.build
index cb32b05c1..7eb0e2a2f 100644
--- a/src/plugins/rust-analyzer/meson.build
+++ b/src/plugins/rust-analyzer/meson.build
@@ -14,6 +14,7 @@ plugins_sources += files([
   'rust-analyzer-search-provider.c',
   'rust-analyzer-search-result.c',
   'rust-analyzer-workbench-addin.c',
+  'rust-analyzer-preferences-addin.c',
 ])
 
 plugin_rust_analyzer_resources = gnome.compile_resources(
@@ -24,4 +25,6 @@ plugin_rust_analyzer_resources = gnome.compile_resources(
 
 plugins_sources += plugin_rust_analyzer_resources
 
+install_data(['org.gnome.builder.rust-analyzer.gschema.xml'], install_dir: schema_dir)
+
 endif
diff --git a/src/plugins/rust-analyzer/org.gnome.builder.rust-analyzer.gschema.xml 
b/src/plugins/rust-analyzer/org.gnome.builder.rust-analyzer.gschema.xml
new file mode 100644
index 000000000..f9c6549a5
--- /dev/null
+++ b/src/plugins/rust-analyzer/org.gnome.builder.rust-analyzer.gschema.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<schemalist>
+  <schema id="org.gnome.builder.rust-analyzer" path="/org/gnome/builder/plugins/rust-analyzer/" 
gettext-domain="gnome-builder">
+    <key name="cargo-command" type="s">
+      <choices>
+        <choice value="check"/>
+        <choice value="clippy"/>
+      </choices>
+      <default>'check'</default>
+    </key>
+  </schema>
+</schemalist>
diff --git a/src/plugins/rust-analyzer/rust-analyzer-preferences-addin.c 
b/src/plugins/rust-analyzer/rust-analyzer-preferences-addin.c
new file mode 100644
index 000000000..2ef63cb4a
--- /dev/null
+++ b/src/plugins/rust-analyzer/rust-analyzer-preferences-addin.c
@@ -0,0 +1,98 @@
+/* rust-analyzer-preferences-addin.c
+ *
+ * Copyright 2020 Günther Wagner <info gunibert de>
+ *
+ * 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
+ */
+
+#include "rust-analyzer-preferences-addin.h"
+#include <glib/gi18n.h>
+
+struct _RustAnalyzerPreferencesAddin
+{
+  IdeObject parent_instance;
+  guint check_id;
+  guint clippy_id;
+};
+
+static void preferences_addin_iface_init (IdePreferencesAddinInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (RustAnalyzerPreferencesAddin,
+                         rust_analyzer_preferences_addin,
+                         IDE_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_PREFERENCES_ADDIN, preferences_addin_iface_init))
+
+static void
+rust_analyzer_preferences_addin_class_init (RustAnalyzerPreferencesAddinClass *klass)
+{
+}
+
+static void
+rust_analyzer_preferences_addin_init (RustAnalyzerPreferencesAddin *self)
+{
+}
+
+static void
+rust_analyzer_preferences_addin_load (IdePreferencesAddin *addin,
+                                      DzlPreferences      *preferences)
+{
+  RustAnalyzerPreferencesAddin *self = (RustAnalyzerPreferencesAddin *)addin;
+
+  g_return_if_fail (RUST_IS_ANALYZER_PREFERENCES_ADDIN (self));
+  g_return_if_fail (DZL_IS_PREFERENCES (preferences));
+
+  dzl_preferences_add_list_group (preferences, "code-insight", "rust-analyzer", _("Rust Analyzer: Cargo 
command for diagnostics"), GTK_SELECTION_NONE, 0);
+  self->check_id = dzl_preferences_add_radio (preferences,
+                                              "code-insight",
+                                              "rust-analyzer",
+                                              "org.gnome.builder.rust-analyzer",
+                                              "cargo-command",
+                                              NULL,
+                                              "\"check\"",
+                                              "Cargo check",
+                                              _("the default cargo command"),
+                                              NULL, 1);
+  self->clippy_id = dzl_preferences_add_radio (preferences,
+                                               "code-insight",
+                                               "rust-analyzer",
+                                               "org.gnome.builder.rust-analyzer",
+                                               "cargo-command",
+                                               NULL,
+                                               "\"clippy\"",
+                                               "Cargo clippy",
+                                               _("clippy adds additional lints to catch common mistakes but 
is in general slower"),
+                                               NULL, 2);
+}
+
+static void
+rust_analyzer_preferences_addin_unload (IdePreferencesAddin *addin,
+                                        DzlPreferences      *preferences)
+{
+  RustAnalyzerPreferencesAddin *self = (RustAnalyzerPreferencesAddin *)addin;
+
+  g_return_if_fail (RUST_IS_ANALYZER_PREFERENCES_ADDIN (self));
+  g_return_if_fail (DZL_IS_PREFERENCES (preferences));
+
+  dzl_preferences_remove_id (preferences, self->check_id);
+  dzl_preferences_remove_id (preferences, self->clippy_id);
+}
+
+static void
+preferences_addin_iface_init (IdePreferencesAddinInterface *iface)
+{
+  iface->load = rust_analyzer_preferences_addin_load;
+  iface->unload = rust_analyzer_preferences_addin_unload;
+}
diff --git a/src/plugins/rust-analyzer/rust-analyzer-preferences-addin.h 
b/src/plugins/rust-analyzer/rust-analyzer-preferences-addin.h
new file mode 100644
index 000000000..ea5a5e03f
--- /dev/null
+++ b/src/plugins/rust-analyzer/rust-analyzer-preferences-addin.h
@@ -0,0 +1,31 @@
+/* rust-analyzer-preferences-addin.h
+ *
+ * Copyright 2020 Günther Wagner <info gunibert de>
+ *
+ * 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 <libide-gui.h>
+
+G_BEGIN_DECLS
+
+#define RUST_TYPE_ANALYZER_PREFERENCES_ADDIN (rust_analyzer_preferences_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (RustAnalyzerPreferencesAddin, rust_analyzer_preferences_addin, RUST, 
ANALYZER_PREFERENCES_ADDIN, IdeObject)
+
+G_END_DECLS
diff --git a/src/plugins/rust-analyzer/rust-analyzer-service.c 
b/src/plugins/rust-analyzer/rust-analyzer-service.c
index 88f9cd7a3..2d7548607 100644
--- a/src/plugins/rust-analyzer/rust-analyzer-service.c
+++ b/src/plugins/rust-analyzer/rust-analyzer-service.c
@@ -38,6 +38,8 @@ struct _RustAnalyzerService
   IdeSubprocessSupervisor *supervisor;
   GFileMonitor *cargo_monitor;
   RustAnalyzerSearchProvider *search_provider;
+  GSettings *settings;
+  gchar *cargo_command;
 
   ServiceState state;
 };
@@ -47,6 +49,7 @@ G_DEFINE_TYPE (RustAnalyzerService, rust_analyzer_service, IDE_TYPE_OBJECT)
 enum {
   PROP_0,
   PROP_CLIENT,
+  PROP_CARGO_COMMAND,
   N_PROPS
 };
 
@@ -88,6 +91,27 @@ _get_search_engine (RustAnalyzerService *self)
   return ide_object_get_child_typed (IDE_OBJECT (context), IDE_TYPE_SEARCH_ENGINE);
 }
 
+static GVariant *
+rust_analyzer_service_load_configuration (IdeLspClient *client,
+                                          gpointer      user_data)
+{
+  RustAnalyzerService *self = (RustAnalyzerService *)user_data;
+  GVariant *ret = NULL;
+
+  g_assert (IDE_IS_LSP_CLIENT (client));
+  g_assert (RUST_IS_ANALYZER_SERVICE (self));
+
+  IDE_ENTRY;
+
+  ret = JSONRPC_MESSAGE_NEW_ARRAY ("{",
+                                     "checkOnSave", "{",
+                                       "command", JSONRPC_MESSAGE_PUT_STRING (self->cargo_command),
+                                     "}",
+                                   "}");
+
+  IDE_RETURN (ret);
+}
+
 static void
 rust_analyzer_service_get_property (GObject    *object,
                                     guint       prop_id,
@@ -101,6 +125,9 @@ rust_analyzer_service_get_property (GObject    *object,
     case PROP_CLIENT:
       g_value_set_object (value, rust_analyzer_service_get_client (self));
       break;
+    case PROP_CARGO_COMMAND:
+      g_value_set_string (value, rust_analyzer_service_get_cargo_command (self));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -119,6 +146,9 @@ rust_analyzer_service_set_property (GObject      *object,
     case PROP_CLIENT:
       rust_analyzer_service_set_client (self, g_value_get_object (value));
       break;
+    case PROP_CARGO_COMMAND:
+      rust_analyzer_service_set_cargo_command (self, g_value_dup_string (value));
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -204,6 +234,13 @@ rust_analyzer_service_class_init (RustAnalyzerServiceClass *klass)
                          IDE_TYPE_LSP_CLIENT,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_CARGO_COMMAND] =
+    g_param_spec_string ("cargo-command",
+                         "Cargo-command",
+                         "The used cargo command for rust-analyzer",
+                         "check",
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, N_PROPS, properties);
 }
 
@@ -212,6 +249,8 @@ rust_analyzer_service_init (RustAnalyzerService *self)
 {
   self->client = NULL;
   self->state = RUST_ANALYZER_SERVICE_INIT;
+  self->settings = g_settings_new ("org.gnome.builder.rust-analyzer");
+  g_settings_bind (self->settings, "cargo-command", self, "cargo-command", G_SETTINGS_BIND_DEFAULT);
 }
 
 IdeLspClient *
@@ -231,10 +270,25 @@ rust_analyzer_service_set_client (RustAnalyzerService *self,
 
   if (g_set_object (&self->client, client))
     {
+      g_signal_connect (self->client, "load-configuration", G_CALLBACK 
(rust_analyzer_service_load_configuration), self);
       g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_CLIENT]);
     }
 }
 
+static void
+rust_analyzer_service_lsp_initialized (IdeLspClient *client,
+                                       gpointer      user_data)
+{
+  RustAnalyzerService *self = (RustAnalyzerService *) user_data;
+  g_autoptr(GVariant) params = NULL;
+
+  g_assert (IDE_IS_LSP_CLIENT (client));
+  g_assert (RUST_IS_ANALYZER_SERVICE (self));
+
+  params = JSONRPC_MESSAGE_NEW ("settings", "");
+  ide_lsp_client_send_notification_async (client, "workspace/didChangeConfiguration", params, NULL, NULL, 
NULL);
+}
+
 void
 rust_analyzer_service_lsp_started (IdeSubprocessSupervisor *supervisor,
                                    IdeSubprocess           *subprocess,
@@ -261,6 +315,7 @@ rust_analyzer_service_lsp_started (IdeSubprocessSupervisor *supervisor,
     }
 
   client = ide_lsp_client_new (io_stream);
+  g_signal_connect (client, "initialized", G_CALLBACK (rust_analyzer_service_lsp_initialized), self);
   rust_analyzer_service_set_client (self, client);
   ide_object_append (IDE_OBJECT (self), IDE_OBJECT (client));
   ide_lsp_client_add_language (client, "rust");
@@ -383,3 +438,27 @@ rust_analyzer_service_set_state (RustAnalyzerService *self,
 
   self->state = state;
 }
+
+gchar *
+rust_analyzer_service_get_cargo_command (RustAnalyzerService *self)
+{
+  g_return_val_if_fail (RUST_IS_ANALYZER_SERVICE (self), NULL);
+
+  return self->cargo_command;
+}
+
+void
+rust_analyzer_service_set_cargo_command (RustAnalyzerService *self,
+                                         const gchar         *cargo_command)
+{
+  g_autoptr(GVariant) params = NULL;
+
+  g_return_if_fail (RUST_IS_ANALYZER_SERVICE (self));
+  g_return_if_fail (cargo_command != NULL);
+
+  g_clear_pointer (&self->cargo_command, g_free);
+  self->cargo_command = g_strdup (cargo_command);
+
+  params = JSONRPC_MESSAGE_NEW ("settings", "");
+  ide_lsp_client_send_notification_async (self->client, "workspace/didChangeConfiguration", params, NULL, 
NULL, NULL);
+}
diff --git a/src/plugins/rust-analyzer/rust-analyzer-service.h 
b/src/plugins/rust-analyzer/rust-analyzer-service.h
index 6c3ffae66..eb4b9541b 100644
--- a/src/plugins/rust-analyzer/rust-analyzer-service.h
+++ b/src/plugins/rust-analyzer/rust-analyzer-service.h
@@ -16,12 +16,15 @@ typedef enum {
   RUST_ANALYZER_SERVICE_LSP_STARTED,
 } ServiceState;
 
-RustAnalyzerService *rust_analyzer_service_new            (void);
-IdeLspClient        *rust_analyzer_service_get_client     (RustAnalyzerService *self);
-void                 rust_analyzer_service_set_client     (RustAnalyzerService *self,
-                                                           IdeLspClient        *client);
-void                 rust_analyzer_service_ensure_started (RustAnalyzerService *self);
-void                 rust_analyzer_service_set_state      (RustAnalyzerService *self,
-                                                           ServiceState         state);
+RustAnalyzerService *rust_analyzer_service_new               (void);
+IdeLspClient        *rust_analyzer_service_get_client        (RustAnalyzerService *self);
+void                 rust_analyzer_service_set_client        (RustAnalyzerService *self,
+                                                              IdeLspClient        *client);
+gchar               *rust_analyzer_service_get_cargo_command (RustAnalyzerService *self);
+void                 rust_analyzer_service_set_cargo_command (RustAnalyzerService *self,
+                                                              const gchar         *cargo_command);
+void                 rust_analyzer_service_ensure_started    (RustAnalyzerService *self);
+void                 rust_analyzer_service_set_state         (RustAnalyzerService *self,
+                                                              ServiceState         state);
 
 G_END_DECLS
diff --git a/src/plugins/rust-analyzer/rust-analyzer.c b/src/plugins/rust-analyzer/rust-analyzer.c
index f8d5d0062..fa711ea35 100644
--- a/src/plugins/rust-analyzer/rust-analyzer.c
+++ b/src/plugins/rust-analyzer/rust-analyzer.c
@@ -29,6 +29,7 @@
 #include "rust-analyzer-hover-provider.h"
 #include "rust-analyzer-workbench-addin.h"
 #include "rust-analyzer-rename-provider.h"
+#include "rust-analyzer-preferences-addin.h"
 
 _IDE_EXTERN void
 _rust_analyzer_register_types (PeasObjectModule *module)
@@ -57,4 +58,7 @@ _rust_analyzer_register_types (PeasObjectModule *module)
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_RENAME_PROVIDER,
                                               RUST_TYPE_ANALYZER_RENAME_PROVIDER);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_PREFERENCES_ADDIN,
+                                              RUST_TYPE_ANALYZER_PREFERENCES_ADDIN);
 }


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