Move more logic into sync-client

This commit is contained in:
Andras Schmelczer 2025-08-30 11:02:04 +01:00
parent 3f089bd37e
commit 9177984ff6
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
20 changed files with 68 additions and 143 deletions

View file

@ -1,36 +0,0 @@
/**
* Converts a character position in text to line and column numbers.
*
* @param text The text content to analyze
* @param position The character position to convert
* @returns An object containing line and column numbers
* @throws Will throw an error if the position is negative or exceeds the text length
*/
export function positionToLineAndColumn(
text: string,
position: number
): { line: number; column: number } {
if (position < 0) {
throw new Error("Position cannot be negative");
}
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}`
);
}
const textUpToPosition = text.substring(0, position);
const lines = textUpToPosition.split("\n");
const line = lines.length - 1;
const column = lines[lines.length - 1].length;
return { line, column };
}