Skip to content

Commit

Permalink
Sync tab settings (#1347)
Browse files Browse the repository at this point in the history
* Sync global settings, appmenu, format bar, tab popover

* Fix switching docs with editorconfig plugin on

* Fix space_drawer update

* Fix whitespace

* Same indent width range everywhere

---------

Co-authored-by: Ryan Kornheisl <[email protected]>
  • Loading branch information
Jeremy Wootten and zeebok committed Jul 11, 2023
1 parent afdd9fe commit 380240e
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 25 deletions.
1 change: 1 addition & 0 deletions data/io.elementary.code.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@
<description>Whether Code should use auto indentation</description>
</key>
<key name="indent-width" type="i">
<range min="2" max="16"/>
<default>4</default>
<summary>Tab Size</summary>
<description>Specifies the number of spaces that should be displayed instead of Tab characters.</description>
Expand Down
13 changes: 10 additions & 3 deletions plugins/editorconfig/editorconfig.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand All @@ -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;
}
}
});
Expand Down
58 changes: 38 additions & 20 deletions src/Widgets/FormatBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 () {
Expand All @@ -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);
}
Expand All @@ -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.
Expand All @@ -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 ();
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions src/Widgets/SourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,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 (
Expand All @@ -269,9 +270,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);
Expand Down

0 comments on commit 380240e

Please sign in to comment.