diff --git a/src/cubing/alg/parseAlg.ts b/src/cubing/alg/parseAlg.ts index a931b4dd6..3a8aeed78 100644 --- a/src/cubing/alg/parseAlg.ts +++ b/src/cubing/alg/parseAlg.ts @@ -1,6 +1,4 @@ import { Alg } from "./Alg"; -import { AlgBuilder } from "./AlgBuilder"; -import { algDebugGlobals } from "./debug"; import type { AlgNode } from "./alg-nodes"; import { Commutator } from "./alg-nodes/containers/Commutator"; import { Conjugate } from "./alg-nodes/containers/Conjugate"; @@ -9,6 +7,8 @@ import { LineComment } from "./alg-nodes/leaves/LineComment"; import { Move, QuantumMove } from "./alg-nodes/leaves/Move"; import { Newline } from "./alg-nodes/leaves/Newline"; import { Pause } from "./alg-nodes/leaves/Pause"; +import { AlgBuilder } from "./AlgBuilder"; +import { algDebugGlobals } from "./debug"; type StoppingChar = "," | ":" | "]" | ")"; @@ -35,9 +35,12 @@ export function parseQuantumMove(s: string): QuantumMove { return new AlgParser().parseQuantumMove(s); } +export const startCharIndexKey = Symbol("startCharIndex"); +export const endCharIndexKey = Symbol("endCharIndex"); + export interface ParserIndexed { - startCharIndex: number; - endCharIndex: number; + [startCharIndexKey]: number; + [endCharIndexKey]: number; } export type Parsed = T & ParserIndexed; @@ -49,17 +52,19 @@ function addCharIndices( endCharIndex: number, ): Parsed { const parsedT = t as ParserIndexed & T; - parsedT.startCharIndex = startCharIndex; - parsedT.endCharIndex = endCharIndex; + parsedT[startCharIndexKey] = startCharIndex; + parsedT[endCharIndexKey] = endCharIndex; return parsedT; } export function transferCharIndex(from: T, to: T): T { - if ("startCharIndex" in from) { - (to as Parsed).startCharIndex = (from as Parsed).startCharIndex; + if (startCharIndexKey in from) { + (to as Parsed)[startCharIndexKey] = (from as Parsed)[ + startCharIndexKey + ]; } - if ("endCharIndex" in from) { - (to as Parsed).endCharIndex = (from as Parsed).endCharIndex; + if (endCharIndexKey in from) { + (to as Parsed)[endCharIndexKey] = (from as Parsed)[endCharIndexKey]; } return to; } @@ -84,7 +89,10 @@ class AlgParser { } } const newAlg = new Alg(algNodes) as Parsed; - const { startCharIndex, endCharIndex } = alg; + const { + [startCharIndexKey]: startCharIndex, + [endCharIndexKey]: endCharIndex, + } = alg; addCharIndices(newAlg, startCharIndex, endCharIndex); return newAlg; } diff --git a/src/cubing/twisty/views/TwistyAlgEditor/TwistyAlgEditor.ts b/src/cubing/twisty/views/TwistyAlgEditor/TwistyAlgEditor.ts index 281048032..ce1399615 100644 --- a/src/cubing/twisty/views/TwistyAlgEditor/TwistyAlgEditor.ts +++ b/src/cubing/twisty/views/TwistyAlgEditor/TwistyAlgEditor.ts @@ -13,7 +13,11 @@ import type { ExperimentalParsed } from "../../../alg"; import { Alg, type Move, type Pause } from "../../../alg"; -import type { Parsed } from "../../../alg/parseAlg"; +import { + endCharIndexKey, + startCharIndexKey, + type Parsed, +} from "../../../alg/parseAlg"; import type { AlgProp, AlgWithIssues, @@ -219,14 +223,14 @@ export class TwistyAlgEditor extends ManagedCustomElement { this.#highlightedLeaf = leaf; this.#carbonCopyPrefix.textContent = this.#textarea.value.slice( 0, - leaf.startCharIndex, + leaf[startCharIndexKey], ); this.#carbonCopyHighlight.textContent = this.#textarea.value.slice( - leaf.startCharIndex, - leaf.endCharIndex, + leaf[startCharIndexKey], + leaf[endCharIndexKey], ); this.#carbonCopySuffix.textContent = this.#padSuffix( - this.#textarea.value.slice(leaf.endCharIndex), + this.#textarea.value.slice(leaf[endCharIndexKey]), ); this.#carbonCopyHighlight.hidden = false; } diff --git a/src/cubing/twisty/views/TwistyAlgEditor/model.ts b/src/cubing/twisty/views/TwistyAlgEditor/model.ts index 154ed6ed4..c0c6b8b90 100644 --- a/src/cubing/twisty/views/TwistyAlgEditor/model.ts +++ b/src/cubing/twisty/views/TwistyAlgEditor/model.ts @@ -1,7 +1,11 @@ // TODO: Move this? import type { Alg } from "../../../alg"; -import type { Parsed } from "../../../alg/parseAlg"; +import { + endCharIndexKey, + type Parsed, + startCharIndexKey, +} from "../../../alg/parseAlg"; import { type AlgWithIssues, algWithIssuesFromString, @@ -122,13 +126,13 @@ class LeafToHighlightProp extends TwistyPropDerived< return null; } let where: HighlightWhere; - if (inputs.targetChar < leafInfo.leaf.startCharIndex) { + if (inputs.targetChar < leafInfo.leaf[startCharIndexKey]) { where = "before"; - } else if (inputs.targetChar === leafInfo.leaf.startCharIndex) { + } else if (inputs.targetChar === leafInfo.leaf[startCharIndexKey]) { where = "start"; - } else if (inputs.targetChar < leafInfo.leaf.endCharIndex) { + } else if (inputs.targetChar < leafInfo.leaf[endCharIndexKey]) { where = "inside"; - } else if (inputs.targetChar === leafInfo.leaf.endCharIndex) { + } else if (inputs.targetChar === leafInfo.leaf[endCharIndexKey]) { where = "end"; } else { where = "after"; @@ -143,12 +147,12 @@ class LeafToHighlightProp extends TwistyPropDerived< // TODO: binary search for (const leafInfo of inputs.leafTokens) { if ( - inputs.targetChar < leafInfo.leaf.startCharIndex && + inputs.targetChar < leafInfo.leaf[startCharIndexKey] && lastLeafInfo !== null ) { return withWhere(lastLeafInfo); } - if (inputs.targetChar <= leafInfo.leaf.endCharIndex) { + if (inputs.targetChar <= leafInfo.leaf[endCharIndexKey]) { return withWhere(leafInfo); } lastLeafInfo = leafInfo; diff --git a/src/cubing/twisty/views/TwistyAlgViewer.ts b/src/cubing/twisty/views/TwistyAlgViewer.ts index 5ee8439b0..07071dcd5 100644 --- a/src/cubing/twisty/views/TwistyAlgViewer.ts +++ b/src/cubing/twisty/views/TwistyAlgViewer.ts @@ -15,7 +15,7 @@ import { ExperimentalIterationDirection, experimentalDirect, } from "../../alg/cubing-private"; -import type { Parsed } from "../../alg/parseAlg"; +import { startCharIndexKey, type Parsed } from "../../alg/parseAlg"; import type { MillisecondTimestamp } from "../controllers/AnimationTypes"; import type { CurrentMoveInfo } from "../controllers/indexer/AlgIndexer"; import type { AlgWithIssues } from "../model/props/puzzle/state/AlgProp"; @@ -255,7 +255,7 @@ class AlgToDOMTree extends TraversalDownUp { true, ); dataDown.twistyAlgViewer.highlighter.addMove( - (move as Parsed).startCharIndex, + (move as Parsed)[startCharIndexKey], element, ); return { @@ -387,7 +387,7 @@ class MoveHighlighter { set(move: Parsed | null): void { const newElem = move - ? (this.moveCharIndexMap.get(move.startCharIndex) ?? null) + ? (this.moveCharIndexMap.get(move[startCharIndexKey]) ?? null) : null; if (this.currentElem === newElem) { return; @@ -455,7 +455,7 @@ export class TwistyAlgViewer extends HTMLElementShim { .alg; // TODO: Use proper architecture instead of a heuristic to ensure we have a parsed alg annotated with char indices. const parsedAlg = - "startCharIndex" in (sourceAlg as Partial>) + startCharIndexKey in (sourceAlg as Partial>) ? sourceAlg : Alg.fromString(sourceAlg.toString()); this.setAlg(parsedAlg); diff --git a/src/sites/experiments.cubing.net/cubing.js/alg/inspector.ts b/src/sites/experiments.cubing.net/cubing.js/alg/inspector.ts index fcfafd6f0..577b06fae 100644 --- a/src/sites/experiments.cubing.net/cubing.js/alg/inspector.ts +++ b/src/sites/experiments.cubing.net/cubing.js/alg/inspector.ts @@ -1,5 +1,9 @@ import { Alg, type AlgNode } from "../../../../cubing/alg"; -import type { Parsed } from "../../../../cubing/alg/parseAlg"; +import { + endCharIndexKey, + startCharIndexKey, + type Parsed, +} from "../../../../cubing/alg/parseAlg"; import { extract } from "./extractor"; const algElem = document.querySelector("#alg") as HTMLTextAreaElement; @@ -22,15 +26,18 @@ function updateInspector(s: string): void { const parsed = v as Parsed; inspectorElem.textContent += "\n"; inspectorElem.textContent += `${name}: `.padStart(12, " "); - inspectorElem.textContent += "".padStart(parsed.startCharIndex, " "); + inspectorElem.textContent += "".padStart(parsed[startCharIndexKey], " "); inspectorElem.textContent += singleLineS.slice( - parsed.startCharIndex, - parsed.endCharIndex, + parsed[startCharIndexKey], + parsed[endCharIndexKey], ); inspectorElem.textContent += "\n"; - inspectorElem.textContent += "".padEnd(12 + parsed.startCharIndex, " "); + inspectorElem.textContent += "".padEnd( + 12 + parsed[startCharIndexKey], + " ", + ); inspectorElem.textContent += bracket( - parsed.endCharIndex - parsed.startCharIndex, + parsed[endCharIndexKey] - parsed[startCharIndexKey], ); } } catch (e) {