[gnome-text-editor] settings: add toggle for automatic indentation



commit cd221365c96788cb648d6ea49e2388856ac88b2c
Author: Christian Hergert <chergert redhat com>
Date:   Fri Oct 8 20:14:29 2021 -0700

    settings: add toggle for automatic indentation
    
    Fixes #166

 data/org.gnome.TextEditor.gschema.xml |  5 +++++
 src/editor-page-gsettings.c           |  2 ++
 src/editor-page-settings-provider.c   | 26 ++++++++++++++++++++++++--
 src/editor-page-settings-provider.h   |  4 ++++
 src/editor-page-settings.c            | 23 +++++++++++++++++++++++
 src/editor-page-settings.h            |  1 +
 src/editor-page.c                     |  3 +++
 src/editor-window-actions.c           |  3 ++-
 src/menus.ui                          |  4 ++++
 9 files changed, 68 insertions(+), 3 deletions(-)
---
diff --git a/data/org.gnome.TextEditor.gschema.xml b/data/org.gnome.TextEditor.gschema.xml
index 2126dad..1282ed1 100644
--- a/data/org.gnome.TextEditor.gschema.xml
+++ b/data/org.gnome.TextEditor.gschema.xml
@@ -25,6 +25,11 @@
       <summary>Indentation Style</summary>
       <description>If the editor should insert multiple spaces characters instead of tabs.</description>
     </key>
+    <key name="auto-indent" type="b">
+      <default>true</default>
+      <summary>Auto Indent</summary>
+      <description>Automatically indent new lines copying the previous line's indentation.</description>
+    </key>
     <key name="tab-width" type="u">
       <default>8</default>
       <summary>Tab Width</summary>
diff --git a/src/editor-page-gsettings.c b/src/editor-page-gsettings.c
index e3bc2e9..7a4934b 100644
--- a/src/editor-page-gsettings.c
+++ b/src/editor-page-gsettings.c
@@ -46,6 +46,7 @@ GSETTINGS_GETTER (gboolean, boolean, show_right_margin, "show-right-margin")
 GSETTINGS_GETTER (gboolean, boolean, show_line_numbers, "show-line-numbers")
 GSETTINGS_GETTER (gboolean, boolean, use_system_font, "use-system-font")
 GSETTINGS_GETTER (gboolean, boolean, wrap_text, "wrap-text")
+GSETTINGS_GETTER (gboolean, boolean, auto_indent, "auto-indent")
 GSETTINGS_GETTER (gboolean, boolean, show_map, "show-map")
 GSETTINGS_GETTER (gboolean, boolean, show_grid, "show-grid")
 GSETTINGS_GETTER (gboolean, boolean, highlight_current_line, "highlight-current-line")
@@ -173,6 +174,7 @@ page_settings_provider_iface_init (EditorPageSettingsProviderInterface *iface)
   iface->get_tab_width = editor_page_gsettings_get_tab_width;
   iface->get_use_system_font = editor_page_gsettings_get_use_system_font;
   iface->get_wrap_text = editor_page_gsettings_get_wrap_text;
+  iface->get_auto_indent = editor_page_gsettings_get_auto_indent;
   iface->get_style_scheme = editor_page_gsettings_get_style_scheme;
   iface->get_style_variant = editor_page_gsettings_get_style_variant;
   iface->get_highlight_current_line = editor_page_gsettings_get_highlight_current_line;
diff --git a/src/editor-page-settings-provider.c b/src/editor-page-settings-provider.c
index c990fd8..acb347f 100644
--- a/src/editor-page-settings-provider.c
+++ b/src/editor-page-settings-provider.c
@@ -215,7 +215,7 @@ editor_page_settings_provider_get_show_right_margin (EditorPageSettingsProvider
 /**
  * editor_page_settings_provider_get_use_system_font:
  * @self: a #EditorPageSettingsProvider
- * @wrap_text: (out) (optional): if the system font should be used
+ * @use_system_font: (out) (optional): if the system font should be used
  *
  * Returns: %TRUE if @use_system_font was set by the provider.
  */
@@ -235,7 +235,7 @@ editor_page_settings_provider_get_use_system_font (EditorPageSettingsProvider *s
 }
 
 /**
- * editor_page_settings_provider_get_tab_width:
+ * editor_page_settings_provider_get_wrap_text:
  * @self: a #EditorPageSettingsProvider
  * @wrap_text: (out) (optional): if the text should wrap
  *
@@ -256,6 +256,28 @@ editor_page_settings_provider_get_wrap_text (EditorPageSettingsProvider *self,
   return FALSE;
 }
 
+/**
+ * editor_page_settings_provider_get_auto_indent:
+ * @self: a #EditorPageSettingsProvider
+ * @auto_indent: (out) (optional): if auto-indent should be enabled
+ *
+ * Returns: %TRUE if @auto_indent was set by the provider.
+ */
+gboolean
+editor_page_settings_provider_get_auto_indent (EditorPageSettingsProvider *self,
+                                               gboolean                   *auto_indent)
+{
+  g_return_val_if_fail (EDITOR_IS_PAGE_SETTINGS_PROVIDER (self), FALSE);
+
+  if (auto_indent != NULL)
+    *auto_indent = FALSE;
+
+  if (EDITOR_PAGE_SETTINGS_PROVIDER_GET_IFACE (self)->get_auto_indent)
+    return EDITOR_PAGE_SETTINGS_PROVIDER_GET_IFACE (self)->get_auto_indent (self, auto_indent);
+
+  return FALSE;
+}
+
 /**
  * editor_page_settings_provider_get_tab_width:
  * @self: a #EditorPageSettingsProvider
diff --git a/src/editor-page-settings-provider.h b/src/editor-page-settings-provider.h
index 18becfb..78a2a4d 100644
--- a/src/editor-page-settings-provider.h
+++ b/src/editor-page-settings-provider.h
@@ -61,6 +61,8 @@ struct _EditorPageSettingsProviderInterface
                                                   gboolean                   *show_grid);
   gboolean  (*get_highlight_current_line)        (EditorPageSettingsProvider *self,
                                                   gboolean                   *highlight_current_line);
+  gboolean  (*get_auto_indent)                   (EditorPageSettingsProvider *self,
+                                                  gboolean                   *auto_indent);
 };
 
 void     editor_page_settings_provider_emit_changed                      (EditorPageSettingsProvider  *self);
@@ -84,6 +86,8 @@ gboolean editor_page_settings_provider_get_show_right_margin             (Editor
                                                                           gboolean                    
*show_right_margin);
 gboolean editor_page_settings_provider_get_use_system_font               (EditorPageSettingsProvider  *self,
                                                                           gboolean                    
*use_system_font);
+gboolean editor_page_settings_provider_get_auto_indent                   (EditorPageSettingsProvider  *self,
+                                                                          gboolean                    
*auto_indent);
 gboolean editor_page_settings_provider_get_wrap_text                     (EditorPageSettingsProvider  *self,
                                                                           gboolean                    
*wrap_text);
 gboolean editor_page_settings_provider_get_tab_width                     (EditorPageSettingsProvider  *self,
diff --git a/src/editor-page-settings.c b/src/editor-page-settings.c
index 0ebb93f..3db6c85 100644
--- a/src/editor-page-settings.c
+++ b/src/editor-page-settings.c
@@ -54,10 +54,12 @@ struct _EditorPageSettings
   guint show_right_margin : 1;
   guint use_system_font : 1;
   guint wrap_text : 1;
+  guint auto_indent : 1;
 };
 
 enum {
   PROP_0,
+  PROP_AUTO_INDENT,
   PROP_CUSTOM_FONT,
   PROP_STYLE_VARIANT,
   PROP_DOCUMENT,
@@ -132,6 +134,7 @@ editor_page_settings_update (EditorPageSettings *self)
   UPDATE_SETTING (gboolean, highlight_current_line, HIGHLIGHT_CURRENT_LINE, cmp_boolean, (void), (gboolean));
   UPDATE_SETTING (gboolean, use_system_font, USE_SYSTEM_FONT, cmp_boolean, (void), (gboolean));
   UPDATE_SETTING (gboolean, wrap_text, WRAP_TEXT, cmp_boolean, (void), (gboolean));
+  UPDATE_SETTING (gboolean, auto_indent, AUTO_INDENT, cmp_boolean, (void), (gboolean));
   UPDATE_SETTING (guint, tab_width, TAB_WIDTH, cmp_uint, (void), (guint));
   UPDATE_SETTING (guint, right_margin_position, RIGHT_MARGIN_POSITION, cmp_uint, (void), (guint));
   UPDATE_SETTING (g_autofree gchar *, custom_font, CUSTOM_FONT, cmp_string, g_free, g_strdup);
@@ -304,6 +307,10 @@ editor_page_settings_get_property (GObject    *object,
       g_value_set_boolean (value, editor_page_settings_get_wrap_text (self));
       break;
 
+    case PROP_AUTO_INDENT:
+      g_value_set_boolean (value, editor_page_settings_get_auto_indent (self));
+      break;
+
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     }
@@ -492,6 +499,13 @@ editor_page_settings_class_init (EditorPageSettingsClass *klass)
                           FALSE,
                           (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
 
+  properties [PROP_AUTO_INDENT] =
+    g_param_spec_boolean ("auto-indent",
+                          "Auto Indent",
+                          "Automatically indent new lines by copying the previous line's indentation.",
+                          TRUE,
+                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
   g_object_class_install_properties (object_class, N_PROPS, properties);
 }
 
@@ -499,6 +513,7 @@ static void
 editor_page_settings_init (EditorPageSettings *self)
 {
   self->providers = g_ptr_array_new_with_free_func (g_object_unref);
+  self->auto_indent = TRUE;
   self->use_system_font = TRUE;
   self->right_margin_position = 80;
   self->tab_width = 8;
@@ -599,6 +614,14 @@ editor_page_settings_get_wrap_text (EditorPageSettings *self)
   return self->wrap_text;
 }
 
+gboolean
+editor_page_settings_get_auto_indent (EditorPageSettings *self)
+{
+  g_return_val_if_fail (EDITOR_IS_PAGE_SETTINGS (self), FALSE);
+
+  return self->auto_indent;
+}
+
 guint
 editor_page_settings_get_right_margin_position (EditorPageSettings *self)
 {
diff --git a/src/editor-page-settings.h b/src/editor-page-settings.h
index 29b1ad9..8096bc2 100644
--- a/src/editor-page-settings.h
+++ b/src/editor-page-settings.h
@@ -34,6 +34,7 @@ const gchar        *editor_page_settings_get_style_scheme                  (Edit
 const gchar        *editor_page_settings_get_style_variant                 (EditorPageSettings *self);
 gboolean            editor_page_settings_get_insert_spaces_instead_of_tabs (EditorPageSettings *self);
 guint               editor_page_settings_get_right_margin_position         (EditorPageSettings *self);
+gboolean            editor_page_settings_get_auto_indent                   (EditorPageSettings *self);
 gboolean            editor_page_settings_get_show_line_numbers             (EditorPageSettings *self);
 gboolean            editor_page_settings_get_show_grid                     (EditorPageSettings *self);
 gboolean            editor_page_settings_get_show_map                      (EditorPageSettings *self);
diff --git a/src/editor-page.c b/src/editor-page.c
index 038c8c4..a132898 100644
--- a/src/editor-page.c
+++ b/src/editor-page.c
@@ -314,6 +314,9 @@ editor_page_constructed (GObject *object)
   editor_binding_group_bind (self->settings_bindings, "tab-width",
                              self->view, "tab-width",
                              G_BINDING_SYNC_CREATE);
+  editor_binding_group_bind (self->settings_bindings, "auto-indent",
+                             self->view, "auto-indent",
+                             G_BINDING_SYNC_CREATE);
   editor_binding_group_bind_full (self->settings_bindings, "wrap-text",
                                   self->view, "wrap-mode",
                                   G_BINDING_SYNC_CREATE,
diff --git a/src/editor-window-actions.c b/src/editor-window-actions.c
index 8185bb3..77184b5 100644
--- a/src/editor-window-actions.c
+++ b/src/editor-window-actions.c
@@ -644,12 +644,13 @@ _editor_window_actions_init (EditorWindow *self)
   g_autoptr(GSimpleActionGroup) group = NULL;
   g_autoptr(GSettings) settings = NULL;
   static const gchar *setting_keys[] = {
-    "style-variant",
+    "auto-indent",
     "discover-settings",
     "indent-style",
     "show-line-numbers",
     "show-right-margin",
     "spellcheck",
+    "style-variant",
     "tab-width",
     "wrap-text",
   };
diff --git a/src/menus.ui b/src/menus.ui
index 7046efc..34de1cb 100644
--- a/src/menus.ui
+++ b/src/menus.ui
@@ -86,6 +86,10 @@
     </section>
     <section>
       <attribute name="label" translatable="yes">Indentation</attribute>
+      <item>
+        <attribute name="label" translatable="yes">_Automatic Indentation</attribute>
+        <attribute name="action">settings.auto-indent</attribute>
+      </item>
       <item>
         <attribute name="label" translatable="yes">_Tabs</attribute>
         <attribute name="action">settings.indent-style</attribute>


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