[gnome-builder] use F4 for emacs-style "find-other-file"



commit bbf9e030a2ea68da53b2924002b259ffb14a605a
Author: Christian Hergert <christian hergert me>
Date:   Thu Mar 19 16:17:04 2015 -0700

    use F4 for emacs-style "find-other-file"

 data/keybindings/default.css        |    3 +
 data/keybindings/emacs.css          |    2 +
 data/keybindings/vim.css            |    3 +
 src/editor/gb-editor-view-actions.c |  120 +++++++++++++++++++++++++++++++++++
 4 files changed, 128 insertions(+), 0 deletions(-)
---
diff --git a/data/keybindings/default.css b/data/keybindings/default.css
index baaf257..9722fa5 100644
--- a/data/keybindings/default.css
+++ b/data/keybindings/default.css
@@ -13,6 +13,9 @@
   bind "<ctrl>KP_Page_Up" { "action" ("view-stack", "previous-view", "") };
   bind "<ctrl>Page_Down" { "action" ("view-stack", "next-view", "") };
   bind "<ctrl>KP_Page_Down" { "action" ("view-stack", "next-view", "") };
+
+  /* toggle header/source */
+  bind "F4" { "action" ("view", "find-other-file", "") };
 }
 
 IdeSourceViewMode.default {
diff --git a/data/keybindings/emacs.css b/data/keybindings/emacs.css
index fcde682..2258fbb 100644
--- a/data/keybindings/emacs.css
+++ b/data/keybindings/emacs.css
@@ -63,6 +63,8 @@
   bind "<ctrl><alt>Page_Down" { "action" ("view-stack", "next-view", "") };
   bind "<ctrl><alt>KP_Page_Down" { "action" ("view-stack", "next-view", "") };
 
+  bind "F4" { "action" ("view", "find-other-file", "") };
+
   bind "<alt>0" { "append-to-count" (0) };
   bind "<alt>1" { "append-to-count" (1) };
   bind "<alt>2" { "append-to-count" (2) };
diff --git a/data/keybindings/vim.css b/data/keybindings/vim.css
index 75f399d..4feee92 100644
--- a/data/keybindings/vim.css
+++ b/data/keybindings/vim.css
@@ -449,6 +449,9 @@
 
   /* window controls */
   bind "<ctrl>w" { "set-mode" ("vim-normal-ctrl-w", transient) };
+
+  /* not vim perse, but common mapping */
+  bind "F4" { "action" ("view", "find-other-file", "") };
 }
 
 @binding-set builder-vim-source-view-normal-bracket
diff --git a/src/editor/gb-editor-view-actions.c b/src/editor/gb-editor-view-actions.c
index 7d05319..fcb62ad 100644
--- a/src/editor/gb-editor-view-actions.c
+++ b/src/editor/gb-editor-view-actions.c
@@ -19,11 +19,13 @@
 #define G_LOG_DOMAIN "gb-editor-view"
 
 #include <ide.h>
+#include <string.h>
 
 #include "gb-editor-frame-private.h"
 #include "gb-editor-view-actions.h"
 #include "gb-editor-view-private.h"
 #include "gb-widget.h"
+#include "gb-workbench.h"
 
 static void
 gb_editor_view_actions_source_view_notify (IdeSourceView *source_view,
@@ -271,6 +273,123 @@ gb_editor_view_actions_close (GSimpleAction *action,
     }
 }
 
+static gboolean
+has_suffix (const gchar          *path,
+            const gchar * const *allowed_suffixes)
+{
+  const gchar *dot;
+  gsize i;
+
+  dot = strrchr (path, '.');
+  if (!dot)
+    return FALSE;
+
+  dot++;
+
+  for (i = 0; allowed_suffixes [i]; i++)
+    {
+      if (g_str_equal (dot, allowed_suffixes [i]))
+        return TRUE;
+    }
+
+  return FALSE;
+}
+
+static void
+gb_editor_view_actions_find_other_file_worker (GTask        *task,
+                                               gpointer      source_object,
+                                               gpointer      task_data,
+                                               GCancellable *cancellable)
+{
+  GbEditorView *self = source_object;
+  const gchar *src_suffixes[] = { "c", "cc", "cpp", "cxx", NULL };
+  const gchar *hdr_suffixes[] = { "h", "hh", "hpp", "hxx", NULL };
+  const gchar **target = NULL;
+  IdeFile *file;
+  g_autofree gchar *prefix = NULL;
+  const gchar *path;
+  gsize i;
+
+  g_assert (GB_IS_EDITOR_VIEW (self));
+
+  file = ide_buffer_get_file (IDE_BUFFER (self->document));
+  path = ide_file_get_path (file);
+
+  if (has_suffix (path, src_suffixes))
+    {
+      target = hdr_suffixes;
+    }
+  else if (has_suffix (path, hdr_suffixes))
+    {
+      target = src_suffixes;
+    }
+  else
+    {
+      g_task_return_new_error (task,
+                               G_IO_ERROR,
+                               G_IO_ERROR_INVALID_FILENAME,
+                               "File is missing a suffix.");
+      return;
+    }
+
+  prefix = g_strndup (path, strrchr (path, '.') - path);
+
+  for (i = 0; target [i]; i++)
+    {
+      g_autofree gchar *new_path = NULL;
+
+      new_path = g_strdup_printf ("%s.%s", prefix, target [i]);
+
+      if (g_file_test (new_path, G_FILE_TEST_IS_REGULAR))
+        {
+          g_autoptr(GFile) gfile = NULL;
+
+          gfile = g_file_new_for_path (new_path);
+          g_task_return_pointer (task, g_object_ref (gfile), g_object_unref);
+          return;
+        }
+    }
+
+  g_task_return_new_error (task,
+                           G_IO_ERROR,
+                           G_IO_ERROR_NOT_FOUND,
+                           "Failed to locate other file.");
+}
+
+static void
+find_other_file_cb (GObject      *object,
+                    GAsyncResult *result,
+                    gpointer      user_data)
+{
+  GbEditorView *self = (GbEditorView *)object;
+  GTask *task = (GTask *)result;
+  g_autoptr(GFile) ret = NULL;
+
+  ret = g_task_propagate_pointer (task, NULL);
+
+  if (ret)
+    {
+      GbWorkbench *workbench;
+
+      workbench = gb_widget_get_workbench (GTK_WIDGET (self));
+      gb_workbench_open (workbench, ret);
+    }
+}
+
+static void
+gb_editor_view_actions_find_other_file (GSimpleAction *action,
+                                        GVariant      *param,
+                                        gpointer       user_data)
+{
+  GbEditorView *self = user_data;
+  g_autoptr(GTask) task = NULL;
+
+  g_assert (GB_IS_EDITOR_VIEW (self));
+
+  task = g_task_new (self, NULL, find_other_file_cb, NULL);
+  g_task_run_in_thread (task, gb_editor_view_actions_find_other_file_worker);
+}
+
 static GActionEntry GbEditorViewActions[] = {
   { "auto-indent", NULL, NULL, "false", gb_editor_view_actions_auto_indent },
   { "language", NULL, "s", "''", gb_editor_view_actions_language },
@@ -283,6 +402,7 @@ static GActionEntry GbEditorViewActions[] = {
   { "tab-width", NULL, "i", "8", gb_editor_view_actions_tab_width },
   { "use-spaces", NULL, "b", "false", gb_editor_view_actions_use_spaces },
   { "toggle-split", gb_editor_view_actions_toggle_split },
+  { "find-other-file", gb_editor_view_actions_find_other_file },
 };
 
 void


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