(
{ rootSource, names }: RenderResult,
indentation: string
)
| 163 | } |
| 164 | |
| 165 | export function serializeRenderResult( |
| 166 | { rootSource, names }: RenderResult, |
| 167 | indentation: string |
| 168 | ): SerializedRenderResult { |
| 169 | let indent = 0; |
| 170 | let indentNeeded = 0; |
| 171 | |
| 172 | const lines: string[] = []; |
| 173 | let currentLine: string[] = []; |
| 174 | const annotations: Annotation[] = []; |
| 175 | |
| 176 | function indentIfNeeded(): void { |
| 177 | if (indentNeeded === 0) return; |
| 178 | currentLine.push(_.repeat(indentation, indentNeeded)); |
| 179 | indentNeeded = 0; |
| 180 | } |
| 181 | |
| 182 | function flattenCurrentLine(): string { |
| 183 | const str = currentLine.join(""); |
| 184 | currentLine = [str]; |
| 185 | return str; |
| 186 | } |
| 187 | |
| 188 | function currentLocation(): Location { |
| 189 | return { line: lines.length, column: flattenCurrentLine().length }; |
| 190 | } |
| 191 | |
| 192 | function finishLine(): void { |
| 193 | lines.push(flattenCurrentLine()); |
| 194 | currentLine = []; |
| 195 | } |
| 196 | |
| 197 | function serializeToStringArray(source: Source): void { |
| 198 | switch (source.kind) { |
| 199 | case "text": |
| 200 | indentIfNeeded(); |
| 201 | currentLine.push(source.text); |
| 202 | break; |
| 203 | case "newline": |
| 204 | finishLine(); |
| 205 | indent += source.indentationChange; |
| 206 | indentNeeded = indent; |
| 207 | break; |
| 208 | case "sequence": |
| 209 | source.sequence.forEach((s: Source) => serializeToStringArray(s)); |
| 210 | break; |
| 211 | case "table": |
| 212 | const t = source.table; |
| 213 | const widths = t |
| 214 | .map((l: List<Source>) => l.map((s: Source) => sourceLineLength(s, names)).toList()) |
| 215 | .toList(); |
| 216 | const numRows = t.size; |
| 217 | if (numRows === 0) break; |
| 218 | const numColumns = defined(t.map((l: List<Source>) => l.size).max()); |
| 219 | if (numColumns === 0) break; |
| 220 | const columnWidths = defined( |
| 221 | Range(0, numColumns).map((i: number) => widths.map((l: List<number>) => l.get(i) || 0).max()) |
| 222 | ); |
no test coverage detected
searching dependent graphs…