[gnome-builder] libide/editor: add page.editor.format action



commit f15da1cc9e9a0067ede6e4fdf8061df04c9c884d
Author: Christian Hergert <chergert redhat com>
Date:   Fri Jul 29 12:11:01 2022 -0700

    libide/editor: add page.editor.format action
    
    This action really belongs on an editor page rather than on the editorui
    workspace addin since it applies to the specific page.
    
    Additionally, namespace to page.editor.print the former page.print action.

 src/libide/editor/gtk/menus.ui      |  1 +
 src/libide/editor/ide-editor-page.c | 64 ++++++++++++++++++++++++++++++++++++-
 src/plugins/editorui/gtk/menus.ui   |  8 ++++-
 3 files changed, 71 insertions(+), 2 deletions(-)
---
diff --git a/src/libide/editor/gtk/menus.ui b/src/libide/editor/gtk/menus.ui
index 63d0fe820..0f526cc7c 100644
--- a/src/libide/editor/gtk/menus.ui
+++ b/src/libide/editor/gtk/menus.ui
@@ -63,6 +63,7 @@
     <section id="ide-editor-page-reveal-section"/>
     <section id="ide-editor-page-search-section"/>
     <section id="ide-editor-page-save-section"/>
+    <section id="ide-editor-page-format-section"/>
     <section id="ide-editor-page-print-section"/>
   </menu>
 </interface>
diff --git a/src/libide/editor/ide-editor-page.c b/src/libide/editor/ide-editor-page.c
index 2479eca32..9747c9a39 100644
--- a/src/libide/editor/ide-editor-page.c
+++ b/src/libide/editor/ide-editor-page.c
@@ -462,6 +462,67 @@ print_action (GtkWidget  *widget,
   handle_print_result (self, GTK_PRINT_OPERATION (operation), result);
 }
 
+static void
+format_selection_cb (GObject      *object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
+{
+  IdeBuffer *buffer = (IdeBuffer *)object;
+  g_autoptr(IdeEditorPage) self = user_data;
+  g_autoptr(GError) error = NULL;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_BUFFER (buffer));
+  g_assert (G_IS_ASYNC_RESULT (result));
+  g_assert (IDE_IS_EDITOR_PAGE (self));
+
+  if (!ide_buffer_format_selection_finish (buffer, result, &error))
+    ide_page_report_error (IDE_PAGE (self),
+                           /* translators: %s contains the error message */
+                           _("Failed to format selection: %s"),
+                           error->message);
+
+  gtk_text_view_set_editable (GTK_TEXT_VIEW (self->view), TRUE);
+
+  IDE_EXIT;
+}
+
+static void
+format_action (GtkWidget  *widget,
+               const char *action_name,
+               GVariant   *param)
+{
+  IdeEditorPage *self = (IdeEditorPage *)widget;
+  g_autoptr(IdeFormatterOptions) options = NULL;
+  gboolean insert_spaces_instead_of_tabs;
+  guint tab_width;
+
+  IDE_ENTRY;
+
+  g_assert (IDE_IS_EDITOR_PAGE (self));
+
+  g_object_get (self->view,
+                "tab-width", &tab_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 (self->view), FALSE);
+
+  ide_buffer_format_selection_async (self->buffer,
+                                     options,
+                                     NULL,
+                                     format_selection_cb,
+                                     g_object_ref (self));
+
+  IDE_EXIT;
+}
+
 static void
 ide_editor_page_constructed (GObject *object)
 {
@@ -616,7 +677,8 @@ ide_editor_page_class_init (IdeEditorPageClass *klass)
   panel_widget_class_install_action (panel_widget_class, "search.hide", NULL, search_hide_action);
   panel_widget_class_install_action (panel_widget_class, "search.begin-find", NULL, 
search_begin_find_action);
   panel_widget_class_install_action (panel_widget_class, "search.begin-replace", NULL, 
search_begin_replace_action);
-  panel_widget_class_install_action (panel_widget_class, "print", NULL, print_action);
+  panel_widget_class_install_action (panel_widget_class, "editor.print", NULL, print_action);
+  panel_widget_class_install_action (panel_widget_class, "editor.format", NULL, format_action);
 
   g_type_ensure (IDE_TYPE_EDITOR_SEARCH_BAR);
 }
diff --git a/src/plugins/editorui/gtk/menus.ui b/src/plugins/editorui/gtk/menus.ui
index 4dbfc1528..8ee37b37f 100644
--- a/src/plugins/editorui/gtk/menus.ui
+++ b/src/plugins/editorui/gtk/menus.ui
@@ -173,10 +173,16 @@
         <attribute name="accel">&lt;ctrl&gt;h</attribute>
       </item>
     </section>
+    <section id="ide-editor-page-format-section">
+      <item>
+        <attribute name="label" translatable="yes">Format Selection</attribute>
+        <attribute name="action">page.editor.format</attribute>
+      </item>
+    </section>
     <section id="ide-editor-page-print-section">
       <item>
         <attribute name="label" translatable="yes">Print…</attribute>
-        <attribute name="action">page.print</attribute>
+        <attribute name="action">page.editor.print</attribute>
         <attribute name="accel">&lt;control&gt;p</attribute>
       </item>
     </section>


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