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 ce5ee73fdf..bba84fc630 100644 --- a/src/MainWindow.vala +++ b/src/MainWindow.vala @@ -442,7 +442,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 (); @@ -451,6 +450,9 @@ namespace Scratch { sidebar.choose_project_button.set_document (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..ea06e1a0b0 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,20 @@ 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 () { + 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 +238,35 @@ 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 = 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 (tab_set_by_editor_config) { + tab_toggle.tooltip_text = _("Indent width and style set by EditorConfig file"); + } + + 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) {