line: 1..., column: 1...
(line: number, column: number, message?: string)
| 82 | |
| 83 | /** line: 1..., column: 1... */ |
| 84 | sourceAround(line: number, column: number, message?: string): string { |
| 85 | const linesAbove = 2; |
| 86 | const linesBelow = 3; |
| 87 | const start = Math.max(line - 1 - (linesAbove + 1), 0); |
| 88 | const end = Math.min(this.lines.length, line + linesBelow) - 1; |
| 89 | |
| 90 | const hasColumn = typeof column === 'number'; |
| 91 | const numberMaxWidth = String(end).length; |
| 92 | const result: string[] = []; |
| 93 | // index = 0..., start = 0... end = 0... |
| 94 | for (let index = start; index <= end; index++) { |
| 95 | const paddedNumber = ` ${index + 1}`.slice(-numberMaxWidth); |
| 96 | const gutter = ` ${paddedNumber} \u2506 `; |
| 97 | let markerLine = ''; |
| 98 | if (index === line - 1) { |
| 99 | if (hasColumn) { |
| 100 | const markerSpacing = this.lines[index] |
| 101 | .slice(0, Math.max(column - 1, 0)) |
| 102 | .replace(/[^\t]/g, ' '); |
| 103 | markerLine = |
| 104 | '\n ' + |
| 105 | this.chalkGutter(gutter.replace(/\d/g, ' ')) + |
| 106 | markerSpacing + |
| 107 | this.chalkMarker('^'); |
| 108 | |
| 109 | if (message) { |
| 110 | markerLine += ' ' + this.chalkMessage(message); |
| 111 | } |
| 112 | } else if (message) { |
| 113 | markerLine = |
| 114 | '\n' + this.chalkGutter(gutter.replace(/\d/g, ' ')) + message; |
| 115 | } |
| 116 | result.push( |
| 117 | [ |
| 118 | this.chalkMarker('>'), |
| 119 | this.chalkGutter(gutter), |
| 120 | this.lines[index], |
| 121 | markerLine, |
| 122 | ].join('') |
| 123 | ); |
| 124 | } else { |
| 125 | result.push(` ${this.chalkGutter(gutter)}${this.lines[index]}`); |
| 126 | } |
| 127 | } |
| 128 | return terminal.renderBlock({ |
| 129 | tag: 'block', |
| 130 | spans: result.map((line) => ({ content: line + '\n' })), |
| 131 | }); |
| 132 | /** |
| 133 | 1111 | expect( |
| 134 | 12 | rawExpression('\\sqrt{(1+x_0)}=\\frac{\\pi^2}{2}') |
| 135 | > 13 | ).toMatchInlineSnapshot( |
| 136 | | ^ |
| 137 | 14 | `'["Latex","\\\\sqrt","<{>","(",1,"+","x","_",0,")","<}>","=","\\\\frac","<{>","\\\\pi","^",2,"<}>","<{>",2,"<}>"]'` |
| 138 | 15 | ); |
| 139 | 16 | }); |
| 140 | */ |
| 141 | } |
no test coverage detected