[gnome-builder/wip/gtk4-port: 1314/1774] plugins/editorui: fixup formatting action




commit ee93f3ef16b2c97acfb7b291dfeedb05ee634c0c
Author: Christian Hergert <chergert redhat com>
Date:   Thu Jun 2 10:25:23 2022 -0700

    plugins/editorui: fixup formatting action
    
     * Release formatter options object when we're done with it
     * Cleanup assertions/formatting
     * Use ide_object_warning() so the user can get a message
     * Rename some functions to avoid short functions which can clash with
       libc/stdlib type stuff.
     * No need to fetch indent-width since we don't use it currently (although
       we should probably start doing that in formatters soon).

 .../editorui/gbp-editorui-workspace-addin.c        | 39 ++++++++++++----------
 1 file changed, 21 insertions(+), 18 deletions(-)
---
diff --git a/src/plugins/editorui/gbp-editorui-workspace-addin.c 
b/src/plugins/editorui/gbp-editorui-workspace-addin.c
index e4d7333e9..3f5839022 100644
--- a/src/plugins/editorui/gbp-editorui-workspace-addin.c
+++ b/src/plugins/editorui/gbp-editorui-workspace-addin.c
@@ -461,7 +461,7 @@ show_go_to_line (GSimpleAction *action,
 }
 
 static void
-format_cb (GObject      *object,
+format_selection_cb (GObject      *object,
                      GAsyncResult *result,
                      gpointer      user_data)
 {
@@ -471,13 +471,17 @@ format_cb (GObject      *object,
 
   IDE_ENTRY;
 
-  g_assert (IDE_IS_SOURCE_VIEW (view));
+  g_assert (IDE_IS_BUFFER (buffer));
   g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_SOURCE_VIEW (view));
 
   if (!ide_buffer_format_selection_finish (buffer, result, &error))
     {
-      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED))
-        g_warning ("%s", error->message);
+      IdeObjectBox *box = ide_object_box_from_object (G_OBJECT (buffer));
+
+      if (!ide_error_ignore (error))
+        /* translators: %s is replaced with the error message */
+        ide_object_warning (box, _("Format Selection Failed: %s"), error->message);
     }
 
   gtk_text_view_set_editable (GTK_TEXT_VIEW (view), TRUE);
@@ -486,40 +490,39 @@ format_cb (GObject      *object,
 }
 
 static void
-format (GSimpleAction *action,
-        GVariant      *param,
-        gpointer user_data)
+format_action (GSimpleAction *action,
+               GVariant      *param,
+               gpointer       user_data)
 {
   IdeSourceView *view = NULL;
   GbpEditoruiWorkspaceAddin *self = user_data;
 
   g_assert (G_IS_SIMPLE_ACTION (action));
 
-  if (self->page == NULL)
-      return;
-
   if ((view = ide_signal_group_get_target (self->view_signals)))
     {
-      IdeBuffer *buffer = NULL;
-      IdeFormatterOptions *options = NULL;
+      g_autoptr(IdeFormatterOptions) options = NULL;
+      IdeBuffer *buffer;
       gboolean insert_spaces_instead_of_tabs;
       guint tab_width;
-      int indent_width;
 
-      buffer = ide_editor_page_get_buffer (self->page);
-      options = ide_formatter_options_new ();
       g_object_get (view,
                     "tab-width", &tab_width,
-                    "indent-width", &indent_width,
                     "insert-spaces-instead-of-tabs", &insert_spaces_instead_of_tabs,
                     NULL);
+
+      options = ide_formatter_options_new ();
       ide_formatter_options_set_tab_width (options, tab_width);
       ide_formatter_options_set_insert_spaces (options, insert_spaces_instead_of_tabs);
+
+      /* Disable editing while we format */
       gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE);
+
+      buffer = ide_signal_group_get_target (self->buffer_signals);
       ide_buffer_format_selection_async (buffer,
                                          options,
                                          NULL,
-                                         format_cb,
+                                         format_selection_cb,
                                          g_object_ref (view));
     }
 }
@@ -530,7 +533,7 @@ static const GActionEntry actions[] = {
   { "new-file", new_file },
   { "new-workspace", new_workspace },
   { "show-go-to-line", show_go_to_line },
-  { "format", format},
+  { "format", format_action },
 };
 
 static void


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