(text: string, pattern: string)
| 3 | import { Position } from "source-map"; |
| 4 | |
| 5 | export function lineAndColumnOf(text: string, pattern: string): Position { |
| 6 | const pos = text.indexOf(pattern); |
| 7 | if (pos === -1) { |
| 8 | return { line: -1, column: -1 }; |
| 9 | } |
| 10 | |
| 11 | const lineLengths = text.split("\n").map(s => s.length); |
| 12 | |
| 13 | let totalPos = 0; |
| 14 | for (let line = 1; line <= lineLengths.length; line++) { |
| 15 | // Add + 1 for the removed \n |
| 16 | const lineLength = lineLengths[line - 1] + 1; |
| 17 | if (pos < totalPos + lineLength) { |
| 18 | return { line, column: pos - totalPos }; |
| 19 | } |
| 20 | |
| 21 | totalPos += lineLengths[line - 1] + 1; |
| 22 | } |
| 23 | |
| 24 | return { line: -1, column: -1 }; |
| 25 | } |
no outgoing calls
no test coverage detected