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/plugins/editorconfig/editorconfig.vala b/plugins/editorconfig/editorconfig.vala index 79e99efb30..8a5b8f4285 100644 --- a/plugins/editorconfig/editorconfig.vala +++ b/plugins/editorconfig/editorconfig.vala @@ -32,7 +32,11 @@ public class Scratch.Plugins.EditorConfigPlugin: Peas.ExtensionBase, Peas.Activa }); plugins.hook_document.connect ((d) => { - format_bar.tab_set_by_editor_config = false; + // 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; @@ -52,13 +56,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; @@ -73,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 04c3ca0fb6..53d4673eef 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 (2, 16, 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,42 @@ 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); - 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); + format_tab_header_from_global_settings (); + width_spinbutton.value_changed.connect (() => { + if (!tab_width_set_by_editor_config) { + Scratch.settings.set_int ( + "indent-width", + (int)width_spinbutton.@value + ); + } + }); + + space_tab_modelbutton.clicked.connect (() => { + if (!tab_style_set_by_editor_config) { + 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")); } - var indent_width = Scratch.settings.get_int ("indent-width"); - var 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")); + } - 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 () { @@ -209,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); } @@ -236,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. @@ -250,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 (); @@ -265,6 +282,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 ee079893c7..bb8bab1600 100644 --- a/src/Widgets/SourceView.vala +++ b/src/Widgets/SourceView.vala @@ -243,9 +243,10 @@ 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"); - + space_drawer.enable_matrix = false; switch ((ScratchDrawSpacesState) Scratch.settings.get_enum ("draw-spaces")) { case ScratchDrawSpacesState.ALWAYS: space_drawer.set_types_for_locations ( @@ -271,9 +272,10 @@ namespace Scratch.Widgets { break; } + space_drawer.enable_matrix = true; 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);