forked from denoland/vscode_deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextension.ts
More file actions
314 lines (292 loc) · 11.5 KB
/
extension.ts
File metadata and controls
314 lines (292 loc) · 11.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
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import * as commands from "./commands";
import {
ENABLEMENT_FLAG,
EXTENSION_NS,
LANGUAGE_CLIENT_NAME,
} from "./constants";
import { DenoTextDocumentContentProvider, SCHEME } from "./content_provider";
import { DenoDebugConfigurationProvider } from "./debug_config_provider";
import { refreshEnableSettings } from "./enable";
import { DenoStatusBar } from "./status_bar";
import { activateTaskProvider } from "./tasks";
import { getTsApi } from "./ts_api";
import type { DenoExtensionContext } from "./types";
import * as util from "util";
import * as vscode from "vscode";
import { registerSidebar } from "./tasks_sidebar";
import { getDenoInfoJson } from "./util";
function handleConfigurationChange(event: vscode.ConfigurationChangeEvent) {
if (
[EXTENSION_NS, "javascript", "typescript"].some((s) =>
event.affectsConfiguration(s)
)
) {
extensionContext.client?.sendNotification(
"workspace/didChangeConfiguration",
// We actually set this to empty because the language server will
// call back and get the configuration. There can be issues with the
// information on the event not being reliable.
{ settings: null },
);
extensionContext.maxTsServerMemory =
vscode.workspace.getConfiguration(EXTENSION_NS).get(
"maxTsServerMemory",
) ?? null;
refreshEnableSettings(extensionContext);
extensionContext.tsApi.refresh();
extensionContext.statusBar.refresh(extensionContext);
// restart when certain config changes
if (
event.affectsConfiguration("deno.enable") ||
event.affectsConfiguration("deno.disablePaths") ||
event.affectsConfiguration("deno.enablePaths") ||
event.affectsConfiguration("deno.env") ||
event.affectsConfiguration("deno.envFile") ||
event.affectsConfiguration("deno.forcePushBasedDiagnostics") ||
event.affectsConfiguration("deno.future") ||
event.affectsConfiguration("deno.internalInspect") ||
event.affectsConfiguration("deno.logFile") ||
event.affectsConfiguration("deno.path") ||
event.affectsConfiguration("deno.maxTsServerMemory")
) {
vscode.commands.executeCommand("deno.client.restart");
}
}
}
function handleChangeWorkspaceFolders() {
refreshEnableSettings(extensionContext);
extensionContext.tsApi.refresh();
}
export function log(...msgs: unknown[]) {
extensionContext.outputChannel.appendLine(
msgs.map((m) =>
typeof m === "string" ? m : util.inspect(m, { depth: Infinity })
).join(" "),
);
}
const extensionContext = {} as DenoExtensionContext;
/** When the extension activates, this function is called with the extension
* context, and the extension bootstraps itself. */
export async function activate(
context: vscode.ExtensionContext,
): Promise<void> {
extensionContext.outputChannel = extensionContext.outputChannel ??
vscode.window.createOutputChannel(LANGUAGE_CLIENT_NAME, { log: true });
extensionContext.denoInfoJson = await getDenoInfoJson(
extensionContext.outputChannel,
);
const p2cMap = new Map<string, string>();
extensionContext.clientOptions = {
documentSelector: [
{ scheme: "file", language: "javascript" },
{ scheme: "file", language: "javascriptreact" },
{ scheme: "file", language: "typescript" },
{ scheme: "file", language: "typescriptreact" },
{ scheme: "file", language: "json" },
{ scheme: "file", language: "jsonc" },
{ scheme: "file", language: "markdown" },
{ scheme: "file", language: "html" },
{ scheme: "file", language: "css" },
{ scheme: "file", language: "scss" },
{ scheme: "file", language: "sass" },
{ scheme: "file", language: "less" },
{ scheme: "file", language: "yaml" },
{ scheme: "file", language: "sql" },
{ scheme: "file", language: "svelte" },
{ scheme: "file", language: "vue" },
{ scheme: "file", language: "astro" },
{ scheme: "file", language: "vento" },
{ scheme: "file", language: "nunjucks" },
{ scheme: "untitled", language: "javascript" },
{ scheme: "untitled", language: "javascriptreact" },
{ scheme: "untitled", language: "typescript" },
{ scheme: "untitled", language: "typescriptreact" },
{ scheme: "untitled", language: "json" },
{ scheme: "untitled", language: "jsonc" },
{ scheme: "untitled", language: "markdown" },
{ scheme: "untitled", language: "html" },
{ scheme: "untitled", language: "css" },
{ scheme: "untitled", language: "scss" },
{ scheme: "untitled", language: "sass" },
{ scheme: "untitled", language: "less" },
{ scheme: "untitled", language: "yaml" },
{ scheme: "untitled", language: "sql" },
{ scheme: "untitled", language: "svelte" },
{ scheme: "untitled", language: "vue" },
{ scheme: "untitled", language: "astro" },
{ scheme: "untitled", language: "vento" },
{ scheme: "untitled", language: "nunjucks" },
{ scheme: "deno", language: "javascript" },
{ scheme: "deno", language: "javascriptreact" },
{ scheme: "deno", language: "typescript" },
{ scheme: "deno", language: "typescriptreact" },
{ scheme: "deno", language: "json" },
{ scheme: "deno", language: "jsonc" },
{ scheme: "deno", language: "markdown" },
{ scheme: "deno", language: "html" },
{ scheme: "deno", language: "css" },
{ scheme: "deno", language: "scss" },
{ scheme: "deno", language: "sass" },
{ scheme: "deno", language: "less" },
{ scheme: "deno", language: "yaml" },
{ scheme: "deno", language: "sql" },
{ scheme: "deno", language: "svelte" },
{ scheme: "deno", language: "vue" },
{ scheme: "deno", language: "astro" },
{ scheme: "deno", language: "vento" },
{ scheme: "deno", language: "nunjucks" },
{ scheme: "vscode-userdata", language: "jsonc" },
{ notebook: "*", language: "javascript" },
{ notebook: "*", language: "javascriptreact" },
{ notebook: "*", language: "typescript" },
{ notebook: "*", language: "typescriptreact" },
],
uriConverters: {
code2Protocol: (uri) => {
if (uri.scheme == "vscode-notebook-cell") {
const string = uri.with({
scheme: "deno-notebook-cell",
}).toString();
p2cMap.set(string, uri.toString());
return string;
}
return uri.toString();
},
protocol2Code: (s) => {
const maybeMapped = p2cMap.get(s);
if (maybeMapped) {
return vscode.Uri.parse(maybeMapped);
}
return vscode.Uri.parse(s);
},
},
diagnosticCollectionName: "deno",
initializationOptions: () => {
const denoConfiguration = vscode.workspace.getConfiguration().get(
EXTENSION_NS,
) as Record<string, unknown>;
commands.transformDenoConfiguration(extensionContext, denoConfiguration);
return {
...denoConfiguration,
javascript: vscode.workspace.getConfiguration().get("javascript"),
typescript: vscode.workspace.getConfiguration().get("typescript"),
enableBuiltinCommands: true,
} as object;
},
markdown: {
isTrusted: true,
},
};
// When a workspace folder is opened, the updates or changes to the workspace
// folders need to be sent to the TypeScript language service plugin
vscode.workspace.onDidChangeWorkspaceFolders(
handleChangeWorkspaceFolders,
extensionContext,
context.subscriptions,
);
// Send a notification to the language server when the configuration changes
// as well as update the TypeScript language service plugin
vscode.workspace.onDidChangeConfiguration(
handleConfigurationChange,
extensionContext,
context.subscriptions,
);
extensionContext.statusBar = new DenoStatusBar();
context.subscriptions.push(extensionContext.statusBar);
// Register a content provider for Deno resolved read-only files.
context.subscriptions.push(
vscode.workspace.registerTextDocumentContentProvider(
SCHEME,
new DenoTextDocumentContentProvider(extensionContext),
),
);
context.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider(
"deno",
new DenoDebugConfigurationProvider(extensionContext),
),
);
// Activate the task provider.
context.subscriptions.push(activateTaskProvider(extensionContext));
extensionContext.maxTsServerMemory =
vscode.workspace.getConfiguration(EXTENSION_NS).get("maxTsServerMemory") ??
null;
refreshEnableSettings(extensionContext);
extensionContext.tsApi = getTsApi(() => {
return {
enableSettingsUnscoped: extensionContext.enableSettingsUnscoped,
enableSettingsByFolder: extensionContext.enableSettingsByFolder,
scopesWithDenoJson: Array.from(extensionContext.scopesWithDenoJson ?? []),
npmCache: extensionContext.denoInfoJson?.npmCache ?? null,
};
});
extensionContext.tasksSidebar = registerSidebar(
extensionContext,
context.subscriptions,
)!;
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration("deno.defaultTaskCommand")) {
extensionContext.tasksSidebar.refresh();
}
}));
const registerCommand = createRegisterCommand(context);
registerCommand("deno.client.showReferences", commands.showReferences);
// TODO(nayeemrmn): LSP versions <= 1.40.2 use this alias. Remove it
// eventually.
registerCommand("deno.showReferences", commands.showReferences);
registerCommand("deno.client.test", commands.test);
// TODO(nayeemrmn): LSP versions <= 1.40.2 use this alias. Remove it
// eventually.
registerCommand("deno.test", commands.test);
registerCommand(
"deno.client.cacheActiveDocument",
commands.cacheActiveDocument,
);
registerCommand(
"deno.client.clearHiddenPromptStorage",
commands.clearHiddenPromptStorage,
);
registerCommand("deno.client.restart", commands.startLanguageServer);
registerCommand("deno.client.info", commands.info);
registerCommand("deno.client.status", commands.status);
registerCommand("deno.client.welcome", commands.welcome);
registerCommand("deno.client.enable", commands.enable);
// Legacy alias for `deno.client.enable`.
registerCommand("deno.client.initializeWorkspace", commands.enable);
registerCommand("deno.client.disable", commands.disable);
registerCommand("deno.client.statusBarClicked", commands.statusBarClicked);
await commands.startLanguageServer(context, extensionContext)();
}
export function deactivate(): Thenable<void> | undefined {
if (!extensionContext.client) {
return undefined;
}
const client = extensionContext.client;
extensionContext.client = undefined;
for (const disposable of extensionContext.clientSubscriptions ?? []) {
disposable.dispose();
}
extensionContext.clientSubscriptions = undefined;
extensionContext.statusBar.refresh(extensionContext);
vscode.commands.executeCommand("setContext", ENABLEMENT_FLAG, false);
return client.stop();
}
/** Internal function factory that returns a registerCommand function that is
* bound to the extension context. */
function createRegisterCommand(
context: vscode.ExtensionContext,
): (name: string, factory: commands.Factory) => void {
return function registerCommand(
name: string,
factory: (
context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
) => commands.Callback,
): void {
const command = factory(context, extensionContext);
context.subscriptions.push(
vscode.commands.registerCommand(name, command),
);
};
}