-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathindex.ts
More file actions
425 lines (406 loc) · 12.8 KB
/
index.ts
File metadata and controls
425 lines (406 loc) · 12.8 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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import type { PluginSettings } from "../../client/src/shared_types";
import type * as ts from "../node_modules/typescript/lib/tsserverlibrary";
import * as path from "path";
import * as process from "process";
import * as os from "os";
import { setImmediate } from "timers";
import * as util from "util";
import { isMatch } from "picomatch";
/** Extract the return type from a maybe function. */
// deno-lint-ignore no-explicit-any
type ReturnType<T = (...args: any) => any> = T extends // deno-lint-ignore no-explicit-any
(...args: any) => infer R ? R
// deno-lint-ignore no-explicit-any
: any;
/** Extract the parameter types from a maybe function. */
// deno-lint-ignore no-explicit-any
type Parameters<T = (...args: any) => any> = T extends // deno-lint-ignore no-explicit-any
(...args: infer P) => any ? P
: never;
type CallIfDisabledFunction = <T extends ts.LanguageService, J extends keyof T>(
fn: J,
fileNameArg: number | undefined,
enabledReturn: (() => ReturnType<T[J]>) | ReturnType<T[J]>,
) => (...args: Parameters<T[J]>) => ReturnType<T[J]>;
/** Contains the project settings that have been provided by the extension for
* each workspace. */
const projectSettings = new Map<string, PluginSettings>();
class Plugin implements ts.server.PluginModule {
#project!: ts.server.Project;
#projectName!: string;
// determines if a deno is enabled "globally" or not for those APIs which
// don't reference a file name
#denoEnabled(): boolean {
const pluginSettings = projectSettings.get(this.#projectName);
if (!pluginSettings) {
return false;
}
const enableSettingsUnscoped = pluginSettings.enableSettingsUnscoped ??
{ enable: null, enablePaths: null, disablePaths: [] };
const scopesWithDenoJson = pluginSettings.scopesWithDenoJson ?? [];
if (enableSettingsUnscoped.enable != null) {
return enableSettingsUnscoped.enable;
}
return scopesWithDenoJson.length != 0;
}
// determines if a specific filename is Deno enabled or not.
#fileNameDenoEnabled(fileName: string): boolean {
if (process.platform === "win32") {
fileName = fileName.replace(/\//g, "\\");
}
fileName = fileName.replace(/^\^\/vscode-notebook-cell\/[^/]*/, "");
const pluginSettings = projectSettings.get(this.#projectName);
if (!pluginSettings) {
return false;
}
const enableSettings =
pluginSettings.enableSettingsByFolder?.find(([workspace, _]) =>
pathStartsWith(fileName, workspace)
)?.[1] ?? pluginSettings.enableSettingsUnscoped ??
{ enable: null, enablePaths: null, disablePaths: [] };
const scopesWithDenoJson = pluginSettings.scopesWithDenoJson ?? [];
if (isMatch(fileName, enableSettings.disablePaths)) return false;
for (const path of enableSettings.disablePaths) {
if (pathStartsWith(fileName, path)) {
return false;
}
}
if (enableSettings.enablePaths) {
return isMatch(fileName, enableSettings.enablePaths) ||
enableSettings.enablePaths.some((path) =>
pathStartsWith(fileName, path)
);
}
if (enableSettings.enable != null) {
return enableSettings.enable;
}
return scopesWithDenoJson.some((scope) => pathStartsWith(fileName, scope));
}
#log = (..._msgs: unknown[]) => {};
#loggingEnabled = () => true;
create(info: ts.server.PluginCreateInfo): ts.LanguageService {
const { languageService: ls, project, config } = info;
this.#log = (...msgs) => {
project.projectService.logger.info(
`[typescript-deno-plugin] ${
msgs.map((m) => typeof m === "string" ? m : util.inspect(m)).join(" ")
}`,
);
};
this.#loggingEnabled = () => {
return project.projectService.logger.loggingEnabled();
};
this.#project = project;
this.#projectName = project.getProjectName();
projectSettings.set(this.#project.getProjectName(), config);
setImmediate(() => {
this.#project.refreshDiagnostics();
});
/** Given an object and a method name on that object, call if disabled. */
const callIfDisabled: CallIfDisabledFunction = (
fn,
fileNameArg,
emptyReturn,
) => {
// deno-lint-ignore no-explicit-any
const target = (ls as any)[fn];
return (...args) => {
if (this.#loggingEnabled()) {
this.#log(fn, args);
}
const enabled = fileNameArg !== undefined
? this.#fileNameDenoEnabled(args[fileNameArg] as string)
: this.#denoEnabled();
return enabled
// in order to keep the `emptyReturn` separate instances, we do some
// analysis here to ensure we are returning a "fresh" `emptyReturn`
? Array.isArray(emptyReturn)
? []
: typeof emptyReturn === "function"
? (emptyReturn as () => unknown)()
: emptyReturn
: target.call(ls, ...args);
};
};
// This "mutes" diagnostics for things like tsconfig files.
const projectGetGlobalProjectErrors = this.#project.getGlobalProjectErrors;
this.#project.getGlobalProjectErrors = () =>
this.#denoEnabled()
? []
: projectGetGlobalProjectErrors.call(this.#project);
const projectGetAllProjectErrors = this.#project.getAllProjectErrors;
this.#project.getAllProjectErrors = () =>
this.#denoEnabled() ? [] : projectGetAllProjectErrors.call(this.#project);
const commentSelection = callIfDisabled("commentSelection", 0, []);
const findReferences = callIfDisabled("findReferences", 0, undefined);
const findRenameLocations = callIfDisabled(
"findRenameLocations",
0,
undefined,
) as ts.LanguageService["findRenameLocations"];
const getApplicableRefactors = callIfDisabled(
"getApplicableRefactors",
0,
[],
);
const getBraceMatchingAtPosition = callIfDisabled(
"getBraceMatchingAtPosition",
0,
[],
);
const getBreakpointStatementAtPosition = callIfDisabled(
"getBreakpointStatementAtPosition",
0,
undefined,
);
const getCodeFixesAtPosition = callIfDisabled(
"getCodeFixesAtPosition",
0,
[],
);
const getCompilerOptionsDiagnostics = callIfDisabled(
"getCompilerOptionsDiagnostics",
undefined,
[],
);
const getCompletionEntryDetails = callIfDisabled(
"getCompletionEntryDetails",
0,
undefined,
);
const getCompletionEntrySymbol = callIfDisabled(
"getCompletionEntrySymbol",
0,
undefined,
);
const getCompletionsAtPosition = callIfDisabled(
"getCompletionsAtPosition",
0,
undefined,
);
const getDefinitionAndBoundSpan = callIfDisabled(
"getDefinitionAndBoundSpan",
0,
undefined,
);
const getDefinitionAtPosition = callIfDisabled(
"getDefinitionAtPosition",
0,
undefined,
);
const getDocCommentTemplateAtPosition = callIfDisabled(
"getDocCommentTemplateAtPosition",
0,
undefined,
);
const getDocumentHighlights = callIfDisabled(
"getDocumentHighlights",
0,
undefined,
);
const getEditsForFileRename = callIfDisabled(
"getEditsForFileRename",
0,
[],
);
const getEditsForRefactor = callIfDisabled(
"getEditsForRefactor",
0,
undefined,
);
const getEncodedSemanticClassifications = callIfDisabled(
"getEncodedSemanticClassifications",
0,
() => ({ spans: [], endOfLineState: 0 }),
);
const getEncodedSyntacticClassifications = callIfDisabled(
"getEncodedSyntacticClassifications",
0,
() => ({ spans: [], endOfLineState: 0 }),
);
const getImplementationAtPosition = callIfDisabled(
"getImplementationAtPosition",
0,
undefined,
);
const getJsxClosingTagAtPosition = callIfDisabled(
"getJsxClosingTagAtPosition",
0,
undefined,
);
const getNameOrDottedNameSpan = callIfDisabled(
"getNameOrDottedNameSpan",
0,
undefined,
);
const getNavigateToItems = callIfDisabled(
"getNavigateToItems",
undefined,
[],
);
const getNavigationBarItems = callIfDisabled(
"getNavigationBarItems",
0,
[],
);
const getNavigationTree = callIfDisabled("getNavigationTree", 0, () => ({
text: "",
kind: "" as ts.ScriptElementKind.unknown,
kindModifiers: "",
spans: [],
nameSpan: undefined,
}));
const getOutliningSpans = callIfDisabled("getOutliningSpans", 0, []);
const getQuickInfoAtPosition = callIfDisabled(
"getQuickInfoAtPosition",
0,
undefined,
);
const getReferencesAtPosition = callIfDisabled(
"getReferencesAtPosition",
0,
undefined,
);
const getSemanticClassifications = callIfDisabled(
"getSemanticClassifications",
0,
[],
) as ts.LanguageService["getSemanticClassifications"];
const getSemanticDiagnostics = callIfDisabled(
"getSemanticDiagnostics",
0,
[],
);
const getSignatureHelpItems = callIfDisabled(
"getSignatureHelpItems",
0,
undefined,
);
const getSpanOfEnclosingComment = callIfDisabled(
"getSpanOfEnclosingComment",
0,
undefined,
);
const getSuggestionDiagnostics = callIfDisabled(
"getSuggestionDiagnostics",
0,
[],
);
const getSyntacticDiagnostics = callIfDisabled(
"getSyntacticDiagnostics",
0,
[],
);
const getSyntacticClassifications = callIfDisabled(
"getSyntacticClassifications",
0,
[],
) as ts.LanguageService["getSyntacticClassifications"];
const getTodoComments = callIfDisabled("getTodoComments", 0, []);
const getTypeDefinitionAtPosition = callIfDisabled(
"getTypeDefinitionAtPosition",
0,
undefined,
);
const prepareCallHierarchy = callIfDisabled(
"prepareCallHierarchy",
0,
undefined,
);
const provideCallHierarchyIncomingCalls = callIfDisabled(
"provideCallHierarchyIncomingCalls",
0,
[],
);
const provideCallHierarchyOutgoingCalls = callIfDisabled(
"provideCallHierarchyOutgoingCalls",
0,
[],
);
const provideInlayHints = callIfDisabled("provideInlayHints", 0, []);
const toggleLineComment = callIfDisabled("toggleLineComment", 0, []);
const toggleMultilineComment = callIfDisabled(
"toggleMultilineComment",
0,
[],
);
const uncommentSelection = callIfDisabled("uncommentSelection", 0, []);
const getSupportedCodeFixes = callIfDisabled(
"getSupportedCodeFixes",
0,
[],
);
return {
...ls,
commentSelection,
findReferences,
findRenameLocations,
getApplicableRefactors,
getBraceMatchingAtPosition,
getBreakpointStatementAtPosition,
getCodeFixesAtPosition,
getCompilerOptionsDiagnostics,
getCompletionEntryDetails,
getCompletionEntrySymbol,
getCompletionsAtPosition,
getDefinitionAndBoundSpan,
getDefinitionAtPosition,
getDocCommentTemplateAtPosition,
getDocumentHighlights,
getEditsForFileRename,
getEditsForRefactor,
getEncodedSemanticClassifications,
getEncodedSyntacticClassifications,
getImplementationAtPosition,
getJsxClosingTagAtPosition,
getNameOrDottedNameSpan,
getNavigateToItems,
getNavigationBarItems,
getNavigationTree,
getOutliningSpans,
getQuickInfoAtPosition,
getReferencesAtPosition,
getSemanticClassifications,
getSemanticDiagnostics,
getSignatureHelpItems,
getSpanOfEnclosingComment,
getSuggestionDiagnostics,
getSyntacticClassifications,
getSyntacticDiagnostics,
getTodoComments,
getTypeDefinitionAtPosition,
prepareCallHierarchy,
provideCallHierarchyIncomingCalls,
provideCallHierarchyOutgoingCalls,
provideInlayHints,
toggleLineComment,
toggleMultilineComment,
uncommentSelection,
getSupportedCodeFixes,
};
}
onConfigurationChanged(settings: PluginSettings): void {
if (this.#loggingEnabled()) {
this.#log(`onConfigurationChanged(${JSON.stringify(settings)})`);
}
projectSettings.set(this.#project.getProjectName(), settings);
this.#project.refreshDiagnostics();
}
}
function init(): ts.server.PluginModule {
console.log(`INIT typescript-deno-plugin`);
return new Plugin();
}
const PARENT_RELATIVE_REGEX = os.platform() === "win32"
? /\.\.(?:[/\\]|$)/
: /\.\.(?:\/|$)/;
/** Checks if `parent` is an ancestor of `child`. */
function pathStartsWith(child: string, parent: string) {
if (path.isAbsolute(child) !== path.isAbsolute(parent)) {
return false;
}
const relative = path.relative(parent, child);
return !relative.match(PARENT_RELATIVE_REGEX);
}
export = init;