Skip to content

Commit

Permalink
pnpm fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
LyonSyonII committed Nov 21, 2024
1 parent 12d718c commit 1a9576f
Show file tree
Hide file tree
Showing 16 changed files with 166 additions and 269 deletions.
2 changes: 1 addition & 1 deletion frontend/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"indentStyle": "space",
"indentWidth": 2,
"lineEnding": "lf",
"lineWidth": 80,
"lineWidth": 100,
"attributePosition": "auto"
},
"linter": {
Expand Down
27 changes: 8 additions & 19 deletions frontend/src/components/Checkpoint/checkpoint.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import * as idb from "idb-keyval";

const store = idb.createStore(
"rustquest-checkpoints",
"rustquest-checkpoints-store",
);
const store = idb.createStore("rustquest-checkpoints", "rustquest-checkpoints-store");

export async function add(id: string) {
const level = getLevel(id);
Expand Down Expand Up @@ -58,30 +55,22 @@ export async function getAll() {
}

export async function stringifyStore(): Promise<string> {
const entries = (await idb.entries(store)).map(
([k, v]: [IDBValidKey, Set<string>]) => [
k,
JSON.stringify([...v.values()]),
],
);
const entries = (await idb.entries(store)).map(([k, v]: [IDBValidKey, Set<string>]) => [
k,
JSON.stringify([...v.values()]),
]);
return JSON.stringify(entries);
}

/** Parses a specified JSON, sets the store to the parsed values and returns it. */
export async function parseStore(
json: string,
): Promise<[IDBValidKey, Set<string>][]> {
export async function parseStore(json: string): Promise<[IDBValidKey, Set<string>][]> {
const parsed = JSON.parse(json) as [IDBValidKey, string][];
const entries: [IDBValidKey, Set<string>][] = parsed.map(([k, v]) => [
k,
new Set(JSON.parse(v)),
]);
const entries: [IDBValidKey, Set<string>][] = parsed.map(([k, v]) => [k, new Set(JSON.parse(v))]);
await idb.setMany(entries, store);
return entries;
}

const subscribed: Map<string, ((checkpoints: Set<string>) => Promise<void>)[]> =
new Map();
const subscribed: Map<string, ((checkpoints: Set<string>) => Promise<void>)[]> = new Map();

async function callSubscribed(level: string) {
const events = subscribed.get(level);
Expand Down
6 changes: 1 addition & 5 deletions frontend/src/components/CodeBlock/CodeBlock.astro
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ const {
showOutput,
} = Astro.props;
const code = (
Astro.props.code !== undefined
? Astro.props.code
: (await importQuestion(id)).code
)
const code = (Astro.props.code !== undefined ? Astro.props.code : (await importQuestion(id)).code)
.trim()
.replaceAll("$\\n", "");
Expand Down
113 changes: 33 additions & 80 deletions frontend/src/components/CodeBlock/CodeBlock.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import type {
Compartment,
EditorState,
Extension,
RangeSet,
Text,
} from "@codemirror/state";
import type {
Decoration,
EditorView,
ViewPlugin,
ViewUpdate,
} from "@codemirror/view";
import type { Compartment, EditorState, Extension, RangeSet, Text } from "@codemirror/state";
import type { Decoration, EditorView, ViewPlugin, ViewUpdate } from "@codemirror/view";

import {
type CodeQuestion,
cleanProtectedCode,
importQuestion,
mc,
mo,
getModifiableSelection,
getModifiableRanges,
getProtectedRanges,
getNearestModifiable,
getNearestModifiableInLine,
getProtectedRanges,
importQuestion,
mc,
mo,
} from "src/content/questions/CodeQuestion";
import { onThemeChange } from "src/utils/onThemeChange";
import { log } from "src/utils/popup";
Expand All @@ -38,10 +28,8 @@ export class CodeBlock extends HTMLElement {
code = "";
rangeProtected = false;
setup = "__VALUE__";
validator: (
value: string,
test: (regex: RegExp) => boolean,
) => string | undefined = () => undefined;
validator: (value: string, test: (regex: RegExp) => boolean) => string | undefined = () =>
undefined;
onsuccess: (stdout: string, value: string) => void = () => {};
errorMsg: string;

Expand Down Expand Up @@ -90,22 +78,14 @@ export class CodeBlock extends HTMLElement {
}

async loadCodemirror() {
const { closeBrackets, closeBracketsKeymap } = await import(
"@codemirror/autocomplete"
);
const { closeBrackets, closeBracketsKeymap } = await import("@codemirror/autocomplete");
const { defaultKeymap, history, historyKeymap, insertTab } = await import(
"@codemirror/commands"
);
const { rust } = await import("@codemirror/lang-rust");
const { bracketMatching, foldKeymap, indentOnInput } = await import(
"@codemirror/language"
);
const { bracketMatching, foldKeymap, indentOnInput } = await import("@codemirror/language");
const { highlightSelectionMatches } = await import("@codemirror/search");
const {
Compartment,
EditorState: _EditorState,
RangeSet,
} = await import("@codemirror/state");
const { Compartment, EditorState: _EditorState, RangeSet } = await import("@codemirror/state");
const {
EditorView,
highlightActiveLine,
Expand All @@ -120,8 +100,7 @@ export class CodeBlock extends HTMLElement {

this.EditorState = _EditorState;

const lineNums =
this.getAttribute("showLineNumbers") === "true" ? lineNumbers() : [];
const lineNums = this.getAttribute("showLineNumbers") === "true" ? lineNumbers() : [];
const basicSetup = [
lineNums,
highlightActiveLineGutter(),
Expand All @@ -130,9 +109,7 @@ export class CodeBlock extends HTMLElement {
indentOnInput(),
bracketMatching(),
closeBrackets(),
rectangularSelection(),
highlightActiveLine(),
highlightSelectionMatches(),
keymap.of([
...closeBracketsKeymap,
...defaultKeymap.filter((k) => k.key !== "Mod-Enter"),
Expand Down Expand Up @@ -180,9 +157,7 @@ export class CodeBlock extends HTMLElement {
rust(),
basicSetup,
runKeymap,
placeholder(
this.getAttribute("placeholder") || "PLACEHOLDER NOT DEFINED",
),
placeholder(this.getAttribute("placeholder") || "PLACEHOLDER NOT DEFINED"),
this.theme.of(await this.importTheme(theme)),
this.readonly.of(this.EditorState.readOnly.of(!editable)),
EditorView.editable.of(editable),
Expand All @@ -198,8 +173,7 @@ export class CodeBlock extends HTMLElement {
public setProps({ setup, vars = [], validator, onsuccess }: CodeQuestion) {
const replaceVars = (r: string) =>
vars.reduce(
(acc, { v, d, c = (v) => v }) =>
acc.replaceAll(`$${v}`, c(localStorage.getItem(v) || d)),
(acc, { v, d, c = (v) => v }) => acc.replaceAll(`$${v}`, c(localStorage.getItem(v) || d)),
r,
);
this.code = replaceVars(this.getAttribute("code") || this.code);
Expand All @@ -221,9 +195,7 @@ export class CodeBlock extends HTMLElement {

public setReadonly(readonly: boolean) {
this.editor.dispatch({
effects: this.readonly.reconfigure(
this.EditorState.readOnly.of(readonly),
),
effects: this.readonly.reconfigure(this.EditorState.readOnly.of(readonly)),
});
}

Expand Down Expand Up @@ -303,12 +275,8 @@ export class CodeBlock extends HTMLElement {
/** Returns `undefined` if the validation was successful or a `string` with the error. */
public async validateSnippet(snippet: string): Promise<string | undefined> {
snippet = cleanProtectedCode(snippet);
const v = this.validator(
snippet,
(regex: RegExp, ignoreWhitespace = false) =>
ignoreWhitespace
? regex.test(snippet.replaceAll(/\s/g, ""))
: regex.test(snippet),
const v = this.validator(snippet, (regex: RegExp, ignoreWhitespace = false) =>
ignoreWhitespace ? regex.test(snippet.replaceAll(/\s/g, "")) : regex.test(snippet),
)?.trim();

return v;
Expand Down Expand Up @@ -371,9 +339,7 @@ function rangeHighlighter(
}

readonlyDec() {
return this.readonly.map(([start, end]) =>
_Decoration.mark({}).range(start, end),
);
return this.readonly.map(([start, end]) => _Decoration.mark({}).range(start, end));
}

modifiableDec() {
Expand Down Expand Up @@ -428,26 +394,15 @@ const domHandlers = ({ domEventHandlers }: typeof EditorView) =>
}
return false;
},
click(event, view) {
// TODO: Fix error when clicking too much at the left of a modifiable section and warping up
/* console.log(`Selected line ${view.state.doc.lineAt(view.state.selection.main.head).number}`);
const doc = view.state.doc;
const pos = view.state.selection.main.head;
const line = doc.lineAt(pos).number;
const { leftModifiable, rightModifiable } = getLeftRightModifiable(doc.toString(), pos);
const nearestModifiable = getNearestModifiable(doc, pos, leftModifiable, rightModifiable, line);
const something = view.lineBlockAtHeight(event.clientY);
console.log({something, maybeLine: doc.lineAt(something.to).number});
view.dispatch({ selection: { head: nearestModifiable, anchor: nearestModifiable } }); */
},
});

const navigationExtension = ({ transactionFilter }: typeof EditorState) =>
transactionFilter.of((tr) => {
if (tr.docChanged || !tr.selection) return tr;

// allow selecting all text
if (Math.abs(tr.newSelection.main.from - tr.newSelection.main.to) > 1) {
if (Math.abs(tr.newSelection.main.from - tr.newSelection.main.to) > 0) {
console.log(tr.newSelection.ranges);
return tr;
}

Expand All @@ -462,11 +417,8 @@ const navigationExtension = ({ transactionFilter }: typeof EditorState) =>
const line = doc.lineAt(pos);
const newLine = doc.lineAt(newPos);
const lineDist = line.number - newLine.number;

if (
!tr.isUserEvent("select.pointer") &&
Math.abs(line.number - newLine.number) > 1
) {

if (!tr.isUserEvent("select.pointer") && Math.abs(line.number - newLine.number) > 1) {
// Solve line skip bug
let nearestLine = line;
if (lineDist > 1) {
Expand All @@ -476,19 +428,20 @@ const navigationExtension = ({ transactionFilter }: typeof EditorState) =>
}
const col = pos - line.from;
const newPos = Math.min(nearestLine.to, nearestLine.from + col);
const nearest = getNearestModifiableInLine(
newPos,
modifiableRanges,
nearestLine,
);
const { nearest, index } = getNearestModifiableInLine(newPos, modifiableRanges, nearestLine);
console.log({nearest, index, newPos});
// if modifiable section found in line
if (nearest !== Number.POSITIVE_INFINITY)
return { selection: { anchor: nearest, head: nearest } };
return getModifiableSelection(nearest, modifiableRanges[index], doc);
}

const nearest = getNearestModifiable(newPos, modifiableRanges);

// TODO: Consider if worth adding logic for handling multiple lines when going up (preserve column)

// get nearest modifiable section and go to it
const { nearest, index } = getNearestModifiable(newPos, modifiableRanges);
console.log({nearest, index, newPos, newSelection: tr.newSelection});
if (nearest === Number.POSITIVE_INFINITY) return [];
return { selection: { anchor: nearest, head: nearest } };
return getModifiableSelection(nearest, modifiableRanges[index], doc);
});

const protectedRangesExtension = ({ changeFilter }: typeof EditorState) =>
Expand Down
Loading

0 comments on commit 1a9576f

Please sign in to comment.