-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathScopeInfoProvider.ts
More file actions
191 lines (173 loc) · 5 KB
/
Copy pathScopeInfoProvider.ts
File metadata and controls
191 lines (173 loc) · 5 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { pull } from "lodash-es";
import type {
Disposable,
ScopeType,
ScopeTypeInfo,
ScopeTypeInfoEventCallback,
SurroundingPairScopeType,
} from "@cursorless/lib-common";
import {
isPseudoScopeType,
simpleScopeTypeTypes,
surroundingPairNames,
} from "@cursorless/lib-common";
import type { CustomSpokenFormGeneratorImpl } from "../generateSpokenForm/CustomSpokenFormGeneratorImpl";
import { scopeTypeToString } from "./scopeTypeToString";
/**
* Maintains a list of all scope types and notifies listeners when it changes.
*/
export class ScopeInfoProvider {
private disposable: Disposable;
private listeners: ScopeTypeInfoEventCallback[] = [];
private scopeInfos!: ScopeTypeInfo[];
constructor(
private customSpokenFormGenerator: CustomSpokenFormGeneratorImpl,
) {
this.disposable = customSpokenFormGenerator.onDidChangeCustomSpokenForms(
() => this.onChange(),
);
this.onDidChangeScopeInfo = this.onDidChangeScopeInfo.bind(this);
this.getScopeTypeInfo = this.getScopeTypeInfo.bind(this);
this.updateScopeTypeInfos();
}
/**
* Registers a callback to be run when the scope info changes. The callback
* will be run immediately once with the current scope info.
*
* Includes information about the available scopes, including their custom
* spoken forms, if available. Note that even custom regex scopes will be
* available, as reported to the engine by Talon.
* @param callback The callback to run when the scope support changes
* @returns A {@link Disposable} which will stop the callback from running
*/
onDidChangeScopeInfo(callback: ScopeTypeInfoEventCallback): Disposable {
callback(this.getScopeTypeInfos());
this.listeners.push(callback);
return {
dispose: () => {
pull(this.listeners, callback);
},
};
}
private onChange() {
this.updateScopeTypeInfos();
for (const listener of this.listeners.slice()) {
listener(this.scopeInfos);
}
}
private updateScopeTypeInfos(): void {
const scopeTypes: ScopeType[] = [
...simpleScopeTypeTypes
// Create simple scope types from simple scope type types
.map((scopeTypeType) => ({ type: scopeTypeType }))
// Ignore pseudo-scope because it's not really a scope
.filter((scopeType) => !isPseudoScopeType(scopeType)),
...surroundingPairNames.map(
(surroundingPairName): SurroundingPairScopeType => ({
type: "surroundingPair",
delimiter: surroundingPairName,
}),
),
...this.customSpokenFormGenerator.getCustomRegexScopeTypes(),
];
this.scopeInfos = scopeTypes.map((scopeType) =>
this.getScopeTypeInfo(scopeType),
);
}
getScopeTypeInfos(): ScopeTypeInfo[] {
return this.scopeInfos;
}
getScopeTypeInfo(scopeType: ScopeType): ScopeTypeInfo {
return {
scopeType,
spokenForm:
this.customSpokenFormGenerator.scopeTypeToSpokenForm(scopeType),
humanReadableName: scopeTypeToString(scopeType),
isLanguageSpecific: isLanguageSpecific(scopeType),
};
}
dispose() {
this.disposable.dispose();
}
}
/**
* @param scopeType The scope type to check
* @returns A boolean indicating whether the given scope type is defined on a
* per-language basis.
*/
function isLanguageSpecific(scopeType: ScopeType): boolean {
switch (scopeType.type) {
case "string":
case "argumentOrParameter":
case "argumentList":
case "anonymousFunction":
case "attribute":
case "branch":
case "class":
case "className":
case "collectionItem":
case "collectionKey":
case "command":
case "comment":
case "private.fieldAccess":
case "functionCall":
case "functionCallee":
case "functionName":
case "ifStatement":
case "instance":
case "interior":
case "list":
case "map":
case "name":
case "namedFunction":
case "regularExpression":
case "statement":
case "type":
case "value":
case "condition":
case "section":
case "sectionLevelOne":
case "sectionLevelTwo":
case "sectionLevelThree":
case "sectionLevelFour":
case "sectionLevelFive":
case "sectionLevelSix":
case "selector":
case "unit":
case "xmlBothTags":
case "xmlElement":
case "xmlEndTag":
case "xmlStartTag":
case "part":
case "chapter":
case "subSection":
case "subSubSection":
case "namedParagraph":
case "subParagraph":
case "environment":
case "textFragment":
case "disqualifyDelimiter":
case "pairDelimiter":
return true;
case "character":
case "word":
case "token":
case "identifier":
case "line":
case "fullLine":
case "sentence":
case "paragraph":
case "boundedParagraph":
case "document":
case "nonWhitespaceSequence":
case "boundedNonWhitespaceSequence":
case "url":
case "notebookCell":
case "surroundingPair":
case "surroundingPairInterior":
case "customRegex":
case "glyph":
return false;
// No default
}
}