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,CAAC,CAAC;aAC/B;YACD,IAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;YAC5B,IAAI,IAAI,CAAC,mBAAmB,EAAE;gBAC5B,IAAI,IAAI,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE;oBAC7B,IAAI,CAAC,mBAAmB,GAAG,KAAK,CAAC;iBAClC;aACF;YACD,IAAI,CAAC,cAAc,GAAG,KAAK,CAAC;SAC7B;IACH,CAAC;IAEO,aAAa;QACnB,IAAI,IAAI,CAAC,gBAAgB,EAAE;YACzB,IAAI,IAAI,CAAC,cAAc,IAAI,IAAI,CAAC,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE;gBACtD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;aAC/B;SACF;QAED,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,mBAAmB,CAAC;QACrD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAClB,IAAI,CAAC,mBAAmB,GAAG,IAAI,CAAC;QAChC,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC;IAC7B,CAAC;IAEO,MAAM,CAAC,CAAS;QACtB,IAAI,CAAC,cAAc,GAAG,IAAI,CAAC,YAAY,CAAC;QACxC,IAAI,CAAC,YAAY,GAAG,CAAC,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC;IAC1B,CAAC;IAEO,iBAAiB;QACvB,IAAI,CAAC,WAAW,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAChD,CAAC;CACF;AAjPD,wCAiPC","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.\n// See LICENSE in the project root for license information.\n\nimport { StringBuilder, type IStringBuilder } from '@rushstack/node-core-library';\n\n/**\n * A utility for writing indented text.\n *\n * @remarks\n *\n * Note that the indentation is inserted at the last possible opportunity.\n * For example, this code...\n *\n * ```ts\n * writer.write('begin\\n');\n * writer.increaseIndent();\n * writer.write('one\\ntwo\\n');\n * writer.decreaseIndent();\n * writer.increaseIndent();\n * writer.decreaseIndent();\n * writer.write('end');\n * ```\n *\n * ...would produce this output:\n *\n * ```\n * begin\n * one\n * two\n * end\n * ```\n */\nexport class IndentedWriter {\n /**\n * The text characters used to create one level of indentation.\n * Two spaces by default.\n */\n public defaultIndentPrefix: string = ' ';\n\n /**\n * Whether to indent blank lines\n */\n public indentBlankLines: boolean = false;\n\n /**\n * Trims leading spaces from the input text before applying the indent.\n *\n * @remarks\n * Consider the following example:\n *\n * ```ts\n * indentedWriter.increaseIndent(' '); // four spaces\n * indentedWriter.write(' a\\n b c\\n');\n * indentedWriter.decreaseIndent();\n * ```\n *\n * Normally the output would be indented by 6 spaces: 4 from `increaseIndent()`, plus the 2 spaces\n * from `write()`:\n * ```\n * a\n * b c\n * ```\n *\n * Setting `trimLeadingSpaces=true` will trim the leading spaces, so that the lines are indented\n * by 4 spaces only:\n * ```\n * a\n * b c\n * ```\n */\n public trimLeadingSpaces: boolean = false;\n\n private readonly _builder: IStringBuilder;\n\n private _latestChunk: string | undefined;\n private _previousChunk: string | undefined;\n private _atStartOfLine: boolean;\n\n private readonly _indentStack: string[];\n private _indentText: string;\n\n private _previousLineIsBlank: boolean;\n private _currentLineIsBlank: boolean;\n\n public constructor(builder?: IStringBuilder) {\n this._builder = builder === undefined ? new StringBuilder() : builder;\n this._latestChunk = undefined;\n this._previousChunk = undefined;\n this._atStartOfLine = true;\n this._previousLineIsBlank = true;\n this._currentLineIsBlank = true;\n\n this._indentStack = [];\n this._indentText = '';\n }\n\n /**\n * Retrieves the output that was built so far.\n */\n public getText(): string {\n return this._builder.toString();\n }\n\n public toString(): string {\n return this.getText();\n }\n\n /**\n * Increases the indentation. Normally the indentation is two spaces,\n * however an arbitrary prefix can optional be specified. (For example,\n * the prefix could be \"// \" to indent and comment simultaneously.)\n * Each call to IndentedWriter.increaseIndent() must be followed by a\n * corresponding call to IndentedWriter.decreaseIndent().\n */\n public increaseIndent(indentPrefix?: string): void {\n this._indentStack.push(indentPrefix !== undefined ? indentPrefix : this.defaultIndentPrefix);\n this._updateIndentText();\n }\n\n /**\n * Decreases the indentation, reverting the effect of the corresponding call\n * to IndentedWriter.increaseIndent().\n */\n public decreaseIndent(): void {\n this._indentStack.pop();\n this._updateIndentText();\n }\n\n /**\n * A shorthand for ensuring that increaseIndent()/decreaseIndent() occur\n * in pairs.\n */\n public indentScope(scope: () => void, indentPrefix?: string): void {\n this.increaseIndent(indentPrefix);\n scope();\n this.decreaseIndent();\n }\n\n /**\n * Adds a newline if the file pointer is not already at the start of the line (or start of the stream).\n */\n public ensureNewLine(): void {\n const lastCharacter: string = this.peekLastCharacter();\n if (lastCharacter !== '\\n' && lastCharacter !== '') {\n this._writeNewLine();\n }\n }\n\n /**\n * Adds up to two newlines to ensure that there is a blank line above the current position.\n * The start of the stream is considered to be a blank line, so `ensureSkippedLine()` has no effect\n * unless some text has been written.\n */\n public ensureSkippedLine(): void {\n this.ensureNewLine();\n if (!this._previousLineIsBlank) {\n this._writeNewLine();\n }\n }\n\n /**\n * Returns the last character that was written, or an empty string if no characters have been written yet.\n */\n public peekLastCharacter(): string {\n if (this._latestChunk !== undefined) {\n return this._latestChunk.substr(-1, 1);\n }\n return '';\n }\n\n /**\n * Returns the second to last character that was written, or an empty string if less than one characters\n * have been written yet.\n */\n public peekSecondLastCharacter(): string {\n if (this._latestChunk !== undefined) {\n if (this._latestChunk.length > 1) {\n return this._latestChunk.substr(-2, 1);\n }\n if (this._previousChunk !== undefined) {\n return this._previousChunk.substr(-1, 1);\n }\n }\n return '';\n }\n\n /**\n * Writes some text to the internal string buffer, applying indentation according\n * to the current indentation level. If the string contains multiple newlines,\n * each line will be indented separately.\n */\n public write(message: string): void {\n if (message.length === 0) {\n return;\n }\n\n // If there are no newline characters, then append the string verbatim\n if (!/[\\r\\n]/.test(message)) {\n this._writeLinePart(message);\n return;\n }\n\n // Otherwise split the lines and write each one individually\n let first: boolean = true;\n for (const linePart of message.split('\\n')) {\n if (!first) {\n this._writeNewLine();\n } else {\n first = false;\n }\n if (linePart) {\n this._writeLinePart(linePart.replace(/[\\r]/g, ''));\n }\n }\n }\n\n /**\n * A shorthand for writing an optional message, followed by a newline.\n * Indentation is applied following the semantics of IndentedWriter.write().\n */\n public writeLine(message: string = ''): void {\n if (message.length > 0) {\n this.write(message);\n }\n this._writeNewLine();\n }\n\n /**\n * Writes a string that does not contain any newline characters.\n */\n private _writeLinePart(message: string): void {\n let trimmedMessage: string = message;\n\n if (this.trimLeadingSpaces && this._atStartOfLine) {\n trimmedMessage = message.replace(/^ +/, '');\n }\n\n if (trimmedMessage.length > 0) {\n if (this._atStartOfLine && this._indentText.length > 0) {\n this._write(this._indentText);\n }\n this._write(trimmedMessage);\n if (this._currentLineIsBlank) {\n if (/\\S/.test(trimmedMessage)) {\n this._currentLineIsBlank = false;\n }\n }\n this._atStartOfLine = false;\n }\n }\n\n private _writeNewLine(): void {\n if (this.indentBlankLines) {\n if (this._atStartOfLine && this._indentText.length > 0) {\n this._write(this._indentText);\n }\n }\n\n this._previousLineIsBlank = this._currentLineIsBlank;\n this._write('\\n');\n this._currentLineIsBlank = true;\n this._atStartOfLine = true;\n }\n\n private _write(s: string): void {\n this._previousChunk = this._latestChunk;\n this._latestChunk = s;\n this._builder.append(s);\n }\n\n private _updateIndentText(): void {\n this._indentText = this._indentStack.join('');\n }\n}\n"]} |