modern-monaco 0.0.0-beta.0
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/LICENSE +21 -0
- package/README.md +359 -0
- package/dist/cache.js +94 -0
- package/dist/editor-core.js +177856 -0
- package/dist/editor-worker.js +13528 -0
- package/dist/index.js +635 -0
- package/dist/lsp/css/setup.js +43 -0
- package/dist/lsp/css/worker.js +40804 -0
- package/dist/lsp/html/setup.js +90 -0
- package/dist/lsp/html/worker.js +16632 -0
- package/dist/lsp/json/setup.js +258 -0
- package/dist/lsp/json/worker.js +7964 -0
- package/dist/lsp/language-service.js +2244 -0
- package/dist/lsp/typescript/libs.js +100 -0
- package/dist/lsp/typescript/setup.js +425 -0
- package/dist/lsp/typescript/worker.js +2858 -0
- package/dist/onig.wasm +0 -0
- package/dist/shiki-wasm.js +9 -0
- package/dist/shiki.js +8744 -0
- package/dist/ssr/index.js +47 -0
- package/dist/ssr/workerd.js +46 -0
- package/dist/util.js +192 -0
- package/dist/workspace.js +611 -0
- package/package.json +67 -0
- package/types/cache.d.ts +7 -0
- package/types/index.d.ts +47 -0
- package/types/jsonSchema.d.ts +90 -0
- package/types/lsp.d.ts +106 -0
- package/types/monaco.d.ts +8284 -0
- package/types/ssr.d.ts +14 -0
- package/types/textmate.d.ts +2 -0
- package/types/vscode.d.ts +303 -0
- package/types/workspace.d.ts +74 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
// node_modules/.pnpm/@esm.sh+import-map@0.1.1/node_modules/@esm.sh/import-map/dist/import-map.mjs
|
|
2
|
+
function createBlankImportMap(baseURL) {
|
|
3
|
+
return {
|
|
4
|
+
$baseURL: new URL(baseURL ?? ".", "file:///").href,
|
|
5
|
+
imports: {},
|
|
6
|
+
scopes: {}
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
function parseImportMapFromJson(json, baseURL) {
|
|
10
|
+
const importMap = {
|
|
11
|
+
$baseURL: new URL(baseURL ?? ".", "file:///").href,
|
|
12
|
+
imports: {},
|
|
13
|
+
scopes: {}
|
|
14
|
+
};
|
|
15
|
+
const v = JSON.parse(json);
|
|
16
|
+
if (isObject(v)) {
|
|
17
|
+
const { imports, scopes } = v;
|
|
18
|
+
if (isObject(imports)) {
|
|
19
|
+
validateImports(imports);
|
|
20
|
+
importMap.imports = imports;
|
|
21
|
+
}
|
|
22
|
+
if (isObject(scopes)) {
|
|
23
|
+
validateScopes(scopes);
|
|
24
|
+
importMap.scopes = scopes;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return importMap;
|
|
28
|
+
}
|
|
29
|
+
function parseImportMapFromHtml(html, baseURL) {
|
|
30
|
+
const tplEl = document.createElement("template");
|
|
31
|
+
tplEl.innerHTML = html;
|
|
32
|
+
const scriptEl = tplEl.content.querySelector("script[type='importmap']");
|
|
33
|
+
if (scriptEl) {
|
|
34
|
+
return parseImportMapFromJson(scriptEl.textContent, baseURL);
|
|
35
|
+
}
|
|
36
|
+
return createBlankImportMap(baseURL);
|
|
37
|
+
}
|
|
38
|
+
function validateImports(imports) {
|
|
39
|
+
for (const [k, v] of Object.entries(imports)) {
|
|
40
|
+
if (!v || typeof v !== "string") {
|
|
41
|
+
delete imports[k];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function validateScopes(imports) {
|
|
46
|
+
for (const [k, v] of Object.entries(imports)) {
|
|
47
|
+
if (isObject(v)) {
|
|
48
|
+
validateImports(v);
|
|
49
|
+
} else {
|
|
50
|
+
delete imports[k];
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function isObject(v) {
|
|
55
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// src/lsp/json/schemas.ts
|
|
59
|
+
var schemas = [
|
|
60
|
+
{
|
|
61
|
+
uri: "https://github.com/denoland/vscode_deno/blob/main/schemas/import_map.schema.json",
|
|
62
|
+
fileMatch: [
|
|
63
|
+
"import_map.json",
|
|
64
|
+
"import-map.json",
|
|
65
|
+
"importmap.json",
|
|
66
|
+
"importMap.json",
|
|
67
|
+
"*.importmap"
|
|
68
|
+
],
|
|
69
|
+
schema: {
|
|
70
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
71
|
+
title: "An Import Map",
|
|
72
|
+
description: "An import map which is used to remap imports when modules are loaded.",
|
|
73
|
+
type: "object",
|
|
74
|
+
properties: {
|
|
75
|
+
imports: {
|
|
76
|
+
description: "A map of specifiers to their remapped specifiers.",
|
|
77
|
+
type: "object",
|
|
78
|
+
properties: {
|
|
79
|
+
"@jsxRuntime": {
|
|
80
|
+
description: "The key is the specifier for JSX import source, with a value that represents the target specifier.",
|
|
81
|
+
type: "string",
|
|
82
|
+
default: "https://esm.sh/react@18.3.1"
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
additionalProperties: {
|
|
86
|
+
description: "The key is the specifier or partial specifier to match, with a value that represents the target specifier.",
|
|
87
|
+
type: "string"
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
scopes: {
|
|
91
|
+
description: "Define a scope which remaps a specifier in only a specified scope",
|
|
92
|
+
type: "object",
|
|
93
|
+
additionalProperties: {
|
|
94
|
+
description: "A definition of a scoped remapping.",
|
|
95
|
+
type: "object",
|
|
96
|
+
additionalProperties: {
|
|
97
|
+
description: "The key is the specifier or partial specifier to match within the referring scope, with a value that represents the target specifier.",
|
|
98
|
+
type: "string"
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
uri: "https://json.schemastore.org/tsconfig",
|
|
107
|
+
fileMatch: [
|
|
108
|
+
"tsconfig.json"
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
// src/lsp/json/setup.ts
|
|
114
|
+
import * as ls from "../language-service.js";
|
|
115
|
+
async function setup2(monaco, languageId, workspace, languageSettings, formattingOptions) {
|
|
116
|
+
const { editor, languages } = monaco;
|
|
117
|
+
const createData = {
|
|
118
|
+
settings: {
|
|
119
|
+
validate: true,
|
|
120
|
+
allowComments: false,
|
|
121
|
+
schemas: Array.isArray(languageSettings?.schemas) ? schemas.concat(languageSettings.schemas) : schemas,
|
|
122
|
+
comments: "error",
|
|
123
|
+
trailingCommas: "error",
|
|
124
|
+
schemaRequest: "warning",
|
|
125
|
+
schemaValidation: "warning"
|
|
126
|
+
},
|
|
127
|
+
format: {
|
|
128
|
+
tabSize: 4,
|
|
129
|
+
insertSpaces: false,
|
|
130
|
+
trimTrailingWhitespace: true,
|
|
131
|
+
insertFinalNewline: true,
|
|
132
|
+
trimFinalNewlines: true,
|
|
133
|
+
...formattingOptions
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
const worker = editor.createWebWorker({
|
|
137
|
+
moduleId: "lsp/json/worker",
|
|
138
|
+
label: languageId,
|
|
139
|
+
createData,
|
|
140
|
+
host: ls.createHost(workspace)
|
|
141
|
+
});
|
|
142
|
+
const resetSchema = async (uri) => {
|
|
143
|
+
(await worker.getProxy()).resetSchema(uri.toString());
|
|
144
|
+
};
|
|
145
|
+
editor.onWillDisposeModel((model) => {
|
|
146
|
+
if (model.getLanguageId() === languageId) {
|
|
147
|
+
resetSchema(model.uri);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
editor.onDidChangeModelLanguage((event) => {
|
|
151
|
+
if (event.model.getLanguageId() === languageId) {
|
|
152
|
+
resetSchema(event.model.uri);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
ls.setup(monaco);
|
|
156
|
+
ls.enableBasicFeatures(languageId, worker, [" ", ":", '"'], workspace);
|
|
157
|
+
ls.enableColorPresentation(languageId, worker);
|
|
158
|
+
ls.enableDocumentLinks(languageId, worker);
|
|
159
|
+
languages.registerCodeLensProvider(languageId, {
|
|
160
|
+
provideCodeLenses: function(model, _token) {
|
|
161
|
+
const isImportMap = model.uri.scheme == "file" && ["importmap.json", "import_map.json", "import-map.json", "importMap.json"].some((name) => model.uri.path === "/" + name);
|
|
162
|
+
if (isImportMap) {
|
|
163
|
+
const m2 = model.findNextMatch(`"imports":\\s*\\{`, { column: 1, lineNumber: 1 }, true, false, null, false);
|
|
164
|
+
return {
|
|
165
|
+
lenses: [
|
|
166
|
+
{
|
|
167
|
+
range: m2?.range ?? new monaco.Range(1, 1, 1, 1),
|
|
168
|
+
command: {
|
|
169
|
+
id: "search-npm-package",
|
|
170
|
+
title: "$(sparkle-filled) Search packages on NPM",
|
|
171
|
+
tooltip: "Search packages on NPM",
|
|
172
|
+
arguments: [model]
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
],
|
|
176
|
+
dispose: () => {
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
editor.registerCommand("search-npm-package", async (_accessor, model) => {
|
|
183
|
+
const keyword = await monaco.showInputBox({
|
|
184
|
+
placeHolder: "Enter package name, e.g. lodash",
|
|
185
|
+
validateInput: (value) => {
|
|
186
|
+
return /^[\w\-\.@]+$/.test(value) ? null : "Invalid package name, only word characters are allowed";
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (!keyword) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
const pkg = await monaco.showQuickPick(searchPackagesFromNpm(keyword, 32), {
|
|
193
|
+
placeHolder: "Select a package",
|
|
194
|
+
matchOnDetail: true
|
|
195
|
+
});
|
|
196
|
+
if (!pkg) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const editor2 = monaco.editor.getEditors().filter((e) => e.hasWidgetFocus())[0];
|
|
200
|
+
const modelPath = model.uri.path;
|
|
201
|
+
const { imports, scopes } = modelPath.endsWith(".json") ? parseImportMapFromJson(model.getValue()) : parseImportMapFromHtml(model.getValue());
|
|
202
|
+
const specifier = "https://esm.sh/" + pkg.name + "@" + pkg.version;
|
|
203
|
+
if (imports[pkg.name] === specifier) {
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
imports[pkg.name] = specifier;
|
|
207
|
+
const json = JSON.stringify({ imports, scopes: Object.keys(scopes).length > 0 ? scopes : void 0 }, null, 2);
|
|
208
|
+
if (modelPath.endsWith(".json")) {
|
|
209
|
+
const viewState = editor2?.saveViewState();
|
|
210
|
+
model.setValue(model.normalizeIndentation(json));
|
|
211
|
+
editor2?.restoreViewState(viewState);
|
|
212
|
+
} else if (modelPath.endsWith(".html")) {
|
|
213
|
+
const html = model.getValue();
|
|
214
|
+
const newHtml = html.replace(
|
|
215
|
+
/<script[^>]*? type="importmap"[^>]*?>[^]*?<\/script>/,
|
|
216
|
+
['<script type="importmap">', ...json.split("\n").map((l) => " " + l), "<\/script>"].join("\n ")
|
|
217
|
+
);
|
|
218
|
+
const viewState = editor2?.saveViewState();
|
|
219
|
+
model.setValue(model.normalizeIndentation(newHtml));
|
|
220
|
+
editor2?.restoreViewState(viewState);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
async function searchPackagesFromNpm(keyword, size = 20) {
|
|
225
|
+
const res = await fetch(`https://registry.npmjs.com/-/v1/search?text=${keyword}&size=${size}`);
|
|
226
|
+
if (!res.ok) {
|
|
227
|
+
throw new Error(`Failed to search npm packages: ${res.statusText}`);
|
|
228
|
+
}
|
|
229
|
+
const { objects } = await res.json();
|
|
230
|
+
if (!Array.isArray(objects)) {
|
|
231
|
+
return [];
|
|
232
|
+
}
|
|
233
|
+
const items = new Array(objects.length);
|
|
234
|
+
let len = 0;
|
|
235
|
+
for (const { package: pkg } of objects) {
|
|
236
|
+
if (!pkg.name.startsWith("@types/")) {
|
|
237
|
+
items[len] = {
|
|
238
|
+
label: (keyword === pkg.name ? "$(star-empty) " : "") + pkg.name,
|
|
239
|
+
description: pkg.version,
|
|
240
|
+
detail: pkg.description,
|
|
241
|
+
name: pkg.name,
|
|
242
|
+
version: pkg.version
|
|
243
|
+
};
|
|
244
|
+
len++;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
return items.slice(0, len);
|
|
248
|
+
}
|
|
249
|
+
function getWorkerUrl() {
|
|
250
|
+
const i = () => import("./worker.js");
|
|
251
|
+
const m = getWorkerUrl.toString().match(/import\(['"](.+?)['"]\)/);
|
|
252
|
+
if (!m) throw new Error("worker url not found", { cause: i });
|
|
253
|
+
return new URL(m[1], import.meta.url);
|
|
254
|
+
}
|
|
255
|
+
export {
|
|
256
|
+
getWorkerUrl,
|
|
257
|
+
setup2 as setup
|
|
258
|
+
};
|