ts-budgie
Version:
Converts TypeScript code to Budgie.
79 lines • 2.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var budgie_1 = require("budgie");
/**
* Generates spaces equivalent to 4-space code tabbing.
*
* @param amount How many tabs should be added.
* @returns An all-spaces String of length = amount * 4.
*/
var generateTabs = function (amount) {
var output = "";
for (var i = 0; i < amount; i += 1) {
output += " ";
}
return output;
};
/**
* Indents Budgie lines using their command metadata.
*/
var LineIndenter = /** @class */ (function () {
function LineIndenter() {
this.commandsBag = budgie_1.CommandsBagFactory.forContext(new budgie_1.RenderContext(new budgie_1.LanguagesBag().getLanguageByName("TypeScript")));
}
/**
* Indents Budgie lines using their command metadata.
*
* @param lines Budgie and literal string lines.
* @returns Indented versions of the lines.
*/
LineIndenter.prototype.indent = function (lines) {
var output = [];
if (lines.length === 0) {
return output;
}
var currentIndentation = 0;
for (var _i = 0, lines_1 = lines; _i < lines_1.length; _i++) {
var line = lines_1[_i];
if (typeof line === "string") {
output.push(line);
continue;
}
var indentationChanges = this.getIndentationChanges(line);
var stringified = line.toString();
for (var _a = 0, indentationChanges_1 = indentationChanges; _a < indentationChanges_1.length; _a++) {
var change = indentationChanges_1[_a];
if (change < 0) {
currentIndentation += change;
}
}
if (stringified === "") {
output.push("");
}
else {
output.push(generateTabs(currentIndentation) + line.toString());
}
for (var _b = 0, indentationChanges_2 = indentationChanges; _b < indentationChanges_2.length; _b++) {
var change = indentationChanges_2[_b];
if (change > 0) {
currentIndentation += change;
}
}
}
return output;
};
/**
* Computes how a line's command changes indentation.
*
* @param line Output Budgie line.
* @returns How the line's command changes indentation.
*/
LineIndenter.prototype.getIndentationChanges = function (line) {
var command = this.commandsBag.getCommand(line.command);
var metadata = command.getMetadata();
return metadata.indentation;
};
return LineIndenter;
}());
exports.LineIndenter = LineIndenter;
//# sourceMappingURL=lineIndenter.js.map