Skip to content

Commit

Permalink
Move strip trailing whitespace into main code; remove plugin (#1151)
Browse files Browse the repository at this point in the history
* Strip whitespace on every saving

# Conflicts fixed in:
#	src/Services/Document.vala

* Add strip-trailing-spaces controls to Preferences Dialog

* Strip trailing space when setting turned on

* Remove strip-trailing-save plugin

# Conflicts fixed in:
#	plugins/meson.build

* Fix some regressions

* Limit scope of undoable action
* Do not strip when loading or empty

* Remove commented out code

* Only strip Vala for now

* Do not strip trailing spaces on save while completion showing

* Check for null language

* Remove unused widget

* Overwrite unwanted plugins with dummy files

* Restore strip only on manual save behaviour

* No longer restrict to Vala files

* Remove code to block old plugin

Co-authored-by: Ryan Kornheisl <[email protected]>
  • Loading branch information
Jeremy Wootten and zeebok authored Oct 4, 2022
1 parent 9fca4ac commit c5908db
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 167 deletions.
5 changes: 5 additions & 0 deletions data/io.elementary.code.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@
<summary>Whether search is cyclic</summary>
<description>Whether text searching should cycle back to the beginning of the document after reaching the end of the document.</description>
</key>
<key name="strip-trailing-on-save" type="b">
<default>false</default>
<summary>Whether to automatically remove trailing whitespace on saving</summary>
<description>Whether trailing whitespace should be removed from a document whenever it is saved, including on autosave.</description>
</key>
</schema>

<schema path="/io/elementary/code/services/" id="io.elementary.code.services" gettext-domain="io.elementary.code">
Expand Down
1 change: 0 additions & 1 deletion plugins/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ subdir('outline')
subdir('pastebin')
subdir('preserve-indent')
subdir('spell')
subdir('strip-trailing-save')
subdir('terminal')
subdir('vim-emulation')
subdir('word-completion')
32 changes: 0 additions & 32 deletions plugins/strip-trailing-save/meson.build

This file was deleted.

10 changes: 0 additions & 10 deletions plugins/strip-trailing-save/strip-trailing-save.plugin

This file was deleted.

112 changes: 0 additions & 112 deletions plugins/strip-trailing-save/strip-trailing-save.vala

This file was deleted.

9 changes: 6 additions & 3 deletions src/Dialogs/PreferencesDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ namespace Scratch.Dialogs {
general_grid.attach (new SettingsSwitch ("auto-indent"), 1, 4, 2);
general_grid.attach (new SettingsLabel (_("Insert spaces instead of tabs:")), 0, 5);
general_grid.attach (new SettingsSwitch ("spaces-instead-of-tabs"), 1, 5, 2);
general_grid.attach (new SettingsLabel (_("Tab width:")), 0, 6);
general_grid.attach (indent_width, 1, 6, 2);
general_grid.attach (new SettingsLabel (_("Strip trailing whitespace:")), 0, 6);
general_grid.attach (new SettingsSwitch ("strip-trailing-on-save"), 1, 6, 2);
general_grid.attach (new SettingsLabel (_("Tab width:")), 0, 7);
general_grid.attach (indent_width, 1, 7, 2);

main_stack = new Gtk.Stack () {
margin = 12
Expand All @@ -82,6 +84,7 @@ namespace Scratch.Dialogs {
main_grid.attach (main_stackswitcher, 0, 0);
main_grid.attach (main_stack, 0, 1);


border_width = 0;
get_content_area ().add (main_grid);

Expand Down Expand Up @@ -157,7 +160,7 @@ namespace Scratch.Dialogs {
content.attach (show_mini_map, 1, 5, 1, 1);
content.attach (show_right_margin_label, 0, 6, 1, 1);
content.attach (show_right_margin, 1, 6, 1, 1);
content.attach (right_margin_position, 2, 6, 1, 1);
content.attach (right_margin_position, 2, 7, 1, 1);
content.attach (font_header, 0, 7, 3, 1);
content.attach (use_custom_font_label , 0, 9, 1, 1);
content.attach (use_custom_font, 1, 9, 1, 1);
Expand Down
2 changes: 1 addition & 1 deletion src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ namespace Scratch {
if (doc.is_file_temporary == true) {
action_save_as ();
} else {
doc.save.begin ();
doc.save.begin (true);
}
}
}
Expand Down
91 changes: 84 additions & 7 deletions src/Services/Document.vala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ namespace Scratch.Services {
public string original_content;
private string last_save_content;
public bool saved = true;
private bool completion_shown = false;

private Gtk.ScrolledWindow scroll;
private Gtk.InfoBar info_bar;
Expand Down Expand Up @@ -193,8 +194,16 @@ namespace Scratch.Services {
}
});

/* Create as loaded - could be new document */
loaded = true;
source_view.completion.show.connect (() => {
completion_shown = true;
});

source_view.completion.hide.connect (() => {
completion_shown = false;
});

// /* Create as loaded - could be new document */
loaded = file == null;
ellipsize_mode = Pango.EllipsizeMode.MIDDLE;
}

Expand Down Expand Up @@ -233,6 +242,7 @@ namespace Scratch.Services {
public async void open (bool force = false) {
/* Loading improper files may hang so we cancel after a certain time as a fallback.
* In most cases, an error will be thrown and caught. */
loaded = false;
if (load_cancellable != null) { /* just in case */
load_cancellable.cancel ();
}
Expand All @@ -251,7 +261,6 @@ namespace Scratch.Services {

source_view.sensitive = false;
this.working = true;
loaded = false;

var content_type = ContentType.from_mime_type (mime_type);

Expand Down Expand Up @@ -312,7 +321,6 @@ namespace Scratch.Services {
source_view.buffer.text = buffer.text;
}

loaded = true;
} catch (Error e) {
critical (e.message);
source_view.buffer.text = "";
Expand Down Expand Up @@ -349,7 +357,8 @@ namespace Scratch.Services {
* (large documents take time to format/display after loading)
*/
Idle.add (() => {
this.working = false;
working = false;
loaded = true;
return false;
});

Expand Down Expand Up @@ -442,12 +451,19 @@ namespace Scratch.Services {
}

public async bool save (bool force = false) {
if (!force && (source_view.buffer.get_modified () == false || this.loaded == false)) {
if (completion_shown ||
!force && (source_view.buffer.get_modified () == false ||
!loaded)) {

return false;
}

this.create_backup ();

if (Scratch.settings.get_boolean ("strip-trailing-on-save") && force) {
strip_trailing_spaces ();
}

// Replace old content with the new one
save_cancellable.cancel ();
save_cancellable = new GLib.Cancellable ();
Expand Down Expand Up @@ -511,7 +527,7 @@ namespace Scratch.Services {

if (success) {
source_view.buffer.set_modified (true);
var is_saved = yield save ();
var is_saved = yield save (true);

if (is_saved && is_current_file_temporary) {
try {
Expand Down Expand Up @@ -549,6 +565,10 @@ namespace Scratch.Services {
source_map.no_show_all = true;
scroll.vscrollbar_policy = Gtk.PolicyType.AUTOMATIC;
}

if (Scratch.settings.get_boolean ("strip-trailing-on-save")) {
strip_trailing_spaces ();
}
}

// Focus the SourceView
Expand Down Expand Up @@ -911,5 +931,62 @@ namespace Scratch.Services {
warning ("Folder containing the file was unmounted");
mounted = false;
}

/* Pull the buffer into an array and then work out which parts are to be deleted.
* Do not strip line currently being edited unless forced */
private void strip_trailing_spaces () {
if (!loaded || source_view.language == null) {
return;
}

var source_buffer = (Gtk.SourceBuffer)source_view.buffer;
Gtk.TextIter iter;

var cursor_pos = source_buffer.cursor_position;
source_buffer.get_iter_at_offset (out iter, cursor_pos);
var orig_line = iter.get_line ();
var orig_offset = iter.get_line_offset ();

var text = source_buffer.text;

string[] lines = Regex.split_simple ("""[\r\n]""", text);
if (lines.length == 0) { // Can legitimately happen at startup or new document
return;
}

if (lines.length != source_buffer.get_line_count ()) {
critical ("Mismatch between line counts when stripping trailing spaces, not continuing");
debug ("lines.length %u, buffer lines %u \n %s", lines.length, source_buffer.get_line_count (), text);
return;
}

MatchInfo info;
Gtk.TextIter start_delete, end_delete;
Regex whitespace;

try {
whitespace = new Regex ("[ \t]+$", 0);
} catch (RegexError e) {
critical ("Error while building regex to replace trailing whitespace: %s", e.message);
return;
}

for (int line_no = 0; line_no < lines.length; line_no++) {
if (whitespace.match (lines[line_no], 0, out info)) {

source_buffer.get_iter_at_line (out start_delete, line_no);
start_delete.forward_to_line_end ();
end_delete = start_delete;
end_delete.backward_chars (info.fetch (0).length);

source_buffer.begin_not_undoable_action ();
source_buffer.@delete (ref start_delete, ref end_delete);
source_buffer.end_not_undoable_action ();
}
}

source_buffer.get_iter_at_line_offset (out iter, orig_line, orig_offset);
source_buffer.place_cursor (iter);
}
}
}
2 changes: 1 addition & 1 deletion src/Services/PluginManager.vala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/***
BEGIN LICENSE
Copyright (C) 2013 Mario Guerriero <[email protected]>
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
Expand Down

0 comments on commit c5908db

Please sign in to comment.