Skip to content

Commit 031494f

Browse files
Added debug logging to tutorial precondition validation (#3371)
1 parent 0eaf0c1 commit 031494f

5 files changed

Lines changed: 53 additions & 24 deletions

File tree

‎packages/lib-common/src/ide/normalized/NormalizedIDE.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ export class NormalizedIDE extends PassthroughIDE {
2626
}
2727

2828
private initializeConfiguration() {
29+
if (this.runMode === "development") {
30+
this.configuration.mockConfiguration("debug", true);
31+
}
2932
this.configuration.mockConfigurationScope(
3033
{ languageId: "css" },
3134
{ wordSeparators: ["_", "-"] },

‎packages/lib-engine/src/core/HatTokenMapImpl.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class HatTokenMapImpl implements HatTokenMap {
139139
await this.commandServerApi.signals.prePhrase.getVersion();
140140

141141
if (newSignalVersion !== this.lastSignalVersion) {
142-
this.debug.log("taking snapshot");
142+
this.debug.log("Taking snapshot");
143143
this.lastSignalVersion = newSignalVersion;
144144

145145
if (newSignalVersion != null) {

‎packages/lib-tutorial/src/TutorialImpl.ts‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ export class TutorialImpl implements Tutorial, CommandRunnerDecorator {
278278

279279
async resume() {
280280
await this.setupStep();
281-
await this.checkPreconditions();
282281
}
283282

284283
async list() {
@@ -353,17 +352,30 @@ export class TutorialImpl implements Tutorial, CommandRunnerDecorator {
353352

354353
const currentStep = this.currentTutorial!.steps[state.stepNumber];
355354

356-
const preConditionsMet = await arePreconditionsMet(
355+
const preconditionError = await arePreconditionsMet(
357356
this.ide.activeTextEditor,
358357
this.editor,
359358
this.hatTokenMap,
360359
currentStep,
361360
);
362361

362+
// Verify that the state hasn't changed since we started checking preconditions
363363
if (this.state_ !== state) {
364+
this.debug(
365+
`Tutorial ${state.id} step ${state.stepNumber + 1}: precondition check discarded because the tutorial state changed`,
366+
);
364367
return;
365368
}
366369

370+
const preConditionsMet = preconditionError == null;
371+
372+
if (!preConditionsMet) {
373+
this.debug(
374+
`Tutorial ${state.id} step ${state.stepNumber + 1}: ${preconditionError}`,
375+
);
376+
}
377+
378+
// Update the state if the preconditions have changed
367379
if (preConditionsMet !== state.preConditionsMet) {
368380
this.setState({
369381
...state,
@@ -372,4 +384,11 @@ export class TutorialImpl implements Tutorial, CommandRunnerDecorator {
372384
await this.ensureHighlights();
373385
}
374386
}
387+
388+
private debug(message: string) {
389+
const debug = this.ide.configuration.getOwnConfiguration("debug");
390+
if (debug) {
391+
console.log(message);
392+
}
393+
}
375394
}

‎packages/lib-tutorial/src/arePreconditionsMet.ts‎

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,56 @@ import type { TutorialStep } from "./types/tutorial.types";
1313
* @param editor The editor to check.
1414
* @param hatTokenMap The hat token map to use for checking if hats are correct.
1515
* @param step The tutorial step whose prerequesites are to be checked.
16-
* @returns `true` if the preconditions are met, `false` otherwise.
16+
* @returns A description of the first failed precondition, or `undefined` if
17+
* all preconditions are met.
1718
*/
1819
export async function arePreconditionsMet(
1920
activeTextEditor: TextEditor | undefined,
2021
editor: TextEditor | undefined,
2122
hatTokenMap: HatTokenMap,
2223
{ initialState: snapshot, languageId }: TutorialStep,
23-
): Promise<boolean> {
24+
): Promise<string | undefined> {
2425
if (snapshot == null) {
25-
return true;
26+
return undefined;
2627
}
2728

2829
if (activeTextEditor !== editor) {
29-
return false;
30+
return `Active editor differs from tutorial editor`;
3031
}
3132

32-
if (editor == null || editor.document.languageId !== languageId) {
33-
return false;
33+
if (editor == null) {
34+
return "Tutorial editor is unavailable";
35+
}
36+
37+
if (editor.document.languageId !== languageId) {
38+
return `Language differs (expected: ${languageId}, actual: ${editor.document.languageId})`;
3439
}
3540

3641
if (editor.document.getText() !== snapshot.documentContents) {
37-
return false;
42+
return "Document contents differ";
3843
}
3944

40-
if (
41-
!isEqual(editor.selections, snapshot.selections.map(plainObjectToSelection))
42-
) {
43-
return false;
45+
const expectedSelections = snapshot.selections.map(plainObjectToSelection);
46+
if (!isEqual(editor.selections, expectedSelections)) {
47+
const expected = expectedSelections
48+
.map((selection) => selection.concise())
49+
.join(", ");
50+
const actual = editor.selections
51+
.map((selection) => selection.concise())
52+
.join(", ");
53+
return `Selections differ (expected: ${expected}, actual: ${actual})`;
4454
}
4555

4656
const readableHatMap = await hatTokenMap.getReadableMap(false);
4757
for (const mark of serializedMarksToTokenHats(snapshot.marks, editor)) {
48-
if (
49-
!readableHatMap
50-
.getToken(mark.hatStyle, mark.grapheme)
51-
?.range.isRangeEqual(mark.hatRange)
52-
) {
53-
return false;
58+
const actualRange = readableHatMap.getToken(
59+
mark.hatStyle,
60+
mark.grapheme,
61+
)?.range;
62+
if (!actualRange?.isRangeEqual(mark.hatRange)) {
63+
return `Hat ${mark.hatStyle}.${mark.grapheme} differs (expected: ${mark.hatRange.concise()}, actual: ${actualRange?.concise() ?? "none"})`;
5464
}
5565
}
5666

57-
return true;
67+
return undefined;
5868
}

‎packages/test-vscode-e2e/src/suite/tutorial/tutorial.vscode.test.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,6 @@ async function runBasicTutorialTest(spyIde: SpyIDE) {
154154
// Test resuming tutorial
155155
await commands.executeCommand("cursorless.tutorial.resume");
156156
await checkStepSetup(fixtures[0]);
157-
const resumedStateEvent = getTutorialWebviewEventLog().at(-2);
158-
assert.ok(resumedStateEvent?.type === "messageSent");
159-
assert.equal(resumedStateEvent.data.preConditionsMet, true);
160157

161158
// Test automatic advancing
162159
await runCursorlessCommand({

0 commit comments

Comments
 (0)