forked from denoland/vscode_deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.ts
More file actions
581 lines (540 loc) · 18.6 KB
/
commands.ts
File metadata and controls
581 lines (540 loc) · 18.6 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** Contains handlers for commands that are enabled in Visual Studio Code for
* the extension. */
import {
ENABLEMENT_FLAG,
EXTENSION_NS,
LANGUAGE_CLIENT_ID,
LANGUAGE_CLIENT_NAME,
SERVER_SEMVER,
} from "./constants";
import * as tasks from "./tasks";
import { DenoTestController, TestingFeature } from "./testing";
import type {
DenoExtensionContext,
DidChangeDenoConfigurationParams,
DidUpgradeCheckParams,
TestCommandOptions,
} from "./types";
import { WelcomePanel } from "./welcome";
import {
assert,
getDenoCommandName,
getDenoCommandPath,
getInspectArg,
} from "./util";
import { registryState } from "./lsp_extensions";
import { createRegistryStateHandler } from "./notification_handlers";
import { DenoServerInfo } from "./server_info";
import * as dotenv from "dotenv";
import * as vscode from "vscode";
import { LanguageClient, ServerOptions } from "vscode-languageclient/node";
import type {
Executable,
Location,
Position,
} from "vscode-languageclient/node";
import { getWorkspacesEnabledInfo } from "./enable";
import { denoUpgradePromptAndExecute } from "./upgrade";
import * as fs from "fs";
import * as path from "path";
import * as process from "process";
import { semver } from "./semver";
// deno-lint-ignore no-explicit-any
export type Callback = (...args: any[]) => unknown;
export type Factory = (
context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
) => Callback;
export function cacheActiveDocument(
_context: vscode.ExtensionContext,
_extensionContext: DenoExtensionContext,
): Callback {
return () => {
const activeEditor = vscode.window.activeTextEditor;
if (!activeEditor) {
return;
}
const uri = activeEditor.document.uri.toString();
return vscode.window.withProgress({
location: vscode.ProgressLocation.Window,
title: "caching",
}, () => {
return vscode.commands.executeCommand("deno.cache", [uri], uri);
});
};
}
export function clearHiddenPromptStorage(
_context: vscode.ExtensionContext,
_extensionContext: DenoExtensionContext,
): Callback {
return () => {
// Reset states for 'Don't show this again' prompt responses, etc.
// Currently unused.
};
}
export function info(
context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
): Callback {
return async () => {
await vscode.window.showInformationMessage(
`deno ${extensionContext.serverInfo?.versionWithBuildInfo} | vscode_deno ${context.extension.packageJSON?.version} | vscode ${vscode.version}`,
);
};
}
/** Start (or restart) the Deno Language Server */
export function startLanguageServer(
context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
): Callback {
return async () => {
// Stop the existing language server and reset the state
if (extensionContext.client) {
const client = extensionContext.client;
extensionContext.client = undefined;
for (const disposable of extensionContext.clientSubscriptions ?? []) {
disposable.dispose();
}
extensionContext.statusBar.refresh(extensionContext);
vscode.commands.executeCommand("setContext", ENABLEMENT_FLAG, false);
const timeoutMs = 10_000;
await client.stop(timeoutMs);
}
extensionContext.clientSubscriptions = [];
if (isDenoDisabledCompletely()) {
extensionContext.outputChannel.appendLine(
'Warning: The Deno language server is explicitly disabled for every directory. If this is not intentional, check your user and workspace settings for entries like `"deno.enable": false` and `"deno.enablePaths": []`.',
);
return;
}
// Start a new language server
const command = await getDenoCommandPath(extensionContext.approvedPaths);
if (command == null) {
const message =
"Could not resolve Deno executable. Please ensure it is available " +
`on the PATH used by VS Code or set an explicit "deno.path" setting.`;
// only show the message if the user has enabled deno or they have
// a deno configuration file and haven't explicitly disabled deno
const enabledInfo = await getWorkspacesEnabledInfo();
const shouldShowMessage = enabledInfo
.some((e) => e.enabled || e.hasDenoConfig && e.enabled !== false);
if (shouldShowMessage) {
vscode.window.showErrorMessage(message);
}
extensionContext.outputChannel.appendLine(`Error: ${message}`);
return;
}
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const env: Record<string, string | undefined> = {
...process.env,
};
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
if (workspaceFolder) {
const denoEnvPath = path.join(workspaceFolder.uri.fsPath, denoEnvFile);
try {
const content = fs.readFileSync(denoEnvPath, { encoding: "utf8" });
const parsed = dotenv.parse(content);
Object.assign(env, parsed);
} catch (error) {
vscode.window.showErrorMessage(
`Could not read env file "${denoEnvPath}": ${error}`,
);
}
}
}
const denoEnv = config.get<Record<string, string>>("env");
env["FORCE_COLOR"] = "1";
if (denoEnv) {
Object.assign(env, denoEnv);
}
if (config.get<boolean>("future")) {
env["DENO_FUTURE"] = "1";
}
env["DENO_V8_FLAGS"] = getV8Flags();
const shell = process.platform === "win32" &&
/\.([Cc][Mm][Dd]|[Bb][Aa][Tt])$/.test(command);
const serverOptions: ServerOptions = {
run: {
command,
args: ["lsp"],
options: { env, shell },
} as Executable,
debug: {
command,
// disabled for now, as this gets super chatty during development
// args: ["lsp", "-L", "debug"],
args: ["lsp"],
options: { env, shell },
} as Executable,
};
const client = new LanguageClient(
LANGUAGE_CLIENT_ID,
LANGUAGE_CLIENT_NAME,
serverOptions,
{
outputChannel: extensionContext.outputChannel,
middleware: {
workspace: {
configuration: (params, token, next) => {
const response = next(params, token) as Record<string, unknown>[];
for (let i = 0; i < response.length; i++) {
const item = params.items[i];
if (item.section == "deno") {
transformDenoConfiguration(extensionContext, response[i]);
}
}
return response;
},
},
},
...extensionContext.clientOptions,
},
);
const testingFeature = new TestingFeature();
client.registerFeature(testingFeature);
client.registerFeature({
fillClientCapabilities(capabilities) {
capabilities.experimental ??= {};
const experimental: { clientProvidedOrganizeImports?: boolean } =
capabilities.experimental ??
{};
experimental.clientProvidedOrganizeImports = true;
capabilities.experimental = experimental;
},
getState() {
return { kind: "static" };
},
initialize() {},
clear() {},
});
await client.start();
// set this after a successful start
extensionContext.client = client;
vscode.commands.executeCommand("setContext", ENABLEMENT_FLAG, true);
extensionContext.serverInfo = new DenoServerInfo(
client.initializeResult?.serverInfo,
);
extensionContext.serverCapabilities = client.initializeResult?.capabilities;
extensionContext.statusBar.refresh(extensionContext);
extensionContext.clientSubscriptions.push(
extensionContext.client.onNotification(
"deno/didUpgradeCheck",
(params: DidUpgradeCheckParams) => {
if (extensionContext.serverInfo) {
extensionContext.serverInfo.upgradeAvailable =
params.upgradeAvailable;
extensionContext.statusBar.refresh(extensionContext);
}
},
),
);
if (testingFeature.enabled) {
extensionContext.clientSubscriptions.push(
new DenoTestController(extensionContext.client),
);
}
extensionContext.clientSubscriptions.push(
client.onNotification(
registryState,
createRegistryStateHandler(),
),
);
const scopesWithDenoJson = new Set<string>();
extensionContext.scopesWithDenoJson = scopesWithDenoJson;
extensionContext.clientSubscriptions.push(
extensionContext.client.onNotification(
"deno/didChangeDenoConfiguration",
({ changes }: DidChangeDenoConfigurationParams) => {
let changedScopes = false;
for (const change of changes) {
if (change.configurationType != "denoJson") {
continue;
}
if (change.type == "added") {
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
scopesWithDenoJson.add(scopePath);
changedScopes = true;
} else if (change.type == "removed") {
const scopePath = vscode.Uri.parse(change.scopeUri).fsPath;
scopesWithDenoJson.delete(scopePath);
changedScopes = true;
}
}
if (changedScopes) {
extensionContext.tsApi?.refresh();
}
extensionContext.tasksSidebar.refresh();
},
),
);
extensionContext.tsApi.refresh();
if (
semver.valid(extensionContext.serverInfo.version) &&
!semver.satisfies(extensionContext.serverInfo.version, SERVER_SEMVER)
) {
notifyServerSemver(extensionContext.serverInfo.version);
} else {
showWelcomePageIfFirstUse(context, extensionContext);
}
};
function getV8Flags() {
let v8Flags = process.env.DENO_V8_FLAGS ?? "";
const hasMaxOldSpaceSizeFlag = v8Flags.includes("--max-old-space-size=") ||
v8Flags.includes("--max_old_space_size=");
if (
hasMaxOldSpaceSizeFlag &&
extensionContext.maxTsServerMemory == null
) {
// the v8 flags already include a max-old-space-size and the user
// has not provided a maxTsServerMemory value
return v8Flags;
}
// Use the same defaults and minimum as vscode uses for this setting
// https://github.com/microsoft/vscode/blob/48d4ba271686e8072fc6674137415bc80d936bc7/extensions/typescript-language-features/src/configuration/configuration.ts#L213-L214
const maxTsServerMemory = Math.max(
128,
extensionContext.maxTsServerMemory ?? 3072,
);
if (v8Flags.length > 0) {
v8Flags += ",";
}
// flags at the end take precedence
v8Flags += `--max-old-space-size=${maxTsServerMemory}`;
return v8Flags;
}
}
function notifyServerSemver(serverVersion: string) {
return vscode.window.showWarningMessage(
`The version of Deno language server ("${serverVersion}") does not meet the requirements of the extension ("${SERVER_SEMVER}"). Please update Deno and restart.`,
"OK",
);
}
/** Mutates the `config` parameter. For compatibility currently. */
export function transformDenoConfiguration(
extensionContext: DenoExtensionContext,
config: Record<string, unknown>,
) {
// TODO(nayeemrmn): Deno > 2.0.0-rc.1 expects `deno.unstable` as
// an array of features. Remove this eventually.
if (
semver.lte(extensionContext.serverInfo?.version || "1.0.0", "2.0.0-rc.1")
) {
config.unstable = !!config.unstable;
}
}
function showWelcomePageIfFirstUse(
context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
) {
if (process.env.DENO_DISABLE_WELCOME_PAGE != null) {
return;
}
const welcomeShown = context.globalState.get<boolean>("deno.welcomeShown") ??
false;
if (welcomeShown) {
return;
}
welcome(context, extensionContext)();
context.globalState.update("deno.welcomeShown", true);
}
export function showReferences(
_content: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
): Callback {
return (uri: string, position: Position, locations: Location[]) => {
if (!extensionContext.client) {
return;
}
vscode.commands.executeCommand(
"editor.action.showReferences",
vscode.Uri.parse(uri),
extensionContext.client.protocol2CodeConverter.asPosition(position),
locations.map(extensionContext.client.protocol2CodeConverter.asLocation),
);
};
}
let statusRequestIndex = 0;
/** Open and display the "virtual document" which provides the status of the
* Deno Language Server. */
export function status(
_context: vscode.ExtensionContext,
_extensionContext: DenoExtensionContext,
): Callback {
return () => {
const uri = vscode.Uri.parse(`deno:/status.md?${statusRequestIndex++}`);
return vscode.commands.executeCommand("markdown.showPreviewToSide", uri);
};
}
export function test(
_context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
): Callback {
return async (uriStr: string, name: string, options: TestCommandOptions) => {
const uri = vscode.Uri.parse(uriStr, true);
const filePath = uri.fsPath;
const config = vscode.workspace.getConfiguration(EXTENSION_NS, uri);
const testArgs: string[] = [
...(config.get<string[]>("codeLens.testArgs") ?? []),
];
const unstable = config.get("unstable") as string[] ?? [];
for (const unstableFeature of unstable) {
const flag = `--unstable-${unstableFeature}`;
if (!testArgs.includes(flag)) {
testArgs.push(flag);
}
}
if (options?.inspect) {
testArgs.push(getInspectArg(extensionContext.serverInfo?.version));
}
if (!testArgs.includes("--import-map")) {
const importMap: string | undefined | null = config.get("importMap");
if (importMap?.trim()) {
testArgs.push("--import-map", importMap.trim());
}
}
const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
const env = {} as Record<string, string>;
const denoEnvFile = config.get<string>("envFile");
if (denoEnvFile) {
if (workspaceFolder) {
const denoEnvPath = path.join(workspaceFolder.uri.fsPath, denoEnvFile);
try {
const content = fs.readFileSync(denoEnvPath, { encoding: "utf8" });
const parsed = dotenv.parse(content);
Object.assign(env, parsed);
} catch (error) {
vscode.window.showErrorMessage(
`Could not read env file "${denoEnvPath}": ${error}`,
);
}
}
}
const denoEnv = config.get<Record<string, string>>("env");
if (denoEnv) {
Object.assign(env, denoEnv);
}
const cacheDir: string | undefined | null = config.get("cache");
if (cacheDir?.trim()) {
env["DENO_DIR"] = cacheDir.trim();
}
if (config.get<boolean>("future")) {
env["DENO_FUTURE"] = "1";
}
const nameRegex = `/^${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$/`;
const args = ["test", ...testArgs, "--filter", nameRegex, filePath];
const definition: tasks.DenoTaskDefinition = {
type: tasks.TASK_TYPE,
command: "test",
args,
env,
};
assert(workspaceFolder);
const denoCommand = await getDenoCommandName(
extensionContext.approvedPaths,
);
const task = tasks.buildDenoTask(
workspaceFolder,
denoCommand,
definition,
`test "${name}"`,
args,
["$deno-test"],
);
task.presentationOptions = {
reveal: vscode.TaskRevealKind.Always,
panel: vscode.TaskPanelKind.Dedicated,
clear: true,
};
task.group = vscode.TaskGroup.Test;
const createdTask = await vscode.tasks.executeTask(task);
if (options?.inspect) {
await vscode.debug.startDebugging(workspaceFolder, {
name,
request: "attach",
type: "node",
});
}
return createdTask;
};
}
export function welcome(
context: vscode.ExtensionContext,
_extensionContext: DenoExtensionContext,
): Callback {
return () => {
WelcomePanel.createOrShow(context.extensionUri);
};
}
export function statusBarClicked(
_context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
) {
return () => {
extensionContext.outputChannel.show(true);
if (extensionContext.serverInfo?.upgradeAvailable) {
// Async dispatch on purpose.
denoUpgradePromptAndExecute(
extensionContext.serverInfo.upgradeAvailable,
extensionContext.approvedPaths,
);
}
};
}
export function enable(
_context: vscode.ExtensionContext,
_extensionContext: DenoExtensionContext,
) {
return async () => {
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
await config.update("enable", true);
vscode.window.showInformationMessage("Deno workspace initialized.");
const tsserverConfig = vscode.workspace.getConfiguration(
"typescript.tsserver",
);
if (tsserverConfig.get<boolean>("experimental.enableProjectDiagnostics")) {
try {
await tsserverConfig.update(
"experimental.enableProjectDiagnostics",
false,
vscode.ConfigurationTarget.Workspace,
);
vscode.window.showInformationMessage(
'Disabled "typescript.tsserver.experimental.enableProjectDiagnostics" for the workspace. The Deno extension is incompatible with this setting. See: https://github.com/denoland/vscode_deno/issues/437#issuecomment-1720393193.',
);
} catch {
vscode.window.showWarningMessage(
'Setting "typescript.tsserver.experimental.enableProjectDiagnostics" is incompatible with the Deno extension. Either disable it in your user settings, or create a workspace and run the "Deno: Enable" command again. See: https://github.com/denoland/vscode_deno/issues/437#issuecomment-1720393193.',
);
}
}
};
}
export function disable(
_context: vscode.ExtensionContext,
_extensionContext: DenoExtensionContext,
) {
return async () => {
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
await config.update("enable", false);
};
}
function isDenoDisabledCompletely(): boolean {
function isScopeDisabled(config: vscode.WorkspaceConfiguration): boolean {
const enable = config.get<boolean | null>("enable") ?? null;
const enablePaths = config.get<string[] | null>("enablePaths") ?? null;
if (enablePaths && enablePaths.length == 0) {
return true;
}
return enable === false && enablePaths == null;
}
const workspaceFolders = vscode.workspace.workspaceFolders ?? [];
if (workspaceFolders.length == 0) {
return isScopeDisabled(vscode.workspace.getConfiguration(EXTENSION_NS));
}
return workspaceFolders.map((f) =>
vscode.workspace.getConfiguration(EXTENSION_NS, f)
).every(isScopeDisabled);
}