[gnome-builder/editor-layout: 21/21] editor: wip on code assistance via document



commit e35259866931fe8d98bf24bad05acf24e7809318
Author: Christian Hergert <christian hergert me>
Date:   Fri Nov 28 21:08:12 2014 -0800

    editor: wip on code assistance via document

 src/code-assistant/gb-source-code-assistant.c |  292 +++++++++++++++++++++++++
 src/code-assistant/gb-source-code-assistant.h |   58 +++++
 src/editor/gb-editor-document.c               |   13 +-
 src/editor/gb-editor-document.h               |    2 +
 src/gnome-builder.mk                          |    3 +
 5 files changed, 367 insertions(+), 1 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..148a22d
--- /dev/null
+++ b/src/code-assistant/gb-source-code-assistant.c
@@ -0,0 +1,292 @@
+/* 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 "gb-source-code-assistant.h"
+#include "gca-structs.h"
+
+struct _GbSourceCodeAssistantPrivate
+{
+  GtkTextBuffer *buffer;
+  GArray        *diagnostics;
+  gulong         changed_handler;
+  guint          parse_timeout;
+  guint          active : 1;
+};
+
+enum
+{
+  PROP_0,
+  PROP_ACTIVE,
+  PROP_BUFFER,
+  LAST_PROP
+};
+
+G_DEFINE_TYPE_WITH_PRIVATE (GbSourceCodeAssistant,
+                            gb_source_code_assistant,
+                            G_TYPE_OBJECT)
+
+static GParamSpec *gParamSpecs [LAST_PROP];
+
+#define PARSE_TIMEOUT_MSEC 350
+
+GbSourceCodeAssistant *
+gb_source_code_assistant_new (GtkTextBuffer *buffer)
+{
+  return g_object_new (GB_TYPE_SOURCE_CODE_ASSISTANT,
+                       "buffer", buffer,
+                       NULL);
+}
+
+/**
+ * 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 gboolean
+gb_source_code_assistant_do_parse (gpointer data)
+{
+  GbSourceCodeAssistant *assistant = data;
+
+  assistant->priv->parse_timeout = 0;
+
+  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_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;
+}
+
+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);
+}
+
+/**
+ * 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);
+        }
+
+      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;
+
+  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_OBJECT_CLASS (gb_source_code_assistant_parent_class)->finalize (object);
+}
+
+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);
+
+  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_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]);
+}
+
+static void
+gb_source_code_assistant_init (GbSourceCodeAssistant *assistant)
+{
+  assistant->priv = gb_source_code_assistant_get_instance_private (assistant);
+}
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..1ef6364
--- /dev/null
+++ b/src/code-assistant/gb-source-code-assistant.h
@@ -0,0 +1,58 @@
+/* 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;
+};
+
+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 0c7d7d8..1accb7b 100644
--- a/src/editor/gb-editor-document.c
+++ b/src/editor/gb-editor-document.c
@@ -26,8 +26,9 @@
 struct _GbEditorDocumentPrivate
 {
   GtkSourceFile         *file;
-  GbSourceChangeMonitor *change_monitor;
   GBinding              *file_binding;
+  GbSourceChangeMonitor *change_monitor;
+  GbSourceCodeAssistant *code_assistant;
 };
 
 enum {
@@ -62,6 +63,14 @@ gb_editor_document_get_change_monitor (GbEditorDocument *document)
   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)
 {
@@ -144,6 +153,7 @@ gb_editor_document_finalize (GObject *object)
   g_clear_object (&priv->file);
   g_clear_object (&priv->file_binding);
   g_clear_object (&priv->change_monitor);
+  g_clear_object (&priv->code_assistant);
 
   G_OBJECT_CLASS(gb_editor_document_parent_class)->finalize (object);
 }
@@ -254,4 +264,5 @@ gb_editor_document_init (GbEditorDocument *document)
   document->priv = gb_editor_document_get_instance_private (document);
   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));
 }
diff --git a/src/editor/gb-editor-document.h b/src/editor/gb-editor-document.h
index 80d8eda..2c6f902 100644
--- a/src/editor/gb-editor-document.h
+++ b/src/editor/gb-editor-document.h
@@ -22,6 +22,7 @@
 #include <gtksourceview/gtksourcebuffer.h>
 
 #include "gb-source-change-monitor.h"
+#include "gb-source-code-assistant.h"
 
 G_BEGIN_DECLS
 
@@ -58,6 +59,7 @@ GtkSourceFile         *gb_editor_document_get_file           (GbEditorDocument *
 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);
 
 G_END_DECLS
 
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index dd469b8..2688c5b 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -17,6 +17,8 @@ libgnome_builder_la_SOURCES = \
        src/auto-indent/gb-source-auto-indenter-python.h \
        src/auto-indent/gb-source-auto-indenter-xml.c \
        src/auto-indent/gb-source-auto-indenter-xml.h \
+       src/code-assistant/gb-source-code-assistant.h \
+       src/code-assistant/gb-source-code-assistant.c \
        src/commands/gb-command.c \
        src/commands/gb-command.h \
        src/commands/gb-command-bar.c \
@@ -201,6 +203,7 @@ libgnome_builder_la_CFLAGS = \
        -I$(top_srcdir)/src/app \
        -I$(top_srcdir)/src/auto-indent \
        -I$(top_srcdir)/src/commands \
+       -I$(top_srcdir)/src/code-assistant \
        -I$(top_srcdir)/src/credits \
        -I$(top_srcdir)/src/devhelp \
        -I$(top_srcdir)/src/editor \


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