-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathrevisualizeOnCustomRegexChange.ts
More file actions
62 lines (59 loc) · 2.03 KB
/
revisualizeOnCustomRegexChange.ts
File metadata and controls
62 lines (59 loc) · 2.03 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
import {
Disposable,
ScopeProvider,
ScopeTypeInfo,
disposableFrom,
} from "@cursorless/common";
import { isEqual } from "lodash";
import {
ScopeVisualizer,
VisualizationType,
} from "./ScopeVisualizerCommandApi";
/**
* Attempts to ensure that the scope visualizer is still visualizing the same
* scope type after the user changes one of their custom regexes. Because custom
* regexes don't have a unique identifier, we have to do some guesswork to
* figure out which custom regex the user changed. This function look for a
* custom regex with the same spoken form as the one that was changed, and if it
* finds one, it starts visualizing that one instead.
*
* @param scopeVisualizer The scope visualizer to listen to
* @param scopeProvider Provides scope information
* @returns A {@link Disposable} which will stop the callback from running
*/
export function revisualizeOnCustomRegexChange(
scopeVisualizer: ScopeVisualizer,
scopeProvider: ScopeProvider,
): Disposable {
let currentRegexScopeInfo: ScopeTypeInfo | undefined;
let currentVisualizationType: VisualizationType | undefined;
return disposableFrom(
scopeVisualizer.onDidChangeScopeType((scopeType, visualizationType) => {
currentRegexScopeInfo =
scopeType?.type === "customRegex"
? scopeProvider.getScopeInfo(scopeType)
: undefined;
currentVisualizationType = visualizationType;
}),
scopeProvider.onDidChangeScopeInfo((scopeInfos) => {
if (
currentRegexScopeInfo != null &&
!scopeInfos.some((scopeInfo) =>
isEqual(scopeInfo.scopeType, currentRegexScopeInfo!.scopeType),
)
) {
const replacement = scopeInfos.find(
(scopeInfo) =>
scopeInfo.scopeType.type === "customRegex" &&
isEqual(scopeInfo.spokenForm, currentRegexScopeInfo!.spokenForm),
);
if (replacement != null) {
scopeVisualizer.start(
replacement.scopeType,
currentVisualizationType!,
);
}
}
}),
);
}