[gnome-builder] plugins/codeui: implement actions to goto declaration/definition



commit d9f90a7343cb1cf48669e386e97be3f0a7543ab1
Author: Christian Hergert <chergert redhat com>
Date:   Tue Sep 20 21:13:30 2022 -0700

    plugins/codeui: implement actions to goto declaration/definition
    
    This was also still missing from the GTK 4 port.

 src/libide/sourceview/gtk/menus.ui                |  12 --
 src/plugins/codeui/gbp-codeui-editor-page-addin.c | 147 +++++++++++++++++++++-
 src/plugins/codeui/gtk/menus.ui                   |  31 ++++-
 3 files changed, 170 insertions(+), 20 deletions(-)
---
diff --git a/src/libide/sourceview/gtk/menus.ui b/src/libide/sourceview/gtk/menus.ui
index aaba9325e..5b13d0011 100644
--- a/src/libide/sourceview/gtk/menus.ui
+++ b/src/libide/sourceview/gtk/menus.ui
@@ -144,18 +144,6 @@
     <section id="ide-source-view-popup-menu-jump-section">
       <attribute name="page" translatable="yes">Text Editor</attribute>
       <attribute name="group" translatable="yes">Code Navigation</attribute>
-      <item>
-        <attribute name="id">source-view-goto-def</attribute>
-        <attribute name="label" translatable="yes">_Go to Definition</attribute>
-        <attribute name="description" translatable="yes">Jump to file and location where item is 
defined</attribute>
-        <attribute name="action">sourceview.goto-definition</attribute>
-        <attribute name="accel">&lt;alt&gt;period</attribute>
-      </item>
-      <item>
-        <attribute name="id">source-view-find-references</attribute>
-        <attribute name="label" translatable="yes">_Find References</attribute>
-        <attribute name="action">sourceview.find-references</attribute>
-      </item>
     </section>
     <section id="ide-source-view-popup-menu-foundry-section"/>
     <section id="ide-source-view-popup-menu-files-section"/>
diff --git a/src/plugins/codeui/gbp-codeui-editor-page-addin.c 
b/src/plugins/codeui/gbp-codeui-editor-page-addin.c
index 5caae5aaf..0bc7f604e 100644
--- a/src/plugins/codeui/gbp-codeui-editor-page-addin.c
+++ b/src/plugins/codeui/gbp-codeui-editor-page-addin.c
@@ -40,11 +40,17 @@ struct _GbpCodeuiEditorPageAddin
   gulong         notify_has_selection;
 };
 
-static void rename_symbol_action (GbpCodeuiEditorPageAddin *self,
-                                  GVariant                 *params);
+static void goto_declaration_action (GbpCodeuiEditorPageAddin *self,
+                                     GVariant                 *params);
+static void goto_definition_action  (GbpCodeuiEditorPageAddin *self,
+                                     GVariant                 *params);
+static void rename_symbol_action    (GbpCodeuiEditorPageAddin *self,
+                                     GVariant                 *params);
 
 IDE_DEFINE_ACTION_GROUP (GbpCodeuiEditorPageAddin, gbp_codeui_editor_page_addin, {
   { "rename-symbol", rename_symbol_action },
+  { "goto-declaration", goto_declaration_action },
+  { "goto-definition", goto_definition_action },
 })
 
 static void
@@ -52,6 +58,7 @@ gbp_codeui_editor_page_addin_update_state (GbpCodeuiEditorPageAddin *self)
 {
   IdeRenameProvider *provider;
   gboolean has_selection;
+  gboolean has_resolvers;
 
   IDE_ENTRY;
 
@@ -59,10 +66,13 @@ gbp_codeui_editor_page_addin_update_state (GbpCodeuiEditorPageAddin *self)
   g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
   g_assert (IDE_IS_BUFFER (self->buffer));
 
+  has_resolvers = ide_buffer_has_symbol_resolvers (self->buffer);
   provider = ide_buffer_get_rename_provider (self->buffer);
   has_selection = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (self->buffer));
 
   gbp_codeui_editor_page_addin_set_action_enabled (self, "rename-symbol", has_selection && provider);
+  gbp_codeui_editor_page_addin_set_action_enabled (self, "goto-declaration", has_resolvers);
+  gbp_codeui_editor_page_addin_set_action_enabled (self, "goto-definition", has_resolvers);
 
   IDE_EXIT;
 }
@@ -194,6 +204,139 @@ rename_symbol_action (GbpCodeuiEditorPageAddin *self,
   IDE_EXIT;
 }
 
+static void
+gbp_codeui_editor_page_addin_error (GbpCodeuiEditorPageAddin *self,
+                                    const GError             *error)
+{
+  IdeContext *context;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
+  g_assert (error != NULL);
+
+  if (self->page == NULL)
+    return;
+
+  context = ide_widget_get_context (GTK_WIDGET (self->page));
+  ide_context_warning (context, "%s", error->message);
+}
+
+static void
+gbp_codeui_editor_page_addin_navigate_to (GbpCodeuiEditorPageAddin *self,
+                                          IdeLocation              *location)
+{
+  g_autoptr(PanelPosition) position = NULL;
+  IdeWorkspace *workspace;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
+
+  if (self->page == NULL || location == NULL)
+    return;
+
+  workspace = ide_widget_get_workspace (GTK_WIDGET (self->page));
+  position = panel_widget_get_position (PANEL_WIDGET (self->page));
+
+  ide_editor_focus_location (workspace, position, location);
+}
+
+static void
+goto_declaration_cb (GObject      *object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
+{
+  IdeBuffer *buffer = (IdeBuffer *)object;
+  g_autoptr(GbpCodeuiEditorPageAddin) self = user_data;
+  g_autoptr(IdeSymbol) symbol = NULL;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
+
+  if (!(symbol = ide_buffer_get_symbol_at_location_finish (buffer, result, &error)))
+    gbp_codeui_editor_page_addin_error (self, error);
+  else
+    gbp_codeui_editor_page_addin_navigate_to (self,
+                                              ide_symbol_get_header_location (symbol) ?
+                                              ide_symbol_get_header_location (symbol) :
+                                              ide_symbol_get_location (symbol));
+
+  IDE_EXIT;
+}
+
+static void
+goto_declaration_action (GbpCodeuiEditorPageAddin *self,
+                         GVariant                 *params)
+{
+  GtkTextIter insert;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
+
+  ide_buffer_get_selection_bounds (self->buffer, &insert, NULL);
+
+  ide_buffer_get_symbol_at_location_async (self->buffer,
+                                           &insert,
+                                           NULL,
+                                           goto_declaration_cb,
+                                           g_object_ref (self));
+
+  IDE_EXIT;
+}
+
+static void
+goto_definition_cb (GObject      *object,
+                    GAsyncResult *result,
+                    gpointer      user_data)
+{
+  IdeBuffer *buffer = (IdeBuffer *)object;
+  g_autoptr(GbpCodeuiEditorPageAddin) self = user_data;
+  g_autoptr(IdeSymbol) symbol = NULL;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
+
+  if (!(symbol = ide_buffer_get_symbol_at_location_finish (buffer, result, &error)))
+    gbp_codeui_editor_page_addin_error (self, error);
+  else
+    gbp_codeui_editor_page_addin_navigate_to (self, ide_symbol_get_location (symbol));
+
+  IDE_EXIT;
+}
+
+static void
+goto_definition_action (GbpCodeuiEditorPageAddin *self,
+                        GVariant                 *params)
+{
+  GtkTextIter insert;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_MAIN_THREAD ());
+  g_assert (GBP_IS_CODEUI_EDITOR_PAGE_ADDIN (self));
+
+  ide_buffer_get_selection_bounds (self->buffer, &insert, NULL);
+
+  ide_buffer_get_symbol_at_location_async (self->buffer,
+                                           &insert,
+                                           NULL,
+                                           goto_definition_cb,
+                                           g_object_ref (self));
+
+  IDE_EXIT;
+}
+
 G_DEFINE_FINAL_TYPE_WITH_CODE (GbpCodeuiEditorPageAddin, gbp_codeui_editor_page_addin, G_TYPE_OBJECT,
                                G_IMPLEMENT_INTERFACE (G_TYPE_ACTION_GROUP, 
gbp_codeui_editor_page_addin_init_action_group)
                                G_IMPLEMENT_INTERFACE (IDE_TYPE_EDITOR_PAGE_ADDIN, 
editor_page_addin_iface_init))
diff --git a/src/plugins/codeui/gtk/menus.ui b/src/plugins/codeui/gtk/menus.ui
index 732026eb9..afbedbaf2 100644
--- a/src/plugins/codeui/gtk/menus.ui
+++ b/src/plugins/codeui/gtk/menus.ui
@@ -1,10 +1,29 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <interface>
-  <menu id="ide-source-view-popup-menu-foundry-section">
-    <item>
-      <attribute name="id">codeui-rename-symbol</attribute>
-      <attribute name="label" translatable="yes">R_ename Symbol…</attribute>
-      <attribute name="action">page.codeui.rename-symbol</attribute>
-    </item>
+  <menu id="ide-source-view-popup-menu">
+    <section id="ide-source-view-popup-menu-foundry-section">
+      <item>
+        <attribute name="id">codeui-rename-symbol</attribute>
+        <attribute name="label" translatable="yes">R_ename Symbol…</attribute>
+        <attribute name="action">page.codeui.rename-symbol</attribute>
+      </item>
+    </section>
+    <section id="ide-source-view-popup-menu-jump-section">
+      <item>
+        <attribute name="label" translatable="yes">_Go to Declaration</attribute>
+        <attribute name="description" translatable="yes">Jump to file and location where item is 
declared</attribute>
+        <attribute name="action">page.codeui.goto-declaration</attribute>
+      </item>
+      <item>
+        <attribute name="label" translatable="yes">_Go to Definition</attribute>
+        <attribute name="description" translatable="yes">Jump to file and location where item is 
defined</attribute>
+        <attribute name="action">page.codeui.goto-definition</attribute>
+      </item>
+      <item>
+        <attribute name="id">source-view-find-references</attribute>
+        <attribute name="label" translatable="yes">_Find References</attribute>
+        <attribute name="action">sourceview.find-references</attribute>
+      </item>
+    </section>
   </menu>
 </interface>


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