[gnome-builder/albfan/grep-filters] grep: add path filters
- From: Alberto Fanjul <albfan src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/albfan/grep-filters] grep: add path filters
- Date: Sat, 3 Nov 2018 08:42:34 +0000 (UTC)
commit 928808114dd662dc407c193a5ee193cd8fcfea42
Author: Alberto Fanjul <albertofanjul gmail com>
Date: Sat Nov 3 08:53:43 2018 +0100
grep: add path filters
file filter allow to select filenames
ignore filter allow to ignore directories
src/plugins/grep/gbp-grep-model.c | 141 +++++++++++++++++++++++++++++++++++
src/plugins/grep/gbp-grep-model.h | 6 ++
src/plugins/grep/gbp-grep-popover.c | 6 ++
src/plugins/grep/gbp-grep-popover.ui | 37 ++++++---
4 files changed, 181 insertions(+), 9 deletions(-)
---
diff --git a/src/plugins/grep/gbp-grep-model.c b/src/plugins/grep/gbp-grep-model.c
index f4b120f45..4a2d0b443 100644
--- a/src/plugins/grep/gbp-grep-model.c
+++ b/src/plugins/grep/gbp-grep-model.c
@@ -42,6 +42,14 @@ struct _GbpGrepModel
*/
gchar *query;
+ /* The filter text, which we use to select filenames in result.
+ */
+ gchar *file_filter;
+
+ /* The filter text, which we use to ignore dirs in result.
+ */
+ gchar *ignore_filter;
+
/* We need to do client-side processing to extract the exact message
* locations after grep gives us the matching lines. This allows us to
* create IdeProjectEdit source ranges later as well as creating the
@@ -88,6 +96,8 @@ enum {
PROP_RECURSIVE,
PROP_USE_REGEX,
PROP_QUERY,
+ PROP_FILE_FILTER,
+ PROP_IGNORE_FILTER,
N_PROPS
};
@@ -262,6 +272,14 @@ gbp_grep_model_get_property (GObject *object,
g_value_set_string (value, gbp_grep_model_get_query (self));
break;
+ case PROP_FILE_FILTER:
+ g_value_set_string (value, gbp_grep_model_get_file_filter (self));
+ break;
+
+ case PROP_IGNORE_FILTER:
+ g_value_set_string (value, gbp_grep_model_get_ignore_filter (self));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -301,6 +319,14 @@ gbp_grep_model_set_property (GObject *object,
gbp_grep_model_set_query (self, g_value_get_string (value));
break;
+ case PROP_FILE_FILTER:
+ gbp_grep_model_set_file_filter (self, g_value_get_string (value));
+ break;
+
+ case PROP_IGNORE_FILTER:
+ gbp_grep_model_set_ignore_filter (self, g_value_get_string (value));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -344,6 +370,14 @@ gbp_grep_model_class_init (GbpGrepModelClass *klass)
g_param_spec_string ("query", NULL, NULL, NULL,
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+ properties [PROP_FILE_FILTER] =
+ g_param_spec_string ("file-filter", NULL, NULL, NULL,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
+ properties [PROP_IGNORE_FILTER] =
+ g_param_spec_string ("ignore-filter", NULL, NULL, NULL,
+ (G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS));
+
g_object_class_install_properties (object_class, N_PROPS, properties);
line_regex = g_regex_new ("([a-zA-Z0-9\\+\\-\\.\\/_]+):(\\d+):(.*)", 0, 0, NULL);
@@ -404,6 +438,22 @@ gbp_grep_model_get_query (GbpGrepModel *self)
return self->query;
}
+const gchar *
+gbp_grep_model_get_file_filter (GbpGrepModel *self)
+{
+ g_return_val_if_fail (GBP_IS_GREP_MODEL (self), NULL);
+
+ return self->file_filter;
+}
+
+const gchar *
+gbp_grep_model_get_ignore_filter (GbpGrepModel *self)
+{
+ g_return_val_if_fail (GBP_IS_GREP_MODEL (self), NULL);
+
+ return self->ignore_filter;
+}
+
void
gbp_grep_model_set_query (GbpGrepModel *self,
const gchar *query)
@@ -419,6 +469,36 @@ gbp_grep_model_set_query (GbpGrepModel *self,
}
}
+void
+gbp_grep_model_set_ignore_filter (GbpGrepModel *self,
+ const gchar *ignore_filter)
+{
+ g_return_if_fail (GBP_IS_GREP_MODEL (self));
+
+ if (g_strcmp0 (ignore_filter, self->ignore_filter) != 0)
+ {
+ g_free (self->ignore_filter);
+ self->ignore_filter = g_strdup (ignore_filter);
+ gbp_grep_model_clear_regex (self);
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_IGNORE_FILTER]);
+ }
+}
+
+void
+gbp_grep_model_set_file_filter (GbpGrepModel *self,
+ const gchar *file_filter)
+{
+ g_return_if_fail (GBP_IS_GREP_MODEL (self));
+
+ if (g_strcmp0 (file_filter, self->file_filter) != 0)
+ {
+ g_free (self->file_filter);
+ self->file_filter = g_strdup (file_filter);
+ gbp_grep_model_clear_regex (self);
+ g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_FILE_FILTER]);
+ }
+}
+
/**
* gbp_grep_model_get_directory:
* @self: a #GbpGrepModel
@@ -554,6 +634,9 @@ gbp_grep_model_create_launcher (GbpGrepModel *self)
GFile *workdir;
GType git_vcs;
gboolean use_git_grep = FALSE;
+ gchar **file_filters;
+ gchar **ignore_filters;
+ gsize i;
g_assert (GBP_IS_GREP_MODEL (self));
g_assert (self->query != NULL);
@@ -643,6 +726,64 @@ gbp_grep_model_create_launcher (GbpGrepModel *self)
ide_subprocess_launcher_push_argv (launcher, "^.{0,256}$");
}
+ if (self->file_filter != NULL)
+ {
+ if (use_git_grep)
+ {
+ ide_subprocess_launcher_push_argv (launcher, "--");
+ }
+
+ file_filters = g_strsplit (self->file_filter, ",", 0);
+
+ for (i = 0; file_filters [i]; i++)
+ {
+ gchar *file_filter = file_filters [i];
+ gchar *chomp_file_filter = g_strchomp(file_filter);
+
+ g_autofree gchar *file_filter_pattern;
+ if (use_git_grep)
+ {
+ file_filter_pattern = g_strconcat ("*/", chomp_file_filter, NULL);
+ ide_subprocess_launcher_push_argv (launcher, file_filter_pattern);
+ }
+ else
+ {
+ file_filter_pattern = g_strconcat ("*/", chomp_file_filter, NULL);
+ ide_subprocess_launcher_push_argv (launcher, "--include");
+ ide_subprocess_launcher_push_argv (launcher, file_filter_pattern);
+ }
+ }
+ }
+
+ if (self->ignore_filter != NULL)
+ {
+ if (use_git_grep && self->file_filter == NULL)
+ {
+ ide_subprocess_launcher_push_argv (launcher, "--");
+ }
+
+ ignore_filters = g_strsplit (self->ignore_filter, ",", 0);
+
+ for (i = 0; ignore_filters [i]; i++)
+ {
+ gchar *ignore_filter = ignore_filters [i];
+ gchar *chomp_ignore_filter = g_strchomp(ignore_filter);
+
+ g_autofree gchar *ignore_filter_pattern;
+ if (use_git_grep)
+ {
+ ignore_filter_pattern = g_strconcat (":(exclude)", chomp_ignore_filter, "/", NULL);
+ ide_subprocess_launcher_push_argv (launcher, ignore_filter_pattern);
+ }
+ else
+ {
+ ignore_filter_pattern = g_strconcat ("*/", chomp_ignore_filter, NULL);
+ ide_subprocess_launcher_push_argv (launcher, "--exclude-dir");
+ ide_subprocess_launcher_push_argv (launcher, ignore_filter_pattern);
+ }
+ }
+ }
+
if (g_file_test (path, G_FILE_TEST_IS_DIR))
{
ide_subprocess_launcher_set_cwd (launcher, path);
diff --git a/src/plugins/grep/gbp-grep-model.h b/src/plugins/grep/gbp-grep-model.h
index de81a8885..ce41d8a2d 100644
--- a/src/plugins/grep/gbp-grep-model.h
+++ b/src/plugins/grep/gbp-grep-model.h
@@ -64,6 +64,12 @@ void gbp_grep_model_set_at_word_boundaries (GbpGrepModel *se
const gchar *gbp_grep_model_get_query (GbpGrepModel *self);
void gbp_grep_model_set_query (GbpGrepModel *self,
const gchar *query);
+const gchar *gbp_grep_model_get_file_filter (GbpGrepModel *self);
+void gbp_grep_model_set_file_filter (GbpGrepModel *self,
+ const gchar *file_filter);
+const gchar *gbp_grep_model_get_ignore_filter (GbpGrepModel *self);
+void gbp_grep_model_set_ignore_filter (GbpGrepModel *self,
+ const gchar *ignore_filter);
GPtrArray *gbp_grep_model_create_edits (GbpGrepModel *self);
void gbp_grep_model_select_all (GbpGrepModel *self);
void gbp_grep_model_select_none (GbpGrepModel *self);
diff --git a/src/plugins/grep/gbp-grep-popover.c b/src/plugins/grep/gbp-grep-popover.c
index 56f61fa65..7ac56efb1 100644
--- a/src/plugins/grep/gbp-grep-popover.c
+++ b/src/plugins/grep/gbp-grep-popover.c
@@ -35,6 +35,8 @@ struct _GbpGrepPopover
GFile *file;
GtkEntry *entry;
+ GtkEntry *file_filter;
+ GtkEntry *ignore_filter;
GtkButton *button;
GtkCheckButton *regex_button;
GtkCheckButton *whole_button;
@@ -108,6 +110,8 @@ 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_file_filter (model, gtk_entry_get_text (self->file_filter));
+ gbp_grep_model_set_ignore_filter (model, gtk_entry_get_text (self->ignore_filter));
if (gtk_widget_get_visible (GTK_WIDGET (self->recursive_button)))
gbp_grep_model_set_recursive (model, recursive);
@@ -216,6 +220,8 @@ gbp_grep_popover_class_init (GbpGrepPopoverClass *klass)
gtk_widget_class_set_template_from_resource (widget_class,
"/org/gnome/builder/plugins/grep/gbp-grep-popover.ui");
gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, button);
gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, entry);
+ gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, file_filter);
+ gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, ignore_filter);
gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, regex_button);
gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, whole_button);
gtk_widget_class_bind_template_child (widget_class, GbpGrepPopover, case_button);
diff --git a/src/plugins/grep/gbp-grep-popover.ui b/src/plugins/grep/gbp-grep-popover.ui
index 2a8baa2c8..80b406183 100644
--- a/src/plugins/grep/gbp-grep-popover.ui
+++ b/src/plugins/grep/gbp-grep-popover.ui
@@ -19,10 +19,9 @@
</child>
<child>
<object class="GtkBox">
- <property name="margin-top">6</property>
- <property name="orientation">horizontal</property>
- <property name="spacing">6</property>
- <property name="visible">true</property>
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <property name="orientation">vertical</property>
<child>
<object class="GtkEntry" id="entry">
<property name="placeholder-text" translatable="yes">Search for…</property>
@@ -31,14 +30,34 @@
</object>
</child>
<child>
- <object class="GtkButton" id="button">
+ <object class="GtkEntry" id="file_filter">
+ <property name="placeholder-text" translatable="yes">File filter…</property>
+ <property name="width-chars">24</property>
+ <property name="visible">true</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkBox">
+ <property name="visible">True</property>
+ <property name="can_focus">False</property>
+ <child>
+ <object class="GtkEntry" id="ignore_filter">
+ <property name="placeholder-text" translatable="yes">Dirs to ignore…</property>
+ <property name="width-chars">24</property>
+ <property name="visible">true</property>
+ </object>
+ </child>
+ <child>
+ <object class="GtkButton" id="button">
<property name="hexpand">false</property>
<property name="halign">end</property>
- <property name="label" translatable="yes">Find</property>
+ <property name="label" translatable="yes">Find</property>
<property name="visible">true</property>
- <style>
- <class name="suggested-action"/>
- </style>
+ <style>
+ <class name="suggested-action"/>
+ </style>
+ </object>
+ </child>
</object>
</child>
</object>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]