Added a 'trim whitespace' command and trigger it on save if enabled. Fixes #151

This commit is contained in:
Thomas Wilburn 2014-03-05 08:52:48 -08:00
parent c2c8f64799
commit befba25f86
5 changed files with 24 additions and 1 deletions

View file

@ -32,6 +32,7 @@
{ "label": "Convert Tabs to Spaces", "command": "sublime:tabs-to-spaces" },
{ "label": "Convert Spaces to Tabs", "command": "sublime:spaces-to-tabs" },
{ "label": "Trim Trailing Whitespace", "command": "ace:trim-whitespace" },
{ "label": "Sort Lines", "command": "ace:command", "argument": "sortlines" },
{ "label": "Split Line", "command": "ace:command", "argument": "splitline" },
{ "label": "To Uppercase", "command": "ace:command", "argument": "touppercase" },

View file

@ -32,6 +32,7 @@
{ "label": "Block Comment", "command": "ace:command", "argument": "toggleBlockComment" },
{ "label": "Convert Tabs to Spaces", "command": "sublime:tabs-to-spaces" },
{ "label": "Convert Spaces to Tabs", "command": "sublime:spaces-to-tabs" },
{ "label": "Trim Trailing Whitespace", "command": "ace:trim-whitespace" },
"divider",
{ "label": "Sort Lines", "command": "ace:command", "argument": "sortlines" },
{ "label": "Split Line", "command": "ace:command", "argument": "splitline" },

View file

@ -47,6 +47,7 @@
"showMargin": false,
"lineEndings": "auto", //newline format - "windows", "unix", or "auto"
"scrollPastEnd": true, //allow the editor to scroll past the end of the document
"trimTrailingWhitespace": true, //remove whitespace from the end of lines on save
//only fixed-width fonts supported, for now
"fontFamily": "",

View file

@ -123,6 +123,20 @@ define([
session.setValue(text);
if (c) c();
});
command.on("ace:trim-whitespace", function(c) {
var session = editor.getSession();
var doc = session.doc;
var selection = editor.getSelection();
var length = session.getLength();
for (var i = 0; i < length; i++) {
var range = selection.getLineRange(i);
var line = doc.getTextRange(range);
line = line.replace(/\s+([\n\r$])/, "$1");
doc.replace(selection.getLineRange(i), line);
}
if (c) c();
});
//we also add a command redirect for firing Ace commands via regular command attributes
command.on("ace:command", editor.execCommand.bind(editor));

View file

@ -2,7 +2,7 @@ define([
"command",
"storage/file",
"util/manos",
"settings!ace",
"settings!ace,user",
"util/template!templates/tab.html"
], function(command, File, M, Settings, inflate) {
@ -58,6 +58,12 @@ define([
c = as;
as = false;
}
//strip final whitespace, if enabled
if (Settings.get("user").trimTrailingWhitespace) {
command.fire("ace:trim-whitespace");
}
var content = this.getValue();
var self = this;
var deferred = M.deferred();