[gnome-builder: 130/139] trim-spaces: add trim-spaces plugin



commit cde93afb32f5e8688d00dcb0c66e69562754dd64
Author: Christian Hergert <chergert redhat com>
Date:   Wed Jan 9 17:42:10 2019 -0800

    trim-spaces: add trim-spaces plugin
    
    This moves the buffer spaces trimming code into a plugin rather than
    complicating the buffer code.

 .../trim-spaces/gbp-trim-spaces-buffer-addin.c     | 77 ++++++++++++++++++++++
 .../trim-spaces/gbp-trim-spaces-buffer-addin.h     | 31 +++++++++
 src/plugins/trim-spaces/meson.build                | 12 ++++
 src/plugins/trim-spaces/trim-spaces-plugin.c       | 36 ++++++++++
 src/plugins/trim-spaces/trim-spaces.gresource.xml  |  6 ++
 src/plugins/trim-spaces/trim-spaces.plugin         | 10 +++
 6 files changed, 172 insertions(+)
---
diff --git a/src/plugins/trim-spaces/gbp-trim-spaces-buffer-addin.c 
b/src/plugins/trim-spaces/gbp-trim-spaces-buffer-addin.c
new file mode 100644
index 000000000..57cffb35f
--- /dev/null
+++ b/src/plugins/trim-spaces/gbp-trim-spaces-buffer-addin.c
@@ -0,0 +1,77 @@
+/* gbp-trim-spaces-buffer-addin.c
+ *
+ * Copyright 2018-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 "gbp-trim-spaces-buffer-addin"
+
+#include "config.h"
+
+#include <libide-code.h>
+
+#include "ide-buffer-private.h"
+
+#include "gbp-trim-spaces-buffer-addin.h"
+
+struct _GbpTrimSpacesBufferAddin
+{
+  GObject parent_instance;
+};
+
+static void
+gbp_trim_spaces_buffer_addin_save_file (IdeBufferAddin *addin,
+                                        IdeBuffer      *buffer,
+                                        GFile          *file)
+{
+  IdeFileSettings *file_settings;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_TRIM_SPACES_BUFFER_ADDIN (addin));
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_FILE (file));
+
+  if (!(file_settings = ide_buffer_get_file_settings (buffer)) ||
+      !ide_file_settings_get_trim_trailing_whitespace (file_settings))
+    return;
+
+  /*
+   * If file-settings dictate that we should trim trailing whitespace, trim it
+   * from the modified lines in the IdeBuffer. This is performed automatically
+   * based on line state within ide_buffer_trim_trailing_whitespace().
+   */
+  ide_buffer_trim_trailing_whitespace (buffer);
+}
+
+static void
+buffer_addin_iface_init (IdeBufferAddinInterface *iface)
+{
+  iface->save_file = gbp_trim_spaces_buffer_addin_save_file;
+}
+
+G_DEFINE_TYPE_WITH_CODE (GbpTrimSpacesBufferAddin, gbp_trim_spaces_buffer_addin, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (IDE_TYPE_BUFFER_ADDIN, buffer_addin_iface_init))
+
+static void
+gbp_trim_spaces_buffer_addin_class_init (GbpTrimSpacesBufferAddinClass *klass)
+{
+}
+
+static void
+gbp_trim_spaces_buffer_addin_init (GbpTrimSpacesBufferAddin *self)
+{
+}
diff --git a/src/plugins/trim-spaces/gbp-trim-spaces-buffer-addin.h 
b/src/plugins/trim-spaces/gbp-trim-spaces-buffer-addin.h
new file mode 100644
index 000000000..44d39332e
--- /dev/null
+++ b/src/plugins/trim-spaces/gbp-trim-spaces-buffer-addin.h
@@ -0,0 +1,31 @@
+/* gbp-trim-spaces-buffer-addin.h
+ *
+ * Copyright 2018-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 <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_TRIM_SPACES_BUFFER_ADDIN (gbp_trim_spaces_buffer_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpTrimSpacesBufferAddin, gbp_trim_spaces_buffer_addin, GBP, TRIM_SPACES_BUFFER_ADDIN, 
GObject)
+
+G_END_DECLS
diff --git a/src/plugins/trim-spaces/meson.build b/src/plugins/trim-spaces/meson.build
new file mode 100644
index 000000000..e366b0e69
--- /dev/null
+++ b/src/plugins/trim-spaces/meson.build
@@ -0,0 +1,12 @@
+plugins_sources += files([
+  'trim-spaces-plugin.c',
+  'gbp-trim-spaces-buffer-addin.c',
+])
+
+plugin_trim_spaces_resources = gnome.compile_resources(
+  'gbp-trim-spaces-resources',
+  'trim-spaces.gresource.xml',
+  c_name: 'gbp_trim_spaces',
+)
+
+plugins_sources += plugin_trim_spaces_resources[0]
diff --git a/src/plugins/trim-spaces/trim-spaces-plugin.c b/src/plugins/trim-spaces/trim-spaces-plugin.c
new file mode 100644
index 000000000..8fbbe1024
--- /dev/null
+++ b/src/plugins/trim-spaces/trim-spaces-plugin.c
@@ -0,0 +1,36 @@
+/* trim-spaces-plugin.c
+ *
+ * Copyright 2018-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 "trim-spaces-plugin"
+
+#include "config.h"
+
+#include <libpeas/peas.h>
+#include <libide-code.h>
+
+#include "gbp-trim-spaces-buffer-addin.h"
+
+_IDE_EXTERN void
+_gbp_trim_spaces_register_types (PeasObjectModule *module)
+{
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_BUFFER_ADDIN,
+                                              GBP_TYPE_TRIM_SPACES_BUFFER_ADDIN);
+}
diff --git a/src/plugins/trim-spaces/trim-spaces.gresource.xml 
b/src/plugins/trim-spaces/trim-spaces.gresource.xml
new file mode 100644
index 000000000..d492fd495
--- /dev/null
+++ b/src/plugins/trim-spaces/trim-spaces.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/plugins/trim-spaces">
+    <file>trim-spaces.plugin</file>
+  </gresource>
+</gresources>
diff --git a/src/plugins/trim-spaces/trim-spaces.plugin b/src/plugins/trim-spaces/trim-spaces.plugin
new file mode 100644
index 000000000..d97ccde0c
--- /dev/null
+++ b/src/plugins/trim-spaces/trim-spaces.plugin
@@ -0,0 +1,10 @@
+[Plugin]
+Authors=Christian Hergert <christian hergert me>
+Builtin=true
+Copyright=Copyright © 2018 Christian Hergert
+Depends=editor;
+Description=Trim trailing whitespace when saving buffers
+Embedded=_gbp_trim_spaces_register_types
+Hidden=true
+Module=trim-spaces
+Name=Trim Spaces


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