[gnome-builder] project-files: add helper to locate a given GFile



commit 5e2f0bbb5f295baa6e2f5f7a665798ecaeb8c1e9
Author: Christian Hergert <christian hergert me>
Date:   Wed Apr 15 17:43:41 2015 -0700

    project-files: add helper to locate a given GFile

 libide/ide-project-files.c |   50 ++++++++++++++++++++++++++++++++++++++++++++
 libide/ide-project-files.h |   10 +++++---
 2 files changed, 56 insertions(+), 4 deletions(-)
---
diff --git a/libide/ide-project-files.c b/libide/ide-project-files.c
index be37d07..9238923 100644
--- a/libide/ide-project-files.c
+++ b/libide/ide-project-files.c
@@ -91,6 +91,56 @@ ide_project_files_find_child (IdeProjectItem *item,
 }
 
 /**
+ * ide_project_files_find_file:
+ * @self: (in): A #IdeProjectFiles.
+ * @file: A #GFile.
+ *
+ * Tries to locate an #IdeProjectFile matching the given file.
+ *
+ * Returns: (transfer none) (nullable): An #IdeProjectItem or %NULL.
+ */
+IdeProjectItem *
+ide_project_files_find_file (IdeProjectFiles *self,
+                             GFile           *file)
+{
+  IdeProjectItem *item;
+  IdeContext *context;
+  IdeVcs *vcs;
+  GFile *workdir;
+  gchar **parts;
+  gchar *path;
+  gsize i;
+
+  g_return_if_fail (IDE_IS_PROJECT_FILES (self));
+  g_return_if_fail (G_IS_FILE (file));
+
+  item = IDE_PROJECT_ITEM (self);
+  context = ide_object_get_context (IDE_OBJECT (self));
+  vcs = ide_context_get_vcs (context);
+  workdir = ide_vcs_get_working_directory (vcs);
+
+  if (g_file_equal (workdir, file))
+    return NULL;
+
+  path = g_file_get_relative_path (workdir, file);
+  if (path == NULL)
+    return NULL;
+
+  parts = g_strsplit (path, G_DIR_SEPARATOR_S, 0);
+
+  for (i = 0; parts [i]; i++)
+    {
+      if (!(item = ide_project_files_find_child (item, parts [i])))
+        break;
+    }
+
+  g_strfreev (parts);
+  g_free (path);
+
+  return item;
+}
+
+/**
  * ide_project_files_get_file_for_path:
  *
  * Retrieves an #IdeFile for the path. If no such path exists within the
diff --git a/libide/ide-project-files.h b/libide/ide-project-files.h
index d96269e..466ab3c 100644
--- a/libide/ide-project-files.h
+++ b/libide/ide-project-files.h
@@ -34,10 +34,12 @@ struct _IdeProjectFiles
   IdeProjectItem parent_instance;
 };
 
-IdeFile *ide_project_files_get_file_for_path (IdeProjectFiles *self,
-                                              const gchar     *path);
-void     ide_project_files_add_file          (IdeProjectFiles *self,
-                                              IdeProjectFile  *file);
+IdeFile        *ide_project_files_get_file_for_path (IdeProjectFiles *self,
+                                                     const gchar     *path);
+void            ide_project_files_add_file          (IdeProjectFiles *self,
+                                                     IdeProjectFile  *file);
+IdeProjectItem *ide_project_files_find_file         (IdeProjectFiles *self,
+                                                     GFile           *file);
 
 G_END_DECLS
 


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