Skip to content

Commit

Permalink
[twisty] Use Symbol for parsed alg char index tracking avoid exposi…
Browse files Browse the repository at this point in the history
…ng them publicly.

This also prevents them from showing up as normal fields `Alg` when logged directly.

This is also not meant to be a long-term workaround, as the parsing metadata should all be attached directly to the `Alg` rather than individual parts.
  • Loading branch information
lgarron committed Nov 30, 2024
1 parent 132d8e0 commit 400c287
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 33 deletions.
30 changes: 19 additions & 11 deletions src/cubing/alg/parseAlg.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 = "," | ":" | "]" | ")";

Expand All @@ -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 extends Alg | AlgNode> = T & ParserIndexed;
Expand All @@ -49,17 +52,19 @@ function addCharIndices<T extends Alg | AlgNode>(
endCharIndex: number,
): Parsed<T> {
const parsedT = t as ParserIndexed & T;
parsedT.startCharIndex = startCharIndex;
parsedT.endCharIndex = endCharIndex;
parsedT[startCharIndexKey] = startCharIndex;
parsedT[endCharIndexKey] = endCharIndex;
return parsedT;
}

export function transferCharIndex<T extends Alg | AlgNode>(from: T, to: T): T {
if ("startCharIndex" in from) {
(to as Parsed<T>).startCharIndex = (from as Parsed<T>).startCharIndex;
if (startCharIndexKey in from) {
(to as Parsed<T>)[startCharIndexKey] = (from as Parsed<T>)[
startCharIndexKey
];
}
if ("endCharIndex" in from) {
(to as Parsed<T>).endCharIndex = (from as Parsed<T>).endCharIndex;
if (endCharIndexKey in from) {
(to as Parsed<T>)[endCharIndexKey] = (from as Parsed<T>)[endCharIndexKey];
}
return to;
}
Expand All @@ -84,7 +89,10 @@ class AlgParser {
}
}
const newAlg = new Alg(algNodes) as Parsed<Alg>;
const { startCharIndex, endCharIndex } = alg;
const {
[startCharIndexKey]: startCharIndex,
[endCharIndexKey]: endCharIndex,
} = alg;
addCharIndices(newAlg, startCharIndex, endCharIndex);
return newAlg;
}
Expand Down
14 changes: 9 additions & 5 deletions src/cubing/twisty/views/TwistyAlgEditor/TwistyAlgEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down
18 changes: 11 additions & 7 deletions src/cubing/twisty/views/TwistyAlgEditor/model.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/cubing/twisty/views/TwistyAlgViewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -255,7 +255,7 @@ class AlgToDOMTree extends TraversalDownUp<DataDown, DataUp, DataUp> {
true,
);
dataDown.twistyAlgViewer.highlighter.addMove(
(move as Parsed<Move>).startCharIndex,
(move as Parsed<Move>)[startCharIndexKey],
element,
);
return {
Expand Down Expand Up @@ -387,7 +387,7 @@ class MoveHighlighter {

set(move: Parsed<Move> | null): void {
const newElem = move
? (this.moveCharIndexMap.get(move.startCharIndex) ?? null)
? (this.moveCharIndexMap.get(move[startCharIndexKey]) ?? null)
: null;
if (this.currentElem === newElem) {
return;
Expand Down Expand Up @@ -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<Parsed<Alg>>)
startCharIndexKey in (sourceAlg as Partial<Parsed<Alg>>)
? sourceAlg
: Alg.fromString(sourceAlg.toString());
this.setAlg(parsedAlg);
Expand Down
19 changes: 13 additions & 6 deletions src/sites/experiments.cubing.net/cubing.js/alg/inspector.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -22,15 +26,18 @@ function updateInspector(s: string): void {
const parsed = v as Parsed<Alg | AlgNode>;
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) {
Expand Down

0 comments on commit 400c287

Please sign in to comment.