Fix out of bounds cursor checking

This commit is contained in:
Andras Schmelczer 2025-04-13 22:23:28 +01:00
parent 7c081e8939
commit 438f0b4d97
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -14,13 +14,18 @@ export function positionToLineAndColumn(
throw new Error("Position cannot be negative");
}
if (position > text.length) {
text = text.replace("\r", "");
if (
position >
text.length + 1
// +1 to account for the cursor being after last character
) {
throw new Error(
`Position ${position} exceeds text length ${text.length}`
);
}
text = text.replace("\r", "");
const textUpToPosition = text.substring(0, position);
const lines = textUpToPosition.split("\n");