[gnome-builder] formatter: add interface for formatting text buffers
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] formatter: add interface for formatting text buffers
- Date: Fri, 31 Mar 2017 00:56:36 +0000 (UTC)
commit 560e974d3950c855f18ef261f111d8e6e8dde96c
Author: Christian Hergert <chergert redhat com>
Date: Thu Mar 30 18:49:57 2017 -0600
formatter: add interface for formatting text buffers
libide/Makefile.am | 4 +
libide/formatting/ide-formatter-options.c | 166 ++++++++++++++++++++++++++++
libide/formatting/ide-formatter-options.h | 40 +++++++
libide/formatting/ide-formatter.c | 170 +++++++++++++++++++++++++++++
libide/formatting/ide-formatter.h | 85 ++++++++++++++
5 files changed, 465 insertions(+), 0 deletions(-)
---
diff --git a/libide/Makefile.am b/libide/Makefile.am
index 6c2ad77..6ec65f1 100644
--- a/libide/Makefile.am
+++ b/libide/Makefile.am
@@ -70,6 +70,8 @@ libide_1_0_la_public_headers = \
files/ide-file-settings.h \
files/ide-file.h \
files/ide-indent-style.h \
+ formatting/ide-formatter.h \
+ formatting/ide-formatter-options.h \
genesis/ide-genesis-addin.h \
highlighting/ide-highlight-engine.h \
highlighting/ide-highlight-index.h \
@@ -247,6 +249,8 @@ libide_1_0_la_public_sources = \
files/ide-file-settings.c \
files/ide-file-settings.defs \
files/ide-file.c \
+ formatting/ide-formatter.c \
+ formatting/ide-formatter-options.c \
genesis/ide-genesis-addin.c \
highlighting/ide-highlight-engine.c \
highlighting/ide-highlight-index.c \
diff --git a/libide/formatting/ide-formatter-options.c b/libide/formatting/ide-formatter-options.c
new file mode 100644
index 0000000..838f071
--- /dev/null
+++ b/libide/formatting/ide-formatter-options.c
@@ -0,0 +1,166 @@
+/* ide-formatter-options.c
+ *
+ * Copyright (C) 2017 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-formatter-options"
+
+#include "ide-formatter-options.h"
+
+struct _IdeFormatterOptions
+{
+ GObject parent_instance;
+ guint tab_width;
+ guint insert_spaces : 1;
+};
+
+enum {
+ PROP_0,
+ PROP_TAB_WIDTH,
+ PROP_INSERT_SPACES,
+ N_PROPS
+};
+
+G_DEFINE_TYPE (IdeFormatterOptions, ide_formatter_options, G_TYPE_OBJECT)
+
+static GParamSpec *properties [N_PROPS];
+
+static void
+ide_formatter_options_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ IdeFormatterOptions *self = IDE_FORMATTER_OPTIONS (object);
+
+ switch (prop_id)
+ {
+ case PROP_TAB_WIDTH:
+ g_value_set_uint (value, ide_formatter_options_get_tab_width (self));
+ break;
+
+ case PROP_INSERT_SPACES:
+ g_value_set_boolean (value, ide_formatter_options_get_insert_spaces (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_formatter_options_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ IdeFormatterOptions *self = IDE_FORMATTER_OPTIONS (object);
+
+ switch (prop_id)
+ {
+ case PROP_TAB_WIDTH:
+ ide_formatter_options_set_tab_width (self, g_value_get_uint (value));
+ break;
+
+ case PROP_INSERT_SPACES:
+ ide_formatter_options_set_insert_spaces (self, g_value_get_boolean (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+ide_formatter_options_class_init (IdeFormatterOptionsClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+ object_class->get_property = ide_formatter_options_get_property;
+ object_class->set_property = ide_formatter_options_set_property;
+
+ properties [PROP_INSERT_SPACES] =
+ g_param_spec_boolean ("insert-spaces",
+ "Insert Spaces",
+ "Insert spaces instead of tabs",
+ FALSE,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ properties [PROP_TAB_WIDTH] =
+ g_param_spec_uint ("tab-width",
+ "Tab Width",
+ "The width of a tab in spaces",
+ 1, 32, 8,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_properties (object_class, N_PROPS, properties);
+}
+
+static void
+ide_formatter_options_init (IdeFormatterOptions *self)
+{
+ self->tab_width = 8;
+}
+
+guint
+ide_formatter_options_get_tab_width (IdeFormatterOptions *self)
+{
+ g_return_val_if_fail (IDE_IS_FORMATTER_OPTIONS (self), 0);
+
+ return self->tab_width;
+}
+
+void
+ide_formatter_options_set_tab_width (IdeFormatterOptions *self,
+ guint tab_width)
+{
+ g_return_if_fail (IDE_IS_FORMATTER_OPTIONS (self));
+
+ if (tab_width != self->tab_width)
+ {
+ self->tab_width = tab_width;
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TAB_WIDTH]);
+ }
+}
+
+gboolean
+ide_formatter_options_get_insert_spaces (IdeFormatterOptions *self)
+{
+ g_return_val_if_fail (IDE_IS_FORMATTER_OPTIONS (self), FALSE);
+
+ return self->insert_spaces;
+}
+
+void
+ide_formatter_options_set_insert_spaces (IdeFormatterOptions *self,
+ gboolean insert_spaces)
+{
+ g_return_if_fail (IDE_IS_FORMATTER_OPTIONS (self));
+
+ insert_spaces = !!insert_spaces;
+
+ if (insert_spaces != self->insert_spaces)
+ {
+ self->insert_spaces = insert_spaces;
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_INSERT_SPACES]);
+ }
+}
+
+IdeFormatterOptions *
+ide_formatter_options_new (void)
+{
+ return g_object_new (IDE_TYPE_FORMATTER_OPTIONS, NULL);
+}
diff --git a/libide/formatting/ide-formatter-options.h b/libide/formatting/ide-formatter-options.h
new file mode 100644
index 0000000..96fd60a
--- /dev/null
+++ b/libide/formatting/ide-formatter-options.h
@@ -0,0 +1,40 @@
+/* ide-formatter-options.h
+ *
+ * Copyright (C) 2017 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/>.
+ */
+
+#ifndef IDE_FORMATTER_OPTIONS_H
+#define IDE_FORMATTER_OPTIONS_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_FORMATTER_OPTIONS (ide_formatter_options_get_type())
+
+G_DECLARE_FINAL_TYPE (IdeFormatterOptions, ide_formatter_options, IDE, FORMATTER_OPTIONS, GObject)
+
+IdeFormatterOptions *ide_formatter_options_new (void);
+guint ide_formatter_options_get_tab_width (IdeFormatterOptions *self);
+void ide_formatter_options_set_tab_width (IdeFormatterOptions *self,
+ guint tab_width);
+gboolean ide_formatter_options_get_insert_spaces (IdeFormatterOptions *self);
+void ide_formatter_options_set_insert_spaces (IdeFormatterOptions *self,
+ gboolean insert_spaces);
+
+G_END_DECLS
+
+#endif /* IDE_FORMATTER_OPTIONS_H */
diff --git a/libide/formatting/ide-formatter.c b/libide/formatting/ide-formatter.c
new file mode 100644
index 0000000..a4eeaa3
--- /dev/null
+++ b/libide/formatting/ide-formatter.c
@@ -0,0 +1,170 @@
+/* ide-formatter.c
+ *
+ * Copyright (C) 2017 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/>.
+ */
+
+#define G_LOG_DOMAIN "ide-formatter"
+
+#include "buffers/ide-buffer.h"
+#include "formatting/ide-formatter.h"
+
+G_DEFINE_INTERFACE (IdeFormatter, ide_formatter, G_TYPE_OBJECT)
+
+static void
+ide_formatter_real_format_async (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_assert (IDE_IS_FORMATTER (self));
+ g_assert (IDE_IS_BUFFER (buffer));
+ g_assert (IDE_IS_FORMATTER_OPTIONS (options));
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ g_task_report_new_error (self,
+ callback,
+ user_data,
+ ide_formatter_real_format_async,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_SUPPORTED,
+ "The operation is not supported");
+}
+
+static gboolean
+ide_formatter_real_format_finish (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_assert (IDE_IS_FORMATTER (self));
+ g_assert (G_IS_TASK (result));
+
+ return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void
+ide_formatter_real_format_range_async (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ const GtkTextIter *begin,
+ const GtkTextIter *end,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_assert (IDE_IS_FORMATTER (self));
+ g_assert (IDE_IS_BUFFER (buffer));
+ g_assert (IDE_IS_FORMATTER_OPTIONS (options));
+ g_assert (begin != NULL);
+ g_assert (end != NULL);
+ g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ g_task_report_new_error (self,
+ callback,
+ user_data,
+ ide_formatter_real_format_async,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_SUPPORTED,
+ "The operation is not supported");
+}
+
+static gboolean
+ide_formatter_real_format_range_finish (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_assert (IDE_IS_FORMATTER (self));
+ g_assert (G_IS_TASK (result));
+
+ return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+static void
+ide_formatter_default_init (IdeFormatterInterface *iface)
+{
+ iface->format_async = ide_formatter_real_format_async;
+ iface->format_finish = ide_formatter_real_format_finish;
+ iface->format_range_async = ide_formatter_real_format_range_async;
+ iface->format_range_finish = ide_formatter_real_format_range_finish;
+}
+
+void
+ide_formatter_format_async (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_return_if_fail (IDE_IS_FORMATTER (self));
+ g_return_if_fail (IDE_IS_BUFFER (buffer));
+ g_return_if_fail (IDE_IS_FORMATTER_OPTIONS (options));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ IDE_FORMATTER_GET_IFACE (self)->format_async (self, buffer, options, cancellable, callback, user_data);
+}
+
+gboolean
+ide_formatter_format_finish (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (IDE_IS_FORMATTER (self), FALSE);
+ g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+ return IDE_FORMATTER_GET_IFACE (self)->format_finish (self, result, error);
+}
+
+void
+ide_formatter_format_range_async (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ const GtkTextIter *begin,
+ const GtkTextIter *end,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ g_return_if_fail (IDE_IS_FORMATTER (self));
+ g_return_if_fail (IDE_IS_BUFFER (buffer));
+ g_return_if_fail (IDE_IS_FORMATTER_OPTIONS (options));
+ g_return_if_fail (begin != NULL);
+ g_return_if_fail (end != NULL);
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ IDE_FORMATTER_GET_IFACE (self)->format_range_async (self, buffer, options, begin, end, cancellable,
callback, user_data);
+}
+
+gboolean
+ide_formatter_format_range_finish (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error)
+{
+ g_return_val_if_fail (IDE_IS_FORMATTER (self), FALSE);
+ g_return_val_if_fail (G_IS_ASYNC_RESULT (result), FALSE);
+
+ return IDE_FORMATTER_GET_IFACE (self)->format_range_finish (self, result, error);
+}
+
+void
+ide_formatter_load (IdeFormatter *self)
+{
+ g_return_if_fail (IDE_IS_FORMATTER (self));
+
+ if (IDE_FORMATTER_GET_IFACE (self)->load)
+ IDE_FORMATTER_GET_IFACE (self)->load (self);
+}
diff --git a/libide/formatting/ide-formatter.h b/libide/formatting/ide-formatter.h
new file mode 100644
index 0000000..d32b4be
--- /dev/null
+++ b/libide/formatting/ide-formatter.h
@@ -0,0 +1,85 @@
+/* ide-formatter.h
+ *
+ * Copyright (C) 2017 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/>.
+ */
+
+#ifndef IDE_FORMATTER_H
+#define IDE_FORMATTER_H
+
+#include <gtk/gtk.h>
+
+#include "ide-object.h"
+
+#include "formatting/ide-formatter-options.h"
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_FORMATTER (ide_formatter_get_type())
+
+G_DECLARE_INTERFACE (IdeFormatter, ide_formatter, IDE, FORMATTER, IdeObject)
+
+struct _IdeFormatterInterface
+{
+ GTypeInterface parent;
+
+ void (*load) (IdeFormatter *self);
+ void (*format_async) (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+ gboolean (*format_finish) (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error);
+ void (*format_range_async) (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ const GtkTextIter *begin,
+ const GtkTextIter *end,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+ gboolean (*format_range_finish) (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error);
+};
+
+void ide_formatter_load (IdeFormatter *self);
+void ide_formatter_format_async (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean ide_formatter_format_finish (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error);
+void ide_formatter_format_range_async (IdeFormatter *self,
+ IdeBuffer *buffer,
+ IdeFormatterOptions *options,
+ const GtkTextIter *begin,
+ const GtkTextIter *end,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean ide_formatter_format_range_finish (IdeFormatter *self,
+ GAsyncResult *result,
+ GError **error);
+
+G_END_DECLS
+
+#endif /* IDE_FORMATTER_H */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]