[gnome-builder/wip/gtk4-port] Port copyright plugin to C from Python



commit c375bbf85f3b661a8aaf83c3040fb15ef0fdabc9
Author: Tristan Partin <tristan partin io>
Date:   Fri May 13 22:31:58 2022 -0500

    Port copyright plugin to C from Python

 src/plugins/copyright/copyright-plugin.c           |  39 +++++
 src/plugins/copyright/copyright.plugin             |   4 +-
 src/plugins/copyright/copyright_plugin.py          | 120 -------------
 src/plugins/copyright/gbp-copyright-buffer-addin.c | 192 +++++++++++++++++++++
 src/plugins/copyright/gbp-copyright-buffer-addin.h |  32 ++++
 .../copyright/gbp-copyright-preferences-addin.c    |  85 +++++++++
 .../copyright/gbp-copyright-preferences-addin.h    |  32 ++++
 src/plugins/copyright/meson.build                  |  17 +-
 8 files changed, 388 insertions(+), 133 deletions(-)
---
diff --git a/src/plugins/copyright/copyright-plugin.c b/src/plugins/copyright/copyright-plugin.c
new file mode 100644
index 000000000..3bb6ac1ac
--- /dev/null
+++ b/src/plugins/copyright/copyright-plugin.c
@@ -0,0 +1,39 @@
+/* copyright-plugin.c
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ * Copyright 2022 Tristan Partin <tristan partin io>
+ *
+ * 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 <libpeas/peas.h>
+
+#include <libide-core.h>
+#include <libide-gui.h>
+
+#include "gbp-copyright-buffer-addin.h"
+#include "gbp-copyright-preferences-addin.h"
+
+_IDE_EXTERN void
+_gbp_copyright_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_PREFERENCES_ADDIN,
+                                              GBP_TYPE_COPYRIGHT_PREFERENCES_ADDIN);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_BUFFER_ADDIN,
+                                              GBP_TYPE_COPYRIGHT_BUFFER_ADDIN);
+}
diff --git a/src/plugins/copyright/copyright.plugin b/src/plugins/copyright/copyright.plugin
index 22af641ee..f389cb6a4 100644
--- a/src/plugins/copyright/copyright.plugin
+++ b/src/plugins/copyright/copyright.plugin
@@ -3,9 +3,9 @@ Authors=Christian Hergert <chergert redhat com>
 Builtin=true
 Copyright=Copyright © 2020 Christian Hergert
 Description=Update copyright when files are saved
-Loader=python3
+Embedded=_gbp_copyright_register_types
 Module=copyright_plugin
 Name=Update Copyright
-X-Builder-ABI=@PACKAGE_ABI@
 X-Category=editing
 X-Preferences-Kind=application;
+
diff --git a/src/plugins/copyright/gbp-copyright-buffer-addin.c 
b/src/plugins/copyright/gbp-copyright-buffer-addin.c
new file mode 100644
index 000000000..b6839a4f0
--- /dev/null
+++ b/src/plugins/copyright/gbp-copyright-buffer-addin.c
@@ -0,0 +1,192 @@
+/* gbp-copyright-buffer-addin.c
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ * Copyright 2022 Tristan Partin <tristan partin io>
+ *
+ * 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-copyright-buffer-addin"
+
+#include "config.h"
+
+#include <string.h>
+
+#include <gio/gio.h>
+#include <glib.h>
+#include <glib-object.h>
+#include <glib/gi18n.h>
+
+#include <libide-code.h>
+#include <libide-gui.h>
+
+#include "gbp-copyright-buffer-addin.h"
+
+#define MAX_LINE 100
+#define MAX_BYTES_IN_SCAN (64 << 10) /* 64kb */
+
+static GSettings *copyright_settings;
+static GRegex *year_regex;
+
+struct _GbpCopyrightBufferAddin
+{
+  GObject parent_instance;
+};
+
+static void
+gbp_copyright_buffer_addin_load (IdeBufferAddin *addin,
+                                 IdeBuffer      *buffer)
+{
+}
+
+static void
+gbp_copyright_buffer_addin_unload (IdeBufferAddin *addin,
+                                   IdeBuffer      *buffer)
+{
+}
+
+static void
+gbp_copyright_buffer_addin_save_file (IdeBufferAddin *addin,
+                                      IdeBuffer      *buffer,
+                                      GFile          *file)
+{
+  GbpCopyrightBufferAddin *self = GBP_COPYRIGHT_BUFFER_ADDIN (addin);
+  const char *name;
+  g_autoptr(GDateTime) now = NULL;
+  g_autofree char *year = NULL;
+  GtkTextIter iter, limit;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_COPYRIGHT_BUFFER_ADDIN (self));
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_FILE (file));
+
+  if (!g_settings_get_boolean (copyright_settings, "update-on-save"))
+    IDE_EXIT;
+
+  name = g_get_real_name ();
+  if (g_strcmp0 (name, "Unknown") == 0)
+    IDE_EXIT;
+
+  now = g_date_time_new_now_local ();
+  year = g_date_time_format (now, "%Y");
+
+  gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &iter);
+  gtk_text_buffer_get_iter_at_line_offset (GTK_TEXT_BUFFER (buffer), &limit, MAX_LINE, 0);
+
+  /* Protect against situations where a user may have opened a minified file */
+  if ((gtk_text_iter_get_offset (&limit) - gtk_text_iter_get_offset (&iter)) > MAX_BYTES_IN_SCAN)
+    IDE_EXIT;
+
+  while (gtk_text_iter_compare (&iter, &limit) < 0)
+    {
+      GtkTextIter match_begin, match_end;
+      g_auto(GStrv) tokens = NULL;
+      guint tokens_len;
+      g_autofree char *text = NULL;
+
+      if (!gtk_text_iter_forward_search (&iter, name, GTK_TEXT_SEARCH_TEXT_ONLY, &match_begin, &match_end, 
&limit))
+        continue;
+
+      gtk_text_iter_set_line_offset (&match_begin, 0);
+      if (!gtk_text_iter_ends_line (&match_end))
+        gtk_text_iter_forward_to_line_end (&match_end);
+
+      text = gtk_text_iter_get_slice (&match_begin, &match_end);
+
+      tokens = g_regex_split (year_regex, text, 0);
+      /* Constant check for strv length < 2 */
+      if (tokens[0] == NULL || tokens[1] == NULL)
+        continue;
+
+      tokens_len = g_strv_length (tokens);
+
+      for (guint i = 0; i < tokens_len; i++)
+        {
+          if (strstr (tokens[i], year))
+            IDE_EXIT;
+        }
+
+      if (tokens_len >= 2)
+        {
+          g_autofree char *new_text = NULL;
+          g_auto(GStrv) new_tokens = g_new0 (char *, tokens_len + 1);
+          guint dash_idx = 0;
+          gboolean found_dash = FALSE;
+
+          for (guint i = 0; i < tokens_len; i++)
+            {
+              if (tokens[i][0] == '-' && tokens[i][1] == 0)
+                {
+                  dash_idx = i;
+                  found_dash = TRUE;
+
+                  break;
+                }
+
+              new_tokens[i] = tokens[i];
+            }
+
+          if (found_dash)
+            {
+              new_tokens[dash_idx + 1] = year;
+            }
+          else
+            {
+              new_tokens[2] = (char *) "-";
+              new_tokens[3] = year;
+            }
+
+          new_text = g_strjoinv (NULL, new_tokens);
+
+          gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (buffer));
+          gtk_text_buffer_delete (GTK_TEXT_BUFFER (buffer), &match_begin, &match_end);
+          gtk_text_buffer_insert (GTK_TEXT_BUFFER (buffer), &match_begin, new_text, -1);
+          gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (buffer));
+
+          break;
+        }
+
+      iter = match_end;
+    }
+
+  IDE_EXIT;
+}
+
+static void
+buffer_addin_init (IdeBufferAddinInterface *iface)
+{
+  iface->load = gbp_copyright_buffer_addin_load;
+  iface->unload = gbp_copyright_buffer_addin_unload;
+  iface->save_file = gbp_copyright_buffer_addin_save_file;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpCopyrightBufferAddin, gbp_copyright_buffer_addin, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_BUFFER_ADDIN, buffer_addin_init))
+
+static void
+gbp_copyright_buffer_addin_class_init (GbpCopyrightBufferAddinClass *klass)
+{
+  copyright_settings = g_settings_new("org.gnome.builder.plugins.copyright");
+  year_regex = g_regex_new("([0-9]{4})", G_REGEX_OPTIMIZE, 0, NULL);
+}
+
+static void
+gbp_copyright_buffer_addin_init (GbpCopyrightBufferAddin *self)
+{
+}
diff --git a/src/plugins/copyright/gbp-copyright-buffer-addin.h 
b/src/plugins/copyright/gbp-copyright-buffer-addin.h
new file mode 100644
index 000000000..101fa7557
--- /dev/null
+++ b/src/plugins/copyright/gbp-copyright-buffer-addin.h
@@ -0,0 +1,32 @@
+/* gbp-copyright-buffer-addin.c
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ * Copyright 2022 Tristan Partin <tristan partin io>
+ *
+ * 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 <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_COPYRIGHT_BUFFER_ADDIN (gbp_copyright_buffer_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpCopyrightBufferAddin, gbp_copyright_buffer_addin, GBP, COPYRIGHT_BUFFER_ADDIN, 
GObject)
+
+G_END_DECLS
diff --git a/src/plugins/copyright/gbp-copyright-preferences-addin.c 
b/src/plugins/copyright/gbp-copyright-preferences-addin.c
new file mode 100644
index 000000000..39e11fbda
--- /dev/null
+++ b/src/plugins/copyright/gbp-copyright-preferences-addin.c
@@ -0,0 +1,85 @@
+/* gbp-copyright-preferences-addin.c
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ * Copyright 2022 Tristan Partin <tristan partin io>
+ *
+ * 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-copyright-preferences-addin"
+
+#include "config.h"
+
+#include <glib-object.h>
+#include <glib/gi18n.h>
+
+#include <libide-gui.h>
+#include <libide-sourceview.h>
+
+#include "gbp-copyright-preferences-addin.h"
+
+#define SCHEMA_ID "org.gnome.builder.plugins.copyright"
+
+struct _GbpCopyrightPreferencesAddin
+{
+  GObject parent_instance;
+};
+
+static const IdePreferenceItemEntry preference_items[] = {
+  { .page = "editing", .group = "formatting", .name = "update-copyright", .priority = 0,
+    .callback = ide_preferences_window_toggle, .title = N_("Update Copyright"),
+    .subtitle = N_("Automatically update copyright headers when saving a file"),
+    .schema_id = SCHEMA_ID, .key = "update-on-save",
+  }
+};
+
+static void
+gbp_copyright_preferences_addin_load (IdePreferencesAddin  *addin,
+                                     IdePreferencesWindow *window,
+                                     IdeContext           *context)
+{
+  GbpCopyrightPreferencesAddin *self = GBP_COPYRIGHT_PREFERENCES_ADDIN (addin);
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_COPYRIGHT_PREFERENCES_ADDIN (self));
+  g_assert (IDE_IS_PREFERENCES_WINDOW (window));
+  g_assert (!context || IDE_IS_CONTEXT (context));
+
+  ide_preferences_window_add_items (window, preference_items, G_N_ELEMENTS (preference_items), NULL, NULL);
+
+  IDE_EXIT;
+}
+
+static void
+preferences_addin_init (IdePreferencesAddinInterface *iface)
+{
+  iface->load = gbp_copyright_preferences_addin_load;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpCopyrightPreferencesAddin, gbp_copyright_preferences_addin, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_PREFERENCES_ADDIN, preferences_addin_init))
+
+static void
+gbp_copyright_preferences_addin_class_init (GbpCopyrightPreferencesAddinClass *klass)
+{
+}
+
+static void
+gbp_copyright_preferences_addin_init (GbpCopyrightPreferencesAddin *self)
+{
+}
diff --git a/src/plugins/copyright/gbp-copyright-preferences-addin.h 
b/src/plugins/copyright/gbp-copyright-preferences-addin.h
new file mode 100644
index 000000000..a7e7cc430
--- /dev/null
+++ b/src/plugins/copyright/gbp-copyright-preferences-addin.h
@@ -0,0 +1,32 @@
+/* gbp-copyright-preferences-addin.h
+ *
+ * Copyright 2018-2019 Christian Hergert <chergert redhat com>
+ * Copyright 2022 Tristan Partin <tristan partin io>
+ *
+ * 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 <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_COPYRIGHT_PREFERENCES_ADDIN (gbp_copyright_preferences_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpCopyrightPreferencesAddin, gbp_copyright_preferences_addin, GBP, 
COPYRIGHT_PREFERENCES_ADDIN, GObject)
+
+G_END_DECLS
diff --git a/src/plugins/copyright/meson.build b/src/plugins/copyright/meson.build
index ba75e7bbe..54ef08e3e 100644
--- a/src/plugins/copyright/meson.build
+++ b/src/plugins/copyright/meson.build
@@ -1,14 +1,9 @@
 if get_option('plugin_copyright')
+  plugins_sources += files(
+    'copyright-plugin.c',
+    'gbp-copyright-buffer-addin.c',
+    'gbp-copyright-preferences-addin.c',
+  )
 
-install_data('copyright_plugin.py', install_dir: plugindir)
-install_data('org.gnome.builder.plugins.copyright.gschema.xml', install_dir: schema_dir)
-
-configure_file(
-          input: 'copyright.plugin',
-         output: 'copyright.plugin',
-  configuration: config_h,
-        install: true,
-    install_dir: plugindir,
-)
-
+  install_data('org.gnome.builder.plugins.copyright.gschema.xml', install_dir: schema_dir)
 endif


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