Skip to content

Commit

Permalink
feat: Implemented file Quest Editor saving
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-coster committed Nov 8, 2023
1 parent 1fe432c commit 77f99b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
9 changes: 3 additions & 6 deletions packages/cl2-editor/src/gc.fs.mts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,13 @@ export class GameChangerFs implements vscode.FileSystemProvider {
createDirectory(uri: vscode.Uri): void | Thenable<void> {
throw new Error('CreateDir not implemented.');
}
writeFile(
uri: vscode.Uri,
content: Uint8Array,
options: { readonly create: boolean; readonly overwrite: boolean },
): void | Thenable<void> {
async writeFile(uri: vscode.Uri, content: Uint8Array) {
const doc = this.getMoteDoc(uri);
assertInternalClaim(isQuestMote(doc.mote), 'Only quests are supported.');
if (!doc.parseResults?.saved) {
if (doc.parseResults?.diagnostics.length !== 0) {
throw new Error('Cannot save until issues are resolved.');
}
await doc.save(content.toString());
}
delete(
uri: vscode.Uri,
Expand Down
34 changes: 18 additions & 16 deletions packages/cl2-editor/src/quests.doc.mts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import {
QuestUpdateResult,
isQuestMote,
parseStringifiedQuest,
updateChangesFromParsedQuest,
type Crashlands2,
type GameChanger,
type Mote,
} from '@bscotch/gcdata';
import { stringifyQuest } from '@bscotch/gcdata/dist/cl2.quest.stringify.js';
import vscode from 'vscode';
import { assertInternalClaim } from './assert.mjs';
import { assertInternalClaim, assertLoudly } from './assert.mjs';
import { diagnostics } from './diagnostics.mjs';
import {
filterRanges,
Expand All @@ -22,8 +23,6 @@ import type { CrashlandsWorkspace } from './workspace.mjs';
export class QuestDocument {
static cache = new Map<string, QuestDocument>();

protected debouncedSave: NodeJS.Timeout | undefined;

protected constructor(
readonly uri: vscode.Uri,
readonly mote: Mote<Crashlands2.Quest>,
Expand Down Expand Up @@ -199,23 +198,26 @@ export class QuestDocument {
this.parse(this.document?.getText());
}

async parse(content?: string) {
/** Save the last-parsed content to the changes file */
async save(content: string) {
this.parse(content);
assertLoudly(
this.parseResults?.diagnostics.length === 0,
'Cannot save a quest with errors.',
);
await updateChangesFromParsedQuest(
this.parseResults.parsed,
this.mote.id,
this.packed,
);
}

parse(content?: string) {
try {
if (!content) {
content = this.toString();
}
this.parseResults = await parseStringifiedQuest(
content!,
this.mote.id,
this.packed,
);
if (this.parseResults.saved && this.document) {
clearTimeout(this.debouncedSave);
this.debouncedSave = setTimeout(
() => this.parseResults?.saved && this.document?.save(),
100,
);
}
this.parseResults = parseStringifiedQuest(content!, this.packed);

// Apply any edits
for (const edit of this.parseResults.edits) {
Expand Down

0 comments on commit 77f99b9

Please sign in to comment.