From 4b5999937549dcd07bb58b088c92f5ae47bd752d Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 09:01:19 +0100 Subject: [PATCH 1/5] Sync global settings, appmenu, format bar, tab popover --- plugins/editorconfig/editorconfig.vala | 7 ++-- src/Widgets/FormatBar.vala | 53 +++++++++++++++++--------- src/Widgets/SourceView.vala | 4 +- 3 files changed, 41 insertions(+), 23 deletions(-) diff --git a/plugins/editorconfig/editorconfig.vala b/plugins/editorconfig/editorconfig.vala index 79e99efb30..a58a6c5928 100644 --- a/plugins/editorconfig/editorconfig.vala +++ b/plugins/editorconfig/editorconfig.vala @@ -32,7 +32,8 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa }); plugins.hook_document.connect ((d) => { - format_bar.tab_set_by_editor_config = false; + format_bar.tab_style_set_by_editor_config = false; + format_bar.tab_width_set_by_editor_config = false; Scratch.Widgets.SourceView view = d.source_view; File file = d.file; @@ -52,13 +53,13 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa /* These are all properties (https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties) */ switch (name) { case "indent_style": - format_bar.tab_set_by_editor_config = true; + format_bar.tab_style_set_by_editor_config = true; var use_spaces = (val != "tab"); format_bar.set_insert_spaces_instead_of_tabs (use_spaces); break; case "indent_size": case "tab_width": - format_bar.tab_set_by_editor_config = true; + format_bar.tab_width_set_by_editor_config = true; var indent_width = (int.parse (val)).clamp (2, 16); format_bar.set_tab_width (indent_width); break; diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index 04c3ca0fb6..c998fc8a38 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -18,8 +18,12 @@ */ public class Code.FormatBar : Gtk.Box { - public bool tab_set_by_editor_config { get; set; default = false; } + public bool tab_style_set_by_editor_config { get; set; default = false; } + public bool tab_width_set_by_editor_config { get; set; default = false; } public FormatButton line_menubutton { get; private set;} + public Gtk.InfoBar editorconfig_infobar { get; set construct; } + public Gtk.Box tab_box { get; set construct; } + public Gtk.SpinButton width_spinbutton { get; set construct; } private FormatButton lang_menubutton; private FormatButton tab_menubutton; @@ -137,7 +141,7 @@ public class Code.FormatBar : Gtk.Box { } private void create_tabulation_popover () { - var editorconfig_infobar = new Gtk.InfoBar () { + editorconfig_infobar = new Gtk.InfoBar () { margin_top = 9, margin_end = 9, margin_start = 9 @@ -154,15 +158,15 @@ public class Code.FormatBar : Gtk.Box { hexpand = true }; - var tab_width = new Gtk.SpinButton.with_range (1, 24, 1); + width_spinbutton = new Gtk.SpinButton.with_range (1, 24, 1); - var tab_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12) { + tab_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12) { margin_top = 6, margin_end = 12, margin_start = 12, }; tab_box.add (width_label); - tab_box.add (tab_width); + tab_box.add (width_spinbutton); var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0) { margin_bottom = 12 @@ -180,27 +184,38 @@ public class Code.FormatBar : Gtk.Box { tab_menubutton.popover = tab_popover; - Scratch.settings.bind ("auto-indent", autoindent_modelbutton, "active", SettingsBindFlags.DEFAULT); - Scratch.settings.bind ("indent-width", tab_width, "value", SettingsBindFlags.GET); - Scratch.settings.bind ("spaces-instead-of-tabs", space_tab_modelbutton, "active", SettingsBindFlags.GET); 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); + Scratch.settings.bind ("auto-indent", autoindent_modelbutton, "active", SettingsBindFlags.DEFAULT); + + format_tab_header_from_global_settings (); + width_spinbutton.value_changed.connect (() => { + Scratch.settings.set_int ( + "indent-width", + ((int)width_spinbutton.@value).clamp (2, 16) + ); + }); - bind_property ("tab-set-by-editor-config", editorconfig_infobar, "revealed", BindingFlags.SYNC_CREATE); - bind_property ("tab-set-by-editor-config", space_tab_modelbutton, "sensitive", BindingFlags.INVERT_BOOLEAN | BindingFlags.SYNC_CREATE); - bind_property ("tab-set-by-editor-config", tab_box, "sensitive", BindingFlags.INVERT_BOOLEAN | BindingFlags.SYNC_CREATE); + space_tab_modelbutton.clicked.connect (() => { + Scratch.settings.set_boolean ( + "spaces-instead-of-tabs", + space_tab_modelbutton.active + ); + }); } + private void format_tab_header_from_global_settings () { - if (tab_set_by_editor_config) { - return; + if (!tab_style_set_by_editor_config) { + set_insert_spaces_instead_of_tabs (Scratch.settings.get_boolean ("spaces-instead-of-tabs")); + } + if (!tab_width_set_by_editor_config) { + set_tab_width (Scratch.settings.get_int ("indent-width")); } - var indent_width = Scratch.settings.get_int ("indent-width"); - var spaces_instead_of_tabs = Scratch.settings.get_boolean ("spaces-instead-of-tabs"); - - set_tab_width (indent_width); - set_insert_spaces_instead_of_tabs (spaces_instead_of_tabs); + editorconfig_infobar.revealed = tab_style_set_by_editor_config || tab_width_set_by_editor_config; + space_tab_modelbutton.sensitive = !tab_style_set_by_editor_config; + tab_box.sensitive = !tab_width_set_by_editor_config; } private void format_line_header () { @@ -258,6 +273,7 @@ public class Code.FormatBar : Gtk.Box { } public void set_insert_spaces_instead_of_tabs (bool use_spaces) { + space_tab_modelbutton.active = use_spaces; if (doc != null) { doc.source_view.insert_spaces_instead_of_tabs = use_spaces; @@ -265,6 +281,7 @@ public class Code.FormatBar : Gtk.Box { } public void set_tab_width (int indent_width) { + width_spinbutton.@value = indent_width; if (space_tab_modelbutton.active) { tab_menubutton.text = ngettext ("%d Space", "%d Spaces", indent_width).printf (indent_width); } else { diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index 349d3007c5..990c75f648 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -241,9 +241,9 @@ namespace Scratch.Widgets { auto_indent = Scratch.settings.get_boolean ("auto-indent"); show_right_margin = Scratch.settings.get_boolean ("show-right-margin"); right_margin_position = Scratch.settings.get_int ("right-margin-position"); + insert_spaces_instead_of_tabs = Scratch.settings.get_boolean ("spaces-instead-of-tabs"); var source_buffer = (Gtk.SourceBuffer) buffer; source_buffer.highlight_matching_brackets = Scratch.settings.get_boolean ("highlight-matching-brackets"); - switch ((ScratchDrawSpacesState) Scratch.settings.get_enum ("draw-spaces")) { case ScratchDrawSpacesState.ALWAYS: space_drawer.set_types_for_locations ( @@ -271,7 +271,7 @@ namespace Scratch.Widgets { update_draw_spaces (); - insert_spaces_instead_of_tabs = Scratch.settings.get_boolean ("spaces-instead-of-tabs"); + tab_width = (uint) Scratch.settings.get_int ("indent-width"); if (Scratch.settings.get_boolean ("line-wrap")) { set_wrap_mode (Gtk.WrapMode.WORD); From 71dd64e9f71330dc949e1159b8d43fe92b817b45 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 10:21:49 +0100 Subject: [PATCH 2/5] Fix switching docs with editorconfig plugin on --- plugins/editorconfig/editorconfig.vala | 6 ++++++ src/Widgets/FormatBar.vala | 20 ++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/plugins/editorconfig/editorconfig.vala b/plugins/editorconfig/editorconfig.vala index a58a6c5928..8a5b8f4285 100644 --- a/plugins/editorconfig/editorconfig.vala +++ b/plugins/editorconfig/editorconfig.vala @@ -32,8 +32,11 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa }); plugins.hook_document.connect ((d) => { + // Ensure use global settings by default format_bar.tab_style_set_by_editor_config = false; format_bar.tab_width_set_by_editor_config = false; + format_bar.set_document (d); + Scratch.Widgets.SourceView view = d.source_view; File file = d.file; @@ -74,6 +77,9 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa case "max_line_length": view.right_margin_position = int.parse (val); break; + default: + warning ("unrecognised name/value %s/%s", name, val); + break; } } }); diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index c998fc8a38..3a771e9bf8 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -190,17 +190,21 @@ public class Code.FormatBar : Gtk.Box { format_tab_header_from_global_settings (); width_spinbutton.value_changed.connect (() => { - Scratch.settings.set_int ( - "indent-width", - ((int)width_spinbutton.@value).clamp (2, 16) - ); + if (!tab_width_set_by_editor_config) { + Scratch.settings.set_int ( + "indent-width", + ((int)width_spinbutton.@value).clamp (2, 16) + ); + } }); space_tab_modelbutton.clicked.connect (() => { - Scratch.settings.set_boolean ( - "spaces-instead-of-tabs", - space_tab_modelbutton.active - ); + if (!tab_style_set_by_editor_config) { + Scratch.settings.set_boolean ( + "spaces-instead-of-tabs", + space_tab_modelbutton.active + ); + } }); } From 46fd562a6d5da71c46b0c623c16926cc127cd23e Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 10:30:45 +0100 Subject: [PATCH 3/5] Fix space_drawer update --- src/Widgets/SourceView.vala | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Widgets/SourceView.vala b/src/Widgets/SourceView.vala index 990c75f648..ef67e56d1d 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -244,6 +244,7 @@ namespace Scratch.Widgets { insert_spaces_instead_of_tabs = Scratch.settings.get_boolean ("spaces-instead-of-tabs"); var source_buffer = (Gtk.SourceBuffer) buffer; source_buffer.highlight_matching_brackets = Scratch.settings.get_boolean ("highlight-matching-brackets"); + space_drawer.enable_matrix = false; switch ((ScratchDrawSpacesState) Scratch.settings.get_enum ("draw-spaces")) { case ScratchDrawSpacesState.ALWAYS: space_drawer.set_types_for_locations ( @@ -269,6 +270,7 @@ namespace Scratch.Widgets { break; } + space_drawer.enable_matrix = true; update_draw_spaces (); From 24ef7b3f57051c40428d89bceeb80adbd912b2fa Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Fri, 7 Jul 2023 10:40:36 +0100 Subject: [PATCH 4/5] Fix whitespace --- src/Widgets/FormatBar.vala | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index 3a771e9bf8..33ed28aa43 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -208,11 +208,11 @@ public class Code.FormatBar : Gtk.Box { }); } - private void format_tab_header_from_global_settings () { if (!tab_style_set_by_editor_config) { set_insert_spaces_instead_of_tabs (Scratch.settings.get_boolean ("spaces-instead-of-tabs")); } + if (!tab_width_set_by_editor_config) { set_tab_width (Scratch.settings.get_int ("indent-width")); } @@ -228,7 +228,6 @@ public class Code.FormatBar : Gtk.Box { Gtk.TextIter iter; buffer.get_iter_at_offset (out iter, position); var line = iter.get_line () + 1; - line_menubutton.text = "%d.%d".printf (line, iter.get_line_offset () + 1); goto_entry.text = "%d.%d".printf (line, iter.get_line_offset () + 1); } @@ -255,9 +254,7 @@ public class Code.FormatBar : Gtk.Box { // We need to connect_after because otherwise, the text isn't parsed into the "value" property and we only get the previous value goto_entry.activate.connect_after (() => { int line, column; - goto_entry.text = goto_entry.text.replace (":", "."); - goto_entry.text.scanf ("%i.%i", out line, out column); doc.source_view.go_to_line (line, column - 1); // Focuses parent to the source view, so that the cursor, which indicates line and column is actually visible. @@ -269,6 +266,7 @@ public class Code.FormatBar : Gtk.Box { if (this.doc != null) { this.doc.source_view.buffer.notify["cursor-position"].disconnect (format_line_header); } + this.doc = doc; update_current_lang (); format_tab_header_from_global_settings (); @@ -277,7 +275,6 @@ public class Code.FormatBar : Gtk.Box { } public void set_insert_spaces_instead_of_tabs (bool use_spaces) { - space_tab_modelbutton.active = use_spaces; if (doc != null) { doc.source_view.insert_spaces_instead_of_tabs = use_spaces; From 33eb40d7576e51fa7cf6a0b333c337581fd0a699 Mon Sep 17 00:00:00 2001 From: Jeremy Wootten Date: Mon, 10 Jul 2023 18:49:25 +0100 Subject: [PATCH 5/5] Same indent width range everywhere --- data/io.elementary.code.gschema.xml | 1 + src/Widgets/FormatBar.vala | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/data/io.elementary.code.gschema.xml b/data/io.elementary.code.gschema.xml index 1794882fa9..d0bad47f8d 100644 --- a/data/io.elementary.code.gschema.xml +++ b/data/io.elementary.code.gschema.xml @@ -121,6 +121,7 @@ Whether Code should use auto indentation + 4 Tab Size Specifies the number of spaces that should be displayed instead of Tab characters. diff --git a/src/Widgets/FormatBar.vala b/src/Widgets/FormatBar.vala index 33ed28aa43..53d4673eef 100644 --- a/src/Widgets/FormatBar.vala +++ b/src/Widgets/FormatBar.vala @@ -158,7 +158,7 @@ public class Code.FormatBar : Gtk.Box { hexpand = true }; - width_spinbutton = new Gtk.SpinButton.with_range (1, 24, 1); + width_spinbutton = new Gtk.SpinButton.with_range (2, 16, 1); tab_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 12) { margin_top = 6, @@ -193,7 +193,7 @@ public class Code.FormatBar : Gtk.Box { if (!tab_width_set_by_editor_config) { Scratch.settings.set_int ( "indent-width", - ((int)width_spinbutton.@value).clamp (2, 16) + (int)width_spinbutton.@value ); } });