@typespec/playground 0.14.0-dev.3 → 0.14.0-dev.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +17 -19
- package/dist/react/index.js +2597 -3056
- package/dist/react/viewers/index.js +23 -17
- package/dist/react-wrapper-B-poU4Lv.js +18 -0
- package/dist/services-CELCHECS.js +723 -0
- package/dist/state-storage.js +65 -79
- package/dist/style.css +2 -1
- package/dist/tooling/index.js +24 -23
- package/dist/vite/index.js +54 -78
- package/package.json +13 -13
- package/dist/react-wrapper-BE4armyk.js +0 -20
- package/dist/services-Z619RuQS.js +0 -760
|
@@ -1,760 +0,0 @@
|
|
|
1
|
-
import { resolvePath, getSourceFileKindFromExt, createSourceFile, TypeSpecLanguageConfiguration } from '@typespec/compiler';
|
|
2
|
-
import * as monaco from 'monaco-editor';
|
|
3
|
-
import * as lsp from 'vscode-languageserver';
|
|
4
|
-
import { DiagnosticSeverity, Range, FormattingOptions, DocumentHighlightKind } from 'vscode-languageserver';
|
|
5
|
-
import { TextDocument } from 'vscode-languageserver-textdocument';
|
|
6
|
-
|
|
7
|
-
function printDebugInfo() {
|
|
8
|
-
console.info("TypeSpec Playground");
|
|
9
|
-
console.info(
|
|
10
|
-
"Some variables are bound to the `window` object in the browser console for debugging."
|
|
11
|
-
);
|
|
12
|
-
console.info(" - `program`: The current TypeSpec program.");
|
|
13
|
-
console.info(" - `host`: The current compiler host.");
|
|
14
|
-
console.info(" - `compiler`: The TypeSpec compiler API.");
|
|
15
|
-
console.info(" - `$$`: A Typekit instance bound to the current program.");
|
|
16
|
-
console.info(" - `libs`: A map of all loaded TypeSpec libraries.");
|
|
17
|
-
console.info(" - `vars`: Types bookmarked in the type graph.");
|
|
18
|
-
}
|
|
19
|
-
function debugGlobals() {
|
|
20
|
-
return window;
|
|
21
|
-
}
|
|
22
|
-
function debugLibs() {
|
|
23
|
-
return window.libs ??= {};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
async function importTypeSpecCompiler(config) {
|
|
27
|
-
const compiler = await importLibrary("@typespec/compiler", config);
|
|
28
|
-
debugGlobals().compiler = compiler;
|
|
29
|
-
return compiler;
|
|
30
|
-
}
|
|
31
|
-
async function importLibrary(name, config) {
|
|
32
|
-
const lib = await (config.useShim ? importShim(name) : import(
|
|
33
|
-
/* @vite-ignore */
|
|
34
|
-
/* webpackIgnore: true */
|
|
35
|
-
name
|
|
36
|
-
));
|
|
37
|
-
debugLibs()[name] = lib;
|
|
38
|
-
return lib;
|
|
39
|
-
}
|
|
40
|
-
async function importShim(name) {
|
|
41
|
-
return window.importShim(name);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function resolveVirtualPath(path, ...paths) {
|
|
45
|
-
return resolvePath("/test", path, ...paths);
|
|
46
|
-
}
|
|
47
|
-
function createBrowserHostInternal(options) {
|
|
48
|
-
const virtualFs = /* @__PURE__ */ new Map();
|
|
49
|
-
const jsImports = /* @__PURE__ */ new Map();
|
|
50
|
-
const libraries = {
|
|
51
|
-
...options.libraries
|
|
52
|
-
};
|
|
53
|
-
function registerLibraryFiles(libName, lib) {
|
|
54
|
-
for (const [key, value] of Object.entries(lib._TypeSpecLibrary_.typespecSourceFiles)) {
|
|
55
|
-
virtualFs.set(`/test/node_modules/${libName}/${key}`, value);
|
|
56
|
-
}
|
|
57
|
-
for (const [key, value] of Object.entries(lib._TypeSpecLibrary_.jsSourceFiles)) {
|
|
58
|
-
addJsImport(`/test/node_modules/${libName}/${key}`, value);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
function updatePackageJson() {
|
|
62
|
-
virtualFs.set(
|
|
63
|
-
`/test/package.json`,
|
|
64
|
-
JSON.stringify({
|
|
65
|
-
name: "playground-pkg",
|
|
66
|
-
dependencies: Object.fromEntries(
|
|
67
|
-
Object.values(libraries).map((x) => [x.name, x.packageJson.version])
|
|
68
|
-
)
|
|
69
|
-
})
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
for (const [libName, lib] of Object.entries(libraries)) {
|
|
73
|
-
registerLibraryFiles(libName, lib);
|
|
74
|
-
}
|
|
75
|
-
updatePackageJson();
|
|
76
|
-
function addJsImport(path, value) {
|
|
77
|
-
virtualFs.set(path, "");
|
|
78
|
-
jsImports.set(path, value);
|
|
79
|
-
}
|
|
80
|
-
return {
|
|
81
|
-
compiler: options.compiler,
|
|
82
|
-
libraries,
|
|
83
|
-
async readUrl(url) {
|
|
84
|
-
const contents = virtualFs.get(url);
|
|
85
|
-
if (contents === void 0) {
|
|
86
|
-
const e = new Error(`File ${url} not found.`);
|
|
87
|
-
e.code = "ENOENT";
|
|
88
|
-
throw e;
|
|
89
|
-
}
|
|
90
|
-
return createSourceFile(contents, url);
|
|
91
|
-
},
|
|
92
|
-
async readFile(path) {
|
|
93
|
-
path = resolveVirtualPath(path);
|
|
94
|
-
const contents = virtualFs.get(path);
|
|
95
|
-
if (contents === void 0) {
|
|
96
|
-
const e = new Error(`File ${path} not found.`);
|
|
97
|
-
e.code = "ENOENT";
|
|
98
|
-
throw e;
|
|
99
|
-
}
|
|
100
|
-
return createSourceFile(contents, path);
|
|
101
|
-
},
|
|
102
|
-
async writeFile(path, content) {
|
|
103
|
-
path = resolveVirtualPath(path);
|
|
104
|
-
virtualFs.set(path, content);
|
|
105
|
-
},
|
|
106
|
-
async readDir(path) {
|
|
107
|
-
path = resolveVirtualPath(path);
|
|
108
|
-
const fileFolder = [...virtualFs.keys()].filter((x) => x.startsWith(`${path}/`)).map((x) => x.replace(`${path}/`, "")).map((x) => {
|
|
109
|
-
const index = x.indexOf("/");
|
|
110
|
-
return index !== -1 ? x.substring(0, index) : x;
|
|
111
|
-
});
|
|
112
|
-
return [...new Set(fileFolder)];
|
|
113
|
-
},
|
|
114
|
-
async rm(path) {
|
|
115
|
-
path = resolveVirtualPath(path);
|
|
116
|
-
for (const key of virtualFs.keys()) {
|
|
117
|
-
if (key === path || key.startsWith(`${path}/`)) {
|
|
118
|
-
virtualFs.delete(key);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
getLibDirs() {
|
|
123
|
-
if (virtualFs.has(resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std/main.tsp"))) {
|
|
124
|
-
return [resolveVirtualPath("/test/node_modules/@typespec/compiler/lib/std")];
|
|
125
|
-
} else {
|
|
126
|
-
return [resolveVirtualPath("/test/node_modules/@typespec/compiler/lib")];
|
|
127
|
-
}
|
|
128
|
-
},
|
|
129
|
-
getExecutionRoot() {
|
|
130
|
-
return resolveVirtualPath("/test/node_modules/@typespec/compiler");
|
|
131
|
-
},
|
|
132
|
-
async getJsImport(path) {
|
|
133
|
-
path = resolveVirtualPath(path);
|
|
134
|
-
const module = await jsImports.get(path);
|
|
135
|
-
if (module === void 0) {
|
|
136
|
-
const e = new Error(`Module ${path} not found`);
|
|
137
|
-
e.code = "MODULE_NOT_FOUND";
|
|
138
|
-
throw e;
|
|
139
|
-
}
|
|
140
|
-
return module;
|
|
141
|
-
},
|
|
142
|
-
async stat(path) {
|
|
143
|
-
path = resolveVirtualPath(path);
|
|
144
|
-
if (virtualFs.has(path)) {
|
|
145
|
-
return {
|
|
146
|
-
isDirectory() {
|
|
147
|
-
return false;
|
|
148
|
-
},
|
|
149
|
-
isFile() {
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
for (const fsPath of virtualFs.keys()) {
|
|
155
|
-
if (fsPath.startsWith(path) && fsPath !== path) {
|
|
156
|
-
return {
|
|
157
|
-
isDirectory() {
|
|
158
|
-
return true;
|
|
159
|
-
},
|
|
160
|
-
isFile() {
|
|
161
|
-
return false;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
const e = new Error(`File ${path} not found.`);
|
|
167
|
-
e.code = "ENOENT";
|
|
168
|
-
throw e;
|
|
169
|
-
},
|
|
170
|
-
// symlinks not supported in test-host
|
|
171
|
-
async realpath(path) {
|
|
172
|
-
return path;
|
|
173
|
-
},
|
|
174
|
-
getSourceFileKind: getSourceFileKindFromExt,
|
|
175
|
-
logSink: console,
|
|
176
|
-
mkdirp: async (path) => path,
|
|
177
|
-
fileURLToPath(path) {
|
|
178
|
-
return path.replace("inmemory:/", "");
|
|
179
|
-
},
|
|
180
|
-
pathToFileURL(path) {
|
|
181
|
-
return "inmemory:/" + resolveVirtualPath(path);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
async function loadLibraries(libsToLoad, importOptions = {}) {
|
|
186
|
-
const entries = await Promise.all(
|
|
187
|
-
libsToLoad.map(async (libName) => {
|
|
188
|
-
const { _TypeSpecLibrary_, $lib, $linter } = await importLibrary(
|
|
189
|
-
libName,
|
|
190
|
-
importOptions
|
|
191
|
-
);
|
|
192
|
-
const lib = {
|
|
193
|
-
name: libName,
|
|
194
|
-
isEmitter: $lib?.emitter,
|
|
195
|
-
definition: $lib,
|
|
196
|
-
packageJson: JSON.parse(_TypeSpecLibrary_.typespecSourceFiles["package.json"]),
|
|
197
|
-
linter: $linter,
|
|
198
|
-
_TypeSpecLibrary_
|
|
199
|
-
};
|
|
200
|
-
return [libName, lib];
|
|
201
|
-
})
|
|
202
|
-
);
|
|
203
|
-
return Object.fromEntries(entries);
|
|
204
|
-
}
|
|
205
|
-
async function createBrowserHost(libsToLoad, importOptions = {}) {
|
|
206
|
-
const [libraries, compiler] = await Promise.all([
|
|
207
|
-
loadLibraries(libsToLoad, importOptions),
|
|
208
|
-
importTypeSpecCompiler(importOptions)
|
|
209
|
-
]);
|
|
210
|
-
return createBrowserHostInternal({
|
|
211
|
-
compiler,
|
|
212
|
-
libraries
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
function range$1(range2) {
|
|
217
|
-
return {
|
|
218
|
-
startColumn: range2.start.character + 1,
|
|
219
|
-
startLineNumber: range2.start.line + 1,
|
|
220
|
-
endColumn: range2.end.character + 1,
|
|
221
|
-
endLineNumber: range2.end.line + 1
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
function foldingRange(range2) {
|
|
225
|
-
return {
|
|
226
|
-
start: range2.startLine + 1,
|
|
227
|
-
end: range2.endLine + 1,
|
|
228
|
-
kind: range2.kind ? new monaco.languages.FoldingRangeKind(range2.kind) : void 0
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
function textEdits(edit) {
|
|
232
|
-
return edit.map(textEdit);
|
|
233
|
-
}
|
|
234
|
-
function textEdit(edit) {
|
|
235
|
-
return {
|
|
236
|
-
range: range$1(edit.range),
|
|
237
|
-
text: edit.newText
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
function signatureHelp(help) {
|
|
241
|
-
return {
|
|
242
|
-
signatures: help?.signatures.map((x) => ({
|
|
243
|
-
...x,
|
|
244
|
-
parameters: x.parameters ?? []
|
|
245
|
-
})) ?? [],
|
|
246
|
-
activeSignature: help?.activeSignature ?? 0,
|
|
247
|
-
activeParameter: help?.activeParameter ?? 0
|
|
248
|
-
};
|
|
249
|
-
}
|
|
250
|
-
function command(command2) {
|
|
251
|
-
return {
|
|
252
|
-
id: command2.command,
|
|
253
|
-
title: command2.title,
|
|
254
|
-
arguments: command2.arguments
|
|
255
|
-
};
|
|
256
|
-
}
|
|
257
|
-
function severity$1(severity2) {
|
|
258
|
-
switch (severity2) {
|
|
259
|
-
case DiagnosticSeverity.Error:
|
|
260
|
-
return monaco.MarkerSeverity.Error;
|
|
261
|
-
case DiagnosticSeverity.Warning:
|
|
262
|
-
return monaco.MarkerSeverity.Warning;
|
|
263
|
-
case DiagnosticSeverity.Hint:
|
|
264
|
-
return monaco.MarkerSeverity.Hint;
|
|
265
|
-
case DiagnosticSeverity.Information:
|
|
266
|
-
return monaco.MarkerSeverity.Info;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
function diagnostic(diagnostic2) {
|
|
270
|
-
return {
|
|
271
|
-
severity: diagnostic2.severity ? severity$1(diagnostic2.severity) : monaco.MarkerSeverity.Error,
|
|
272
|
-
message: diagnostic2.message,
|
|
273
|
-
code: diagnostic2.code?.toString(),
|
|
274
|
-
source: diagnostic2.source,
|
|
275
|
-
startLineNumber: diagnostic2.range.start.line + 1,
|
|
276
|
-
startColumn: diagnostic2.range.start.character + 1,
|
|
277
|
-
endLineNumber: diagnostic2.range.end.line + 1,
|
|
278
|
-
endColumn: diagnostic2.range.end.character + 1
|
|
279
|
-
};
|
|
280
|
-
}
|
|
281
|
-
function codeAction(action) {
|
|
282
|
-
return {
|
|
283
|
-
title: action.title,
|
|
284
|
-
kind: action.kind,
|
|
285
|
-
diagnostics: action.diagnostics?.map(diagnostic),
|
|
286
|
-
command: action.command && command(action.command)
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
const LspToMonaco = {
|
|
290
|
-
range: range$1,
|
|
291
|
-
foldingRange,
|
|
292
|
-
textEdits,
|
|
293
|
-
textEdit,
|
|
294
|
-
signatureHelp,
|
|
295
|
-
codeAction,
|
|
296
|
-
command
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
function textDocumentForModel(model) {
|
|
300
|
-
return TextDocument.create(
|
|
301
|
-
model.uri.toString(),
|
|
302
|
-
"typespec",
|
|
303
|
-
model.getVersionId(),
|
|
304
|
-
model.getValue()
|
|
305
|
-
);
|
|
306
|
-
}
|
|
307
|
-
function position(pos) {
|
|
308
|
-
return {
|
|
309
|
-
line: pos.lineNumber - 1,
|
|
310
|
-
character: pos.column - 1
|
|
311
|
-
};
|
|
312
|
-
}
|
|
313
|
-
function range(range2) {
|
|
314
|
-
return Range.create(
|
|
315
|
-
{
|
|
316
|
-
line: range2.startLineNumber - 1,
|
|
317
|
-
character: range2.startColumn - 1
|
|
318
|
-
},
|
|
319
|
-
{
|
|
320
|
-
line: range2.endLineNumber - 1,
|
|
321
|
-
character: range2.endColumn - 1
|
|
322
|
-
}
|
|
323
|
-
);
|
|
324
|
-
}
|
|
325
|
-
function severity(severity2) {
|
|
326
|
-
switch (severity2) {
|
|
327
|
-
case monaco.MarkerSeverity.Error:
|
|
328
|
-
return DiagnosticSeverity.Error;
|
|
329
|
-
case monaco.MarkerSeverity.Warning:
|
|
330
|
-
return DiagnosticSeverity.Warning;
|
|
331
|
-
case monaco.MarkerSeverity.Hint:
|
|
332
|
-
return DiagnosticSeverity.Hint;
|
|
333
|
-
case monaco.MarkerSeverity.Info:
|
|
334
|
-
return DiagnosticSeverity.Information;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
function markerToDiagnostic(marker) {
|
|
338
|
-
return {
|
|
339
|
-
severity: marker.severity ? severity(marker.severity) : DiagnosticSeverity.Error,
|
|
340
|
-
range: range(marker),
|
|
341
|
-
message: marker.message,
|
|
342
|
-
code: typeof marker.code === "string" ? marker.code : marker.code?.value,
|
|
343
|
-
source: marker.source,
|
|
344
|
-
data: marker.relatedInformation
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
function codeActionContext(context) {
|
|
348
|
-
return {
|
|
349
|
-
only: context.only ? [context.only] : void 0,
|
|
350
|
-
triggerKind: context.trigger,
|
|
351
|
-
diagnostics: context.markers.map(markerToDiagnostic)
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
const MonacoToLsp = {
|
|
355
|
-
textDocumentForModel,
|
|
356
|
-
position,
|
|
357
|
-
range,
|
|
358
|
-
codeActionContext
|
|
359
|
-
};
|
|
360
|
-
|
|
361
|
-
let _currentDiagnostics = [];
|
|
362
|
-
let _currentCompiler;
|
|
363
|
-
function updateDiagnosticsForCodeFixes(compiler, diagnostics) {
|
|
364
|
-
_currentDiagnostics = diagnostics;
|
|
365
|
-
_currentCompiler = compiler;
|
|
366
|
-
}
|
|
367
|
-
function getIndentAction(value) {
|
|
368
|
-
switch (value) {
|
|
369
|
-
case "none":
|
|
370
|
-
return monaco.languages.IndentAction.None;
|
|
371
|
-
case "indent":
|
|
372
|
-
return monaco.languages.IndentAction.Indent;
|
|
373
|
-
case "indentOutdent":
|
|
374
|
-
return monaco.languages.IndentAction.IndentOutdent;
|
|
375
|
-
case "outdent":
|
|
376
|
-
return monaco.languages.IndentAction.Outdent;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
function getTypeSpecLanguageConfiguration() {
|
|
380
|
-
return {
|
|
381
|
-
...TypeSpecLanguageConfiguration,
|
|
382
|
-
onEnterRules: TypeSpecLanguageConfiguration.onEnterRules.map((rule) => {
|
|
383
|
-
return {
|
|
384
|
-
beforeText: new RegExp(rule.beforeText.pattern),
|
|
385
|
-
previousLineText: "previousLineText" in rule ? new RegExp(rule.previousLineText.pattern) : void 0,
|
|
386
|
-
action: {
|
|
387
|
-
indentAction: getIndentAction(rule.action.indent),
|
|
388
|
-
appendText: "appendText" in rule.action ? rule.action.appendText : void 0,
|
|
389
|
-
removeText: "removeText" in rule.action ? rule.action.removeText : void 0
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
})
|
|
393
|
-
};
|
|
394
|
-
}
|
|
395
|
-
async function registerMonacoLanguage(host) {
|
|
396
|
-
monaco.languages.register({ id: "typespec", extensions: [".tsp"] });
|
|
397
|
-
monaco.languages.setLanguageConfiguration("typespec", getTypeSpecLanguageConfiguration());
|
|
398
|
-
if (window.registeredServices) {
|
|
399
|
-
return;
|
|
400
|
-
}
|
|
401
|
-
window.registeredServices = true;
|
|
402
|
-
const serverHost = {
|
|
403
|
-
compilerHost: host,
|
|
404
|
-
getOpenDocumentByURL(url) {
|
|
405
|
-
const model = monaco.editor.getModel(monaco.Uri.parse(url));
|
|
406
|
-
return model ? MonacoToLsp.textDocumentForModel(model) : void 0;
|
|
407
|
-
},
|
|
408
|
-
sendDiagnostics() {
|
|
409
|
-
},
|
|
410
|
-
log: (log) => {
|
|
411
|
-
switch (log.level) {
|
|
412
|
-
case "error":
|
|
413
|
-
console.error(log);
|
|
414
|
-
break;
|
|
415
|
-
case "warning":
|
|
416
|
-
console.warn(log);
|
|
417
|
-
break;
|
|
418
|
-
case "info":
|
|
419
|
-
console.info(log);
|
|
420
|
-
break;
|
|
421
|
-
case "debug":
|
|
422
|
-
console.debug(log);
|
|
423
|
-
break;
|
|
424
|
-
}
|
|
425
|
-
},
|
|
426
|
-
applyEdit(param) {
|
|
427
|
-
return Promise.resolve({ applied: false });
|
|
428
|
-
}
|
|
429
|
-
};
|
|
430
|
-
const { createServer } = host.compiler;
|
|
431
|
-
const serverLib = createServer(serverHost);
|
|
432
|
-
const lsConfig = await serverLib.initialize({
|
|
433
|
-
capabilities: {},
|
|
434
|
-
processId: 1,
|
|
435
|
-
workspaceFolders: [],
|
|
436
|
-
rootUri: "inmemory://"
|
|
437
|
-
});
|
|
438
|
-
serverLib.initialized({});
|
|
439
|
-
monaco.editor.onDidCreateModel((model) => {
|
|
440
|
-
if (model.getLanguageId() !== "typespec") return;
|
|
441
|
-
model.onDidChangeContent(async (changes) => {
|
|
442
|
-
serverLib.checkChange(monacoToLspChangeEvent(model));
|
|
443
|
-
});
|
|
444
|
-
});
|
|
445
|
-
function monacoToLspChangeEvent(model) {
|
|
446
|
-
return {
|
|
447
|
-
document: MonacoToLsp.textDocumentForModel(model)
|
|
448
|
-
};
|
|
449
|
-
}
|
|
450
|
-
function lspDocumentArgs(model) {
|
|
451
|
-
return {
|
|
452
|
-
textDocument: MonacoToLsp.textDocumentForModel(model)
|
|
453
|
-
};
|
|
454
|
-
}
|
|
455
|
-
function lspArgs(model, pos) {
|
|
456
|
-
return {
|
|
457
|
-
...lspDocumentArgs(model),
|
|
458
|
-
position: lspPosition(pos)
|
|
459
|
-
};
|
|
460
|
-
}
|
|
461
|
-
function lspPosition(pos) {
|
|
462
|
-
return {
|
|
463
|
-
line: pos.lineNumber - 1,
|
|
464
|
-
character: pos.column - 1
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
function lspFormattingOptions(options) {
|
|
468
|
-
return FormattingOptions.create(options.tabSize, options.insertSpaces);
|
|
469
|
-
}
|
|
470
|
-
function monacoLocation(loc) {
|
|
471
|
-
return {
|
|
472
|
-
uri: monaco.Uri.parse(loc.uri),
|
|
473
|
-
range: LspToMonaco.range(loc.range)
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
function monacoDocumentHighlight(highlight) {
|
|
477
|
-
return {
|
|
478
|
-
range: LspToMonaco.range(highlight.range),
|
|
479
|
-
kind: monacoDocumentHighlightKind(highlight.kind)
|
|
480
|
-
};
|
|
481
|
-
}
|
|
482
|
-
function monacoDocumentHighlightKind(kind) {
|
|
483
|
-
switch (kind) {
|
|
484
|
-
case DocumentHighlightKind.Text:
|
|
485
|
-
return monaco.languages.DocumentHighlightKind.Text;
|
|
486
|
-
case DocumentHighlightKind.Read:
|
|
487
|
-
return monaco.languages.DocumentHighlightKind.Read;
|
|
488
|
-
case DocumentHighlightKind.Write:
|
|
489
|
-
return monaco.languages.DocumentHighlightKind.Write;
|
|
490
|
-
default:
|
|
491
|
-
return void 0;
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
function monacoHoverContents(contents) {
|
|
495
|
-
return [{ value: contents.value }];
|
|
496
|
-
}
|
|
497
|
-
function monacoHover(hover) {
|
|
498
|
-
if (Array.isArray(hover.contents) || lsp.MarkedString.is(hover.contents)) {
|
|
499
|
-
throw new Error("MarkedString (deprecated) not supported.");
|
|
500
|
-
}
|
|
501
|
-
return {
|
|
502
|
-
contents: monacoHoverContents(hover.contents),
|
|
503
|
-
range: hover.range ? LspToMonaco.range(hover.range) : void 0
|
|
504
|
-
};
|
|
505
|
-
}
|
|
506
|
-
function monacoWorkspaceEdit(edit) {
|
|
507
|
-
const edits = [];
|
|
508
|
-
for (const [uri, changes] of Object.entries(edit.changes ?? {})) {
|
|
509
|
-
const resource = monaco.Uri.parse(uri);
|
|
510
|
-
for (const change of changes) {
|
|
511
|
-
edits.push({ resource, textEdit: LspToMonaco.textEdit(change), versionId: void 0 });
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return { edits };
|
|
515
|
-
}
|
|
516
|
-
monaco.languages.registerDefinitionProvider("typespec", {
|
|
517
|
-
async provideDefinition(model, position) {
|
|
518
|
-
const results = await serverLib.gotoDefinition(lspArgs(model, position));
|
|
519
|
-
return results.map(monacoLocation);
|
|
520
|
-
}
|
|
521
|
-
});
|
|
522
|
-
monaco.languages.registerReferenceProvider("typespec", {
|
|
523
|
-
async provideReferences(model, position, context) {
|
|
524
|
-
const results = await serverLib.findReferences({ ...lspArgs(model, position), context });
|
|
525
|
-
return results.map(monacoLocation);
|
|
526
|
-
}
|
|
527
|
-
});
|
|
528
|
-
monaco.languages.registerRenameProvider("typespec", {
|
|
529
|
-
async resolveRenameLocation(model, position) {
|
|
530
|
-
const result = await serverLib.prepareRename(lspArgs(model, position));
|
|
531
|
-
if (!result) {
|
|
532
|
-
throw new Error("This element can't be renamed.");
|
|
533
|
-
}
|
|
534
|
-
const text = model.getWordAtPosition(position)?.word;
|
|
535
|
-
if (!text) {
|
|
536
|
-
throw new Error("Failed to obtain word at position.");
|
|
537
|
-
}
|
|
538
|
-
return { text, range: LspToMonaco.range(result) };
|
|
539
|
-
},
|
|
540
|
-
async provideRenameEdits(model, position, newName) {
|
|
541
|
-
const result = await serverLib.rename({ ...lspArgs(model, position), newName });
|
|
542
|
-
return monacoWorkspaceEdit(result);
|
|
543
|
-
}
|
|
544
|
-
});
|
|
545
|
-
monaco.languages.registerFoldingRangeProvider("typespec", {
|
|
546
|
-
async provideFoldingRanges(model) {
|
|
547
|
-
const ranges = await serverLib.getFoldingRanges(lspDocumentArgs(model));
|
|
548
|
-
const output = ranges.map(LspToMonaco.foldingRange);
|
|
549
|
-
return output;
|
|
550
|
-
}
|
|
551
|
-
});
|
|
552
|
-
monaco.languages.registerHoverProvider("typespec", {
|
|
553
|
-
async provideHover(model, position) {
|
|
554
|
-
const hover = await serverLib.getHover(lspArgs(model, position));
|
|
555
|
-
return monacoHover(hover);
|
|
556
|
-
}
|
|
557
|
-
});
|
|
558
|
-
monaco.languages.registerSignatureHelpProvider("typespec", {
|
|
559
|
-
signatureHelpTriggerCharacters: ["(", ",", "<"],
|
|
560
|
-
signatureHelpRetriggerCharacters: [")"],
|
|
561
|
-
async provideSignatureHelp(model, position) {
|
|
562
|
-
const help = await serverLib.getSignatureHelp(lspArgs(model, position));
|
|
563
|
-
return { value: LspToMonaco.signatureHelp(help), dispose: () => {
|
|
564
|
-
} };
|
|
565
|
-
}
|
|
566
|
-
});
|
|
567
|
-
monaco.languages.registerDocumentFormattingEditProvider("typespec", {
|
|
568
|
-
async provideDocumentFormattingEdits(model, options, token) {
|
|
569
|
-
const edits = await serverLib.formatDocument({
|
|
570
|
-
...lspDocumentArgs(model),
|
|
571
|
-
options: lspFormattingOptions(options)
|
|
572
|
-
});
|
|
573
|
-
return LspToMonaco.textEdits(edits);
|
|
574
|
-
}
|
|
575
|
-
});
|
|
576
|
-
monaco.languages.registerDocumentHighlightProvider("typespec", {
|
|
577
|
-
async provideDocumentHighlights(model, position) {
|
|
578
|
-
const highlights = await serverLib.findDocumentHighlight(lspArgs(model, position));
|
|
579
|
-
return highlights.map(monacoDocumentHighlight);
|
|
580
|
-
}
|
|
581
|
-
});
|
|
582
|
-
monaco.languages.registerCompletionItemProvider("typespec", {
|
|
583
|
-
triggerCharacters: lsConfig.capabilities.completionProvider.triggerCharacters,
|
|
584
|
-
async provideCompletionItems(model, position) {
|
|
585
|
-
const result = await serverLib.complete(lspArgs(model, position));
|
|
586
|
-
const word = model.getWordUntilPosition(position);
|
|
587
|
-
const range = {
|
|
588
|
-
startLineNumber: position.lineNumber,
|
|
589
|
-
endLineNumber: position.lineNumber,
|
|
590
|
-
startColumn: word.startColumn,
|
|
591
|
-
endColumn: word.endColumn
|
|
592
|
-
};
|
|
593
|
-
const suggestions = [];
|
|
594
|
-
for (const item of result.items) {
|
|
595
|
-
let itemRange = range;
|
|
596
|
-
let insertText = item.insertText ?? item.label;
|
|
597
|
-
if (item.textEdit && "range" in item.textEdit) {
|
|
598
|
-
itemRange = LspToMonaco.range(item.textEdit.range);
|
|
599
|
-
insertText = item.textEdit.newText;
|
|
600
|
-
}
|
|
601
|
-
suggestions.push({
|
|
602
|
-
label: item.label,
|
|
603
|
-
kind: item.kind,
|
|
604
|
-
documentation: item.documentation,
|
|
605
|
-
insertText,
|
|
606
|
-
range: itemRange,
|
|
607
|
-
commitCharacters: item.commitCharacters ?? lsConfig.capabilities.completionProvider.allCommitCharacters,
|
|
608
|
-
tags: item.tags
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
return { suggestions };
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
monaco.languages.registerCodeActionProvider("typespec", {
|
|
615
|
-
async provideCodeActions(model, range) {
|
|
616
|
-
const compiler = _currentCompiler;
|
|
617
|
-
if (!compiler) return { actions: [], dispose: () => {
|
|
618
|
-
} };
|
|
619
|
-
const actions = [];
|
|
620
|
-
for (const diag of _currentDiagnostics) {
|
|
621
|
-
if (!diag.codefixes?.length) continue;
|
|
622
|
-
const loc = compiler.getSourceLocation(diag.target, { locateId: true });
|
|
623
|
-
if (!loc || loc.file.path !== "/test/main.tsp") continue;
|
|
624
|
-
const monacoRange = getMonacoRange(compiler, diag.target);
|
|
625
|
-
if (!monacoRangesOverlap(monacoRange, range)) continue;
|
|
626
|
-
for (const fix of diag.codefixes) {
|
|
627
|
-
const edits = await compiler.resolveCodeFix(fix);
|
|
628
|
-
const workspaceEdits = edits.filter((edit) => edit.file.path === "/test/main.tsp").map((edit) => {
|
|
629
|
-
const start = edit.file.getLineAndCharacterOfPosition(edit.pos);
|
|
630
|
-
if (edit.kind === "insert-text") {
|
|
631
|
-
return {
|
|
632
|
-
resource: model.uri,
|
|
633
|
-
textEdit: {
|
|
634
|
-
range: {
|
|
635
|
-
startLineNumber: start.line + 1,
|
|
636
|
-
startColumn: start.character + 1,
|
|
637
|
-
endLineNumber: start.line + 1,
|
|
638
|
-
endColumn: start.character + 1
|
|
639
|
-
},
|
|
640
|
-
text: edit.text
|
|
641
|
-
},
|
|
642
|
-
versionId: void 0
|
|
643
|
-
};
|
|
644
|
-
} else {
|
|
645
|
-
const end = edit.file.getLineAndCharacterOfPosition(edit.end);
|
|
646
|
-
return {
|
|
647
|
-
resource: model.uri,
|
|
648
|
-
textEdit: {
|
|
649
|
-
range: {
|
|
650
|
-
startLineNumber: start.line + 1,
|
|
651
|
-
startColumn: start.character + 1,
|
|
652
|
-
endLineNumber: end.line + 1,
|
|
653
|
-
endColumn: end.character + 1
|
|
654
|
-
},
|
|
655
|
-
text: edit.text
|
|
656
|
-
},
|
|
657
|
-
versionId: void 0
|
|
658
|
-
};
|
|
659
|
-
}
|
|
660
|
-
});
|
|
661
|
-
if (workspaceEdits.length > 0) {
|
|
662
|
-
actions.push({
|
|
663
|
-
title: fix.label,
|
|
664
|
-
kind: "quickfix",
|
|
665
|
-
edit: { edits: workspaceEdits }
|
|
666
|
-
});
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
}
|
|
670
|
-
return { actions, dispose: () => {
|
|
671
|
-
} };
|
|
672
|
-
}
|
|
673
|
-
});
|
|
674
|
-
monaco.editor.defineTheme("typespec", {
|
|
675
|
-
base: "vs",
|
|
676
|
-
inherit: true,
|
|
677
|
-
colors: {},
|
|
678
|
-
rules: [
|
|
679
|
-
{ token: "macro", foreground: "#800000" },
|
|
680
|
-
{ token: "function", foreground: "#795E26" }
|
|
681
|
-
]
|
|
682
|
-
});
|
|
683
|
-
monaco.editor.defineTheme("typespec-dark", {
|
|
684
|
-
base: "vs-dark",
|
|
685
|
-
inherit: true,
|
|
686
|
-
colors: {},
|
|
687
|
-
rules: [
|
|
688
|
-
{ token: "macro", foreground: "#E06C75" },
|
|
689
|
-
{ token: "function", foreground: "#E06C75" }
|
|
690
|
-
]
|
|
691
|
-
});
|
|
692
|
-
monaco.languages.registerDocumentSemanticTokensProvider("typespec", {
|
|
693
|
-
getLegend() {
|
|
694
|
-
const legend = lsConfig.capabilities.semanticTokensProvider.legend;
|
|
695
|
-
return {
|
|
696
|
-
tokenModifiers: legend.tokenModifiers,
|
|
697
|
-
tokenTypes: legend.tokenTypes.map((entry) => {
|
|
698
|
-
switch (entry) {
|
|
699
|
-
case "namespace":
|
|
700
|
-
case "class":
|
|
701
|
-
case "enum":
|
|
702
|
-
case "typeParameter":
|
|
703
|
-
case "struct":
|
|
704
|
-
case "interface":
|
|
705
|
-
return "type";
|
|
706
|
-
case "property":
|
|
707
|
-
case "enumMember":
|
|
708
|
-
return "variable";
|
|
709
|
-
case "docCommentTag":
|
|
710
|
-
return "keyword";
|
|
711
|
-
default:
|
|
712
|
-
return entry;
|
|
713
|
-
}
|
|
714
|
-
})
|
|
715
|
-
};
|
|
716
|
-
},
|
|
717
|
-
async provideDocumentSemanticTokens(model) {
|
|
718
|
-
const result = await serverLib.buildSemanticTokens(lspDocumentArgs(model));
|
|
719
|
-
return {
|
|
720
|
-
resultId: result.resultId,
|
|
721
|
-
data: new Uint32Array(result.data)
|
|
722
|
-
};
|
|
723
|
-
},
|
|
724
|
-
releaseDocumentSemanticTokens() {
|
|
725
|
-
}
|
|
726
|
-
});
|
|
727
|
-
}
|
|
728
|
-
function getMonacoRange(typespecCompiler, target) {
|
|
729
|
-
const loc = typespecCompiler.getSourceLocation(target, { locateId: true });
|
|
730
|
-
if (loc === void 0 || loc.file.path !== "/test/main.tsp") {
|
|
731
|
-
return {
|
|
732
|
-
startLineNumber: 1,
|
|
733
|
-
startColumn: 1,
|
|
734
|
-
endLineNumber: 1,
|
|
735
|
-
endColumn: 1
|
|
736
|
-
};
|
|
737
|
-
}
|
|
738
|
-
const start = loc.file.getLineAndCharacterOfPosition(loc.pos);
|
|
739
|
-
const end = loc.file.getLineAndCharacterOfPosition(loc.end);
|
|
740
|
-
return {
|
|
741
|
-
startLineNumber: start.line + 1,
|
|
742
|
-
startColumn: start.character + 1,
|
|
743
|
-
endLineNumber: end.line + 1,
|
|
744
|
-
endColumn: end.character + 1
|
|
745
|
-
};
|
|
746
|
-
}
|
|
747
|
-
function monacoRangesOverlap(a, b) {
|
|
748
|
-
if (a.endLineNumber < b.startLineNumber || b.endLineNumber < a.startLineNumber) {
|
|
749
|
-
return false;
|
|
750
|
-
}
|
|
751
|
-
if (a.endLineNumber === b.startLineNumber && a.endColumn < b.startColumn) {
|
|
752
|
-
return false;
|
|
753
|
-
}
|
|
754
|
-
if (b.endLineNumber === a.startLineNumber && b.endColumn < a.startColumn) {
|
|
755
|
-
return false;
|
|
756
|
-
}
|
|
757
|
-
return true;
|
|
758
|
-
}
|
|
759
|
-
|
|
760
|
-
export { resolveVirtualPath as a, createBrowserHost as c, debugGlobals as d, getMonacoRange as g, printDebugInfo as p, registerMonacoLanguage as r, updateDiagnosticsForCodeFixes as u };
|