[gnome-builder/wip/chergert/grep] grep: fix directory/recursive tracking



commit 7808176d4d3f5cf8508a0fed9d4d22666b2147d0
Author: Christian Hergert <chergert redhat com>
Date:   Tue Oct 30 14:03:08 2018 -0700

    grep: fix directory/recursive tracking

 src/plugins/grep/gbp-grep-model.c              | 40 ++++++++++++++++++++++++--
 src/plugins/grep/gbp-grep-model.h              |  2 ++
 src/plugins/grep/gbp-grep-panel.c              |  5 +---
 src/plugins/grep/gbp-grep-popover.c            | 19 +++++++++++-
 src/plugins/grep/gbp-grep-project-tree-addin.c | 19 +++++++++---
 5 files changed, 74 insertions(+), 11 deletions(-)
---
diff --git a/src/plugins/grep/gbp-grep-model.c b/src/plugins/grep/gbp-grep-model.c
index dd278d26a..f4b120f45 100644
--- a/src/plugins/grep/gbp-grep-model.c
+++ b/src/plugins/grep/gbp-grep-model.c
@@ -72,6 +72,7 @@ struct _GbpGrepModel
   guint recursive : 1;
   guint case_sensitive : 1;
   guint at_word_boundaries : 1;
+  guint was_directory : 1;
 };
 
 static void tree_model_iface_init (GtkTreeModelIface *iface);
@@ -569,7 +570,6 @@ gbp_grep_model_create_launcher (GbpGrepModel *self)
     path = g_file_peek_path (workdir);
 
   launcher = ide_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE);
-  ide_subprocess_launcher_set_cwd (launcher, path);
 
   /*
    * Soft runtime check for Git support, so that we can use "git grep"
@@ -643,6 +643,22 @@ gbp_grep_model_create_launcher (GbpGrepModel *self)
       ide_subprocess_launcher_push_argv (launcher, "^.{0,256}$");
     }
 
+  if (g_file_test (path, G_FILE_TEST_IS_DIR))
+    {
+      ide_subprocess_launcher_set_cwd (launcher, path);
+      self->was_directory = TRUE;
+    }
+  else
+    {
+      g_autofree gchar *parent = g_path_get_dirname (path);
+      g_autofree gchar *name = g_path_get_basename (path);
+
+      self->was_directory = FALSE;
+
+      ide_subprocess_launcher_set_cwd (launcher, parent);
+      ide_subprocess_launcher_push_argv (launcher, name);
+    }
+
   return g_steal_pointer (&launcher);
 }
 
@@ -1109,7 +1125,7 @@ create_edits_cb (GbpGrepModel *self,
       context = ide_object_get_context (IDE_OBJECT (self));
       g_assert (IDE_IS_CONTEXT (context));
 
-      gfile = g_file_get_child (self->directory, line.path);
+      gfile = gbp_grep_model_get_file (self, line.path);
       g_assert (G_IS_FILE (gfile));
 
       file = ide_file_new (context, gfile);
@@ -1209,3 +1225,23 @@ gbp_grep_model_get_line (GbpGrepModel            *self,
 
   *line = &self->prev_line;
 }
+
+/**
+ * gbp_grep_model_get_file:
+ *
+ * Returns: (transfer full): a #GFile
+ */
+GFile *
+gbp_grep_model_get_file (GbpGrepModel *self,
+                         const gchar  *path)
+{
+  g_return_val_if_fail (GBP_IS_GREP_MODEL (self), NULL);
+
+  if (!path || !*path || g_strcmp0 (path, ".") == 0)
+    return g_file_dup (self->directory);
+
+  if (self->was_directory)
+    return g_file_get_child (self->directory, path);
+  else
+    return g_file_dup (self->directory);
+}
diff --git a/src/plugins/grep/gbp-grep-model.h b/src/plugins/grep/gbp-grep-model.h
index 8c18147d3..de81a8885 100644
--- a/src/plugins/grep/gbp-grep-model.h
+++ b/src/plugins/grep/gbp-grep-model.h
@@ -73,6 +73,8 @@ void          gbp_grep_model_toggle_row             (GbpGrepModel            *se
 void          gbp_grep_model_get_line               (GbpGrepModel            *self,
                                                      GtkTreeIter             *iter,
                                                      const GbpGrepModelLine **line);
+GFile        *gbp_grep_model_get_file               (GbpGrepModel            *self,
+                                                     const gchar             *path);
 void          gbp_grep_model_scan_async             (GbpGrepModel            *self,
                                                      GCancellable            *cancellable,
                                                      GAsyncReadyCallback      callback,
diff --git a/src/plugins/grep/gbp-grep-panel.c b/src/plugins/grep/gbp-grep-panel.c
index a6994dd97..2f41ed33e 100644
--- a/src/plugins/grep/gbp-grep-panel.c
+++ b/src/plugins/grep/gbp-grep-panel.c
@@ -210,9 +210,6 @@ gbp_grep_panel_row_activated_cb (GbpGrepPanel      *self,
       gtk_tree_model_get_iter (model, &iter, path))
     {
       const GbpGrepModelLine *line = NULL;
-      GFile *directory;
-
-      directory = gbp_grep_model_get_directory (GBP_GREP_MODEL (model));
 
       gbp_grep_model_get_line (GBP_GREP_MODEL (model), &iter, &line);
 
@@ -233,7 +230,7 @@ gbp_grep_panel_row_activated_cb (GbpGrepPanel      *self,
           if (lineno > 0)
             lineno--;
 
-          child = g_file_get_child (directory, line->path);
+          child = gbp_grep_model_get_file (GBP_GREP_MODEL (model), line->path);
           ichild = ide_file_new (context, child);
           location = ide_source_location_new (ichild, lineno, 0, 0);
 
diff --git a/src/plugins/grep/gbp-grep-popover.c b/src/plugins/grep/gbp-grep-popover.c
index a7837198f..56f61fa65 100644
--- a/src/plugins/grep/gbp-grep-popover.c
+++ b/src/plugins/grep/gbp-grep-popover.c
@@ -45,6 +45,7 @@ struct _GbpGrepPopover
 enum {
   PROP_0,
   PROP_FILE,
+  PROP_IS_DIRECTORY,
   N_PROPS
 };
 
@@ -107,7 +108,11 @@ gbp_grep_popover_button_clicked_cb (GbpGrepPopover *self,
   gbp_grep_model_set_at_word_boundaries (model, at_word_boundaries);
   gbp_grep_model_set_case_sensitive (model, case_sensitive);
   gbp_grep_model_set_query (model, gtk_entry_get_text (self->entry));
-  gbp_grep_model_set_recursive (model, recursive);
+
+  if (gtk_widget_get_visible (GTK_WIDGET (self->recursive_button)))
+    gbp_grep_model_set_recursive (model, recursive);
+  else
+    gbp_grep_model_set_recursive (model, FALSE);
 
   panel = gbp_grep_panel_new ();
   gtk_container_add (GTK_CONTAINER (utils), panel);
@@ -155,6 +160,10 @@ gbp_grep_popover_get_property (GObject    *object,
       g_value_set_object (value, self->file);
       break;
 
+    case PROP_IS_DIRECTORY:
+      g_value_set_boolean (value, gtk_widget_get_visible (GTK_WIDGET (self->recursive_button)));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -174,6 +183,10 @@ gbp_grep_popover_set_property (GObject      *object,
       g_set_object (&self->file, g_value_get_object (value));
       break;
 
+    case PROP_IS_DIRECTORY:
+      gtk_widget_set_visible (GTK_WIDGET (self->recursive_button), g_value_get_boolean (value));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -194,6 +207,10 @@ gbp_grep_popover_class_init (GbpGrepPopoverClass *klass)
                          G_TYPE_FILE,
                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_IS_DIRECTORY] =
+    g_param_spec_boolean ("is-directory", NULL, NULL, FALSE,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, N_PROPS, properties);
 
   gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/builder/plugins/grep/gbp-grep-popover.ui");
diff --git a/src/plugins/grep/gbp-grep-project-tree-addin.c b/src/plugins/grep/gbp-grep-project-tree-addin.c
index 0c1883e0e..465b56f49 100644
--- a/src/plugins/grep/gbp-grep-project-tree-addin.c
+++ b/src/plugins/grep/gbp-grep-project-tree-addin.c
@@ -39,12 +39,16 @@ struct _GbpGrepProjectTreeAddin
 };
 
 static GFile *
-get_file_for_node (DzlTreeNode *node)
+get_file_for_node (DzlTreeNode *node,
+                   gboolean    *is_dir)
 {
   GObject *item;
 
   g_return_val_if_fail (!node || DZL_IS_TREE_NODE (node), NULL);
 
+  if (is_dir)
+    *is_dir = FALSE;
+
   if (node == NULL)
     return NULL;
 
@@ -52,7 +56,11 @@ get_file_for_node (DzlTreeNode *node)
     return NULL;
 
   if (GB_IS_PROJECT_FILE (item))
-    return gb_project_file_get_file (GB_PROJECT_FILE (item));
+    {
+      if (is_dir)
+        *is_dir = gb_project_file_get_is_directory (GB_PROJECT_FILE (item));
+      return gb_project_file_get_file (GB_PROJECT_FILE (item));
+    }
 
   return NULL;
 }
@@ -83,17 +91,19 @@ find_in_files_action (GSimpleAction *action,
   GbpGrepProjectTreeAddin *self = user_data;
   DzlTreeNode *node;
   GFile *file;
+  gboolean is_dir = FALSE;
 
   g_assert (G_IS_SIMPLE_ACTION (action));
   g_assert (GBP_IS_GREP_PROJECT_TREE_ADDIN (self));
 
   if ((node = dzl_tree_get_selected (self->tree)) &&
-      (file = get_file_for_node (node)))
+      (file = get_file_for_node (node, &is_dir)))
     {
       GtkPopover *popover;
 
       popover = g_object_new (GBP_TYPE_GREP_POPOVER,
                               "file", file,
+                              "is-directory", is_dir,
                               "position", GTK_POS_RIGHT,
                               NULL);
       g_signal_connect_after (popover,
@@ -110,12 +120,13 @@ on_node_selected_cb (GbpGrepProjectTreeAddin *self,
                      DzlTreeBuilder          *builder)
 {
   GFile *file;
+  gboolean is_dir = FALSE;
 
   g_assert (GBP_IS_GREP_PROJECT_TREE_ADDIN (self));
   g_assert (!node || DZL_IS_TREE_NODE (node));
   g_assert (DZL_IS_TREE_BUILDER (builder));
 
-  file = get_file_for_node (node);
+  file = get_file_for_node (node, &is_dir);
 
   dzl_gtk_widget_action_set (GTK_WIDGET (self->tree), "grep", "find-in-files",
                              "enabled", (file != NULL),


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