add trimEmpty options to trimTrailingSpace, cleanup

This commit is contained in:
nightwing 2013-08-26 21:58:31 +04:00
commit c62e0d3062

View file

@ -63,7 +63,7 @@ exports.$detectIndentation = function(lines, fallback) {
// ignore lines ending with backslash // ignore lines ending with backslash
while (line[line.length - 1] == "\\") while (line[line.length - 1] == "\\")
line = lines[i++]; line = lines[i++];
}; }
function getScore(indent) { function getScore(indent) {
var score = 0; var score = 0;
@ -113,15 +113,17 @@ exports.detectIndentation = function(session) {
return indent; return indent;
}; };
exports.trimTrailingSpace = function(session) { exports.trimTrailingSpace = function(session, trimEmpty) {
var doc = session.getDocument(); var doc = session.getDocument();
var lines = doc.getAllLines(); var lines = doc.getAllLines();
var min = trimEmpty ? -1 : 0;
for (var i = 0, l=lines.length; i < l; i++) { for (var i = 0, l=lines.length; i < l; i++) {
var line = lines[i]; var line = lines[i];
var index = line.search(/\s+$/); var index = line.search(/\s+$/);
if (index !== -1) if (index > min)
doc.removeInLine(i, index, line.length); doc.removeInLine(i, index, line.length);
} }
}; };
@ -160,14 +162,14 @@ exports.convertIndentation = function(session, ch, len) {
}; };
exports.$parseStringArg = function(text) { exports.$parseStringArg = function(text) {
var indent = {} var indent = {};
if (/t/.test(text)) if (/t/.test(text))
indent.ch = "\t"; indent.ch = "\t";
else if (/s/.test(text)) else if (/s/.test(text))
indent.ch = " "; indent.ch = " ";
var m = text.match(/\d+/); var m = text.match(/\d+/);
if (m) if (m)
indent.length = parseInt(m[0]); indent.length = parseInt(m[0], 10);
return indent; return indent;
}; };
@ -179,7 +181,7 @@ exports.$parseArg = function(arg) {
if (typeof arg.text == "string") if (typeof arg.text == "string")
return exports.$parseStringArg(arg.text); return exports.$parseStringArg(arg.text);
return arg; return arg;
} };
exports.commands = [{ exports.commands = [{
name: "detectIndentation", name: "detectIndentation",
@ -196,7 +198,7 @@ exports.commands = [{
name: "convertIndentation", name: "convertIndentation",
exec: function(editor, arg) { exec: function(editor, arg) {
var indent = exports.$parseArg(arg); var indent = exports.$parseArg(arg);
exports.convertIndentation(editor.session, arg.ch, arg.length); exports.convertIndentation(editor.session, indent.ch, indent.length);
} }
}, { }, {
name: "setIndentation", name: "setIndentation",
@ -205,6 +207,6 @@ exports.commands = [{
indent.length && editor.session.setTabSize(indent.length); indent.length && editor.session.setTabSize(indent.length);
indent.ch && editor.session.setUseSoftTabs(indent.ch == " "); indent.ch && editor.session.setUseSoftTabs(indent.ch == " ");
} }
}] }];
}); });