[gnome-builder] libide/editor: rename format to format-selection



commit 30608d48eee75af3be2b44e2ecb637a35ca98ac7
Author: Christian Hergert <chergert redhat com>
Date:   Wed Sep 28 19:49:08 2022 -0700

    libide/editor: rename format to format-selection
    
    And add a format-document action for the alternative so that we can map
    these to seperate actions with the keyboard.

 src/libide/editor/ide-editor-page.c       | 41 +++++++++++++++++++++++++------
 src/plugins/editorui/gtk/keybindings.json |  3 ++-
 src/plugins/editorui/gtk/menus.ui         |  7 +++++-
 3 files changed, 42 insertions(+), 9 deletions(-)
---
diff --git a/src/libide/editor/ide-editor-page.c b/src/libide/editor/ide-editor-page.c
index d7bb561b3..17cb39460 100644
--- a/src/libide/editor/ide-editor-page.c
+++ b/src/libide/editor/ide-editor-page.c
@@ -98,21 +98,41 @@ ide_editor_page_notify_file_cb (IdeEditorPage *self,
                            g_object_ref (self));
 }
 
+static void
+ide_editor_page_update_actions (IdeEditorPage *self)
+{
+  IdeFormatter *formatter;
+  gboolean has_selection;
+
+  g_assert (IDE_IS_EDITOR_PAGE (self));
+
+  formatter = ide_buffer_get_formatter (self->buffer);
+  has_selection = gtk_text_buffer_get_has_selection (GTK_TEXT_BUFFER (self->buffer));
+
+  panel_widget_action_set_enabled (PANEL_WIDGET (self), "editor.format-document", formatter && 
!has_selection);
+  panel_widget_action_set_enabled (PANEL_WIDGET (self), "editor.format-selection", formatter && 
has_selection);
+}
+
 static void
 ide_editor_page_notify_formatter_cb (IdeEditorPage *self,
                                      GParamSpec    *pspec,
                                      IdeBuffer     *buffer)
 {
-  IdeFormatter *formatter;
-
   g_assert (IDE_IS_EDITOR_PAGE (self));
   g_assert (IDE_IS_BUFFER (buffer));
 
-  formatter = ide_buffer_get_formatter (buffer);
+  ide_editor_page_update_actions (self);
+}
 
-  panel_widget_action_set_enabled (PANEL_WIDGET (self),
-                                   "editor.format",
-                                   formatter != NULL);
+static void
+ide_editor_page_notify_has_selection_cb (IdeEditorPage *self,
+                                         GParamSpec    *pspec,
+                                         IdeBuffer     *buffer)
+{
+  g_assert (IDE_IS_EDITOR_PAGE (self));
+  g_assert (IDE_IS_BUFFER (buffer));
+
+  ide_editor_page_update_actions (self);
 }
 
 static void
@@ -186,6 +206,12 @@ ide_editor_page_set_buffer (IdeEditorPage *self,
                                self,
                                G_CONNECT_SWAPPED);
 
+      g_signal_connect_object (buffer,
+                               "notify::has-selection",
+                               G_CALLBACK (ide_editor_page_notify_has_selection_cb),
+                               self,
+                               G_CONNECT_SWAPPED);
+
       g_signal_connect_object (buffer,
                                "notify::file-settings",
                                G_CALLBACK (_ide_editor_page_settings_reload),
@@ -769,7 +795,8 @@ ide_editor_page_class_init (IdeEditorPageClass *klass)
   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, "editor.print", NULL, print_action);
-  panel_widget_class_install_action (panel_widget_class, "editor.format", NULL, format_action);
+  panel_widget_class_install_action (panel_widget_class, "editor.format-document", NULL, format_action);
+  panel_widget_class_install_action (panel_widget_class, "editor.format-selection", NULL, format_action);
   panel_widget_class_install_action (panel_widget_class, "editor.reload", NULL, reload_action);
 
   g_type_ensure (IDE_TYPE_EDITOR_INFO_BAR);
diff --git a/src/plugins/editorui/gtk/keybindings.json b/src/plugins/editorui/gtk/keybindings.json
index e296a95cb..a8b018e86 100644
--- a/src/plugins/editorui/gtk/keybindings.json
+++ b/src/plugins/editorui/gtk/keybindings.json
@@ -1,5 +1,6 @@
 { "trigger" : "<Control>i", "action" : "workspace.editorui.page.go-to-line", "when" : "canEdit()", "phase" : 
"capture" },
-{ "trigger" : "<Shift><Alt>f", "action": "page.editor.format", "when" : "canEdit()", "phase" : "capture" },
+{ "trigger" : "<Control><Shift><Alt>f", "action": "page.editor.format-document", "when" : "canEdit()", 
"phase" : "capture" },
+{ "trigger" : "<Shift><Alt>f", "action": "page.editor.format-selection", "when" : "canEdit()", "phase" : 
"capture" },
 { "trigger" : "<Control>f", "action" : "page.search.begin-find", "when" : "canEdit()", "phase" : "bubble" },
 { "trigger" : "<Control>h", "action" : "page.search.begin-replace", "when" : "canEdit()", "phase" : "bubble" 
},
 { "trigger" : "<Control>s", "action" : "page.save", "when" : "canEdit()", "phase" : "capture" },
diff --git a/src/plugins/editorui/gtk/menus.ui b/src/plugins/editorui/gtk/menus.ui
index bf23f1948..19d26c581 100644
--- a/src/plugins/editorui/gtk/menus.ui
+++ b/src/plugins/editorui/gtk/menus.ui
@@ -184,9 +184,14 @@
     </section>
   </menu>
   <menu id="ide-source-view-popup-menu-format-section">
+    <item>
+      <attribute name="label" translatable="yes">Format Document</attribute>
+      <attribute name="action">page.editor.format-document</attribute>
+      <attribute name="accel">&lt;ctrl&gt;&lt;shift&gt;&lt;alt&gt;f</attribute>
+    </item>
     <item>
       <attribute name="label" translatable="yes">Format Selection</attribute>
-      <attribute name="action">page.editor.format</attribute>
+      <attribute name="action">page.editor.format-selection</attribute>
       <attribute name="accel">&lt;shift&gt;&lt;alt&gt;f</attribute>
     </item>
   </menu>


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