[gnome-builder/wip/chergert/perspective] workbench: add loader hint when opening files



commit 0877a4f09c6b63cb765f83731dc67c0b9c823138
Author: Christian Hergert <christian hergert me>
Date:   Mon Nov 23 13:58:25 2015 -0800

    workbench: add loader hint when opening files
    
    This allows you to suggest the IdeWorkbenchAddin that will respond to your
    file open request. For example, "editor" will sort the editor as the first
    item to respond to your handler.

 libide/editor/ide-editor-view-actions.c    |    2 +-
 libide/editor/ide-editor-workbench-addin.c |   11 ++++-
 libide/ide-workbench-actions.c             |    2 +-
 libide/ide-workbench-addin.c               |   29 +++++++++++-
 libide/ide-workbench-addin.h               |   70 ++++++++++++++-------------
 libide/ide-workbench-open.c                |   32 ++++++++++++-
 libide/ide-workbench.h                     |    2 +
 plugins/command-bar/gb-vim.c               |    2 +-
 8 files changed, 108 insertions(+), 42 deletions(-)
---
diff --git a/libide/editor/ide-editor-view-actions.c b/libide/editor/ide-editor-view-actions.c
index e382d09..0fd2a23 100644
--- a/libide/editor/ide-editor-view-actions.c
+++ b/libide/editor/ide-editor-view-actions.c
@@ -511,7 +511,7 @@ find_other_file_cb (GObject      *object,
 
       gfile = ide_file_get_file (ret);
       workbench = ide_widget_get_workbench (GTK_WIDGET (self));
-      ide_workbench_open_files_async (workbench, &gfile, 1, NULL, NULL, NULL);
+      ide_workbench_open_files_async (workbench, &gfile, 1, "editor", NULL, NULL, NULL);
     }
 }
 
diff --git a/libide/editor/ide-editor-workbench-addin.c b/libide/editor/ide-editor-workbench-addin.c
index 777d78c..aeb503c 100644
--- a/libide/editor/ide-editor-workbench-addin.c
+++ b/libide/editor/ide-editor-workbench-addin.c
@@ -214,12 +214,19 @@ ide_editor_workbench_addin_open_finish (IdeWorkbenchAddin  *addin,
   return g_task_propagate_boolean (G_TASK (result), error);
 }
 
+static gchar *
+ide_editor_workbench_addin_get_id (IdeWorkbenchAddin *addin)
+{
+  return g_strdup ("editor");
+}
+
 static void
 ide_workbench_addin_iface_init (IdeWorkbenchAddinInterface *iface)
 {
-  iface->load = ide_editor_workbench_addin_load;
-  iface->unload = ide_editor_workbench_addin_unload;
   iface->can_open = ide_editor_workbench_addin_can_open;
+  iface->get_id = ide_editor_workbench_addin_get_id;
+  iface->load = ide_editor_workbench_addin_load;
   iface->open_async = ide_editor_workbench_addin_open_async;
   iface->open_finish = ide_editor_workbench_addin_open_finish;
+  iface->unload = ide_editor_workbench_addin_unload;
 }
diff --git a/libide/ide-workbench-actions.c b/libide/ide-workbench-actions.c
index 27552c2..68e9c79 100644
--- a/libide/ide-workbench-actions.c
+++ b/libide/ide-workbench-actions.c
@@ -85,7 +85,7 @@ ide_workbench_actions_open_with_dialog (GSimpleAction *action,
       IDE_PROBE;
 
       file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (dialog));
-      ide_workbench_open_files_async (self, &file, 1, NULL,
+      ide_workbench_open_files_async (self, &file, 1, NULL, NULL,
                                       ide_workbench_actions_open_with_dialog_cb,
                                       NULL);
     }
diff --git a/libide/ide-workbench-addin.c b/libide/ide-workbench-addin.c
index 8dfd408..a8be190 100644
--- a/libide/ide-workbench-addin.c
+++ b/libide/ide-workbench-addin.c
@@ -22,6 +22,12 @@
 
 G_DEFINE_INTERFACE (IdeWorkbenchAddin, ide_workbench_addin, G_TYPE_OBJECT)
 
+static gchar *
+ide_workbench_addin_real_get_id (IdeWorkbenchAddin *self)
+{
+  return g_strdup (G_OBJECT_TYPE_NAME (self));
+}
+
 static void
 ide_workbench_addin_real_load (IdeWorkbenchAddin *self,
                                IdeWorkbench      *workbench)
@@ -47,9 +53,10 @@ ide_workbench_addin_real_can_open (IdeWorkbenchAddin *self,
 static void
 ide_workbench_addin_default_init (IdeWorkbenchAddinInterface *iface)
 {
+  iface->can_open = ide_workbench_addin_real_can_open;
+  iface->get_id = ide_workbench_addin_real_get_id;
   iface->load = ide_workbench_addin_real_load;
   iface->unload = ide_workbench_addin_real_unload;
-  iface->can_open = ide_workbench_addin_real_can_open;
 }
 
 /**
@@ -161,3 +168,23 @@ ide_workbench_addin_open_finish (IdeWorkbenchAddin  *self,
 
   return IDE_WORKBENCH_ADDIN_GET_IFACE (self)->open_finish (self, result, error);
 }
+
+/**
+ * ide_workbench_addin_get_id:
+ * @self: An #IdeWorkbenchAddin.
+ *
+ * Gets the identifier for this workbench addin. By default this is the
+ * name of the classes GType (such as "MyObject").
+ *
+ * This can be used as the hint to various open operations in IdeWorkbench
+ * to prefer a given loader.
+ *
+ * Returns: (transfer full): a newly allocated string.
+ */
+gchar *
+ide_workbench_addin_get_id (IdeWorkbenchAddin *self)
+{
+  g_return_val_if_fail (IDE_IS_WORKBENCH_ADDIN (self), NULL);
+
+  return IDE_WORKBENCH_ADDIN_GET_IFACE (self)->get_id (self);
+}
diff --git a/libide/ide-workbench-addin.h b/libide/ide-workbench-addin.h
index 95471c2..8803b9f 100644
--- a/libide/ide-workbench-addin.h
+++ b/libide/ide-workbench-addin.h
@@ -32,43 +32,45 @@ struct _IdeWorkbenchAddinInterface
 {
   GTypeInterface parent;
 
-  void     (*load)        (IdeWorkbenchAddin    *self,
-                           IdeWorkbench         *workbench);
-  void     (*unload)      (IdeWorkbenchAddin    *self,
-                           IdeWorkbench         *workbench);
-  gboolean (*can_open)    (IdeWorkbenchAddin    *self,
-                           IdeUri               *uri,
-                           const gchar          *content_type,
-                           gint                 *priority);
-  void     (*open_async)  (IdeWorkbenchAddin    *self,
-                           IdeUri               *uri,
-                           const gchar          *content_type,
-                           GCancellable         *cancellable,
-                           GAsyncReadyCallback   callback,
-                           gpointer              user_data);
-  gboolean (*open_finish) (IdeWorkbenchAddin    *self,
-                           GAsyncResult         *result,
-                           GError              **error);
+  gchar    *(*get_id)      (IdeWorkbenchAddin    *self);
+  void      (*load)        (IdeWorkbenchAddin    *self,
+                            IdeWorkbench         *workbench);
+  void      (*unload)      (IdeWorkbenchAddin    *self,
+                            IdeWorkbench         *workbench);
+  gboolean  (*can_open)    (IdeWorkbenchAddin    *self,
+                            IdeUri               *uri,
+                            const gchar          *content_type,
+                            gint                 *priority);
+  void      (*open_async)  (IdeWorkbenchAddin    *self,
+                            IdeUri               *uri,
+                            const gchar          *content_type,
+                            GCancellable         *cancellable,
+                            GAsyncReadyCallback   callback,
+                            gpointer              user_data);
+  gboolean  (*open_finish) (IdeWorkbenchAddin    *self,
+                            GAsyncResult         *result,
+                            GError              **error);
 
 };
 
-void     ide_workbench_addin_load        (IdeWorkbenchAddin    *self,
-                                          IdeWorkbench         *workbench);
-void     ide_workbench_addin_unload      (IdeWorkbenchAddin    *self,
-                                          IdeWorkbench         *workbench);
-gboolean ide_workbench_addin_can_open    (IdeWorkbenchAddin    *self,
-                                          IdeUri               *uri,
-                                          const gchar          *content_type,
-                                          gint                 *priority);
-void     ide_workbench_addin_open_async  (IdeWorkbenchAddin    *self,
-                                          IdeUri               *uri,
-                                          const gchar          *content_type,
-                                          GCancellable         *cancellable,
-                                          GAsyncReadyCallback   callback,
-                                          gpointer              user_data);
-gboolean ide_workbench_addin_open_finish (IdeWorkbenchAddin    *self,
-                                          GAsyncResult         *result,
-                                          GError              **error);
+gchar    *ide_workbench_addin_get_id      (IdeWorkbenchAddin    *self);
+void      ide_workbench_addin_load        (IdeWorkbenchAddin    *self,
+                                           IdeWorkbench         *workbench);
+void      ide_workbench_addin_unload      (IdeWorkbenchAddin    *self,
+                                           IdeWorkbench         *workbench);
+gboolean  ide_workbench_addin_can_open    (IdeWorkbenchAddin    *self,
+                                           IdeUri               *uri,
+                                           const gchar          *content_type,
+                                           gint                 *priority);
+void      ide_workbench_addin_open_async  (IdeWorkbenchAddin    *self,
+                                           IdeUri               *uri,
+                                           const gchar          *content_type,
+                                           GCancellable         *cancellable,
+                                           GAsyncReadyCallback   callback,
+                                           gpointer              user_data);
+gboolean  ide_workbench_addin_open_finish (IdeWorkbenchAddin    *self,
+                                           GAsyncResult         *result,
+                                           GError              **error);
 
 G_END_DECLS
 
diff --git a/libide/ide-workbench-open.c b/libide/ide-workbench-open.c
index e9608e0..eb4e21b 100644
--- a/libide/ide-workbench-open.c
+++ b/libide/ide-workbench-open.c
@@ -36,6 +36,7 @@ typedef struct
   IdeUri       *uri;
   GArray       *loaders;
   gchar        *content_type;
+  gchar        *hint;
   guint         did_collect : 1;
 } IdeWorkbenchOpenUriState;
 
@@ -77,10 +78,30 @@ ide_workbench_collect_loaders (PeasExtensionSet *set,
 
 static gint
 ide_workbench_loader_compare (gconstpointer a,
-                              gconstpointer b)
+                              gconstpointer b,
+                              gpointer      user_data)
 {
   const IdeWorkbenchLoader *loadera = a;
   const IdeWorkbenchLoader *loaderb = b;
+  const gchar *hint = user_data;
+
+  if (hint != NULL)
+    {
+      gboolean match;
+      gchar *name;
+
+      name = ide_workbench_addin_get_id (loadera->addin);
+      match = g_strcmp0 (hint, name);
+      g_free (name);
+      if (match)
+        return -1;
+
+      name = ide_workbench_addin_get_id (loaderb->addin);
+      match = g_strcmp0 (hint, name);
+      g_free (name);
+      if (match)
+        return 1;
+    }
 
   return loadera->priority - loaderb->priority;
 }
@@ -102,6 +123,7 @@ ide_workbench_open_uri_state_free (gpointer data)
   g_clear_pointer (&open_uri_state->loaders, g_array_unref);
   g_clear_pointer (&open_uri_state->uri, ide_uri_unref);
   g_clear_pointer (&open_uri_state->content_type, g_free);
+  g_clear_pointer (&open_uri_state->hint, g_free);
   g_free (open_uri_state);
 }
 
@@ -143,7 +165,9 @@ ide_workbench_open_uri_try_next (IdeWorkbenchOpenUriState *open_uri_state)
       peas_extension_set_foreach (open_uri_state->self->addins,
                                   ide_workbench_collect_loaders,
                                   open_uri_state);
-      g_array_sort (open_uri_state->loaders, ide_workbench_loader_compare);
+      g_array_sort_with_data (open_uri_state->loaders,
+                              ide_workbench_loader_compare,
+                              open_uri_state->hint);
     }
 
   if (open_uri_state->loaders->len == 0)
@@ -230,6 +254,7 @@ ide_workbench_open_discover_content_type (IdeWorkbenchOpenUriState *open_uri_sta
 void
 ide_workbench_open_uri_async (IdeWorkbench        *self,
                               IdeUri              *uri,
+                              const gchar         *hint,
                               GCancellable        *cancellable,
                               GAsyncReadyCallback  callback,
                               gpointer             user_data)
@@ -246,6 +271,7 @@ ide_workbench_open_uri_async (IdeWorkbench        *self,
   open_uri_state->content_type = NULL;
   open_uri_state->loaders = g_array_new (FALSE, FALSE, sizeof (IdeWorkbenchLoader));
   open_uri_state->task = g_task_new (self, cancellable, callback, user_data);
+  open_uri_state->hint = g_strdup (hint);
 
   g_array_set_clear_func (open_uri_state->loaders,
                           ide_workbench_loader_destroy);
@@ -312,6 +338,7 @@ void
 ide_workbench_open_files_async (IdeWorkbench         *self,
                                 GFile               **files,
                                 guint                 n_files,
+                                const gchar          *hint,
                                 GCancellable         *cancellable,
                                 GAsyncReadyCallback   callback,
                                 gpointer              user_data)
@@ -349,6 +376,7 @@ ide_workbench_open_files_async (IdeWorkbench         *self,
       uri = ide_uri_new_from_file (files [i]);
       ide_workbench_open_uri_async (self,
                                     uri,
+                                    hint,
                                     cancellable,
                                     ide_workbench_open_files_cb,
                                     open_files_state);
diff --git a/libide/ide-workbench.h b/libide/ide-workbench.h
index f75d1ab..d0c311d 100644
--- a/libide/ide-workbench.h
+++ b/libide/ide-workbench.h
@@ -33,6 +33,7 @@ G_DECLARE_FINAL_TYPE (IdeWorkbench, ide_workbench, IDE, WORKBENCH, GtkApplicatio
 
 void            ide_workbench_open_uri_async               (IdeWorkbench         *self,
                                                             IdeUri               *uri,
+                                                            const gchar          *hint,
                                                             GCancellable         *cancellable,
                                                             GAsyncReadyCallback   callback,
                                                             gpointer              user_data);
@@ -42,6 +43,7 @@ gboolean        ide_workbench_open_uri_finish              (IdeWorkbench
 void            ide_workbench_open_files_async             (IdeWorkbench         *self,
                                                             GFile               **files,
                                                             guint                 n_files,
+                                                            const gchar          *hint,
                                                             GCancellable         *cancellable,
                                                             GAsyncReadyCallback   callback,
                                                             gpointer              user_data);
diff --git a/plugins/command-bar/gb-vim.c b/plugins/command-bar/gb-vim.c
index ea85330..91bfaae 100644
--- a/plugins/command-bar/gb-vim.c
+++ b/plugins/command-bar/gb-vim.c
@@ -428,7 +428,7 @@ gb_vim_command_edit (GtkSourceView  *source_view,
   else
     file = g_file_get_child (workdir, options);
 
-  ide_workbench_open_files_async (workbench, &file, 1, NULL, NULL, NULL);
+  ide_workbench_open_files_async (workbench, &file, 1, "editor", NULL, NULL, NULL);
 
   g_clear_object (&file);
 


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