smart-column-indenter
Version:
A smart source code indenter that indent the code into columns
71 lines • 2.53 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Split each line of the input into columns.
* This method returns a 3D matrix as an array of lines.
* Each line is an array os columns.
* Each line/column position has an array os tokens.
*/
class Columnizer {
constructor(language, commonTokens, originalLines) {
this.language = language;
this.commonTokens = commonTokens;
this.originalLines = originalLines;
this.columnizedLines = this.originalLines.map(line => []);
this.actualTokenByLine = this.originalLines.map(line => -1);
this.actualColumn = -1;
}
columnize() {
this.addColumn();
for (const commonToken of this.commonTokens) {
this.copyUntil(commonToken);
this.addColumn();
this.copyOnce();
this.addColumn();
}
this.copyUntil();
return this.columnizedLines;
}
/**
* Adds a new column in the matrix of columnized list tokens
*/
addColumn() {
this.actualColumn++;
this.columnizedLines.forEach(columnizedLine => columnizedLine[this.actualColumn] = []);
}
/**
* Gets the next token of the line
*/
nextToken(lineNumber) {
this.actualTokenByLine[lineNumber]++;
return this.actualToken(lineNumber);
}
/**
* Gets the next token of the line
*/
actualToken(lineNumber) {
return this.originalLines[lineNumber][this.actualTokenByLine[lineNumber]];
}
copyUntil(tokenContent) {
for (let lineNumber = 0; lineNumber < this.originalLines.length; lineNumber++) {
let token = this.nextToken(lineNumber);
while (token && (tokenContent === undefined || tokenContent !== this.language.token2string(token))) {
this.columnizedLines[lineNumber][this.actualColumn].push(token);
token = this.nextToken(lineNumber);
}
}
}
copyOnce() {
for (let lineNumber = 0; lineNumber < this.originalLines.length; lineNumber++) {
let token = this.actualToken(lineNumber);
if (token) {
this.columnizedLines[lineNumber][this.actualColumn].push(token);
}
else {
throw new Error("There are no more tokens to copy.");
}
}
}
}
exports.default = Columnizer;
//# sourceMappingURL=Columnizer.js.map