Repository navigation
Expand file tree
/
Copy pathHatTokenMapImpl.ts
More file actions
159 lines (136 loc) · 4.67 KB
/
Copy pathHatTokenMapImpl.ts
File metadata and controls
159 lines (136 loc) · 4.67 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import type {
CommandServerApi,
HatAllocationOptions,
HatTokenMap,
Hats,
IDE,
ReadOnlyHatMap,
TokenHat,
} from "@cursorless/lib-common";
import type { TokenGraphemeSplitter } from "../tokenGraphemeSplitter";
import type { Debug } from "./Debug";
import { HatAllocator } from "./HatAllocator";
import { IndividualHatMap } from "./IndividualHatMap";
import type { RangeUpdater } from "./updateSelections/RangeUpdater";
/**
* Maximum age for the pre-phrase snapshot before we consider it to be stale. 60s.
*/
const PRE_PHRASE_SNAPSHOT_MAX_AGE_MS = 60_000;
/**
* Maps from (hatStyle, character) pairs to tokens
*/
export class HatTokenMapImpl implements HatTokenMap {
/**
* This is the active map that changes every time we reallocate hats. It is
* liable to change in the middle of a phrase.
*/
private activeMap: IndividualHatMap;
/**
* This is a snapshot of the hat map that remains stable over the course of a
* phrase. Ranges will be updated to account for changes to the document, but a
* hat with the same color and shape will refer to the same logical range.
*/
private prePhraseMapSnapshot?: IndividualHatMap;
private prePhraseMapsSnapshotTimestamp: number | null = null;
private lastSignalVersion: string | null = null;
private hatAllocator: HatAllocator;
constructor(
private ide: IDE,
tokenGraphemeSplitter: TokenGraphemeSplitter,
rangeUpdater: RangeUpdater,
private debug: Debug,
hats: Hats,
private commandServerApi: CommandServerApi,
) {
ide.disposeOnExit(this);
this.activeMap = new IndividualHatMap(
ide,
tokenGraphemeSplitter,
rangeUpdater,
);
this.getActiveMap = this.getActiveMap.bind(this);
this.allocateHats = this.allocateHats.bind(this);
this.hatAllocator = new HatAllocator(ide, tokenGraphemeSplitter, hats, {
getActiveMap: this.getActiveMap,
});
}
/**
* Allocate hats to the visible tokens.
*
* @param forceTokenHats If supplied, force the allocator to use these hats
* for the given tokens. This is used for the tutorial, and for testing.
*/
allocateHats(forceTokenHats?: TokenHat[], options?: HatAllocationOptions) {
return this.hatAllocator.allocateHats(forceTokenHats, options);
}
private async getActiveMap() {
// NB: We need to take a snapshot of the hat map before we make any
// modifications if it is the beginning of the phrase
await this.maybeTakePrePhraseSnapshot();
return this.activeMap;
}
/**
* Returns a transient, read-only hat map for use during the course of a
* single command.
*
* Please do not hold onto this copy beyond the lifetime of a single command,
* because it will get stale.
* @param usePrePhraseSnapshot Whether to use pre-phrase snapshot
* @returns A readable snapshot of the map
*/
async getReadableMap(usePrePhraseSnapshot: boolean): Promise<ReadOnlyHatMap> {
// NB: Take a snapshot before we return the map if it is the beginning of
// the phrase so all commands will get the same map over the course of the
// phrase
await this.maybeTakePrePhraseSnapshot();
if (usePrePhraseSnapshot) {
if (this.lastSignalVersion == null) {
console.error(
"Pre phrase snapshot requested but no signal was present; please upgrade command client",
);
return this.activeMap;
}
if (this.prePhraseMapSnapshot == null) {
console.error(
"Navigation map pre-phrase snapshot requested, but no snapshot has been taken",
);
return this.activeMap;
}
if (
performance.now() - this.prePhraseMapsSnapshotTimestamp! >
PRE_PHRASE_SNAPSHOT_MAX_AGE_MS
) {
console.error(
"Navigation map pre-phrase snapshot requested, but snapshot is more than a minute old",
);
return this.activeMap;
}
return this.prePhraseMapSnapshot;
}
return this.activeMap;
}
public dispose() {
this.activeMap.dispose();
if (this.prePhraseMapSnapshot != null) {
this.prePhraseMapSnapshot.dispose();
}
}
private async maybeTakePrePhraseSnapshot() {
const newSignalVersion =
await this.commandServerApi.signals.prePhrase.getVersion();
if (newSignalVersion !== this.lastSignalVersion) {
this.debug.log("Taking snapshot");
this.lastSignalVersion = newSignalVersion;
if (newSignalVersion != null) {
this.takePrePhraseSnapshot();
}
}
}
private takePrePhraseSnapshot() {
if (this.prePhraseMapSnapshot != null) {
this.prePhraseMapSnapshot.dispose();
}
this.prePhraseMapSnapshot = this.activeMap.clone();
this.prePhraseMapsSnapshotTimestamp = performance.now();
}
}