[gnome-builder] project-tree: implement reveal GAction to reveal file in project-tree



commit e65915399ff8d7d15bcd8bca3b6f08f5558e085c
Author: Christian Hergert <chergert redhat com>
Date:   Thu Dec 3 22:28:53 2015 -0800

    project-tree: implement reveal GAction to reveal file in project-tree
    
    This needed to be future ported to the new perspecitves design. Now we
    have a "project-tree.reveal" action that can be activated from the
    editor view.

 plugins/project-tree/Makefile.am                   |    2 +
 plugins/project-tree/gb-project-tree-addin.c       |    2 +
 .../project-tree/gb-project-tree-editor-addin.c    |  119 ++++++++++++++++++++
 .../project-tree/gb-project-tree-editor-addin.h    |   32 +++++
 plugins/project-tree/gb-project-tree.c             |   95 +++++++++++++++-
 plugins/project-tree/gb-project-tree.h             |    2 +
 plugins/project-tree/project-tree-plugin.c         |    4 +
 7 files changed, 254 insertions(+), 2 deletions(-)
---
diff --git a/plugins/project-tree/Makefile.am b/plugins/project-tree/Makefile.am
index f407e49..b3bdc85 100644
--- a/plugins/project-tree/Makefile.am
+++ b/plugins/project-tree/Makefile.am
@@ -20,6 +20,8 @@ libproject_tree_plugin_la_SOURCES = \
        gb-project-tree-builder.h \
        gb-project-tree.c \
        gb-project-tree.h \
+       gb-project-tree-editor-addin.c \
+       gb-project-tree-editor-addin.h \
        gb-project-tree-private.h \
        gb-rename-file-popover.c \
        gb-rename-file-popover.h \
diff --git a/plugins/project-tree/gb-project-tree-addin.c b/plugins/project-tree/gb-project-tree-addin.c
index 236791a..2086e51 100644
--- a/plugins/project-tree/gb-project-tree-addin.c
+++ b/plugins/project-tree/gb-project-tree-addin.c
@@ -77,6 +77,8 @@ gb_project_tree_addin_load (IdeWorkbenchAddin *addin,
                             _("Project Tree"), "folder-symbolic");
 
   ide_widget_set_context_handler (self->tree, gb_project_tree_addin_context_set);
+
+  g_object_set_data (G_OBJECT (workbench), "GB_PROJECT_TREE", self->tree);
 }
 
 static void
diff --git a/plugins/project-tree/gb-project-tree-editor-addin.c 
b/plugins/project-tree/gb-project-tree-editor-addin.c
new file mode 100644
index 0000000..6bc532b
--- /dev/null
+++ b/plugins/project-tree/gb-project-tree-editor-addin.c
@@ -0,0 +1,119 @@
+/* gb-project-tree-editor-addin.c
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * 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/>.
+ */
+
+#include "gb-project-tree.h"
+#include "gb-project-tree-editor-addin.h"
+
+struct _GbProjectTreeEditorAddin
+{
+  GObject        parent_instance;
+
+  IdeEditorView *view;
+};
+
+static void editor_view_addin_iface_init (IdeEditorViewAddinInterface *iface);
+
+G_DEFINE_TYPE_EXTENDED (GbProjectTreeEditorAddin, gb_project_tree_editor_addin, G_TYPE_OBJECT, 0,
+                        G_IMPLEMENT_INTERFACE (IDE_TYPE_EDITOR_VIEW_ADDIN,
+                                               editor_view_addin_iface_init))
+
+static void
+gb_project_tree_editor_addin_class_init (GbProjectTreeEditorAddinClass *klass)
+{
+}
+
+static void
+gb_project_tree_editor_addin_init (GbProjectTreeEditorAddin *self)
+{
+}
+
+static void
+gb_project_tree_editor_addin_reveal (GSimpleAction *action,
+                                     GVariant      *variant,
+                                     gpointer       user_data)
+{
+  GbProjectTreeEditorAddin *self = user_data;
+  IdeWorkbench *workbench;
+  GbProjectTree *tree;
+  IdeBuffer *buffer;
+  IdeFile *ifile;
+  GFile *file;
+
+  g_assert (G_IS_SIMPLE_ACTION (action));
+  g_assert (GB_IS_PROJECT_TREE_EDITOR_ADDIN (self));
+
+  workbench = ide_widget_get_workbench (GTK_WIDGET (self->view));
+  g_assert (IDE_IS_WORKBENCH (workbench));
+
+  tree = g_object_get_data (G_OBJECT (workbench), "GB_PROJECT_TREE");
+  g_assert (GB_IS_PROJECT_TREE (tree));
+
+  buffer = ide_editor_view_get_document (self->view);
+  g_assert (IDE_IS_BUFFER (buffer));
+
+  ifile = ide_buffer_get_file (buffer);
+  g_assert (IDE_IS_FILE (ifile));
+
+  file = ide_file_get_file (ifile);
+  g_assert (!file || G_IS_FILE (file));
+
+  if (G_IS_FILE (file))
+    gb_project_tree_reveal (tree, file);
+}
+
+static void
+gb_project_tree_editor_addin_load (IdeEditorViewAddin *addin,
+                                   IdeEditorView      *view)
+{
+  GbProjectTreeEditorAddin *self = (GbProjectTreeEditorAddin *)addin;
+  GSimpleActionGroup *group;
+  static const GActionEntry entries[] = {
+    { "reveal", gb_project_tree_editor_addin_reveal },
+  };
+
+  g_assert (GB_IS_PROJECT_TREE_EDITOR_ADDIN (self));
+  g_assert (IDE_IS_EDITOR_VIEW (view));
+
+  self->view = view;
+
+  group = g_simple_action_group_new ();
+  g_action_map_add_action_entries (G_ACTION_MAP (group), entries, G_N_ELEMENTS (entries), self);
+  gtk_widget_insert_action_group (GTK_WIDGET (view), "project-tree", G_ACTION_GROUP (group));
+  g_object_unref (group);
+}
+
+static void
+gb_project_tree_editor_addin_unload (IdeEditorViewAddin *addin,
+                                     IdeEditorView      *view)
+{
+  GbProjectTreeEditorAddin *self = (GbProjectTreeEditorAddin *)addin;
+
+  g_assert (GB_IS_PROJECT_TREE_EDITOR_ADDIN (self));
+  g_assert (IDE_IS_EDITOR_VIEW (view));
+
+  gtk_widget_insert_action_group (GTK_WIDGET (view), "project-tree", NULL);
+
+  self->view = NULL;
+}
+
+static void
+editor_view_addin_iface_init (IdeEditorViewAddinInterface *iface)
+{
+  iface->load = gb_project_tree_editor_addin_load;
+  iface->unload = gb_project_tree_editor_addin_unload;
+}
diff --git a/plugins/project-tree/gb-project-tree-editor-addin.h 
b/plugins/project-tree/gb-project-tree-editor-addin.h
new file mode 100644
index 0000000..ad2bada
--- /dev/null
+++ b/plugins/project-tree/gb-project-tree-editor-addin.h
@@ -0,0 +1,32 @@
+/* gb-project-tree-editor-addin.h
+ *
+ * Copyright (C) 2015 Christian Hergert <chergert redhat com>
+ *
+ * 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_PROJECT_TREE_EDITOR_ADDIN_H
+#define GB_PROJECT_TREE_EDITOR_ADDIN_H
+
+#include <ide.h>
+
+G_BEGIN_DECLS
+
+#define GB_TYPE_PROJECT_TREE_EDITOR_ADDIN (gb_project_tree_editor_addin_get_type())
+
+G_DECLARE_FINAL_TYPE (GbProjectTreeEditorAddin, gb_project_tree_editor_addin, GB, PROJECT_TREE_EDITOR_ADDIN, 
GObject)
+
+G_END_DECLS
+
+#endif /* GB_PROJECT_TREE_EDITOR_ADDIN_H */
diff --git a/plugins/project-tree/gb-project-tree.c b/plugins/project-tree/gb-project-tree.c
index 2338f99..aa84e7d 100644
--- a/plugins/project-tree/gb-project-tree.c
+++ b/plugins/project-tree/gb-project-tree.c
@@ -20,6 +20,7 @@
 
 #include <glib/gi18n.h>
 
+#include "gb-project-file.h"
 #include "gb-project-tree.h"
 #include "gb-project-tree-actions.h"
 #include "gb-project-tree-builder.h"
@@ -51,8 +52,8 @@ gb_project_tree_get_context (GbProjectTree *self)
 
   if ((root = ide_tree_get_root (IDE_TREE (self))) &&
       (item = ide_tree_node_get_item (root)) &&
-      IDE_IS_OBJECT (item))
-    return ide_object_get_context (IDE_OBJECT (item));
+      IDE_IS_CONTEXT (item))
+    return IDE_CONTEXT (item);
 
   return NULL;
 }
@@ -212,3 +213,93 @@ gb_project_tree_set_show_ignored_files (GbProjectTree *self,
       ide_tree_rebuild (IDE_TREE (self));
     }
 }
+
+static gboolean
+find_child_node (IdeTree     *tree,
+                 IdeTreeNode *node,
+                 IdeTreeNode *child,
+                 gpointer    user_data)
+{
+  const gchar *name = user_data;
+  GObject *item;
+
+  g_assert (IDE_IS_TREE (tree));
+  g_assert (IDE_IS_TREE_NODE (node));
+  g_assert (IDE_IS_TREE_NODE (child));
+
+  item = ide_tree_node_get_item (child);
+
+  if (GB_IS_PROJECT_FILE (item))
+    {
+      const gchar *item_name;
+
+      item_name = gb_project_file_get_display_name (GB_PROJECT_FILE (item));
+
+      return ide_str_equal0 (item_name, name);
+    }
+
+  return FALSE;
+}
+
+static gboolean
+find_files_node (IdeTree     *tree,
+                 IdeTreeNode *node,
+                 IdeTreeNode *child,
+                 gpointer    user_data)
+{
+  GObject *item;
+
+  g_assert (IDE_IS_TREE (tree));
+  g_assert (IDE_IS_TREE_NODE (node));
+  g_assert (IDE_IS_TREE_NODE (child));
+
+  item = ide_tree_node_get_item (child);
+
+  return GB_IS_PROJECT_FILE (item);
+}
+
+void
+gb_project_tree_reveal (GbProjectTree *self,
+                        GFile         *file)
+{
+  g_autofree gchar *relpath = NULL;
+  g_auto(GStrv) parts = NULL;
+  IdeContext *context;
+  IdeTreeNode *node;
+  IdeVcs *vcs;
+  GFile *workdir;
+  guint i;
+
+  g_return_if_fail (GB_IS_PROJECT_TREE (self));
+  g_return_if_fail (G_IS_FILE (file));
+
+  context = gb_project_tree_get_context (self);
+  g_assert (IDE_IS_CONTEXT (context));
+
+  if (context == NULL)
+    return;
+
+  vcs = ide_context_get_vcs (context);
+  workdir = ide_vcs_get_working_directory (vcs);
+  relpath = g_file_get_relative_path (workdir, file);
+
+  if (relpath == NULL)
+    return;
+
+  node = ide_tree_find_child_node (IDE_TREE (self), NULL, find_files_node, NULL);
+  if (node == NULL)
+    return;
+
+  parts = g_strsplit (relpath, G_DIR_SEPARATOR_S, 0);
+
+  for (i = 0; parts [i]; i++)
+    {
+      node = ide_tree_find_child_node (IDE_TREE (self), node, find_child_node, parts [i]);
+      if (node == NULL)
+        return;
+    }
+
+  ide_tree_expand_to_node (IDE_TREE (self), node);
+  ide_tree_scroll_to_node (IDE_TREE (self), node);
+  ide_tree_node_select (node);
+}
diff --git a/plugins/project-tree/gb-project-tree.h b/plugins/project-tree/gb-project-tree.h
index ad492ba..4fd7b33 100644
--- a/plugins/project-tree/gb-project-tree.h
+++ b/plugins/project-tree/gb-project-tree.h
@@ -34,6 +34,8 @@ IdeContext *gb_project_tree_get_context            (GbProjectTree *self);
 gboolean    gb_project_tree_get_show_ignored_files (GbProjectTree *self);
 void        gb_project_tree_set_show_ignored_files (GbProjectTree *self,
                                                     gboolean       show_ignored_files);
+void        gb_project_tree_reveal                 (GbProjectTree *self,
+                                                    GFile         *file);
 
 G_END_DECLS
 
diff --git a/plugins/project-tree/project-tree-plugin.c b/plugins/project-tree/project-tree-plugin.c
index 4e8bbce..baefa26 100644
--- a/plugins/project-tree/project-tree-plugin.c
+++ b/plugins/project-tree/project-tree-plugin.c
@@ -20,6 +20,7 @@
 #include <ide.h>
 
 #include "gb-project-tree-addin.h"
+#include "gb-project-tree-editor-addin.h"
 
 void
 peas_register_types (PeasObjectModule *module)
@@ -27,4 +28,7 @@ peas_register_types (PeasObjectModule *module)
   peas_object_module_register_extension_type (module,
                                               IDE_TYPE_WORKBENCH_ADDIN,
                                               GB_TYPE_PROJECT_TREE_ADDIN);
+  peas_object_module_register_extension_type (module,
+                                              IDE_TYPE_EDITOR_VIEW_ADDIN,
+                                              GB_TYPE_PROJECT_TREE_EDITOR_ADDIN);
 }


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