codehygiene / web /src /columns.js
brianbellx's picture
Deploy validated CodeHygiene app, stats and leaderboard
161cb23 verified
Raw History Blame Contribute Delete
727 Bytes
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 };
}