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