// Helper functions
import { Position } from "source-map";
export function lineAndColumnOf(text: string, pattern: string): Position {
const pos = text.indexOf(pattern);
if (pos === -1) {
return { line: -1, column: -1 };
}
const lineLengths = text.split("\n").map(s => s.length);
let totalPos = 0;
for (let line = 1; line <= lineLengths.length; line++) {
// Add + 1 for the removed \n
const lineLength = lineLengths[line - 1] + 1;
if (pos < totalPos + lineLength) {
return { line, column: pos - totalPos };
}
totalPos += lineLengths[line - 1] + 1;
}
return { line: -1, column: -1 };
}