Skip to content

Commit

Permalink
Fix byte-offset vs char offset mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Mar 11, 2024
1 parent 24d0f8a commit 8223b1a
Showing 1 changed file with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export const preprocess: GlintExtensionPreprocess<PreprocessData> = (source, pat
let sourceOffsetBytes = 0;
let deltaBytes = 0;

// @ts-expect-error TS couldn't find @types/node, which are specified
// in the root package.json
let sourceBuffer = Buffer.from(source);
let sourceBuffer = getBuffer(source);

for (let template of templates) {
let startTagLengthBytes = template.startRange.end - template.startRange.start;
Expand Down Expand Up @@ -69,16 +67,13 @@ export const preprocess: GlintExtensionPreprocess<PreprocessData> = (source, pat

sourceOffsetBytes = endTagOffsetBytes + endTagLengthBytes;

// TODO: is there a way to convert bytes to chars?
// I think maybe all of this code needs to live in content-tag,
// and give us the option to generate this sort of structure
templateLocations.push({
startTagOffset: startTagOffsetBytes,
endTagOffset: endTagOffsetBytes,
startTagLength: startTagLengthBytes,
endTagLength: endTagLengthBytes,
transformedStart: transformedStartBytes,
transformedEnd,
startTagOffset: byteToCharIndex(source, startTagOffsetBytes),
endTagOffset: byteToCharIndex(source, endTagOffsetBytes),
startTagLength: byteToCharIndex(source, startTagLengthBytes),
endTagLength: byteToCharIndex(source, endTagLengthBytes),
transformedStart: byteToCharIndex(source, transformedStartBytes),
transformedEnd: byteToCharIndex(source, transformedEnd),
});
}

Expand All @@ -91,3 +86,19 @@ export const preprocess: GlintExtensionPreprocess<PreprocessData> = (source, pat
},
};
};

function byteToCharIndex(str: string, byteOffset: number): number {
const buf = getBuffer(str);
return buf.slice(0, byteOffset).toString().length;
}

const BufferMap = new Map();

function getBuffer(str: string): Buffer {
let buf = BufferMap.get(str);
if (!buf) {
buf = Buffer.from(str);
BufferMap.set(str, buf);
}
return buf;
}

0 comments on commit 8223b1a

Please sign in to comment.