[gnome-builder/wip/chergert/grep: 10/10] grep: work on applying changes to buffer



commit c535df84c779ae7e5169e01a5ad3bac2a137a2ea
Author: Christian Hergert <chergert redhat com>
Date:   Mon Oct 29 17:46:18 2018 -0700

    grep: work on applying changes to buffer

 src/plugins/grep/gbp-grep-model.c  | 41 +++++++++++++++++++++++-
 src/plugins/grep/gbp-grep-panel.c  | 65 +++++++++++++++++++++++++++++++++++---
 src/plugins/grep/gbp-grep-panel.ui |  2 +-
 3 files changed, 102 insertions(+), 6 deletions(-)
---
diff --git a/src/plugins/grep/gbp-grep-model.c b/src/plugins/grep/gbp-grep-model.c
index ba672a597..80642e90e 100644
--- a/src/plugins/grep/gbp-grep-model.c
+++ b/src/plugins/grep/gbp-grep-model.c
@@ -1082,14 +1082,47 @@ create_edits_cb (GbpGrepModel *self,
                  gpointer      user_data)
 {
   GPtrArray *edits = user_data;
+  GbpGrepModelLine line = {0};
   const gchar *row;
 
   g_assert (GBP_IS_GREP_MODEL (self));
+  g_assert (self->message_regex != NULL);
   g_assert (edits != NULL);
 
   row = g_ptr_array_index (self->index->rows, index_);
 
-  //g_print ("ROW: %s\n", row);
+  if (gbp_grep_model_line_parse (&line, row, self->message_regex))
+    {
+      g_autoptr(IdeFile) file = NULL;
+      IdeContext *context;
+
+      context = ide_object_get_context (IDE_OBJECT (self));
+      g_assert (IDE_IS_CONTEXT (context));
+
+      file = ide_file_new_for_path (context, line.path);
+      g_assert (IDE_IS_FILE (file));
+
+      for (guint i = 0; i < line.matches->len; i++)
+        {
+          const GbpGrepModelMatch *match = &g_array_index (line.matches, GbpGrepModelMatch, i);
+          g_autoptr(IdeProjectEdit) edit = NULL;
+          g_autoptr(IdeSourceRange) range = NULL;
+          g_autoptr(IdeSourceLocation) begin = NULL;
+          g_autoptr(IdeSourceLocation) end = NULL;
+
+          begin = ide_source_location_new (file, line.line, match->match_begin, 0);
+          end = ide_source_location_new (file, line.line, match->match_end, 0);
+          range = ide_source_range_new (begin, end);
+
+          edit = g_object_new (IDE_TYPE_PROJECT_EDIT,
+                               "range", range,
+                               NULL);
+
+          g_ptr_array_add (edits, g_steal_pointer (&edit));
+        }
+    }
+
+  clear_line (&line);
 }
 
 /**
@@ -1105,6 +1138,12 @@ gbp_grep_model_create_edits (GbpGrepModel *self)
 
   g_return_val_if_fail (GBP_IS_GREP_MODEL (self), NULL);
 
+  if (self->message_regex == NULL)
+    {
+      if (!gbp_grep_model_rebuild_regex (self))
+        return NULL;
+    }
+
   edits = g_ptr_array_new_with_free_func (g_object_unref);
   gbp_grep_model_foreach_selected (self, create_edits_cb, edits);
 
diff --git a/src/plugins/grep/gbp-grep-panel.c b/src/plugins/grep/gbp-grep-panel.c
index 4b4cfc1fa..9c9d3c77d 100644
--- a/src/plugins/grep/gbp-grep-panel.c
+++ b/src/plugins/grep/gbp-grep-panel.c
@@ -36,6 +36,7 @@ struct _GbpGrepPanel
   GtkCheckButton    *check;
   GtkButton         *close_button;
   GtkButton         *replace_button;
+  GtkEntry          *replace_entry;
 };
 
 enum {
@@ -277,13 +278,68 @@ gbp_grep_panel_toggle_all_cb (GbpGrepPanel      *self,
 }
 
 static void
-gbp_grep_panel_replace_cb (GbpGrepPanel *self,
-                           GtkButton    *button)
+gbp_grep_panel_replace_edited_cb (GObject      *object,
+                                  GAsyncResult *result,
+                                  gpointer      user_data)
 {
+  IdeBufferManager *bufmgr = (IdeBufferManager *)object;
+  g_autoptr(GbpGrepPanel) self = user_data;
+  g_autoptr(GError) error = NULL;
+
+  g_assert (IDE_IS_BUFFER_MANAGER (bufmgr));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (GBP_IS_GREP_PANEL (self));
+
+  if (!ide_buffer_manager_apply_edits_finish (bufmgr, result, &error))
+    {
+      ide_object_warning (IDE_OBJECT (bufmgr), "Failed to apply edits: %s", error->message);
+      return;
+    }
+
+  /* Make the treeview visible, but show the old content. Allows the user
+   * to jump to the positions that were edited.
+   */
+  gtk_widget_set_sensitive (GTK_WIDGET (self->tree_view), TRUE);
+}
+
+static void
+gbp_grep_panel_replace_clicked_cb (GbpGrepPanel *self,
+                                   GtkButton    *button)
+{
+  g_autoptr(GPtrArray) edits = NULL;
+  IdeBufferManager *bufmgr;
+  const gchar *text;
+  IdeContext *context;
+
   g_assert (GBP_IS_GREP_PANEL (self));
   g_assert (GTK_IS_BUTTON (button));
 
-  gbp_grep_model_create_edits (GBP_GREP_MODEL (gtk_tree_view_get_model (self->tree_view)));
+  edits = gbp_grep_model_create_edits (GBP_GREP_MODEL (gtk_tree_view_get_model (self->tree_view)));
+  if (edits == NULL || edits->len == 0)
+    return;
+
+  text = gtk_entry_get_text (self->replace_entry);
+
+  for (guint i = 0; i < edits->len; i++)
+    {
+      IdeProjectEdit *edit = g_ptr_array_index (edits, i);
+      ide_project_edit_set_replacement (edit, text);
+    }
+
+  g_debug ("Replacing %u edit points with %s", edits->len, text);
+
+  gtk_widget_set_sensitive (GTK_WIDGET (self->tree_view), FALSE);
+  gtk_widget_set_sensitive (GTK_WIDGET (self->replace_button), FALSE);
+  gtk_widget_set_sensitive (GTK_WIDGET (self->replace_entry), FALSE);
+
+  context = ide_widget_get_context (GTK_WIDGET (self));
+  bufmgr = ide_context_get_buffer_manager (context);
+
+  ide_buffer_manager_apply_edits_async (bufmgr,
+                                        IDE_PTR_ARRAY_STEAL_FULL (&edits),
+                                        NULL,
+                                        gbp_grep_panel_replace_edited_cb,
+                                        g_object_ref (self));
 }
 
 static void
@@ -344,6 +400,7 @@ gbp_grep_panel_class_init (GbpGrepPanelClass *klass)
   gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/builder/plugins/grep/gbp-grep-panel.ui");
   gtk_widget_class_bind_template_child (widget_class, GbpGrepPanel, close_button);
   gtk_widget_class_bind_template_child (widget_class, GbpGrepPanel, replace_button);
+  gtk_widget_class_bind_template_child (widget_class, GbpGrepPanel, replace_entry);
   gtk_widget_class_bind_template_child (widget_class, GbpGrepPanel, tree_view);
 }
 
@@ -363,7 +420,7 @@ gbp_grep_panel_init (GbpGrepPanel *self)
 
   g_signal_connect_object (self->replace_button,
                            "clicked",
-                           G_CALLBACK (gbp_grep_panel_replace_cb),
+                           G_CALLBACK (gbp_grep_panel_replace_clicked_cb),
                            self,
                            G_CONNECT_SWAPPED);
 
diff --git a/src/plugins/grep/gbp-grep-panel.ui b/src/plugins/grep/gbp-grep-panel.ui
index 3e579e460..a275ef646 100644
--- a/src/plugins/grep/gbp-grep-panel.ui
+++ b/src/plugins/grep/gbp-grep-panel.ui
@@ -46,7 +46,7 @@
                   </object>
                 </child>
                 <child type="center">
-                  <object class="GtkEntry">
+                  <object class="GtkEntry" id="replace_entry">
                     <property name="visible">true</property>
                     <property name="width-chars">30</property>
                   </object>


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