-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathgetSnapshotForComparison.ts
More file actions
67 lines (59 loc) · 2.02 KB
/
Copy pathgetSnapshotForComparison.ts
File metadata and controls
67 lines (59 loc) · 2.02 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
import { storedTargetKeys } from "../StoredTargetKey";
import type { SpyIDE } from "../ide/spy/SpyIDE";
import type { ReadOnlyHatMap } from "../types/HatTokenMap";
import type { TestHelpers } from "../types/TestHelpers";
import { marksToPlainObject } from "../util/toPlainObject";
import type {
ExcludableSnapshotField,
TestCaseSnapshot,
} from "./TestCaseSnapshot";
import { extractTargetedMarks } from "./extractTargetedMarks";
/**
* Get the state of the editor to compare with the expected state of a test case
*
* @param finalState The final state of the test case; we only use this to
* decide which fields we care about
* @param readableHatMap The hat map for extractin the marks
* @param spyIde The spy IDE
* @param takeSnapshot A function that takes a snapshot of the current state of
* the editor
* @returns A test case snapshot with any fields excluded that we don't care
* about for now
*/
export async function getSnapshotForComparison(
finalState: TestCaseSnapshot | undefined,
readableHatMap: ReadOnlyHatMap,
spyIde: SpyIDE,
takeSnapshot: TestHelpers["takeSnapshot"],
): Promise<Exclude<TestCaseSnapshot, "visibleRanges">> {
const excludeFields: ExcludableSnapshotField[] = [];
const marks =
finalState?.marks == null
? undefined
: marksToPlainObject(
extractTargetedMarks(Object.keys(finalState.marks), readableHatMap),
);
if (finalState?.clipboard == null) {
excludeFields.push("clipboard");
}
for (const storedTargetKey of storedTargetKeys) {
const key = `${storedTargetKey}Mark` as const;
if (finalState?.[key] == null) {
excludeFields.push(key);
}
}
const editor = spyIde.activeTextEditor;
if (editor == null) {
throw new Error("No active text editor found in SpyIDE");
}
// FIXME Visible ranges are not asserted, see:
// https://github.com/cursorless-dev/cursorless/issues/160
const { visibleRanges, ...resultState } = await takeSnapshot(
excludeFields,
[],
editor,
spyIde,
marks,
);
return resultState;
}