[gnome-builder] editor: fix "request-documentation" gsignal action



commit 381f1c73eeec35779ca1d07afce9f8d71354c87a
Author: Christian Hergert <christian hergert me>
Date:   Sun Sep 20 01:44:29 2015 -0700

    editor: fix "request-documentation" gsignal action
    
    This fixes shift+k in vim mode.

 plugins/devhelp/Makefile.am        |    1 +
 plugins/devhelp/gb-devhelp-panel.c |   55 ++++++++++++++++++++++++++++++++++++
 src/editor/gb-editor-view.c        |   47 ++++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 0 deletions(-)
---
diff --git a/plugins/devhelp/Makefile.am b/plugins/devhelp/Makefile.am
index 686f0a3..f45756d 100644
--- a/plugins/devhelp/Makefile.am
+++ b/plugins/devhelp/Makefile.am
@@ -31,6 +31,7 @@ libdevhelp_plugin_la_CFLAGS = \
        $(DEVHELP_CFLAGS) \
        -I$(top_srcdir)/src \
        -I$(top_srcdir)/src/documents \
+       -I$(top_srcdir)/src/editor \
        -I$(top_srcdir)/src/search \
        -I$(top_srcdir)/src/views \
        -I$(top_srcdir)/src/util \
diff --git a/plugins/devhelp/gb-devhelp-panel.c b/plugins/devhelp/gb-devhelp-panel.c
index 8c2880a..6682780 100644
--- a/plugins/devhelp/gb-devhelp-panel.c
+++ b/plugins/devhelp/gb-devhelp-panel.c
@@ -25,6 +25,7 @@
 #include "gb-devhelp-resources.h"
 #include "gb-devhelp-view.h"
 #include "gb-document.h"
+#include "gb-editor-view.h"
 #include "gb-view.h"
 #include "gb-view-grid.h"
 #include "gb-workbench-addin.h"
@@ -39,6 +40,9 @@ struct _GbDevhelpPanel
   DhBookManager     *book_manager;
   GbDevhelpDocument *document;
 
+  GbView            *current_view;
+  gulong             current_view_handler;
+
   GtkWidget         *sidebar;
 };
 
@@ -78,6 +82,50 @@ focus_devhelp_search_cb (GSimpleAction  *action,
 }
 
 static void
+request_documentation_cb (GbDevhelpPanel *self,
+                          const gchar    *keywords,
+                          GbEditorView   *view)
+{
+  g_assert (GB_IS_EDITOR_VIEW (view));
+  g_assert (GB_IS_DEVHELP_PANEL (self));
+
+  if (ide_str_empty0 (keywords))
+    return;
+
+  dh_sidebar_set_search_string (DH_SIDEBAR (self->sidebar), keywords);
+  dh_sidebar_set_search_focus (DH_SIDEBAR (self->sidebar));
+}
+
+static void
+notify_active_view_cb (GbDevhelpPanel *self,
+                       GParamSpec     *pspec,
+                       GbWorkbench    *workbench)
+{
+  GtkWidget *view;
+
+  g_assert (GB_IS_DEVHELP_PANEL (self));
+  g_assert (GB_IS_WORKBENCH (workbench));
+
+  if (self->current_view)
+    {
+      g_signal_handler_disconnect (self->current_view, self->current_view_handler);
+      self->current_view_handler = 0;
+      ide_clear_weak_pointer (&self->current_view);
+    }
+
+  view = gb_workbench_get_active_view (workbench);
+  if (!GB_IS_EDITOR_VIEW (view))
+    return;
+
+  ide_set_weak_pointer (&self->current_view, (GbView *)view);
+  self->current_view_handler = g_signal_connect_object (view,
+                                                        "request-documentation",
+                                                        G_CALLBACK (request_documentation_cb),
+                                                        self,
+                                                        G_CONNECT_SWAPPED);
+}
+
+static void
 gb_devhelp_panel_load (GbWorkbenchAddin *addin)
 {
   GbDevhelpPanel *self = (GbDevhelpPanel *)addin;
@@ -117,6 +165,12 @@ gb_devhelp_panel_load (GbWorkbenchAddin *addin)
   gtk_widget_show (GTK_WIDGET (self));
 
   gtk_stack_set_visible_child (GTK_STACK (parent), GTK_WIDGET (self));
+
+  g_signal_connect_object (self->workbench,
+                           "notify::active-view",
+                           G_CALLBACK (notify_active_view_cb),
+                           self,
+                           G_CONNECT_SWAPPED);
 }
 
 static void
@@ -190,6 +244,7 @@ gb_devhelp_panel_finalize (GObject *object)
 
   g_clear_object (&self->book_manager);
   g_clear_object (&self->document);
+  ide_clear_weak_pointer (&self->current_view);
 
   G_OBJECT_CLASS (gb_devhelp_panel_parent_class)->finalize (object);
 }
diff --git a/src/editor/gb-editor-view.c b/src/editor/gb-editor-view.c
index 39232cb..37f0e66 100644
--- a/src/editor/gb-editor-view.c
+++ b/src/editor/gb-editor-view.c
@@ -40,7 +40,13 @@ enum {
   LAST_PROP
 };
 
+enum {
+  REQUEST_DOCUMENTATION,
+  LAST_SIGNAL
+};
+
 static GParamSpec *gParamSpecs [LAST_PROP];
+static guint gSignals [LAST_SIGNAL];
 
 static GbDocument *
 gb_editor_view_get_document (GbView *view)
@@ -423,6 +429,27 @@ gb_editor_view_grab_focus (GtkWidget *widget)
 }
 
 static void
+gb_editor_view_request_documentation (GbEditorView  *self,
+                                      IdeSourceView *source_view)
+{
+  g_autofree gchar *word = NULL;
+  IdeBuffer *buffer;
+  GtkTextMark *mark;
+  GtkTextIter iter;
+
+  g_assert (GB_IS_EDITOR_VIEW (self));
+  g_assert (IDE_IS_SOURCE_VIEW (source_view));
+
+  buffer = IDE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (source_view)));
+  mark = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (buffer));
+  gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (buffer), &iter, mark);
+
+  word = ide_buffer_get_word_at_iter (buffer, &iter);
+
+  g_signal_emit (self, gSignals [REQUEST_DOCUMENTATION], 0, word);
+}
+
+static void
 gb_editor_view_set_split_view (GbView   *view,
                                gboolean  split_view)
 {
@@ -443,6 +470,11 @@ gb_editor_view_set_split_view (GbView   *view,
                                    "document", self->document,
                                    "visible", TRUE,
                                    NULL);
+      g_signal_connect_object (self->frame2->source_view,
+                               "request-documentation",
+                               G_CALLBACK (gb_editor_view_request_documentation),
+                               self,
+                               G_CONNECT_SWAPPED);
       gtk_container_add_with_properties (GTK_CONTAINER (self->paned), GTK_WIDGET (self->frame2),
                                          "shrink", FALSE,
                                          "resize", TRUE,
@@ -764,6 +796,15 @@ gb_editor_view_class_init (GbEditorViewClass *klass)
 
   g_object_class_install_properties (object_class, LAST_PROP, gParamSpecs);
 
+  gSignals [REQUEST_DOCUMENTATION] =
+    g_signal_new ("request-documentation",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  0, NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  1,
+                  G_TYPE_STRING);
+
   GB_WIDGET_CLASS_TEMPLATE (klass, "gb-editor-view.ui");
 
   GB_WIDGET_CLASS_BIND (klass, GbEditorView, cursor_label);
@@ -797,6 +838,12 @@ gb_editor_view_init (GbEditorView *self)
                            self,
                            G_CONNECT_SWAPPED);
 
+  g_signal_connect_object (self->frame1->source_view,
+                           "request-documentation",
+                           G_CALLBACK (gb_editor_view_request_documentation),
+                           self,
+                           G_CONNECT_SWAPPED);
+
   g_signal_connect_object (self->goto_line_popover,
                            "activate",
                            G_CALLBACK (gb_editor_view_goto_line_activate),


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