From f383208dabb42a22b20d65189641f0fce590f227 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sat, 13 Aug 2022 12:48:26 +0100 Subject: [PATCH 1/4] EditorConfig plugin overrides global settings for indent width and style --- .editorconfig | 5 +-- plugins/editorconfig/editorconfig.vala | 22 +++++---- src/MainWindow.vala | 4 +- src/Widgets/FormatBar.vala | 62 +++++++++++++++++++------- 4 files changed, 62 insertions(+), 31 deletions(-) diff --git a/.editorconfig b/.editorconfig index 5efe9f4342..a2689e4f62 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,15 +1,12 @@ # EditorConfig root = true -# elementary defaults +# elementary defaults - global indent width and style are set in UI [*] charset = utf-8 end_of_line = lf -indent_size = tab -indent_style = space insert_final_newline = true max_line_length = 80 -tab_width = 4 [{*.xml,*.xml.in,*.yml}] tab_width = 2 diff --git a/plugins/editorconfig/editorconfig.vala b/plugins/editorconfig/editorconfig.vala index 7b08284aa5..79e99efb30 100644 --- a/plugins/editorconfig/editorconfig.vala +++ b/plugins/editorconfig/editorconfig.vala @@ -20,13 +20,19 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activatable { Scratch.Services.Interface plugins; public Object object { owned get; construct; } + private Code.FormatBar format_bar; public void update_state () { } public void activate () { plugins = (Scratch.Services.Interface) object; + plugins.hook_toolbar.connect ((tb) => { + format_bar = tb.format_bar; + }); + plugins.hook_document.connect ((d) => { + format_bar.tab_set_by_editor_config = false; Scratch.Widgets.SourceView view = d.source_view; File file = d.file; @@ -35,6 +41,7 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa } var handle = new EditorConfig.Handle (); + handle.set_conf_file_name (".editorconfig"); if (handle.parse (file.get_path ()) != 0) { return; } @@ -42,21 +49,18 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa for (int i = 0; i < handle.get_name_value_count (); i++) { string name, val; handle.get_name_value (i, out name, out val); - /* These are all properties (https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties) */ switch (name) { case "indent_style": - if (val == "space") { - view.set_insert_spaces_instead_of_tabs (true); - } else if (val == "tab") { - view.set_insert_spaces_instead_of_tabs (false); - } + format_bar.tab_set_by_editor_config = true; + var use_spaces = (val != "tab"); + format_bar.set_insert_spaces_instead_of_tabs (use_spaces); break; case "indent_size": - view.tab_width = int.parse (val); - break; case "tab_width": - view.tab_width = int.parse (val); + format_bar.tab_set_by_editor_config = true; + var indent_width = (int.parse (val)).clamp (2, 16); + format_bar.set_tab_width (indent_width); break; case "end_of_line": break; diff --git a/src/MainWindow.vala b/src/MainWindow.vala index fef536c599..186da87248 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -430,7 +430,6 @@ namespace Scratch { document_view.document_change.connect ((doc) => { if (doc != null) { - plugins.hook_document (doc); search_bar.set_text_view (doc.source_view); // Update MainWindow title title = doc.get_basename (); @@ -438,6 +437,9 @@ namespace Scratch { toolbar.set_document_focus (doc); folder_manager_view.select_path (doc.file.get_path ()); + // Must follow setting focus document for editorconfig plug + plugins.hook_document (doc); + // Set actions sensitive property Utils.action_from_group (ACTION_SAVE_AS, actions).set_enabled (doc.file != null); doc.check_undoable_actions (); diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index b713748443..c758305adc 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -28,6 +28,7 @@ public class Code.FormatBar : Gtk.Grid { private Gtk.SpinButton tab_width; private Gtk.Switch space_tab_switch; private Gtk.Switch autoindent_switch; + public bool tab_set_by_editor_config { get; set; default = false; } public FormatButton line_toggle; private Gtk.Entry goto_entry; @@ -41,7 +42,7 @@ public class Code.FormatBar : Gtk.Grid { tab_toggle = new FormatButton (); tab_toggle.icon = new ThemedIcon ("format-indent-more-symbolic"); - tab_toggle.tooltip_text = _("Tabs"); + bind_property ("tab-set-by-editor-config", tab_toggle, "sensitive", BindingFlags.INVERT_BOOLEAN); lang_toggle = new FormatButton (); lang_toggle.icon = new ThemedIcon ("application-x-class-file-symbolic"); @@ -150,11 +151,11 @@ public class Code.FormatBar : Gtk.Grid { Scratch.settings.bind ("auto-indent", autoindent_switch, "active", SettingsBindFlags.DEFAULT); tab_width = new Gtk.SpinButton.with_range (1, 24, 1); - Scratch.settings.bind ("indent-width", tab_width, "value", SettingsBindFlags.DEFAULT); + Scratch.settings.bind ("indent-width", tab_width, "value", SettingsBindFlags.GET); space_tab_switch = new Gtk.Switch (); space_tab_switch.halign = Gtk.Align.START; - Scratch.settings.bind ("spaces-instead-of-tabs", space_tab_switch, "active", SettingsBindFlags.DEFAULT); + Scratch.settings.bind ("spaces-instead-of-tabs", space_tab_switch, "active", SettingsBindFlags.GET); var tab_grid = new Gtk.Grid (); tab_grid.margin = 12; @@ -173,24 +174,21 @@ public class Code.FormatBar : Gtk.Grid { tab_popover.add (tab_grid); tab_toggle.bind_property ("active", tab_popover, "visible", GLib.BindingFlags.BIDIRECTIONAL); - Scratch.settings.changed["indent-width"].connect (format_tab_header); - Scratch.settings.changed["spaces-instead-of-tabs"].connect (format_tab_header); + Scratch.settings.changed["indent-width"].connect (format_tab_header_from_global_settings); + Scratch.settings.changed["spaces-instead-of-tabs"].connect (format_tab_header_from_global_settings); } - private void format_tab_header () { + private void format_tab_header_from_global_settings () { + warning ("format_tab_header_from_global_settings"); + if (tab_set_by_editor_config) { + return; + } + var indent_width = Scratch.settings.get_int ("indent-width"); var spaces_instead_of_tabs = Scratch.settings.get_boolean ("spaces-instead-of-tabs"); - if (spaces_instead_of_tabs) { - tab_toggle.text = ngettext ("%d Space", "%d Spaces", indent_width).printf (indent_width); - } else { - tab_toggle.text = ngettext ("%d Tab", "%d Tabs", indent_width).printf (indent_width); - } - - if (doc != null) { - doc.source_view.tab_width = (uint)indent_width; - doc.source_view.insert_spaces_instead_of_tabs = spaces_instead_of_tabs; - } + set_tab_width (indent_width); + set_insert_spaces_instead_of_tabs (spaces_instead_of_tabs); } private void format_line_header () { @@ -241,11 +239,41 @@ public class Code.FormatBar : Gtk.Grid { } this.doc = doc; update_current_lang (); - format_tab_header (); + format_tab_header_from_global_settings (); format_line_header (); this.doc.source_view.buffer.notify["cursor-position"].connect (format_line_header); } + public void set_insert_spaces_instead_of_tabs (bool use_spaces) { + space_tab_switch.active = use_spaces; + if (doc != null) { + doc.source_view.insert_spaces_instead_of_tabs = use_spaces; + } + } + + public void set_tab_width (int indent_width) { + if (space_tab_switch.active) { + tab_toggle.text = "%d (Space)".printf (indent_width); + } else { + tab_toggle.text = "%d (Tab)".printf (indent_width); + } + + tab_toggle.text = "%d (%s)".printf ( + indent_width, space_tab_switch.active ? _("Using Space") : _("Using Tab")); + if (tab_set_by_editor_config) { + tab_toggle.tooltip_text = _("Indent width and style fixed by editorconfig file"); + } else { + tab_toggle.tooltip_text = _("Indent width using %s").printf ( + space_tab_switch.active ? _("Spaces") : _("Tabs") + ); + } + + if (doc != null) { + doc.source_view.indent_width = indent_width; + doc.source_view.tab_width = indent_width; + } + } + private void update_current_lang () { var language = doc.source_view.language; if (language != null) { From 260f8b0cdd6aa53bd3d86a8ecab184dbc1f17ae3 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Sat, 13 Aug 2022 16:47:26 +0100 Subject: [PATCH 2/4] Remove debug code --- src/Widgets/FormatBar.vala | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index c758305adc..c6d02fdf26 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -179,7 +179,6 @@ public class Code.FormatBar : Gtk.Grid { } private void format_tab_header_from_global_settings () { - warning ("format_tab_header_from_global_settings"); if (tab_set_by_editor_config) { return; } From 4a737df4cdc2dc92633a960ff2c1dc217fbbce5d Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 2 Sep 2022 11:11:07 +0100 Subject: [PATCH 3/4] Remove redundant code --- src/Widgets/FormatBar.vala | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index c6d02fdf26..73b14c2e3f 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -257,8 +257,6 @@ public class Code.FormatBar : Gtk.Grid { tab_toggle.text = "%d (Tab)".printf (indent_width); } - tab_toggle.text = "%d (%s)".printf ( - indent_width, space_tab_switch.active ? _("Using Space") : _("Using Tab")); if (tab_set_by_editor_config) { tab_toggle.tooltip_text = _("Indent width and style fixed by editorconfig file"); } else { From d2f11f4449075aa1e0647870e57eef41e2bd4d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danielle=20For=C3=A9?= Date: Wed, 11 Jan 2023 16:56:19 -0800 Subject: [PATCH 4/4] String changes --- src/Widgets/FormatBar.vala | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index 73b14c2e3f..ea06e1a0b0 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -252,17 +252,13 @@ public class Code.FormatBar : Gtk.Grid { public void set_tab_width (int indent_width) { if (space_tab_switch.active) { - tab_toggle.text = "%d (Space)".printf (indent_width); + tab_toggle.text = ngettext ("%d Space", "%d Spaces", indent_width).printf (indent_width); } else { - tab_toggle.text = "%d (Tab)".printf (indent_width); + tab_toggle.text = ngettext ("%d Tab", "%d Tabs", indent_width).printf (indent_width); } if (tab_set_by_editor_config) { - tab_toggle.tooltip_text = _("Indent width and style fixed by editorconfig file"); - } else { - tab_toggle.tooltip_text = _("Indent width using %s").printf ( - space_tab_switch.active ? _("Spaces") : _("Tabs") - ); + tab_toggle.tooltip_text = _("Indent width and style set by EditorConfig file"); } if (doc != null) {