UNPKG

@microsoft/api-extractor

Version:

Validate, document, and review the exported API for a TypeScript library

128 lines (126 loc) 3.79 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. Object.defineProperty(exports, "__esModule", { value: true }); /** * A utility for writing indented text. In the current implementation, * IndentedWriter builds up an internal string buffer, which can be obtained * by calling IndentedWriter.getOutput(). * * Note that the indentation is inserted at the last possible opportunity. * For example, this code... * * writer.write('begin\n'); * writer.increaseIndent(); * writer.Write('one\ntwo\n'); * writer.decreaseIndent(); * writer.increaseIndent(); * writer.decreaseIndent(); * writer.Write('end'); * * ...would produce this output: * * begin * one * two * end */ class IndentedWriter { constructor() { /** * The text characters used to create one level of indentation. * Two spaces by default. */ this.spacing = ' '; this.clear(); } /** * Resets the stream, erasing any output and indentation levels. * Does not reset the "spacing" configuration. */ clear() { this._output = ''; this._indentStack = []; this._indentText = ''; this._needsIndent = true; } /** * Retrieves the indented output. */ toString() { return this._output; } /** * Increases the indentation. Normally the indentation is two spaces, * however an arbitrary prefix can optional be specified. (For example, * the prefix could be "// " to indent and comment simultaneously.) * Each call to IndentedWriter.increaseIndent() must be followed by a * corresponding call to IndentedWriter.decreaseIndent(). */ increaseIndent() { this._indentStack.push(this.spacing); this._updateIndentText(); } /** * Decreases the indentation, reverting the effect of the corresponding call * to IndentedWriter.increaseIndent(). */ decreaseIndent() { this._indentStack.pop(); this._updateIndentText(); } /** * A shorthand for ensuring that increaseIndent()/decreaseIndent() occur * in pairs. */ indentScope(scope) { this.increaseIndent(); scope(); this.decreaseIndent(); } /** * Writes some text to the internal string buffer, applying indentation according * to the current indentation level. If the string contains multiple newlines, * each line will be indented separately. */ write(message) { let first = true; for (const linePart of message.split('\n')) { if (!first) { this._writeNewLine(); } else { first = false; } if (linePart) { this._writeLinePart(linePart); } } } /** * A shorthand for writing an optional message, followed by a newline. * Indentation is applied following the semantics of IndentedWriter.write(). */ writeLine(message = '') { this.write(message + '\n'); } /** * Writes a string that does not contain any newline characters. */ _writeLinePart(message) { if (this._needsIndent) { this._output += this._indentText; this._needsIndent = false; } this._output += message.replace(/\r/g, ''); } _writeNewLine() { this._output += '\n'; this._needsIndent = true; } _updateIndentText() { this._indentText = this._indentStack.join(''); } } exports.default = IndentedWriter; //# sourceMappingURL=IndentedWriter.js.map