Repository navigation
Expand file tree
/
Copy pathIndentedWriter.ts
More file actions
276 lines (239 loc) · 7.42 KB
/
Copy pathIndentedWriter.ts
File metadata and controls
276 lines (239 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { StringBuilder, type IStringBuilder } from '@rushstack/node-core-library';
/**
* A utility for writing indented text.
*
* @remarks
*
* Note that the indentation is inserted at the last possible opportunity.
* For example, this code...
*
* ```ts
* 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
* ```
*/
export class IndentedWriter {
/**
* The text characters used to create one level of indentation.
* Two spaces by default.
*/
public defaultIndentPrefix: string = ' ';
readonly #builder: IStringBuilder;
#latestChunk: string | undefined;
#previousChunk: string | undefined;
#atStartOfLine: boolean;
readonly #indentStack: string[];
#indentText: string;
#beforeStack: string[];
#isWritingBeforeStack: boolean;
public constructor(builder?: IStringBuilder) {
this.#builder = builder === undefined ? new StringBuilder() : builder;
this.#latestChunk = undefined;
this.#previousChunk = undefined;
this.#atStartOfLine = true;
this.#indentStack = [];
this.#indentText = '';
this.#beforeStack = [];
this.#isWritingBeforeStack = false;
}
/**
* Retrieves the output that was built so far.
*/
public getText(): string {
return this.#builder.toString();
}
public toString(): string {
return this.getText();
}
/**
* 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().
*/
public increaseIndent(indentPrefix?: string): void {
this.#indentStack.push(indentPrefix !== undefined ? indentPrefix : this.defaultIndentPrefix);
this.#updateIndentText();
}
/**
* Decreases the indentation, reverting the effect of the corresponding call
* to IndentedWriter.increaseIndent().
*/
public decreaseIndent(): void {
this.#indentStack.pop();
this.#updateIndentText();
}
/**
* A shorthand for ensuring that increaseIndent()/decreaseIndent() occur
* in pairs.
*/
public indentScope(scope: () => void, indentPrefix?: string): void {
this.increaseIndent(indentPrefix);
scope();
this.decreaseIndent();
}
/**
* Adds a newline if the file pointer is not already at the start of the line (or start of the stream).
*/
public ensureNewLine(): void {
const lastCharacter: string = this.peekLastCharacter();
if (lastCharacter !== '\n' && lastCharacter !== '') {
this.#writeNewLine();
}
}
/**
* Adds up to two newlines to ensure that there is a blank line above the current line.
*/
public ensureSkippedLine(): void {
if (this.peekLastCharacter() !== '\n') {
this.#writeNewLine();
}
const secondLastCharacter: string = this.peekSecondLastCharacter();
if (secondLastCharacter !== '\n' && secondLastCharacter !== '') {
this.#writeNewLine();
}
}
/**
* Returns the last character that was written, or an empty string if no characters have been written yet.
*/
public peekLastCharacter(): string {
if (this.#latestChunk !== undefined) {
return this.#latestChunk.substr(-1, 1);
}
return '';
}
/**
* Returns the second to last character that was written, or an empty string if less than one characters
* have been written yet.
*/
public peekSecondLastCharacter(): string {
if (this.#latestChunk !== undefined) {
if (this.#latestChunk.length > 1) {
return this.#latestChunk.substr(-2, 1);
}
if (this.#previousChunk !== undefined) {
return this.#previousChunk.substr(-1, 1);
}
}
return '';
}
/**
* Writes `before` and `after` messages if and only if `mayWrite` writes anything.
*
* If `mayWrite` writes "CONTENT", this method will write "<before>CONTENT<after>".
* If `mayWrite` writes nothing, this method will write nothing.
*/
public writeTentative(before: string, after: string, mayWrite: () => void): void {
this.#beforeStack.push(before);
// If this function writes anything, then _all_ messages in the "before stack" will also be
// written. This means that the stack will be empty (as when we write a message from the stack,
// we remove it from the stack).
mayWrite();
// If the stack is not empty, it means that `mayWrite` didn't write anything. Pop the last-
// added message from the stack, we'll never write it. Otherwise, if the stack is empty, then
// write the "after" message.
if (this.#beforeStack.length > 0) {
this.#beforeStack.pop();
} else {
this.write(after);
}
}
/**
* 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.
*/
public write(message: string): void {
if (message.length === 0) {
return;
}
if (!this.#isWritingBeforeStack) {
this.#writeBeforeStack();
}
// If there are no newline characters, then append the string verbatim
if (!/[\r\n]/.test(message)) {
this.#writeLinePart(message);
return;
}
// Otherwise split the lines and write each one individually
let first: boolean = true;
for (const linePart of message.split('\n')) {
if (!first) {
this.#writeNewLine();
} else {
first = false;
}
if (linePart) {
this.#writeLinePart(linePart.replace(/[\r]/g, ''));
}
}
}
/**
* A shorthand for writing an optional message, followed by a newline.
* Indentation is applied following the semantics of IndentedWriter.write().
*/
public writeLine(message: string = ''): void {
if (message.length > 0) {
this.write(message);
} else if (!this.#isWritingBeforeStack) {
this.#writeBeforeStack();
}
this.#writeNewLine();
}
/**
* Writes a string that does not contain any newline characters.
*/
#writeLinePart(message: string): void {
if (message.length > 0) {
if (this.#atStartOfLine && this.#indentText.length > 0) {
this.#write(this.#indentText);
}
this.#write(message);
this.#atStartOfLine = false;
}
}
#writeNewLine(): void {
if (this.#atStartOfLine && this.#indentText.length > 0) {
this.#write(this.#indentText);
}
this.#write('\n');
this.#atStartOfLine = true;
}
#write(s: string): void {
this.#previousChunk = this.#latestChunk;
this.#latestChunk = s;
this.#builder.append(s);
}
/**
* Writes all messages in our before stack, processing them in FIFO order. This stack is
* populated by the `writeTentative` method.
*/
#writeBeforeStack(): void {
this.#isWritingBeforeStack = true;
for (const message of this.#beforeStack) {
this.write(message);
}
this.#isWritingBeforeStack = false;
this.#beforeStack = [];
}
#updateIndentText(): void {
this.#indentText = this.#indentStack.join('');
}
}