Repository navigation
Expand file tree
/
Copy pathsetupStep.ts
More file actions
133 lines (116 loc) · 3.44 KB
/
Copy pathsetupStep.ts
File metadata and controls
133 lines (116 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import type {
CharacterRange,
HatTokenMap,
IDE,
TestCaseSnapshot,
TextEditor,
TutorialState,
} from "@cursorless/lib-common";
import {
plainObjectToRange,
plainObjectToSelection,
serializedMarksToTokenHats,
toCharacterRange,
} from "@cursorless/lib-common";
import type { TutorialContent } from "./types/tutorial.types";
interface HighlightedEditor {
editor: TextEditor | undefined;
highlightRanges: CharacterRange[];
}
/**
* Set up the current step. For example, if the current step requires that the
* user has an editor open with certain content, this function will ensure that
* the editor is open and has the correct content.
*
* Initially tries to reuse the existing editor if it's already open. If that
* fails, it will try again with a new editor.
*
* @param hatTokenMap The hat token map to use for allocating hats.
* @param editor The current editor, if any.
* @param state The current tutorial state.
* @param currentTutorial The current tutorial, if any.
* @returns The editor that was set up, or `undefined` if no editor was set up.
* If the current editor was reused, it will be returned.
*/
export async function setupStep(
ide: IDE,
hatTokenMap: HatTokenMap,
editor: TextEditor | undefined,
state: TutorialState,
currentTutorial: TutorialContent | undefined,
): Promise<HighlightedEditor> {
if (state.type !== "doingTutorial") {
return { editor: undefined, highlightRanges: [] };
}
if (currentTutorial == null) {
throw new Error("No current tutorial found");
}
const step = currentTutorial.steps[state.stepNumber];
if (step == null) {
throw new Error(
`No tutorial step found for step number ${state.stepNumber}`,
);
}
const { initialState: snapshot, languageId = "plaintext" } = step;
if (snapshot == null) {
return { editor, highlightRanges: [] };
}
return await applySnapshot(ide, hatTokenMap, editor, snapshot, languageId);
}
async function applySnapshot(
ide: IDE,
hatTokenMap: HatTokenMap,
editorIn: TextEditor | undefined,
snapshot: TestCaseSnapshot,
languageId: string,
): Promise<HighlightedEditor> {
const retry = editorIn != null;
let editor = editorIn;
try {
if (editor == null) {
editor = await ide.openUntitledTextDocument({
content: snapshot.documentContents,
language: languageId,
});
}
const editableEditor = ide.getEditableTextEditor(editor);
if (editableEditor.document.languageId !== languageId) {
throw new Error(
`Expected language id ${languageId}, but got ${editableEditor.document.languageId}`,
);
}
await editableEditor.edit([
{
range: editableEditor.document.range,
text: snapshot.documentContents,
isReplace: true,
},
]);
// Ensure that the expected cursor/selections are present
await editableEditor.setSelections(
snapshot.selections.map(plainObjectToSelection),
);
// Ensure that the expected hats are present
await hatTokenMap.allocateHats(
serializedMarksToTokenHats(snapshot.marks, editor),
);
await editableEditor.focus();
return {
editor,
highlightRanges: Object.values(snapshot.marks ?? {}).map((range) =>
toCharacterRange(plainObjectToRange(range)),
),
};
} catch (error) {
if (retry) {
return await applySnapshot(
ide,
hatTokenMap,
undefined,
snapshot,
languageId,
);
}
throw error;
}
}