| 46 | } |
| 47 | |
| 48 | export class OutputWriter implements IOutputWriter { |
| 49 | private written: OutputWriterContent[] = []; |
| 50 | private indentations: Indentation[] = []; |
| 51 | |
| 52 | get writtenLength(): number { |
| 53 | return this.written.length; |
| 54 | } |
| 55 | |
| 56 | append(str: string | IOutputWriter) { |
| 57 | this.written.push(str); |
| 58 | } |
| 59 | |
| 60 | content(): string { |
| 61 | return outputContent(this.written); |
| 62 | } |
| 63 | |
| 64 | beginIndent(indentation: string) { |
| 65 | const written = this.written; |
| 66 | this.written = []; |
| 67 | this.indentations.push({ indentation, previouslyWritten: written }); |
| 68 | } |
| 69 | |
| 70 | endIndent() { |
| 71 | const indentation = this.indentations.pop(); |
| 72 | if (!indentation) { |
| 73 | throw new Error('Unbalanced beginIndent/endIndent'); |
| 74 | } |
| 75 | const writtenWithIdentation = this.written; |
| 76 | this.written = indentation.previouslyWritten; |
| 77 | if (writtenWithIdentation.length) { |
| 78 | this.append(new IndentedContent(writtenWithIdentation, indentation.indentation)); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | withIndentation(indentation: string, cb: () => void) { |
| 83 | this.beginIndent(indentation); |
| 84 | cb(); |
| 85 | this.endIndent(); |
| 86 | } |
| 87 | } |
nothing calls this directly
no outgoing calls
no test coverage detected