Format files with prettier

This commit is contained in:
Andras Schmelczer 2025-07-06 22:21:17 +01:00
commit 1ce0f9c059
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C

View file

@ -1,54 +1,54 @@
import wasmInit, {
CursorPosition as wasmCursorPosition,
reconcile as wasmReconcile,
TextWithCursors as wasmTextWithCursors,
SpanWithHistory as wasmSpanWithHistory,
BuiltinTokenizer,
reconcileWithHistory as wasmReconcileWithHistory,
History,
InitInput,
} from "reconcile";
CursorPosition as wasmCursorPosition,
reconcile as wasmReconcile,
TextWithCursors as wasmTextWithCursors,
SpanWithHistory as wasmSpanWithHistory,
BuiltinTokenizer,
reconcileWithHistory as wasmReconcileWithHistory,
History,
InitInput,
} from 'reconcile';
export interface TextWithCursors {
/** The document's entire content */
text: string;
/** List of cursor positions, can be null or undefined if there are no cursors */
cursors: null | undefined | CursorPosition[];
/** The document's entire content */
text: string;
/** List of cursor positions, can be null or undefined if there are no cursors */
cursors: null | undefined | CursorPosition[];
}
export interface CursorPosition {
/** Unique identifier for the cursor */
id: number;
/** Character position in the text, 0-based */
position: number;
/** Unique identifier for the cursor */
id: number;
/** Character position in the text, 0-based */
position: number;
}
export interface TextWithCursorsAndHistory {
/** The document's entire content */
text: string;
/** List of cursor positions, can be null or undefined if there are no cursors */
cursors: null | undefined | CursorPosition[];
/** List of operations leading to `text` from the 3 ancestors */
history: SpanWithHistory[];
/** The document's entire content */
text: string;
/** List of cursor positions, can be null or undefined if there are no cursors */
cursors: null | undefined | CursorPosition[];
/** List of operations leading to `text` from the 3 ancestors */
history: SpanWithHistory[];
}
export interface SpanWithHistory {
/** Span of text associated with the historical opearion */
text: string;
/** Origin of the `text` span */
history: History;
/** Span of text associated with the historical opearion */
text: string;
/** Origin of the `text` span */
history: History;
}
export type Tokenizer = "Line" | "Word" | "Character";
const TOKENIZERS = ["Line", "Word", "Character"];
export type Tokenizer = 'Line' | 'Word' | 'Character';
const TOKENIZERS = ['Line', 'Word', 'Character'];
let isInitialised = false;
const UNINITIALISED_MODULE_ERROR =
"Reconcile module has not been initialized. Please call init() before using any other functions.";
'Reconcile module has not been initialized. Please call init() before using any other functions.';
const UNSUPPORTED_TOKENIZER_ERROR = `Unsupported tokenizer. Only ${TOKENIZERS.join(
", "
', '
)} are supported.`;
/**
@ -61,13 +61,13 @@ const UNSUPPORTED_TOKENIZER_ERROR = `Unsupported tokenizer. Only ${TOKENIZERS.jo
* @returns Promise that resolves when initialization is complete
*/
export async function init(content?: InitInput) {
if (isInitialised) {
return;
}
if (isInitialised) {
return;
}
await wasmInit(content);
await wasmInit(content);
isInitialised = true;
isInitialised = true;
}
/**
@ -80,31 +80,31 @@ export async function init(content?: InitInput) {
* @returns The reconciled text with merged cursor positions
*/
export function reconcile(
original: string,
left: string | TextWithCursors,
right: string | TextWithCursors,
tokenizer: BuiltinTokenizer = "Word"
original: string,
left: string | TextWithCursors,
right: string | TextWithCursors,
tokenizer: BuiltinTokenizer = 'Word'
): TextWithCursors {
if (!isInitialised) {
throw new Error(UNINITIALISED_MODULE_ERROR);
}
if (!isInitialised) {
throw new Error(UNINITIALISED_MODULE_ERROR);
}
if (!TOKENIZERS.includes(tokenizer)) {
throw new Error(UNSUPPORTED_TOKENIZER_ERROR);
}
if (!TOKENIZERS.includes(tokenizer)) {
throw new Error(UNSUPPORTED_TOKENIZER_ERROR);
}
const leftCursor = toWasmTextWithCursors(left);
const rightCursor = toWasmTextWithCursors(right);
const leftCursor = toWasmTextWithCursors(left);
const rightCursor = toWasmTextWithCursors(right);
const result = wasmReconcile(original, leftCursor, rightCursor, tokenizer);
const result = wasmReconcile(original, leftCursor, rightCursor, tokenizer);
leftCursor.free();
rightCursor.free();
leftCursor.free();
rightCursor.free();
const jsResult = toTextWithCursors(result);
result.free();
const jsResult = toTextWithCursors(result);
result.free();
return jsResult;
return jsResult;
}
/**
@ -119,83 +119,66 @@ export function reconcile(
* @returns The reconciled text with cursor positions and history of changes
*/
export function reconcileWithHistory(
original: string,
left: string | TextWithCursors,
right: string | TextWithCursors,
tokenizer: BuiltinTokenizer = "Word"
original: string,
left: string | TextWithCursors,
right: string | TextWithCursors,
tokenizer: BuiltinTokenizer = 'Word'
): TextWithCursorsAndHistory {
if (!isInitialised) {
throw new Error(UNINITIALISED_MODULE_ERROR);
}
if (!isInitialised) {
throw new Error(UNINITIALISED_MODULE_ERROR);
}
if (!TOKENIZERS.includes(tokenizer)) {
throw new Error(UNSUPPORTED_TOKENIZER_ERROR);
}
if (!TOKENIZERS.includes(tokenizer)) {
throw new Error(UNSUPPORTED_TOKENIZER_ERROR);
}
const leftCursor = toWasmTextWithCursors(left);
const rightCursor = toWasmTextWithCursors(right);
const leftCursor = toWasmTextWithCursors(left);
const rightCursor = toWasmTextWithCursors(right);
const result = wasmReconcileWithHistory(
original,
leftCursor,
rightCursor,
tokenizer
);
const result = wasmReconcileWithHistory(original, leftCursor, rightCursor, tokenizer);
leftCursor.free();
rightCursor.free();
leftCursor.free();
rightCursor.free();
const jsResult = toTextWithCursors(result);
const history = result.history().map(toSpanWithHistory);
result.free();
const jsResult = toTextWithCursors(result);
const history = result.history().map(toSpanWithHistory);
result.free();
return {
...jsResult,
history,
};
return {
...jsResult,
history,
};
}
function toWasmTextWithCursors(
text: string | TextWithCursors
): wasmTextWithCursors {
const isInputString = typeof text == "string";
const leftText = isInputString ? text : text.text;
const leftCursors = isInputString ? [] : text.cursors ?? [];
function toWasmTextWithCursors(text: string | TextWithCursors): wasmTextWithCursors {
const isInputString = typeof text == 'string';
const leftText = isInputString ? text : text.text;
const leftCursors = isInputString ? [] : (text.cursors ?? []);
return new wasmTextWithCursors(
leftText,
leftCursors.map(toWasmCursorPosition)
);
return new wasmTextWithCursors(leftText, leftCursors.map(toWasmCursorPosition));
}
function toWasmCursorPosition({
id,
position,
}: CursorPosition): wasmCursorPosition {
return new wasmCursorPosition(id, position);
function toWasmCursorPosition({ id, position }: CursorPosition): wasmCursorPosition {
return new wasmCursorPosition(id, position);
}
function toTextWithCursors(
textWithCursor: wasmTextWithCursors
): TextWithCursors {
return {
text: textWithCursor.text(),
cursors: textWithCursor.cursors().map(toCursorPosition),
};
function toTextWithCursors(textWithCursor: wasmTextWithCursors): TextWithCursors {
return {
text: textWithCursor.text(),
cursors: textWithCursor.cursors().map(toCursorPosition),
};
}
function toCursorPosition(cursor: wasmCursorPosition): CursorPosition {
return {
id: cursor.id(),
position: cursor.characterIndex(),
};
return {
id: cursor.id(),
position: cursor.characterIndex(),
};
}
function toSpanWithHistory(
textWithHistory: wasmSpanWithHistory
): SpanWithHistory {
return {
text: textWithHistory.text(),
history: textWithHistory.history(),
};
function toSpanWithHistory(textWithHistory: wasmSpanWithHistory): SpanWithHistory {
return {
text: textWithHistory.text(),
history: textWithHistory.history(),
};
}