UNPKG

3.8 kBTypeScriptView Raw
1import { type IStringBuilder } from '@rushstack/node-core-library';
2/**
3 * A utility for writing indented text.
4 *
5 * @remarks
6 *
7 * Note that the indentation is inserted at the last possible opportunity.
8 * For example, this code...
9 *
10 * ```ts
11 * writer.write('begin\n');
12 * writer.increaseIndent();
13 * writer.write('one\ntwo\n');
14 * writer.decreaseIndent();
15 * writer.increaseIndent();
16 * writer.decreaseIndent();
17 * writer.write('end');
18 * ```
19 *
20 * ...would produce this output:
21 *
22 * ```
23 * begin
24 * one
25 * two
26 * end
27 * ```
28 */
29export declare class IndentedWriter {
30 /**
31 * The text characters used to create one level of indentation.
32 * Two spaces by default.
33 */
34 defaultIndentPrefix: string;
35 private readonly _builder;
36 private _latestChunk;
37 private _previousChunk;
38 private _atStartOfLine;
39 private readonly _indentStack;
40 private _indentText;
41 private _beforeStack;
42 private _isWritingBeforeStack;
43 constructor(builder?: IStringBuilder);
44 /**
45 * Retrieves the output that was built so far.
46 */
47 getText(): string;
48 toString(): string;
49 /**
50 * Increases the indentation. Normally the indentation is two spaces,
51 * however an arbitrary prefix can optional be specified. (For example,
52 * the prefix could be "// " to indent and comment simultaneously.)
53 * Each call to IndentedWriter.increaseIndent() must be followed by a
54 * corresponding call to IndentedWriter.decreaseIndent().
55 */
56 increaseIndent(indentPrefix?: string): void;
57 /**
58 * Decreases the indentation, reverting the effect of the corresponding call
59 * to IndentedWriter.increaseIndent().
60 */
61 decreaseIndent(): void;
62 /**
63 * A shorthand for ensuring that increaseIndent()/decreaseIndent() occur
64 * in pairs.
65 */
66 indentScope(scope: () => void, indentPrefix?: string): void;
67 /**
68 * Adds a newline if the file pointer is not already at the start of the line (or start of the stream).
69 */
70 ensureNewLine(): void;
71 /**
72 * Adds up to two newlines to ensure that there is a blank line above the current line.
73 */
74 ensureSkippedLine(): void;
75 /**
76 * Returns the last character that was written, or an empty string if no characters have been written yet.
77 */
78 peekLastCharacter(): string;
79 /**
80 * Returns the second to last character that was written, or an empty string if less than one characters
81 * have been written yet.
82 */
83 peekSecondLastCharacter(): string;
84 /**
85 * Writes `before` and `after` messages if and only if `mayWrite` writes anything.
86 *
87 * If `mayWrite` writes "CONTENT", this method will write "<before>CONTENT<after>".
88 * If `mayWrite` writes nothing, this method will write nothing.
89 */
90 writeTentative(before: string, after: string, mayWrite: () => void): void;
91 /**
92 * Writes some text to the internal string buffer, applying indentation according
93 * to the current indentation level. If the string contains multiple newlines,
94 * each line will be indented separately.
95 */
96 write(message: string): void;
97 /**
98 * A shorthand for writing an optional message, followed by a newline.
99 * Indentation is applied following the semantics of IndentedWriter.write().
100 */
101 writeLine(message?: string): void;
102 /**
103 * Writes a string that does not contain any newline characters.
104 */
105 private _writeLinePart;
106 private _writeNewLine;
107 private _write;
108 /**
109 * Writes all messages in our before stack, processing them in FIFO order. This stack is
110 * populated by the `writeTentative` method.
111 */
112 private _writeBeforeStack;
113 private _updateIndentText;
114}
115//# sourceMappingURL=IndentedWriter.d.ts.map
\No newline at end of file