[gnome-builder] document: move change monitor and code assistance into GbEditorDocument
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder] document: move change monitor and code assistance into GbEditorDocument
- Date: Tue, 2 Dec 2014 04:25:23 +0000 (UTC)
commit c8742278d66e9bb60be2a32dfc9725e8f7289bbe
Author: Christian Hergert <christian hergert me>
Date: Mon Dec 1 19:59:36 2014 -0800
document: move change monitor and code assistance into GbEditorDocument
src/code-assistant/gb-source-code-assistant.c | 754 +++++++++++++++++++++++++
src/code-assistant/gb-source-code-assistant.h | 60 ++
src/editor/gb-editor-document.c | 688 ++++++++++++++++++++++-
src/editor/gb-editor-document.h | 37 ++-
src/editor/gb-source-change-monitor.c | 41 ++-
src/editor/gb-source-change-monitor.h | 1 +
6 files changed, 1567 insertions(+), 14 deletions(-)
---
diff --git a/src/code-assistant/gb-source-code-assistant.c b/src/code-assistant/gb-source-code-assistant.c
new file mode 100644
index 0000000..44a523c
--- /dev/null
+++ b/src/code-assistant/gb-source-code-assistant.c
@@ -0,0 +1,754 @@
+/* gb-source-code-assistant.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * 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 "code-assistant"
+
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+#include <gtksourceview/gtksource.h>
+
+#include "gb-editor-document.h"
+#include "gb-log.h"
+#include "gb-source-code-assistant.h"
+#include "gb-string.h"
+#include "gca-diagnostics.h"
+#include "gca-service.h"
+#include "gca-structs.h"
+
+struct _GbSourceCodeAssistantPrivate
+{
+ GtkTextBuffer *buffer;
+ GcaService *proxy;
+ GcaDiagnostics *document_proxy;
+ GArray *diagnostics;
+ gchar *document_path;
+
+ gchar *tmpfile_path;
+ int tmpfile_fd;
+
+ gulong changed_handler;
+ gulong notify_language_handler;
+
+ guint parse_timeout;
+ guint active;
+};
+
+enum {
+ PROP_0,
+ PROP_ACTIVE,
+ PROP_BUFFER,
+ LAST_PROP
+};
+
+enum {
+ CHANGED,
+ LAST_SIGNAL
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbSourceCodeAssistant,
+ gb_source_code_assistant,
+ G_TYPE_OBJECT)
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+static guint gSignals [LAST_SIGNAL];
+static GDBusConnection *gDBus;
+
+#define PARSE_TIMEOUT_MSEC 350
+
+static void
+gb_source_code_assistant_queue_parse (GbSourceCodeAssistant *assistant);
+
+GbSourceCodeAssistant *
+gb_source_code_assistant_new (GtkTextBuffer *buffer)
+{
+ return g_object_new (GB_TYPE_SOURCE_CODE_ASSISTANT,
+ "buffer", buffer,
+ NULL);
+}
+
+static const gchar *
+remap_language (const gchar *lang_id)
+{
+ g_return_val_if_fail (lang_id, NULL);
+
+ if (g_str_equal (lang_id, "chdr") ||
+ g_str_equal (lang_id, "objc") ||
+ g_str_equal (lang_id, "cpp"))
+ return "c";
+
+ return lang_id;
+}
+
+static void
+gb_source_code_assistant_inc_active (GbSourceCodeAssistant *assistant,
+ gint amount)
+{
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ assistant->priv->active += amount;
+ g_object_notify_by_pspec (G_OBJECT (assistant), gParamSpecs [PROP_ACTIVE]);
+}
+
+static void
+gb_source_code_assistant_proxy_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GbSourceCodeAssistant *assistant = user_data;
+ GcaService *proxy;
+ GError *error = NULL;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ gb_source_code_assistant_inc_active (assistant, -1);
+
+ proxy = gca_service_proxy_new_finish (result, &error);
+
+ if (!proxy)
+ {
+ g_message ("%s", error->message);
+ g_clear_error (&error);
+ EXIT;
+ }
+
+ g_clear_object (&assistant->priv->proxy);
+ assistant->priv->proxy = proxy;
+
+ gb_source_code_assistant_queue_parse (assistant);
+
+ g_object_unref (assistant);
+
+ EXIT;
+}
+
+static void
+gb_source_code_assistant_load_service (GbSourceCodeAssistant *assistant)
+{
+ GbSourceCodeAssistantPrivate *priv;
+ GtkSourceLanguage *language;
+ GtkSourceBuffer *buffer;
+ const gchar *lang_id;
+ gchar *name;
+ gchar *object_path;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+ g_return_if_fail (assistant->priv->buffer);
+
+ priv = assistant->priv;
+
+ if (!gDBus)
+ EXIT;
+
+ if (!GTK_SOURCE_IS_BUFFER (priv->buffer))
+ EXIT;
+
+ g_clear_object (&priv->proxy);
+
+ buffer = GTK_SOURCE_BUFFER (priv->buffer);
+ language = gtk_source_buffer_get_language (buffer);
+ if (!language)
+ EXIT;
+
+ lang_id = remap_language (gtk_source_language_get_id (language));
+
+ name = g_strdup_printf ("org.gnome.CodeAssist.v1.%s", lang_id);
+ object_path = g_strdup_printf ("/org/gnome/CodeAssist/v1/%s", lang_id);
+
+ gb_source_code_assistant_inc_active (assistant, 1);
+
+ gca_service_proxy_new (gDBus,
+ G_DBUS_PROXY_FLAGS_NONE,
+ name,
+ object_path,
+ NULL,
+ gb_source_code_assistant_proxy_cb,
+ g_object_ref (assistant));
+
+ g_free (name);
+ g_free (object_path);
+
+ EXIT;
+}
+
+/**
+ * gb_source_code_assistant_get_diagnostics:
+ * @assistant: (in): A #GbSourceCodeAssistant.
+ *
+ * Fetches the diagnostics for the buffer. Free the result with
+ * g_array_unref().
+ *
+ * Returns: (transfer full): A #GArray of #GcaDiagnostic.
+ */
+GArray *
+gb_source_code_assistant_get_diagnostics (GbSourceCodeAssistant *assistant)
+{
+ g_return_val_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant), NULL);
+
+ if (assistant->priv->diagnostics)
+ return g_array_ref (assistant->priv->diagnostics);
+
+ return NULL;
+}
+
+static void
+gb_source_code_assistant_diag_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GbSourceCodeAssistantPrivate *priv;
+ GbSourceCodeAssistant *assistant = user_data;
+ GcaDiagnostics *proxy = GCA_DIAGNOSTICS (source_object);
+ GError *error = NULL;
+ GVariant *diags = NULL;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ priv = assistant->priv;
+
+ gb_source_code_assistant_inc_active (assistant, -1);
+
+ if (!gca_diagnostics_call_diagnostics_finish (proxy, &diags, result, &error))
+ {
+ g_warning ("%s", error->message);
+ g_clear_error (&error);
+ GOTO (failure);
+ }
+
+ g_clear_pointer (&priv->diagnostics, g_array_unref);
+
+ priv->diagnostics = gca_diagnostics_from_variant (diags);
+
+ /* TODO: update buffer text tags */
+
+ g_signal_emit (assistant, gSignals [CHANGED], 0);
+
+failure:
+ g_object_unref (assistant);
+ g_clear_pointer (&diags, g_variant_unref);
+
+ EXIT;
+}
+
+static void
+gb_source_code_assistant_diag_proxy_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GbSourceCodeAssistantPrivate *priv;
+ GbSourceCodeAssistant *assistant = user_data;
+ GcaDiagnostics *proxy;
+ GError *error = NULL;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ priv = assistant->priv;
+
+ gb_source_code_assistant_inc_active (assistant, -1);
+
+ if (!(proxy = gca_diagnostics_proxy_new_finish (result, &error)))
+ {
+ g_warning ("%s", error->message);
+ g_clear_error (&error);
+ GOTO (failure);
+ }
+
+ g_clear_object (&priv->document_proxy);
+ priv->document_proxy = proxy;
+
+ gb_source_code_assistant_inc_active (assistant, 1);
+ gca_diagnostics_call_diagnostics (proxy, NULL,
+ gb_source_code_assistant_diag_cb,
+ g_object_ref (assistant));
+
+failure:
+ g_object_unref (assistant);
+
+ EXIT;
+}
+
+static void
+gb_source_code_assistant_parse_cb (GObject *source_object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GbSourceCodeAssistantPrivate *priv;
+ GbSourceCodeAssistant *assistant = user_data;
+ GcaService *service = GCA_SERVICE (source_object);
+ GtkSourceLanguage *language;
+ const gchar *lang_id;
+ GError *error = NULL;
+ gchar *name = NULL;
+ gchar *document_path = NULL;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ priv = assistant->priv;
+
+ gb_source_code_assistant_inc_active (assistant, -1);
+
+ if (!gca_service_call_parse_finish (service, &document_path, result, &error))
+ {
+ g_warning ("%s", error->message);
+ GOTO (failure);
+ }
+
+ language = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (priv->buffer));
+ if (!language)
+ GOTO (failure);
+
+ lang_id = remap_language (gtk_source_language_get_id (language));
+ name = g_strdup_printf ("org.gnome.CodeAssist.v1.%s", lang_id);
+
+ if (priv->document_proxy)
+ {
+ const gchar *object_path;
+
+ object_path = g_dbus_proxy_get_object_path (G_DBUS_PROXY (priv->document_proxy));
+ if (g_strcmp0 (document_path, object_path) != 0)
+ g_clear_object (&priv->document_proxy);
+ }
+
+ if (!priv->document_proxy)
+ {
+ gb_source_code_assistant_inc_active (assistant, 1);
+ gca_diagnostics_proxy_new (gDBus,
+ G_DBUS_PROXY_FLAGS_NONE,
+ name,
+ document_path,
+ NULL,
+ gb_source_code_assistant_diag_proxy_cb,
+ g_object_ref (assistant));
+ }
+ else
+ {
+ gb_source_code_assistant_inc_active (assistant, 1);
+ gca_diagnostics_call_diagnostics (priv->document_proxy, NULL,
+ gb_source_code_assistant_diag_cb,
+ g_object_ref (assistant));
+ }
+
+failure:
+ g_clear_error (&error);
+ g_free (document_path);
+ g_free (name);
+ g_object_unref (assistant);
+
+ EXIT;
+}
+
+static gboolean
+gb_source_code_assistant_do_parse (gpointer data)
+{
+ GbSourceCodeAssistantPrivate *priv;
+ GbSourceCodeAssistant *assistant = data;
+ GError *error = NULL;
+ GtkTextMark *insert;
+ GtkTextIter iter;
+ GtkTextIter begin;
+ GtkTextIter end;
+ GVariant *cursor;
+ GVariant *options;
+ GFile *gfile = NULL;
+ gchar *path = NULL;
+ gchar *text = NULL;
+ gint64 line;
+ gint64 line_offset;
+
+ ENTRY;
+
+ g_return_val_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant), FALSE);
+
+ priv = assistant->priv;
+
+ priv->parse_timeout = 0;
+
+ if (!priv->proxy)
+ RETURN (G_SOURCE_REMOVE);
+
+ insert = gtk_text_buffer_get_insert (priv->buffer);
+ gtk_text_buffer_get_iter_at_mark (priv->buffer, &iter, insert);
+ line = gtk_text_iter_get_line (&iter);
+ line_offset = gtk_text_iter_get_line_offset (&iter);
+ cursor = g_variant_new ("(xx)", line, line_offset);
+ options = g_variant_new ("a{sv}", 0);
+
+ if (GB_IS_EDITOR_DOCUMENT (priv->buffer))
+ {
+ GtkSourceFile *file;
+
+ file = gb_editor_document_get_file (GB_EDITOR_DOCUMENT (priv->buffer));
+ if (file)
+ gfile = gtk_source_file_get_location (file);
+ }
+
+ if (gfile)
+ path = g_file_get_path (gfile);
+
+ if (gb_str_empty0 (path))
+ RETURN (G_SOURCE_REMOVE);
+
+ if (!priv->tmpfile_path)
+ {
+ int fd;
+
+ fd = g_file_open_tmp ("builder-code-assistant.XXXXXX",
+ &priv->tmpfile_path,
+ &error);
+ if (fd == -1)
+ {
+ g_warning ("%s", error->message);
+ g_clear_error (&error);
+ GOTO (failure);
+ }
+
+ priv->tmpfile_fd = fd;
+ }
+
+ gtk_text_buffer_get_bounds (priv->buffer, &begin, &end);
+ text = gtk_text_buffer_get_text (priv->buffer, &begin, &end, TRUE);
+ if (!g_file_set_contents (priv->tmpfile_path, text, -1, &error))
+ {
+ g_warning ("%s", error->message);
+ g_clear_error (&error);
+ GOTO (failure);
+ }
+
+ gb_source_code_assistant_inc_active (assistant, 1);
+ gca_service_call_parse (priv->proxy,
+ path,
+ priv->tmpfile_path,
+ cursor,
+ options,
+ NULL,
+ gb_source_code_assistant_parse_cb,
+ g_object_ref (assistant));
+
+failure:
+ g_free (path);
+ g_free (text);
+
+ RETURN (G_SOURCE_REMOVE);
+}
+
+static void
+gb_source_code_assistant_queue_parse (GbSourceCodeAssistant *assistant)
+{
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ if (assistant->priv->parse_timeout)
+ g_source_remove (assistant->priv->parse_timeout);
+
+ assistant->priv->parse_timeout =
+ g_timeout_add (PARSE_TIMEOUT_MSEC,
+ gb_source_code_assistant_do_parse,
+ assistant);
+}
+
+static void
+gb_source_code_assistant_buffer_notify_language (GbSourceCodeAssistant *assistant,
+ GParamSpec *pspec,
+ GtkSourceBuffer *buffer)
+{
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+ g_return_if_fail (GTK_SOURCE_IS_BUFFER (buffer));
+
+ gb_source_code_assistant_load_service (assistant);
+
+ EXIT;
+}
+
+static void
+gb_source_code_assistant_buffer_changed (GbSourceCodeAssistant *assistant,
+ GtkTextBuffer *buffer)
+{
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+ g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));
+
+ gb_source_code_assistant_queue_parse (assistant);
+}
+
+static void
+gb_source_code_assistant_disconnect (GbSourceCodeAssistant *assistant)
+{
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ g_signal_handler_disconnect (assistant->priv->buffer,
+ assistant->priv->changed_handler);
+ assistant->priv->changed_handler = 0;
+
+ g_signal_handler_disconnect (assistant->priv->buffer,
+ assistant->priv->notify_language_handler);
+ assistant->priv->notify_language_handler = 0;
+}
+
+static void
+gb_source_code_assistant_connect (GbSourceCodeAssistant *assistant)
+{
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ assistant->priv->changed_handler =
+ g_signal_connect_object (assistant->priv->buffer,
+ "changed",
+ G_CALLBACK (gb_source_code_assistant_buffer_changed),
+ assistant,
+ G_CONNECT_SWAPPED);
+
+ assistant->priv->notify_language_handler =
+ g_signal_connect_object (assistant->priv->buffer,
+ "notify::language",
+ G_CALLBACK (gb_source_code_assistant_buffer_notify_language),
+ assistant,
+ G_CONNECT_SWAPPED);
+}
+
+/**
+ * gb_source_code_assistant_get_buffer:
+ * @assistant: (in): A #GbSourceCodeAssistant.
+ *
+ * Fetches the underlying text buffer.
+ *
+ * Returns: (transfer none): A #GtkTextBuffer.
+ */
+GtkTextBuffer *
+gb_source_code_assistant_get_buffer (GbSourceCodeAssistant *assistant)
+{
+ g_return_val_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant), NULL);
+
+ return assistant->priv->buffer;
+}
+
+static void
+gb_source_code_assistant_set_buffer (GbSourceCodeAssistant *assistant,
+ GtkTextBuffer *buffer)
+{
+ GbSourceCodeAssistantPrivate *priv;
+
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant));
+
+ priv = assistant->priv;
+
+ if (priv->buffer != buffer)
+ {
+ if (priv->buffer)
+ {
+ gb_source_code_assistant_disconnect (assistant);
+ g_object_remove_weak_pointer (G_OBJECT (priv->buffer),
+ (gpointer *)&priv->buffer);
+ priv->buffer = NULL;
+ }
+
+ if (buffer)
+ {
+ priv->buffer = buffer;
+ g_object_add_weak_pointer (G_OBJECT (priv->buffer),
+ (gpointer *)&priv->buffer);
+ gb_source_code_assistant_connect (assistant);
+ }
+
+ gb_source_code_assistant_load_service (assistant);
+
+ g_object_notify_by_pspec (G_OBJECT (assistant),
+ gParamSpecs [PROP_BUFFER]);
+ }
+}
+
+/**
+ * gb_source_code_assistant_get_active:
+ * @assistant: (in): A #GbSourceCodeAssistant.
+ *
+ * Fetches the "active" property, indicating if the code assistanace service
+ * is currently parsing the buffer.
+ *
+ * Returns: %TRUE if the file is being parsed.
+ */
+gboolean
+gb_source_code_assistant_get_active (GbSourceCodeAssistant *assistant)
+{
+ g_return_val_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (assistant), FALSE);
+
+ return assistant->priv->active;
+}
+
+static void
+gb_source_code_assistant_finalize (GObject *object)
+{
+ GbSourceCodeAssistantPrivate *priv;
+
+ ENTRY;
+
+ priv = GB_SOURCE_CODE_ASSISTANT (object)->priv;
+
+ if (priv->parse_timeout)
+ {
+ g_source_remove (priv->parse_timeout);
+ priv->parse_timeout = 0;
+ }
+
+ if (priv->buffer)
+ {
+ g_object_add_weak_pointer (G_OBJECT (priv->buffer),
+ (gpointer *)&priv->buffer);
+ priv->buffer = NULL;
+ }
+
+ g_clear_object (&priv->proxy);
+
+ if (priv->tmpfile_path)
+ {
+ g_unlink (priv->tmpfile_path);
+ g_clear_pointer (&priv->tmpfile_path, g_free);
+ }
+
+ close (priv->tmpfile_fd);
+ priv->tmpfile_fd = -1;
+
+ g_clear_pointer (&priv->document_path, g_free);
+ g_clear_object (&priv->document_proxy);
+
+ G_OBJECT_CLASS (gb_source_code_assistant_parent_class)->finalize (object);
+
+ EXIT;
+}
+
+static void
+gb_source_code_assistant_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GbSourceCodeAssistant *self = GB_SOURCE_CODE_ASSISTANT (object);
+
+ switch (prop_id)
+ {
+ case PROP_ACTIVE:
+ g_value_set_boolean (value, gb_source_code_assistant_get_active (self));
+ break;
+
+ case PROP_BUFFER:
+ g_value_set_object (value, gb_source_code_assistant_get_buffer (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+gb_source_code_assistant_set_property (GObject *object,
+ guint prop_id,
+ const GValue *value,
+ GParamSpec *pspec)
+{
+ GbSourceCodeAssistant *self = GB_SOURCE_CODE_ASSISTANT (object);
+
+ switch (prop_id)
+ {
+ case PROP_BUFFER:
+ gb_source_code_assistant_set_buffer (self, g_value_get_object (value));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+ }
+}
+
+static void
+gb_source_code_assistant_class_init (GbSourceCodeAssistantClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GError *error = NULL;
+ gchar *address = NULL;
+
+ object_class->finalize = gb_source_code_assistant_finalize;
+ object_class->get_property = gb_source_code_assistant_get_property;
+ object_class->set_property = gb_source_code_assistant_set_property;
+
+ gParamSpecs [PROP_ACTIVE] =
+ g_param_spec_boolean ("active",
+ _("Active"),
+ _("If code assistance is currently processing."),
+ FALSE,
+ (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_ACTIVE,
+ gParamSpecs [PROP_ACTIVE]);
+
+ gParamSpecs [PROP_BUFFER] =
+ g_param_spec_object ("buffer",
+ _("Buffer"),
+ _("The buffer "),
+ GTK_TYPE_TEXT_BUFFER,
+ (G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_BUFFER,
+ gParamSpecs [PROP_BUFFER]);
+
+ gSignals [CHANGED] =
+ g_signal_new ("changed",
+ GB_TYPE_SOURCE_CODE_ASSISTANT,
+ G_SIGNAL_RUN_FIRST,
+ G_STRUCT_OFFSET (GbSourceCodeAssistantClass, changed),
+ NULL,
+ NULL,
+ g_cclosure_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+
+ address = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, NULL, &error);
+ if (!address)
+ GOTO (failure);
+
+ gDBus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
+ if (!gDBus)
+ GOTO (failure);
+
+#if 0
+ g_dbus_connection_set_exit_on_close (gDBus, FALSE);
+#endif
+
+failure:
+ if (error)
+ {
+ g_warning (_("Failed to load DBus connection. "
+ "Code assistance will be disabled. "
+ "\"%s\" (%s)"),
+ error->message, address);
+ g_clear_error (&error);
+ }
+
+ g_free (address);
+}
+
+static void
+gb_source_code_assistant_init (GbSourceCodeAssistant *assistant)
+{
+ assistant->priv = gb_source_code_assistant_get_instance_private (assistant);
+ assistant->priv->tmpfile_fd = -1;
+}
diff --git a/src/code-assistant/gb-source-code-assistant.h b/src/code-assistant/gb-source-code-assistant.h
new file mode 100644
index 0000000..f646ff0
--- /dev/null
+++ b/src/code-assistant/gb-source-code-assistant.h
@@ -0,0 +1,60 @@
+/* gb-source-code-assistant.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * 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 GB_SOURCE_CODE_ASSISTANT_H
+#define GB_SOURCE_CODE_ASSISTANT_H
+
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_SOURCE_CODE_ASSISTANT (gb_source_code_assistant_get_type())
+#define GB_SOURCE_CODE_ASSISTANT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
GB_TYPE_SOURCE_CODE_ASSISTANT, GbSourceCodeAssistant))
+#define GB_SOURCE_CODE_ASSISTANT_CONST(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj),
GB_TYPE_SOURCE_CODE_ASSISTANT, GbSourceCodeAssistant const))
+#define GB_SOURCE_CODE_ASSISTANT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass),
GB_TYPE_SOURCE_CODE_ASSISTANT, GbSourceCodeAssistantClass))
+#define GB_IS_SOURCE_CODE_ASSISTANT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj),
GB_TYPE_SOURCE_CODE_ASSISTANT))
+#define GB_IS_SOURCE_CODE_ASSISTANT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),
GB_TYPE_SOURCE_CODE_ASSISTANT))
+#define GB_SOURCE_CODE_ASSISTANT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj),
GB_TYPE_SOURCE_CODE_ASSISTANT, GbSourceCodeAssistantClass))
+
+typedef struct _GbSourceCodeAssistant GbSourceCodeAssistant;
+typedef struct _GbSourceCodeAssistantClass GbSourceCodeAssistantClass;
+typedef struct _GbSourceCodeAssistantPrivate GbSourceCodeAssistantPrivate;
+
+struct _GbSourceCodeAssistant
+{
+ GObject parent;
+
+ /*< private >*/
+ GbSourceCodeAssistantPrivate *priv;
+};
+
+struct _GbSourceCodeAssistantClass
+{
+ GObjectClass parent_class;
+
+ void (*changed) (GbSourceCodeAssistant *assistant);
+};
+
+GType gb_source_code_assistant_get_type (void) G_GNUC_CONST;
+GbSourceCodeAssistant *gb_source_code_assistant_new (GtkTextBuffer *buffer);
+GArray *gb_source_code_assistant_get_diagnostics (GbSourceCodeAssistant *assistant);
+
+G_END_DECLS
+
+#endif /* GB_SOURCE_CODE_ASSISTANT_H */
diff --git a/src/editor/gb-editor-document.c b/src/editor/gb-editor-document.c
index f7956fa..e8c9695 100644
--- a/src/editor/gb-editor-document.c
+++ b/src/editor/gb-editor-document.c
@@ -22,10 +22,25 @@
#include <gtksourceview/gtksource.h>
#include "gb-editor-document.h"
+#include "gb-editor-file-marks.h"
+#include "gb-log.h"
+#include "gca-structs.h"
+
+struct _GbEditorDocumentPrivate
+{
+ GtkSourceFile *file;
+ GbSourceChangeMonitor *change_monitor;
+ GbSourceCodeAssistant *code_assistant;
+
+ guint trim_trailing_whitespace : 1;
+};
enum {
PROP_0,
+ PROP_CHANGE_MONITOR,
+ PROP_FILE,
PROP_STYLE_SCHEME_NAME,
+ PROP_TRIM_TRAILING_WHITESPACE,
LAST_PROP
};
@@ -34,9 +49,9 @@ enum {
LAST_SIGNAL
};
-G_DEFINE_TYPE (GbEditorDocument, gb_editor_document, GTK_SOURCE_TYPE_BUFFER)
+G_DEFINE_TYPE_WITH_PRIVATE (GbEditorDocument, gb_editor_document, GTK_SOURCE_TYPE_BUFFER)
-static GParamSpec *gParamSpecs [LAST_PROP];
+static GParamSpec *gParamSpecs [LAST_PROP];
static guint gSignals [LAST_SIGNAL];
GbEditorDocument *
@@ -45,6 +60,52 @@ gb_editor_document_new (void)
return g_object_new (GB_TYPE_EDITOR_DOCUMENT, NULL);
}
+gboolean
+gb_editor_document_get_trim_trailing_whitespace (GbEditorDocument *document)
+{
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), FALSE);
+
+ return document->priv->trim_trailing_whitespace;
+}
+
+void
+gb_editor_document_set_trim_trailing_whitespace (GbEditorDocument *document,
+ gboolean trim_trailing_whitespace)
+{
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+
+ if (trim_trailing_whitespace != document->priv->trim_trailing_whitespace)
+ {
+ document->priv->trim_trailing_whitespace = trim_trailing_whitespace;
+ g_object_notify_by_pspec (G_OBJECT (document),
+ gParamSpecs [PROP_TRIM_TRAILING_WHITESPACE]);
+ }
+}
+
+GbSourceChangeMonitor *
+gb_editor_document_get_change_monitor (GbEditorDocument *document)
+{
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), NULL);
+
+ return document->priv->change_monitor;
+}
+
+GbSourceCodeAssistant *
+gb_editor_document_get_code_assistant (GbEditorDocument *document)
+{
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), NULL);
+
+ return document->priv->code_assistant;
+}
+
+GtkSourceFile *
+gb_editor_document_get_file (GbEditorDocument *document)
+{
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), NULL);
+
+ return document->priv->file;
+}
+
static void
gb_editor_document_set_style_scheme_name (GbEditorDocument *document,
const gchar *style_scheme_name)
@@ -69,7 +130,7 @@ gb_editor_document_mark_set (GtkTextBuffer *buffer,
GTK_TEXT_BUFFER_CLASS (gb_editor_document_parent_class)->mark_set (buffer, iter, mark);
if (mark == gtk_text_buffer_get_insert (buffer))
- g_signal_emit (buffer, gSignals[CURSOR_MOVED], 0);
+ g_signal_emit (buffer, gSignals [CURSOR_MOVED], 0);
}
static void
@@ -77,12 +138,575 @@ gb_editor_document_changed (GtkTextBuffer *buffer)
{
g_assert (GB_IS_EDITOR_DOCUMENT (buffer));
- g_signal_emit (buffer, gSignals[CURSOR_MOVED], 0);
+ g_signal_emit (buffer, gSignals [CURSOR_MOVED], 0);
GTK_TEXT_BUFFER_CLASS (gb_editor_document_parent_class)->changed (buffer);
}
static void
+gb_editor_document_add_diagnostic (GbEditorDocument *document,
+ GcaDiagnostic *diag,
+ GcaSourceRange *range)
+{
+ GtkTextBuffer *buffer;
+ GtkTextIter begin;
+ GtkTextIter end;
+ guint column;
+
+ g_assert (GB_IS_EDITOR_DOCUMENT (document));
+ g_assert (diag);
+ g_assert (range);
+
+ if (range->begin.line == -1 || range->end.line == -1)
+ return;
+
+ buffer = GTK_TEXT_BUFFER (document);
+
+ gtk_text_buffer_get_iter_at_line (buffer, &begin, range->begin.line);
+ for (column = range->begin.column; column; column--)
+ if (gtk_text_iter_ends_line (&begin) || !gtk_text_iter_forward_char (&begin))
+ break;
+
+ gtk_text_buffer_get_iter_at_line (buffer, &end, range->end.line);
+ for (column = range->end.column; column; column--)
+ if (gtk_text_iter_ends_line (&end) || !gtk_text_iter_forward_char (&end))
+ break;
+
+ if (gtk_text_iter_equal (&begin, &end))
+ gtk_text_iter_forward_to_line_end (&end);
+
+ gtk_text_buffer_apply_tag_by_name (buffer, "ErrorTag", &begin, &end);
+}
+
+static void
+apply_tag_style (GbEditorDocument *document,
+ GtkTextTag *tag,
+ const gchar *style_id)
+{
+ GtkSourceStyleScheme *scheme;
+ GtkSourceStyle *style;
+ gboolean background_set;
+ gboolean bold_set;
+ gboolean foreground_set;
+ gboolean line_background_set;
+ gchar *str;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+
+ scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (document));
+ if (!scheme)
+ return;
+
+ style = gtk_source_style_scheme_get_style (scheme, style_id);
+ if (!style)
+ return;
+
+ g_object_get (style,
+ "background-set", &background_set,
+ "bold-set", &bold_set,
+ "foreground-set", &foreground_set,
+ "line-background-set", &line_background_set,
+ NULL);
+
+ if (background_set)
+ {
+ g_object_get (style, "background", &str, NULL);
+ g_object_set (tag, "background", str, NULL);
+ g_free (str);
+ }
+ else
+ g_object_set (tag, "background-set", FALSE, NULL);
+
+ if (bold_set)
+ {
+ PangoWeight weight;
+ gboolean bold;
+
+ g_object_get (style, "bold", &bold, NULL);
+ weight = bold ? PANGO_WEIGHT_NORMAL : PANGO_WEIGHT_BOLD;
+ g_object_set (tag, "weight", weight, NULL);
+ }
+ else
+ g_object_set (tag, "weight-set", FALSE, NULL);
+
+ if (foreground_set)
+ {
+ g_object_get (style, "foreground", &str, NULL);
+ g_object_set (tag, "foreground", str, NULL);
+ g_free (str);
+ }
+ else
+ g_object_set (tag, "foreground-set", FALSE, NULL);
+
+ if (line_background_set)
+ {
+ g_object_get (style, "line-background", &str, NULL);
+ g_object_set (tag, "paragraph-background", str, NULL);
+ g_free (str);
+ }
+ else
+ g_object_set (tag, "paragraph-background-set", FALSE, NULL);
+}
+
+static GtkTextTag *
+gb_editor_document_get_error_tag (GbEditorDocument *document)
+{
+ GtkTextBuffer *buffer;
+ GtkTextTagTable *tag_table;
+ GtkTextTag *tag;
+
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), NULL);
+
+ buffer = GTK_TEXT_BUFFER (document);
+ tag_table = gtk_text_buffer_get_tag_table (buffer);
+ tag = gtk_text_tag_table_lookup (tag_table, "ErrorTag");
+
+ if (!tag)
+ {
+ tag = gtk_text_buffer_create_tag (buffer, "ErrorTag",
+ "underline", PANGO_UNDERLINE_ERROR,
+ NULL);
+ apply_tag_style (document, tag, "def:error");
+ }
+
+ return tag;
+}
+
+static void
+gb_editor_document_notify_style_scheme (GbEditorDocument *document,
+ GParamSpec *pspec,
+ gpointer unused)
+{
+ GtkTextTag *tag;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+
+ tag = gb_editor_document_get_error_tag (document);
+ apply_tag_style (document, tag, "def:error");
+}
+
+static void
+gb_editor_document_code_assistant_changed (GbEditorDocument *document,
+ GbSourceCodeAssistant *code_assistant)
+{
+ GtkTextIter begin;
+ GtkTextIter end;
+ GtkTextTag *tag;
+ GArray *ar;
+ guint i;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+ g_return_if_fail (GB_IS_SOURCE_CODE_ASSISTANT (code_assistant));
+
+ /*
+ * Update all of the error tags in the buffer based on the diagnostics
+ * returned from code assistance. We might want to find a way to do this
+ * iteratively in the background based interactivity.
+ */
+
+ tag = gb_editor_document_get_error_tag (document);
+
+ gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (document), &begin, &end);
+ gtk_text_buffer_remove_tag (GTK_TEXT_BUFFER (document), tag, &begin, &end);
+
+ ar = gb_source_code_assistant_get_diagnostics (code_assistant);
+
+ for (i = 0; i < ar->len; i++)
+ {
+ GcaDiagnostic *diag;
+ guint j;
+
+ diag = &g_array_index (ar, GcaDiagnostic, i);
+
+ for (j = 0; j < diag->locations->len; j++)
+ {
+ GcaSourceRange *range;
+
+ range = &g_array_index (diag->locations, GcaSourceRange, j);
+ gb_editor_document_add_diagnostic (document, diag, range);
+ }
+ }
+
+ g_array_unref (ar);
+}
+
+static gboolean
+gb_editor_document_should_trim_line (GbEditorDocument *document,
+ guint line)
+{
+ GbSourceChangeFlags flags;
+
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), FALSE);
+
+ flags = gb_source_change_monitor_get_line (document->priv->change_monitor,
+ line);
+
+ return !!flags;
+}
+
+static gboolean
+text_iter_is_space (const GtkTextIter *iter)
+{
+ return g_unichar_isspace (gtk_text_iter_get_char (iter));
+}
+
+static void
+gb_editor_document_trim (GbEditorDocument *document)
+{
+ GtkTextBuffer *buffer;
+ GtkTextIter iter;
+ gint line;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+
+ buffer = GTK_TEXT_BUFFER (document);
+
+ gtk_text_buffer_get_end_iter (buffer, &iter);
+
+ for (line = gtk_text_iter_get_line (&iter); line >= 0; line--)
+ {
+ if (gb_editor_document_should_trim_line (document, line))
+ {
+ gtk_text_buffer_get_iter_at_line (buffer, &iter, line);
+
+ if (gtk_text_iter_forward_to_line_end (&iter) &&
+ text_iter_is_space (&iter))
+ {
+ GtkTextIter begin = iter;
+
+ while (text_iter_is_space (&begin))
+ {
+ if (gtk_text_iter_starts_line (&begin))
+ break;
+
+ if (!gtk_text_iter_backward_char (&begin))
+ break;
+ }
+
+ if (!text_iter_is_space (&begin) &&
+ !gtk_text_iter_ends_line (&begin))
+ gtk_text_iter_forward_char (&begin);
+
+ if (!gtk_text_iter_equal (&begin, &iter))
+ gtk_text_buffer_delete (buffer, &begin, &iter);
+ }
+ }
+ }
+
+ EXIT;
+}
+
+static void
+gb_editor_document_guess_language (GbEditorDocument *document)
+{
+ GtkSourceLanguageManager *manager;
+ GtkSourceLanguage *lang;
+ GtkTextIter begin;
+ GtkTextIter end;
+ gboolean result_uncertain = TRUE;
+ GFile *location;
+ gchar *name = NULL;
+ gchar *text = NULL;
+ gchar *content_type = NULL;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+
+ location = gtk_source_file_get_location (document->priv->file);
+ if (location)
+ name = g_file_get_basename (location);
+
+ gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (document), &begin, &end);
+ text = gtk_text_iter_get_slice (&begin, &end);
+
+ content_type = g_content_type_guess (name,
+ (const guint8 *)text, strlen (text),
+ &result_uncertain);
+ if (result_uncertain)
+ g_clear_pointer (&content_type, g_free);
+
+ manager = gtk_source_language_manager_get_default ();
+ lang = gtk_source_language_manager_guess_language (manager, name, content_type);
+
+ gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (document), lang);
+
+ g_free (content_type);
+ g_free (name);
+ g_free (text);
+}
+
+static void
+gb_editor_document_notify_file_location (GbEditorDocument *document,
+ GParamSpec *pspec,
+ GtkSourceFile *file)
+{
+ GbEditorDocumentPrivate *priv;
+ GFile *location;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+ g_return_if_fail (GTK_SOURCE_IS_FILE (file));
+
+ priv = document->priv;
+
+ location = gtk_source_file_get_location (file);
+ gb_source_change_monitor_set_file (priv->change_monitor, location);
+
+ gb_editor_document_guess_language (document);
+}
+
+static void
+gb_editor_document_save_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GtkSourceFileSaver *saver = (GtkSourceFileSaver *)object;
+ GbSourceChangeMonitor *change_monitor;
+ GbEditorDocument *document;
+ GError *error = NULL;
+ GTask *task = user_data;
+
+ ENTRY;
+
+ g_return_if_fail (GTK_SOURCE_IS_FILE_SAVER (saver));
+ g_return_if_fail (G_IS_ASYNC_RESULT (result));
+ g_return_if_fail (G_IS_TASK (task));
+
+ if (!gtk_source_file_saver_save_finish (saver, result, &error))
+ {
+ g_task_return_error (task, error);
+ GOTO (cleanup);
+ }
+
+ /*
+ * FIXME:
+ *
+ * Technically this can race. We need to either disable the editing
+ * for the buffer during the process or keep a sequence number to
+ * ensure it hasn't changed since we started the request to save.
+ */
+ document = g_task_get_source_object (task);
+ gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (document), FALSE);
+
+ change_monitor = gb_editor_document_get_change_monitor (document);
+ gb_source_change_monitor_reload (change_monitor);
+
+ g_task_return_boolean (task, TRUE);
+
+cleanup:
+ g_object_unref (task);
+
+ EXIT;
+}
+
+void
+gb_editor_document_save_async (GbEditorDocument *document,
+ GCancellable *cancellable,
+ GFileProgressCallback progress_callback,
+ gpointer progress_data,
+ GDestroyNotify progress_data_notify,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GtkSourceFileSaver *saver;
+ GbEditorFileMarks *marks;
+ GbEditorFileMark *mark;
+ GFile *location;
+ GTask *task;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ if (document->priv->trim_trailing_whitespace)
+ gb_editor_document_trim (document);
+
+ task = g_task_new (document, cancellable, callback, user_data);
+
+ saver = gtk_source_file_saver_new (GTK_SOURCE_BUFFER (document),
+ document->priv->file);
+
+ location = gtk_source_file_get_location (document->priv->file);
+
+ if (location)
+ {
+ GtkTextMark *insert;
+ GtkTextIter iter;
+ guint line;
+ guint column;
+
+ marks = gb_editor_file_marks_get_default ();
+ mark = gb_editor_file_marks_get_for_file (marks, location);
+
+ insert = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (document));
+ gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (document), &iter,
+ insert);
+ line = gtk_text_iter_get_line (&iter);
+ column = gtk_text_iter_get_line_offset (&iter);
+
+ gb_editor_file_mark_set_line (mark, line);
+ gb_editor_file_mark_set_column (mark, column);
+ }
+
+ gtk_source_file_saver_save_async (saver,
+ G_PRIORITY_DEFAULT,
+ cancellable,
+ progress_callback,
+ progress_data,
+ progress_data_notify,
+ gb_editor_document_save_cb,
+ task);
+
+ g_object_unref (saver);
+
+ EXIT;
+}
+
+gboolean
+gb_editor_document_save_finish (GbEditorDocument *document,
+ GAsyncResult *result,
+ GError **error)
+{
+ GTask *task = (GTask *)result;
+
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), FALSE);
+ g_return_val_if_fail (G_IS_TASK (task), FALSE);
+
+ return g_task_propagate_boolean (task, error);
+}
+
+static void
+gb_editor_document_load_cb (GObject *object,
+ GAsyncResult *result,
+ gpointer user_data)
+{
+ GtkSourceFileLoader *loader = (GtkSourceFileLoader *)object;
+ GbEditorDocument *document;
+ GError *error = NULL;
+ GTask *task = user_data;
+
+ ENTRY;
+
+ g_return_if_fail (GTK_SOURCE_IS_FILE_LOADER (loader));
+ g_return_if_fail (G_IS_ASYNC_RESULT (result));
+ g_return_if_fail (G_IS_TASK (task));
+
+ if (!gtk_source_file_loader_load_finish (loader, result, &error))
+ {
+ g_task_return_error (task, error);
+ GOTO (cleanup);
+ }
+
+ document = g_task_get_source_object (task);
+ gb_editor_document_guess_language (document);
+
+ g_task_return_boolean (task, TRUE);
+
+cleanup:
+ g_object_unref (task);
+
+ EXIT;
+}
+
+void
+gb_editor_document_load_async (GbEditorDocument *document,
+ GFile *file,
+ GCancellable *cancellable,
+ GFileProgressCallback progress_callback,
+ gpointer progress_data,
+ GDestroyNotify progress_data_notify,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GtkSourceFileLoader *loader;
+ GTask *task;
+
+ ENTRY;
+
+ g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
+ g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
+
+ if (file)
+ gtk_source_file_set_location (document->priv->file, file);
+
+ task = g_task_new (document, cancellable, callback, user_data);
+
+ loader = gtk_source_file_loader_new (GTK_SOURCE_BUFFER (document),
+ document->priv->file);
+
+ gtk_source_file_loader_load_async (loader,
+ G_PRIORITY_DEFAULT,
+ cancellable,
+ progress_callback,
+ progress_data,
+ progress_data_notify,
+ gb_editor_document_load_cb,
+ task);
+
+ g_object_unref (loader);
+
+ EXIT;
+}
+
+gboolean
+gb_editor_document_load_finish (GbEditorDocument *document,
+ GAsyncResult *result,
+ GError **error)
+{
+ GTask *task = (GTask *)result;
+
+ g_return_val_if_fail (GB_IS_EDITOR_DOCUMENT (document), FALSE);
+ g_return_val_if_fail (G_IS_TASK (task), FALSE);
+
+ return g_task_propagate_boolean (task, error);
+}
+
+static void
+gb_editor_document_finalize (GObject *object)
+{
+ GbEditorDocumentPrivate *priv = GB_EDITOR_DOCUMENT (object)->priv;
+
+ ENTRY;
+
+ g_clear_object (&priv->file);
+ g_clear_object (&priv->change_monitor);
+ g_clear_object (&priv->code_assistant);
+
+ G_OBJECT_CLASS(gb_editor_document_parent_class)->finalize (object);
+
+ EXIT;
+}
+
+static void
+gb_editor_document_get_property (GObject *object,
+ guint prop_id,
+ GValue *value,
+ GParamSpec *pspec)
+{
+ GbEditorDocument *self = (GbEditorDocument *)object;
+
+ switch (prop_id)
+ {
+ case PROP_CHANGE_MONITOR:
+ g_value_set_object (value, gb_editor_document_get_change_monitor (self));
+ break;
+
+ case PROP_FILE:
+ g_value_set_object (value, gb_editor_document_get_file (self));
+ break;
+
+ case PROP_TRIM_TRAILING_WHITESPACE:
+ g_value_set_boolean (value,
+ gb_editor_document_get_trim_trailing_whitespace (self));
+ break;
+
+ default:
+ G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
+ }
+}
+
+static void
gb_editor_document_set_property (GObject *object,
guint prop_id,
const GValue *value,
@@ -97,6 +721,11 @@ gb_editor_document_set_property (GObject *object,
g_value_get_string (value));
break;
+ case PROP_TRIM_TRAILING_WHITESPACE:
+ gb_editor_document_set_trim_trailing_whitespace (self,
+ g_value_get_boolean (value));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -109,11 +738,31 @@ gb_editor_document_class_init (GbEditorDocumentClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkTextBufferClass *text_buffer_class = GTK_TEXT_BUFFER_CLASS (klass);
+ object_class->finalize = gb_editor_document_finalize;
+ object_class->get_property = gb_editor_document_get_property;
object_class->set_property = gb_editor_document_set_property;
text_buffer_class->mark_set = gb_editor_document_mark_set;
text_buffer_class->changed = gb_editor_document_changed;
+ gParamSpecs [PROP_CHANGE_MONITOR] =
+ g_param_spec_object ("change-monitor",
+ _("Change Monitor"),
+ _("The change monitor for the backing file."),
+ GB_TYPE_SOURCE_CHANGE_MONITOR,
+ (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_CHANGE_MONITOR,
+ gParamSpecs [PROP_CHANGE_MONITOR]);
+
+ gParamSpecs [PROP_FILE] =
+ g_param_spec_object ("file",
+ _("File"),
+ _("The backing file for the document."),
+ GTK_SOURCE_TYPE_FILE,
+ (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_FILE,
+ gParamSpecs [PROP_FILE]);
+
gParamSpecs [PROP_STYLE_SCHEME_NAME] =
g_param_spec_string ("style-scheme-name",
_("Style Scheme Name"),
@@ -123,6 +772,15 @@ gb_editor_document_class_init (GbEditorDocumentClass *klass)
g_object_class_install_property (object_class, PROP_STYLE_SCHEME_NAME,
gParamSpecs [PROP_STYLE_SCHEME_NAME]);
+ gParamSpecs [PROP_TRIM_TRAILING_WHITESPACE] =
+ g_param_spec_boolean ("trim-trailing-whitespace",
+ _("Trim Trailing Whitespace"),
+ _("If whitespace should be trimmed before saving."),
+ TRUE,
+ (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+ g_object_class_install_property (object_class, PROP_TRIM_TRAILING_WHITESPACE,
+ gParamSpecs [PROP_TRIM_TRAILING_WHITESPACE]);
+
gSignals[CURSOR_MOVED] =
g_signal_new ("cursor-moved",
G_OBJECT_CLASS_TYPE (object_class),
@@ -139,4 +797,26 @@ static void
gb_editor_document_init (GbEditorDocument *document)
{
document->priv = gb_editor_document_get_instance_private (document);
+
+ document->priv->trim_trailing_whitespace = TRUE;
+ document->priv->file = gtk_source_file_new ();
+ document->priv->change_monitor = gb_source_change_monitor_new (GTK_TEXT_BUFFER (document));
+ document->priv->code_assistant = gb_source_code_assistant_new (GTK_TEXT_BUFFER (document));
+
+ g_signal_connect_object (document->priv->file,
+ "notify::location",
+ G_CALLBACK (gb_editor_document_notify_file_location),
+ document,
+ G_CONNECT_SWAPPED);
+
+ g_signal_connect_object (document->priv->code_assistant,
+ "changed",
+ G_CALLBACK (gb_editor_document_code_assistant_changed),
+ document,
+ G_CONNECT_SWAPPED);
+
+ g_signal_connect (document,
+ "notify::style-scheme",
+ G_CALLBACK (gb_editor_document_notify_style_scheme),
+ NULL);
}
diff --git a/src/editor/gb-editor-document.h b/src/editor/gb-editor-document.h
index 2ba022a..b7fd268 100644
--- a/src/editor/gb-editor-document.h
+++ b/src/editor/gb-editor-document.h
@@ -21,6 +21,9 @@
#include <gtksourceview/gtksourcebuffer.h>
+#include "gb-source-change-monitor.h"
+#include "gb-source-code-assistant.h"
+
G_BEGIN_DECLS
#define GB_TYPE_EDITOR_DOCUMENT (gb_editor_document_get_type())
@@ -50,8 +53,38 @@ struct _GbEditorDocumentClass
void (*cursor_moved) (GbEditorDocument *document);
};
-GbEditorDocument *gb_editor_document_new (void);
-GType gb_editor_document_get_type (void) G_GNUC_CONST;
+GbEditorDocument *gb_editor_document_new (void);
+GType gb_editor_document_get_type (void) G_GNUC_CONST;
+GtkSourceFile *gb_editor_document_get_file (GbEditorDocument *document);
+void gb_editor_document_set_file (GbEditorDocument *document,
+ GtkSourceFile *file);
+GbSourceChangeMonitor *gb_editor_document_get_change_monitor (GbEditorDocument *document);
+GbSourceCodeAssistant *gb_editor_document_get_code_assistant (GbEditorDocument *document);
+gboolean gb_editor_document_get_trim_trailing_whitespace (GbEditorDocument *document);
+void gb_editor_document_set_trim_trailing_whitespace (GbEditorDocument *document,
+ gboolean
trim_trailing_whitespace);
+void gb_editor_document_load_async (GbEditorDocument *document,
+ GFile *file,
+ GCancellable *cancellable,
+ GFileProgressCallback progress_callback,
+ gpointer progress_data,
+ GDestroyNotify progress_data_notify,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean gb_editor_document_load_finish (GbEditorDocument *document,
+ GAsyncResult *result,
+ GError **error);
+void gb_editor_document_save_async (GbEditorDocument *document,
+ GCancellable *cancellable,
+ GFileProgressCallback progress_callback,
+ gpointer progress_data,
+ GDestroyNotify progress_data_notify,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+gboolean gb_editor_document_save_finish (GbEditorDocument *document,
+ GAsyncResult *result,
+ GError **error);
+void gb_editor_document_reformat (GbEditorDocument *document);
G_END_DECLS
diff --git a/src/editor/gb-source-change-monitor.c b/src/editor/gb-source-change-monitor.c
index cb27e79..7b68bcb 100644
--- a/src/editor/gb-source-change-monitor.c
+++ b/src/editor/gb-source-change-monitor.c
@@ -85,6 +85,14 @@ gb_source_change_monitor_get_line (GbSourceChangeMonitor *monitor,
return (GPOINTER_TO_INT (value) & GB_SOURCE_CHANGE_MASK);
}
+ /*
+ * If we found a repository, but don't have state, then we are
+ * possibly just a new file in the repository. Mark the line as
+ * added.
+ */
+ if (monitor->priv->repo)
+ return GB_SOURCE_CHANGE_ADDED;
+
return GB_SOURCE_CHANGE_NONE;
}
@@ -440,7 +448,7 @@ gb_source_change_monitor_discover_repository (GbSourceChangeMonitor *monitor)
if (!repo_file)
{
- g_message (_("Failed to locate a gir repository: %s"), error->message);
+ g_message (_("Failed to locate a git repository: %s"), error->message);
g_clear_error (&error);
EXIT;
}
@@ -484,12 +492,16 @@ gb_source_change_monitor_set_buffer (GbSourceChangeMonitor *monitor,
{
g_signal_handler_disconnect (priv->buffer, priv->changed_handler);
priv->changed_handler = 0;
- g_clear_object (&priv->buffer);
+ g_object_remove_weak_pointer (G_OBJECT (priv->buffer),
+ (gpointer *)&priv->buffer);
}
if (buffer)
{
- priv->buffer = g_object_ref (buffer);
+ priv->buffer = buffer;
+ g_object_add_weak_pointer (G_OBJECT (priv->buffer),
+ (gpointer *)&priv->buffer);
+
priv->changed_handler =
g_signal_connect_object (priv->buffer,
"changed",
@@ -512,6 +524,23 @@ gb_source_change_monitor_get_file (GbSourceChangeMonitor *monitor)
}
void
+gb_source_change_monitor_reload (GbSourceChangeMonitor *monitor)
+{
+ ENTRY;
+
+ g_return_if_fail (GB_IS_SOURCE_CHANGE_MONITOR (monitor));
+
+ if (monitor->priv->file)
+ {
+ gb_source_change_monitor_discover_repository (monitor);
+ gb_source_change_monitor_load_blob (monitor);
+ gb_source_change_monitor_queue_parse (monitor);
+ }
+
+ EXIT;
+}
+
+void
gb_source_change_monitor_set_file (GbSourceChangeMonitor *monitor,
GFile *file)
{
@@ -534,14 +563,11 @@ gb_source_change_monitor_set_file (GbSourceChangeMonitor *monitor,
if (file)
{
priv->file = g_object_ref (file);
- gb_source_change_monitor_discover_repository (monitor);
- gb_source_change_monitor_load_blob (monitor);
+ gb_source_change_monitor_reload (monitor);
}
g_object_notify_by_pspec (G_OBJECT (monitor), gParamSpecs [PROP_FILE]);
- gb_source_change_monitor_queue_parse (monitor);
-
EXIT;
}
@@ -678,6 +704,5 @@ gb_source_change_monitor_init (GbSourceChangeMonitor *monitor)
{
ENTRY;
monitor->priv = gb_source_change_monitor_get_instance_private (monitor);
- monitor->priv->state = g_hash_table_new (g_direct_hash, g_direct_equal);
EXIT;
}
diff --git a/src/editor/gb-source-change-monitor.h b/src/editor/gb-source-change-monitor.h
index 24b483a..d107849 100644
--- a/src/editor/gb-source-change-monitor.h
+++ b/src/editor/gb-source-change-monitor.h
@@ -64,6 +64,7 @@ void gb_source_change_monitor_set_file (GbSourceChangeMonitor
GFile *file);
GbSourceChangeFlags gb_source_change_monitor_get_line (GbSourceChangeMonitor *monitor,
guint lineno);
+void gb_source_change_monitor_reload (GbSourceChangeMonitor *monitor);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]