File size: 727 Bytes
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
26
const TAB_STOP = 2;

export function scorerSpanToOffsets(lineText, colStart, colEnd) {
  const startCol = colStart - 1;
  const endCol = colEnd - 1;
  let expandedCol = 0;
  let from = null;
  let index = 0;

  while (index < lineText.length) {
    const char = String.fromCodePoint(lineText.codePointAt(index));
    const width = char === "\t" ? TAB_STOP - (expandedCol % TAB_STOP) : 1;
    const nextCol = expandedCol + width;
    if (from === null && nextCol > startCol) {
      from = index;
    }
    if (nextCol > endCol) {
      return { from, to: index + char.length };
    }
    expandedCol = nextCol;
    index += char.length;
  }
  const lineEnd = lineText.length;
  return { from: from ?? lineEnd, to: lineEnd };
}