[gnome-builder/wip/gtk4-port] plugins/snippets: wrap enable-snippets gsetting



commit 0a2489d0cc70f2adf322752ae1ef4621b48d7f1d
Author: Christian Hergert <chergert redhat com>
Date:   Tue Apr 26 15:04:01 2022 -0700

    plugins/snippets: wrap enable-snippets gsetting
    
    This way it is in sync with the editor itself expanding based on the words
    displayed to the user.

 .../snippets/gbp-snippet-completion-provider.c     | 150 +++++++++++++++++++++
 .../snippets/gbp-snippet-completion-provider.h     |  31 +++++
 src/plugins/snippets/meson.build                   |   1 +
 src/plugins/snippets/snippets-plugin.c             |   3 +-
 4 files changed, 184 insertions(+), 1 deletion(-)
---
diff --git a/src/plugins/snippets/gbp-snippet-completion-provider.c 
b/src/plugins/snippets/gbp-snippet-completion-provider.c
new file mode 100644
index 000000000..c2d5c3970
--- /dev/null
+++ b/src/plugins/snippets/gbp-snippet-completion-provider.c
@@ -0,0 +1,150 @@
+/* gbp-snippet-completion-provider.c
+ *
+ * Copyright 2022 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 "gbp-snippet-completion-provider"
+
+#include "config.h"
+
+#include "gbp-snippet-completion-provider.h"
+
+struct _GbpSnippetCompletionProvider
+{
+  GtkSourceCompletionSnippets parent_instance;
+  guint enabled : 1;
+};
+
+enum {
+  PROP_0,
+  PROP_ENABLED,
+  N_PROPS
+};
+
+static GParamSpec *properties [N_PROPS];
+static GtkSourceCompletionProviderInterface *parent_iface;
+
+static GListModel *
+gbp_snippet_completion_provider_populate (GtkSourceCompletionProvider  *provider,
+                                          GtkSourceCompletionContext   *context,
+                                          GError                      **error)
+{
+  GbpSnippetCompletionProvider *self = (GbpSnippetCompletionProvider *)provider;
+
+  g_assert (GBP_IS_SNIPPET_COMPLETION_PROVIDER (self));
+  g_assert (GTK_SOURCE_IS_COMPLETION_CONTEXT (context));
+
+  if (!self->enabled)
+    {
+      g_set_error (error,
+                   G_IO_ERROR,
+                   G_IO_ERROR_NOT_SUPPORTED,
+                   "Provider is disabled");
+      return NULL;
+    }
+
+  return parent_iface->populate (provider, context, error);
+}
+
+static void
+competion_provider_iface_init (GtkSourceCompletionProviderInterface *iface)
+{
+  parent_iface = g_type_interface_peek_parent (iface);
+  iface->populate = gbp_snippet_completion_provider_populate;
+}
+
+G_DEFINE_FINAL_TYPE_WITH_CODE (GbpSnippetCompletionProvider, gbp_snippet_completion_provider, 
GTK_SOURCE_TYPE_COMPLETION_SNIPPETS,
+                               G_IMPLEMENT_INTERFACE (GTK_SOURCE_TYPE_COMPLETION_PROVIDER, 
competion_provider_iface_init))
+
+static void
+gbp_snippet_completion_provider_constructed (GObject *object)
+{
+  GbpSnippetCompletionProvider *self = (GbpSnippetCompletionProvider *)object;
+  static GSettings *editor_settings;
+
+  G_OBJECT_CLASS (gbp_snippet_completion_provider_parent_class)->constructed (object);
+
+  if (editor_settings == NULL)
+    editor_settings = g_settings_new ("org.gnome.builder.editor");
+
+  g_settings_bind (editor_settings, "enable-snippets",
+                   self, "enabled",
+                   G_SETTINGS_BIND_GET);
+}
+
+static void
+gbp_snippet_completion_provider_get_property (GObject    *object,
+                                              guint       prop_id,
+                                              GValue     *value,
+                                              GParamSpec *pspec)
+{
+  GbpSnippetCompletionProvider *self = GBP_SNIPPET_COMPLETION_PROVIDER (object);
+
+  switch (prop_id)
+    {
+    case PROP_ENABLED:
+      g_value_set_boolean (value, self->enabled);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_snippet_completion_provider_set_property (GObject      *object,
+                                              guint         prop_id,
+                                              const GValue *value,
+                                              GParamSpec   *pspec)
+{
+  GbpSnippetCompletionProvider *self = GBP_SNIPPET_COMPLETION_PROVIDER (object);
+
+  switch (prop_id)
+    {
+    case PROP_ENABLED:
+      self->enabled = g_value_get_boolean (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+gbp_snippet_completion_provider_class_init (GbpSnippetCompletionProviderClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->constructed = gbp_snippet_completion_provider_constructed;
+  object_class->get_property = gbp_snippet_completion_provider_get_property;
+  object_class->set_property = gbp_snippet_completion_provider_set_property;
+
+  properties [PROP_ENABLED] =
+    g_param_spec_boolean ("enabled",
+                          "Enabled",
+                          "If the provider is enabled",
+                          FALSE,
+                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+gbp_snippet_completion_provider_init (GbpSnippetCompletionProvider *self)
+{
+}
diff --git a/src/plugins/snippets/gbp-snippet-completion-provider.h 
b/src/plugins/snippets/gbp-snippet-completion-provider.h
new file mode 100644
index 000000000..916f9dda7
--- /dev/null
+++ b/src/plugins/snippets/gbp-snippet-completion-provider.h
@@ -0,0 +1,31 @@
+/* gbp-snippet-completion-provider.h
+ *
+ * Copyright 2022 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 <gtksourceview/gtksource.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_SNIPPET_COMPLETION_PROVIDER (gbp_snippet_completion_provider_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpSnippetCompletionProvider, gbp_snippet_completion_provider, GBP, 
SNIPPET_COMPLETION_PROVIDER, GtkSourceCompletionSnippets)
+
+G_END_DECLS
diff --git a/src/plugins/snippets/meson.build b/src/plugins/snippets/meson.build
index 6eab11d4f..ca78beec2 100644
--- a/src/plugins/snippets/meson.build
+++ b/src/plugins/snippets/meson.build
@@ -1,6 +1,7 @@
 plugins_sources += files([
   'snippets-plugin.c',
   'ide-snippet-application-addin.c',
+  'gbp-snippet-completion-provider.c',
 ])
 
 snippets_resources = gnome.compile_resources(
diff --git a/src/plugins/snippets/snippets-plugin.c b/src/plugins/snippets/snippets-plugin.c
index 7d647013e..c1f737d8a 100644
--- a/src/plugins/snippets/snippets-plugin.c
+++ b/src/plugins/snippets/snippets-plugin.c
@@ -28,13 +28,14 @@
 #include <libide-sourceview.h>
 
 #include "ide-snippet-application-addin.h"
+#include "gbp-snippet-completion-provider.h"
 
 _IDE_EXTERN void
 _gbp_snippets_register_types (PeasObjectModule *module)
 {
   peas_object_module_register_extension_type (module,
                                               GTK_SOURCE_TYPE_COMPLETION_PROVIDER,
-                                              GTK_SOURCE_TYPE_COMPLETION_SNIPPETS);
+                                              GBP_TYPE_SNIPPET_COMPLETION_PROVIDER);
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_APPLICATION_ADDIN,
                                               IDE_TYPE_SNIPPET_APPLICATION_ADDIN);


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