-
-
Notifications
You must be signed in to change notification settings - Fork 185
Expand file tree
/
Copy pathutils.ts
More file actions
25 lines (19 loc) · 682 Bytes
/
utils.ts
File metadata and controls
25 lines (19 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 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 };
}