-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathextension.ts
249 lines (221 loc) · 6.75 KB
/
extension.ts
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
import {
Disposable,
FakeIDE,
getFakeCommandServerApi,
IDE,
isTesting,
NormalizedIDE,
Range,
ScopeProvider,
ScopeType,
TextDocument,
} from "@cursorless/common";
import {
createCursorlessEngine,
TreeSitter,
} from "@cursorless/cursorless-engine";
import {
CursorlessApi,
getCommandServerApi,
getParseTreeApi,
ParseTreeApi,
toVscodeRange,
} from "@cursorless/vscode-common";
import * as crypto from "crypto";
import * as os from "os";
import * as path from "path";
import * as vscode from "vscode";
import { constructTestHelpers } from "./constructTestHelpers";
import { FakeFontMeasurements } from "./ide/vscode/hats/FakeFontMeasurements";
import { FontMeasurementsImpl } from "./ide/vscode/hats/FontMeasurementsImpl";
import { VscodeHats } from "./ide/vscode/hats/VscodeHats";
import { VscodeFileSystem } from "./ide/vscode/VscodeFileSystem";
import { VscodeIDE } from "./ide/vscode/VscodeIDE";
import {
createVscodeScopeVisualizer,
VscodeScopeVisualizer,
} from "./ide/vscode/VSCodeScopeVisualizer";
import { KeyboardCommands } from "./keyboard/KeyboardCommands";
import { registerCommands } from "./registerCommands";
import { ReleaseNotes } from "./ReleaseNotes";
import { revisualizeOnCustomRegexChange } from "./revisualizeOnCustomRegexChange";
import { ScopeTreeProvider } from "./ScopeTreeProvider";
import {
ScopeVisualizer,
ScopeVisualizerListener,
VisualizationType,
} from "./ScopeVisualizerCommandApi";
import { StatusBarItem } from "./StatusBarItem";
import { vscodeApi } from "./vscodeApi";
import { mkdir } from "fs/promises";
import { TestCaseRecorder } from "@cursorless/cursorless-engine";
/**
* 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",
);
const commandServerApi =
vscodeIDE.runMode === "test"
? getFakeCommandServerApi()
: await getCommandServerApi();
const treeSitter: TreeSitter = createTreeSitter(parseTreeApi);
const {
commandApi,
storedTargets,
hatTokenMap,
scopeProvider,
snippets,
injectIde,
runIntegrationTests,
addCommandRunnerDecorator,
customSpokenFormGenerator,
} = createCursorlessEngine(
treeSitter,
normalizedIde,
hats,
commandServerApi,
fileSystem,
);
const testCaseRecorder = new TestCaseRecorder(hatTokenMap, storedTargets);
addCommandRunnerDecorator(testCaseRecorder);
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,
);
registerCommands(
context,
vscodeIDE,
commandApi,
testCaseRecorder,
scopeVisualizer,
keyboardCommands,
hats,
);
new ReleaseNotes(vscodeApi, context, normalizedIde.messages).maybeShow();
return {
testHelpers: isTesting()
? constructTestHelpers(
commandServerApi,
storedTargets,
hatTokenMap,
vscodeIDE,
normalizedIde as NormalizedIDE,
fileSystem.cursorlessTalonStateJsonPath,
scopeProvider,
injectIde,
runIntegrationTests,
)
: undefined,
experimental: {
registerThirdPartySnippets: snippets.registerThirdPartySnippets,
},
};
}
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 = isTesting()
? path.join(os.tmpdir(), crypto.randomBytes(16).toString("hex"))
: path.join(os.homedir(), ".cursorless");
await mkdir(cursorlessDir, { recursive: true });
return { vscodeIDE, hats, fileSystem: new VscodeFileSystem(cursorlessDir) };
}
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,
getLanguage: parseTreeApi.getLanguage,
};
}
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
}