-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathextension.ts
More file actions
326 lines (287 loc) · 9.08 KB
/
Copy pathextension.ts
File metadata and controls
326 lines (287 loc) · 9.08 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import type {
Disposable,
EnforceUndefined,
IDE,
Range,
ScopeProvider,
ScopeType,
TextDocument,
} from "@cursorless/common";
import {
FakeCommandServerApi,
FakeIDE,
NormalizedIDE,
type TreeSitter,
} from "@cursorless/common";
import type { EngineProps } from "@cursorless/cursorless-engine";
import {
CommandHistory,
createCursorlessEngine,
} from "@cursorless/cursorless-engine";
import {
FileSystemCommandHistoryStorage,
FileSystemRawTreeSitterQueryProvider,
FileSystemTalonSpokenForms,
getFixturePath,
} from "@cursorless/node-common";
import {
ScopeTestRecorder,
TestCaseRecorder,
} from "@cursorless/test-case-recorder";
import type { CursorlessApi, ParseTreeApi } from "@cursorless/vscode-common";
import {
getCommandServerApi,
getParseTreeApi,
toVscodeRange,
} from "@cursorless/vscode-common";
import * as crypto from "crypto";
import * as os from "node:os";
import * as path from "node:path";
import * as vscode from "vscode";
import { InstallationDependencies } from "./InstallationDependencies";
import { ReleaseNotes } from "./ReleaseNotes";
import { ScopeTreeProvider } from "./ScopeTreeProvider";
import type {
ScopeVisualizer,
ScopeVisualizerListener,
VisualizationType,
} from "./ScopeVisualizerCommandApi";
import { StatusBarItem } from "./StatusBarItem";
import { VscodeSnippets } from "./VscodeSnippets";
import { constructTestHelpers } from "./constructTestHelpers";
import { createTutorial } from "./createTutorial";
import type { VscodeScopeVisualizer } from "./ide/vscode/VSCodeScopeVisualizer";
import { createVscodeScopeVisualizer } from "./ide/vscode/VSCodeScopeVisualizer";
import { VscodeFileSystem } from "./ide/vscode/VscodeFileSystem";
import { VscodeIDE } from "./ide/vscode/VscodeIDE";
import { FakeFontMeasurements } from "./ide/vscode/hats/FakeFontMeasurements";
import { FontMeasurementsImpl } from "./ide/vscode/hats/FontMeasurementsImpl";
import { VscodeHats } from "./ide/vscode/hats/VscodeHats";
import { KeyboardCommands } from "./keyboard/KeyboardCommands";
import { registerCommands } from "./registerCommands";
import { revisualizeOnCustomRegexChange } from "./revisualizeOnCustomRegexChange";
import { storedTargetHighlighter } from "./storedTargetHighlighter";
import { vscodeApi } from "./vscodeApi";
import * as semver from "semver";
/**
* Extension entrypoint called by VSCode on Cursorless startup.
* - Creates a dependency container {@link Graph} with the components that
* implement Cursorless.
* - Creates test case recorder {@link TestCaseRecorder} for contributors to
* use to record test cases.
* - Creates an entrypoint for running commands {@link CommandRunner}.
*/
export async function activate(
context: vscode.ExtensionContext,
): Promise<CursorlessApi> {
const parseTreeApi = await getParseTreeApi();
const { vscodeIDE, hats, fileSystem } = await createVscodeIde(context);
const normalizedIde =
vscodeIDE.runMode === "production"
? vscodeIDE
: new NormalizedIDE(
vscodeIDE,
new FakeIDE(),
vscodeIDE.runMode === "test",
getFixturePath("cursorless-snippets"),
);
const fakeCommandServerApi = new FakeCommandServerApi();
const commandServerApi =
normalizedIde.runMode === "test"
? fakeCommandServerApi
: await getCommandServerApi();
const treeSitter = createTreeSitter(parseTreeApi);
const talonSpokenForms = new FileSystemTalonSpokenForms(fileSystem);
// NOTE: do not await on snippet loading and hats initialization because we don't want to
// block extension activation
const snippets = new VscodeSnippets(normalizedIde);
void snippets.init();
const treeSitterQueryProvider = new FileSystemRawTreeSitterQueryProvider(
normalizedIde,
fileSystem,
);
context.subscriptions.push(treeSitterQueryProvider);
const engineProps: EnforceUndefined<EngineProps> = {
ide: normalizedIde,
hats,
treeSitterQueryProvider,
treeSitter,
commandServerApi,
talonSpokenForms,
snippets,
};
const {
commandApi,
storedTargets,
hatTokenMap,
scopeProvider,
injectIde,
runIntegrationTests,
addCommandRunnerDecorator,
customSpokenFormGenerator,
} = await createCursorlessEngine(engineProps);
const commandHistoryStorage = new FileSystemCommandHistoryStorage(
fileSystem.cursorlessCommandHistoryDirPath,
);
addCommandRunnerDecorator(
new CommandHistory(normalizedIde, commandHistoryStorage, commandServerApi),
);
const testCaseRecorder = new TestCaseRecorder(
commandServerApi,
hatTokenMap,
storedTargets,
);
addCommandRunnerDecorator(testCaseRecorder);
const scopeTestRecorder = new ScopeTestRecorder(normalizedIde);
const statusBarItem = StatusBarItem.create("cursorless.showQuickPick");
const keyboardCommands = KeyboardCommands.create(
context,
vscodeApi,
statusBarItem,
);
const scopeVisualizer = createScopeVisualizer(normalizedIde, scopeProvider);
context.subscriptions.push(
revisualizeOnCustomRegexChange(scopeVisualizer, scopeProvider),
);
new ScopeTreeProvider(
vscodeApi,
context,
scopeProvider,
scopeVisualizer,
customSpokenFormGenerator,
commandServerApi != null,
);
const installationDependencies = new InstallationDependencies(context);
context.subscriptions.push(storedTargetHighlighter(vscodeIDE, storedTargets));
const vscodeTutorial = createTutorial(
context,
normalizedIde,
scopeVisualizer,
fileSystem,
addCommandRunnerDecorator,
hatTokenMap,
customSpokenFormGenerator,
hats,
);
registerCommands(
context,
vscodeIDE,
commandApi,
commandHistoryStorage,
testCaseRecorder,
scopeTestRecorder,
scopeVisualizer,
keyboardCommands,
hats,
vscodeTutorial,
installationDependencies,
storedTargets,
snippets,
);
void new ReleaseNotes(vscodeApi, context, normalizedIde.messages).maybeShow();
installationDependencies.maybeShow();
return {
testHelpers:
normalizedIde.runMode === "test"
? constructTestHelpers(
fakeCommandServerApi,
storedTargets,
hatTokenMap,
vscodeIDE,
normalizedIde as NormalizedIDE,
fileSystem,
scopeProvider,
injectIde,
runIntegrationTests,
vscodeTutorial,
)
: undefined,
};
}
async function createVscodeIde(context: vscode.ExtensionContext) {
const vscodeIDE = new VscodeIDE(context);
const hats = new VscodeHats(
vscodeIDE,
vscodeApi,
context,
vscodeIDE.runMode === "test"
? new FakeFontMeasurements()
: new FontMeasurementsImpl(context),
);
await hats.init();
// FIXME: Inject this from test harness. Would need to arrange to delay
// extension initialization, probably by returning a function from extension
// init that has parameters consisting of test configuration, and have that
// function do the actual initialization.
const cursorlessDir =
vscodeIDE.runMode === "test"
? path.join(os.tmpdir(), crypto.randomBytes(16).toString("hex"))
: path.join(os.homedir(), ".cursorless");
const fileSystem = new VscodeFileSystem(
context,
vscodeIDE.runMode,
cursorlessDir,
);
await fileSystem.initialize();
return { vscodeIDE, hats, fileSystem };
}
function createTreeSitter(parseTreeApi: ParseTreeApi): TreeSitter {
return {
getNodeAtLocation(document: TextDocument, range: Range) {
return parseTreeApi.getNodeAtLocation(
new vscode.Location(document.uri, toVscodeRange(range)),
);
},
getTree(document: TextDocument) {
return parseTreeApi.getTreeForUri(document.uri);
},
loadLanguage: parseTreeApi.loadLanguage,
createQuery: parseTreeApi.createQuery,
disableLatexMsg: semver.lt(vscode.version, "1.98.0")
? undefined
: "Latex is disabled on vscode versions >= 1.98.0. issues/2879",
};
}
function createScopeVisualizer(
ide: IDE,
scopeProvider: ScopeProvider,
): ScopeVisualizer {
let scopeVisualizer: VscodeScopeVisualizer | undefined;
let currentScopeType: ScopeType | undefined;
const listeners: ScopeVisualizerListener[] = [];
return {
start(scopeType: ScopeType, visualizationType: VisualizationType) {
scopeVisualizer?.dispose();
scopeVisualizer = createVscodeScopeVisualizer(
ide,
scopeProvider,
scopeType,
visualizationType,
);
scopeVisualizer.start();
currentScopeType = scopeType;
listeners.forEach((listener) => listener(scopeType, visualizationType));
},
stop() {
scopeVisualizer?.dispose();
scopeVisualizer = undefined;
currentScopeType = undefined;
listeners.forEach((listener) => listener(undefined, undefined));
},
get scopeType() {
return currentScopeType;
},
onDidChangeScopeType(listener: ScopeVisualizerListener): Disposable {
listeners.push(listener);
return {
dispose() {
listeners.splice(listeners.indexOf(listener), 1);
},
};
},
};
}
// this method is called when your extension is deactivated
export function deactivate() {
// do nothing
}