Refactor buffer for clarity and avoid regex for performance
This commit is contained in:
parent
e01b7d288f
commit
c6f3a55c03
@ -67,32 +67,58 @@ Buffer.prototype.removeLast = function (cha) {
|
||||
|
||||
Buffer.prototype.newline = function (i, removeLast) {
|
||||
if (this.format.compact) return;
|
||||
|
||||
if (_.isBoolean(i)) {
|
||||
removeLast = i;
|
||||
i = null;
|
||||
}
|
||||
removeLast = removeLast || false;
|
||||
|
||||
if (_.isNumber(i)) {
|
||||
if (this.endsWith("{\n")) i--;
|
||||
if (this.endsWith(util.repeat(i, "\n"))) return;
|
||||
|
||||
for (var j = 0; j < i; j++) {
|
||||
this.newline(null, removeLast);
|
||||
while (i--) {
|
||||
this._newline(removeLast);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (_.isBoolean(i)) {
|
||||
removeLast = i;
|
||||
}
|
||||
|
||||
this._newline(removeLast);
|
||||
};
|
||||
|
||||
Buffer.prototype._newline = function (removeLast) {
|
||||
if (removeLast && this.isLast("\n")) this.removeLast("\n");
|
||||
|
||||
this.removeLast(" ");
|
||||
|
||||
// remove whitespace if last character was a newline
|
||||
this.buf = this.buf.replace(/\n +$/, "\n");
|
||||
var trimStart = this._getPostNewlineTrailingSpaceStart();
|
||||
if (trimStart) {
|
||||
this.buf = this.buf.substring(0, trimStart);
|
||||
}
|
||||
|
||||
this._push("\n");
|
||||
};
|
||||
|
||||
Buffer.prototype._getPostNewlineTrailingSpaceStart = function () {
|
||||
var lastNewlineIndex = this.buf.lastIndexOf('\n');
|
||||
if (lastNewlineIndex === -1)
|
||||
return;
|
||||
|
||||
var index = this.buf.length - 1;
|
||||
while (index > lastNewlineIndex) {
|
||||
if (this.buf[index] !== ' ') {
|
||||
break;
|
||||
}
|
||||
|
||||
index--;
|
||||
}
|
||||
|
||||
if (index === lastNewlineIndex) {
|
||||
return index + 1;
|
||||
}
|
||||
};
|
||||
|
||||
Buffer.prototype.push = function (str, noIndent) {
|
||||
if (this._indent && !noIndent && str !== "\n") {
|
||||
// we have an indent level and we aren't pushing a newline
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user