UNPKG

swagger-editor

Version:

- [Anonymized analytics](#anonymized-analytics) - [Getting started](#getting-started) - [Prerequisites](#prerequisites) - [Installation](#installation) - [Usage](#usage) - [Development](#development) - [Prerequisites](#prerequisites) - [Setting

856 lines (855 loc) 20.3 kB
import { jsx as e } from "react/jsx-runtime"; import t from "js-yaml"; import * as n from "monaco-editor"; import { languages as r } from "vscode"; import { ModesRegistry as i } from "@codingame/monaco-vscode-api/vscode/vs/editor/common/languages/modesRegistry"; import { createConverter as a } from "vscode-languageclient/lib/common/codeConverter.js"; import { createConverter as o } from "vscode-languageclient/lib/common/protocolConverter.js"; //#region src/plugins/editor-monaco-language-apidom/language/apidom.js var s = "apidom", c = { comments: { lineComment: "#" }, brackets: [ ["{", "}"], ["[", "]"], ["(", ")"] ], autoClosingPairs: [ { open: "{", close: "}" }, { open: "[", close: "]" }, { open: "(", close: ")" }, { open: "\"", close: "\"" }, { open: "'", close: "'" } ], surroundingPairs: [ { open: "{", close: "}" }, { open: "[", close: "]" }, { open: "(", close: ")" }, { open: "\"", close: "\"" }, { open: "'", close: "'" } ], onEnterRules: [ { beforeText: /:\s*$/, action: { indentAction: n.languages.IndentAction.Indent } }, { beforeText: /-\s+\w*$/, action: { indentAction: n.languages.IndentAction.None, appendText: "- " } }, { beforeText: /-\s*$/, action: { indentAction: n.languages.IndentAction.None, appendText: "- " } } ] }, l = { defaultToken: "invalid", keywords: [ "swagger", "info", "host", "basePath", "tags", "schemes", "paths", "externalDocs" ], typeKeywords: [ "description", "title", "termsOfService" ], escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, tokenizer: { root: [ [/[a-zA-Z_$][\w$]*/, { cases: { "@keywords": { token: "keyword" }, "@typeKeywords": { token: "type" }, "@default": "identifier" } }], { include: "@whitespace" }, [/"([^"\\]|\\.)*$/, "string.invalid"], [ /"/, "string", "@string" ] ], whitespace: [[/[ \t\r\n]+/, ""]], string: [ [/[^\\"]+/, "string"], [/@escapes/, "string.escape"], [/\\./, "string.escape.invalid"], [ /"/, "string", "@pop" ] ] } }, u = { defaultToken: "invalid", escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/, tokenizer: { root: [ [/^#.*$/, "plain.value"], [/\s*{/, { token: "plain.value", next: "@objects" }], [/^(x-[^:\s]+)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/^(x-[^:\s]+)(:)$/, ["plain.keyword", "plain.value"]], [/^([a-zA-Z_$][\w$]*)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/^([a-zA-Z_$][\w$]*)(:)$/, ["plain.keyword", "plain.value"]], [/^(\s*'[^']*')(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/^(\s*'[^']*')(:)$/, ["plain.keyword", "plain.value"]], [/^(\s*"[^"]*")(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/^(\s*"[^"]*")(:)$/, ["plain.keyword", "plain.value"]], [ /^\s*-\s/, "plain.value", "@arrays" ], [ /^\s{1,}/, "", "@nestedKeywords" ], [/^(\s*[^\s]+)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/^(\s*[^\s]+)(:)$/, ["plain.keyword", "plain.value"]], { include: "@whitespace" }, [/"([^"\\]|\\.)*$/, "string.invalid"], [ /"/, "plain.value.string", "@stringDoubleQuoted" ], [ /'/, "plain.value.string", "@stringSingleQuoted" ] ], values: [ [/\s*{/, { token: "plain.value", next: "@objects" }], [/\s*\[/, { token: "plain.value", next: "@jsonArrays" }], [/\s*[0-9]+\s*$/, "plain.value.number"], [/\s*[0-9]+(\.[0-9]+)?\s*$/, "plain.value.number"], [/\s*(true|false)\s*$/, "plain.value.boolean"], [ /^/, "", "@pop" ], [/\s*"/, { token: "plain.value.string", next: "@stringDoubleQuoted" }], [/\s*'/, { token: "plain.value.string", next: "@stringSingleQuoted" }], [/[^\n]+$/, "plain.value"], [ /^/, "", "@pop" ], [ /:/, "plain.value", "@pop" ], [ /.+$/, "", "@pop" ], [ /.*$/, "", "@pop" ] ], whitespace: [[/[ \t\r\n]+/, ""]], stringDoubleQuoted: [ [/[^\\"]+/, "plain.value.string"], [/@escapes/, "string.escape"], [/\\./, "string.escape.invalid"], [ /"/, "plain.value.string", "@pop" ] ], stringSingleQuoted: [ [/[^\\']+/, "plain.value.string"], ["string.escape"], [/\\./, "string.escape.invalid"], [ /'/, "plain.value.string", "@pop" ] ], nestedKeywords: [ [/(x-[^:\s]+)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(x-[^:\s]+)(:)$/, ["plain.keyword", "plain.value"]], [/([a-zA-Z_$][\w$]*)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/([a-zA-Z_$][\w$]*)(:)$/, ["plain.keyword", "plain.value"]], [/(\s*'[^']*')(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(\s*'[^']*')(:)$/, ["plain.keyword", "plain.value"]], [/(\s*"[^"]*")(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(\s*"[^"]*")(:)$/, ["plain.keyword", "plain.value"]], [/(\s*[^\s]+)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(\s*[^\s]+)(:)$/, ["plain.keyword", "plain.value"]], [ /^/, "", "@pop" ], [ /.+$/, "plain.value", "@pop" ] ], arrays: [ [/(x-[^:\s]+)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(x-[^:\s]+)(:)$/, ["plain.keyword", "plain.value"]], [/([a-zA-Z_$][\w$]*)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/([a-zA-Z_$][\w$]*)(:)$/, ["plain.keyword", "plain.value"]], [/(\s*'[^']*')(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(\s*'[^']*')(:)$/, ["plain.keyword", "plain.value"]], [/(\s*"[^"]*")(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(\s*"[^"]*")(:)$/, ["plain.keyword", "plain.value"]], [/(\s*[^\s]+)(: )/, ["plain.keyword", { token: "plain.value", next: "@values" }]], [/(\s*[^\s]+)(:)$/, ["plain.keyword", "plain.value"]], [ /^/, "", "@pop" ], [/\s*([0-9]+)\s*$/, "plain.value.number"], [/\s*([0-9]+\.[0-9]+)\s*$/, "plain.value.number"], [/\s*(true|false)\s*$/, "plain.value.boolean"], [ /^/, "", "@pop" ], [/([^\n'"]+)\s*$/, "plain.value"], [ /\s*"/, "plain.value.string", "@stringDoubleQuoted" ], [ /\s*'/, "plain.value.string", "@stringSingleQuoted" ], [ /^/, "", "@pop" ], [ /:/, "plain.value", "@pop" ], [ /.+$/, "", "@pop" ], [ /.*$/, "", "@pop" ] ], objectValues: [ [/\s*{/, { token: "plain.value", next: "@objects" }], [/\s*\[/, { token: "plain.value", next: "@jsonArrays" }], [/(\s*)([0-9]+\.[0-9]+)(\s*,?\s*)/, [ "plain.value", "plain.value.number", { token: "plain.value", next: "@pop" } ]], [/(\s*)([0-9]+)(\s*,?\s*)/, [ "plain.value", "plain.value.number", { token: "plain.value", next: "@pop" } ]], [/(\s*)(true|false)(\s*,?\s*)/, [ "plain.value", "plain.value.boolean", { token: "plain.value", next: "@pop" } ]], [/\s*"/, { token: "plain.value.string", next: "@stringDoubleQuoted" }], [/\s*'/, { token: "plain.value.string", next: "@stringSingleQuoted" }], [/[^,}\n]+/, "plain.value"], [ /,/, "plain.value", "@pop" ], [ /(?=})/, "plain.value", "@pop" ] ], objects: [ [/(\s*'[^']*'\s*)(:)(?=\s*\S)/, ["plain.keyword", { token: "plain.value", next: "@objectValues" }]], [/(\s*'[^']*')(\s*:\s*)$/, ["plain.keyword", { token: "plain.value", next: "@objectValues" }]], [/(\s*"[^"]*")(\s*:)(?=\s*\S)/, ["plain.keyword", { token: "plain.value", next: "@objectValues" }]], [/(\s*"[^"]*")(\s*:\s*)$/, ["plain.keyword", { token: "plain.value", next: "@objectValues" }]], [/\s*{/, { token: "plain.value", next: "@objects" }], [/\s*\[/, { token: "plain.value", next: "@jsonArrays" }], [/[^,}\n]+/, "plain.value"], [/,/, "plain.value"], [ /}/, "plain.value", "@pop" ] ], jsonArrays: [ [/\s*{/, { token: "plain.value", next: "@objects" }], [/\s*\[/, { token: "plain.value", next: "@jsonArrays" }], [/(\s*)([0-9]+)(\s*,?\s*)/, [ "plain.value", "plain.value.number", "plain.value" ]], [/(\s*)([0-9]+\.[0-9]+)(\s*,?\s*)/, [ "plain.value", "plain.value.number", "plain.value" ]], [/(\s*)(true|false)(\s*,?\s*)/, [ "plain.value", "plain.value.boolean", "plain.value" ]], [/\s*"/, { token: "plain.value.string", next: "@stringDoubleQuoted" }], [/\s*'/, { token: "plain.value.string", next: "@stringSingleQuoted" }], [/[^,\]\n]+/, "plain.value"], [/,/, "plain.value"], [ /\]/, "plain.value", "@pop" ] ] } }, d = 120 * 1e3, f = class { #e = null; #t = null; #n = null; #r; #i = 0; constructor(e) { this.#e = e, this.#r = setInterval(() => this.#o(), 30 * 1e3); } #a() { this.#t &&= (this.#t.dispose(), null), this.#n = null; } #o() { this.#t && Date.now() - this.#i > d && this.#a(); } async #s() { if (this.#i = Date.now(), !this.#n) { let e = this.#e.getLanguageId(), t = globalThis.MonacoEnvironment.getWorker("ApiDOMWorker", e), r = { ...this.#e.getWorkerOptions().data, languageId: e, apiDOMContext: this.#e.getWorkerOptions().apiDOMContext, customWorkerPath: this.#e.getWorkerOptions().customWorkerPath }; t.postMessage(r), this.#t = n.editor.createWebWorker({ worker: t, keepIdleModels: !0 }), this.#n = this.#t.getProxy(); } return this.#n; } async getLanguageServiceWorker(...e) { let t = await this.#s(); return this.#t && await this.#t.withSyncedResources(e), t; } dispose() { clearInterval(this.#r), this.#a(); } }, p = class { #e; #t; #n; constructor(e, t, n) { this.#e = e, this.#t = t, this.#n = n; } get worker() { return this.#e; } get codeConverter() { return this.#t; } get protocolConverter() { return this.#n; } dispose() { this.#e = null, this.#t = null, this.#n = null; } }, m = class extends p { #e = []; #t = []; #n = null; #r = null; constructor(e, t, i, a) { super(e, t, i), this.#r = a(); let o = (e) => { if (e.getLanguageId() !== "apidom") return; let t, n = e.onDidChangeContent(() => { clearTimeout(t), t = setTimeout(() => this.#a(e), 300); }); this.#e[e.uri.toString()] = { dispose() { n.dispose(), clearTimeout(t); } }, this.#a(e); }, c = (e) => { this.#n?.set(e.uri, []); let t = e.uri.toString(); this.#e[t] && (this.#e[t].dispose(), delete this.#e[t]); }, l = (e) => { let t = e.uri.toString(), n = e.getLanguageId() === s, r = !!this.#e[t]; !r && n ? o(e) : r && !n && c(e); }, u = () => (this.#n = r.createDiagnosticCollection(s), this.#n); this.#t.push(n.editor.onDidCreateModel(o)), this.#t.push(n.editor.onDidChangeModelLanguage(l)), this.#t.push(n.editor.onWillDisposeModel(c)), this.#r.monacoInitializationDeferred().promise.then(() => { this.#t.push(u()); }); } async #i(e) { let t = await this.worker(e.uri); if (e.isDisposed()) return []; try { return await t.doValidation(e.uri.toString()); } catch { return []; } } async #a(e) { let t = await this.#i(e); this.protocolConverter && this.#n && this.#n.set(e.uri, await this.protocolConverter.asDiagnostics(t)); } dispose() { super.dispose(), this.#t.forEach((e) => e?.dispose()), this.#t = [], this.#r = null; } }, h = class extends p { async #e(e, t) { let n = await this.worker(e.uri); try { return await n.doHover(e.uri.toString(), this.codeConverter.asPosition(t)); } catch { return; } } async provideHover(e, t) { let n = await this.#e(e, t); return this.protocolConverter.asHover(n); } }, g = class extends p { async #e(e) { let t = await this.worker(e.uri); try { return await t.doLinks(e.uri.toString()); } catch { return []; } } async provideDocumentLinks(e) { let t = (await this.#e(e)).filter((e) => !this.protocolConverter.asRange(e.range).isEmpty); return this.protocolConverter.asDocumentLinks(t); } async resolveDocumentLink(e) { return e; } }, _ = class extends p { async #e(e, t) { let n = await this.worker(e.uri); try { return await n.doComplete(e.uri.toString(), this.codeConverter.asPosition(t)); } catch { return; } } async provideCompletionItems(e, t) { let n = await this.#e(e, t); return this.protocolConverter.asCompletionResult(n); } }, v = class extends p { async #e(e) { let t = await this.worker(e.uri); try { return await t.findSemanticTokens(e.uri.toString()); } catch { return; } } async provideDocumentSemanticTokens(e) { let t = await this.#e(e); return this.protocolConverter.asSemanticTokens(t); } }, y = class extends p { async #e(e, t) { let n = await this.worker(e.uri); try { return await n.doCodeActions(e.uri.toString(), t); } catch { return; } } #t(e) { return e === void 0 ? e : this.protocolConverter.asCodeActionResult(e); } async provideCodeActions(e, t, n) { let r = await this.codeConverter.asDiagnostics(n.diagnostics), i = await this.#e(e, r); return this.#t(i); } }, b = class extends p { async #e(e) { let t = await this.worker(e.uri); try { return await t.findDocumentSymbols(e.uri.toString()); } catch { return; } } async provideDocumentSymbols(e) { let t = await this.#e(e); return this.protocolConverter.asSymbolInformations(t); } }, x = class extends p { async #e(e, t) { let n = await this.worker(e.uri); try { return await n.provideDefinition(e.uri.toString(), this.codeConverter.asPosition(t)); } catch { return; } } async provideDefinition(e, t) { let n = await this.#e(e, t); return this.protocolConverter.asDefinitionResult(n); } }, S, C = (e) => { for (; e.length;) e.pop().dispose(); }, w = (e) => ({ dispose: () => C(e) }), T = () => { if (!S) throw Error("ApiDOM not registered"); return S; }, E = ({ languageId: e, providers: t, dependencies: n, opts: { useApiDOMSyntaxHighlighting: i } = {} }) => { C(t); let { worker: a, codeConverter: o, protocolConverter: s, getSystem: c } = n, l = [ a, o, s ], u = c(); return t.push(new m(...l, c)), (async () => { if (await u.monacoInitializationDeferred().promise, t.push(r.registerHoverProvider(e, new h(...l))), t.push(r.registerDocumentLinkProvider(e, new g(...l))), t.push(r.registerCompletionItemProvider(e, new _(...l))), t.push(r.registerCodeActionsProvider(e, new y(...l))), t.push(r.registerDocumentSymbolProvider(e, new b(...l))), t.push(r.registerDefinitionProvider(e, new x(...l))), i) { let n = await (await a()).getSemanticTokensLegend(); t.push(r.registerDocumentSemanticTokensProvider(e, new v(...l), n)); } })(), t; }; function D(e, { useApiDOMSyntaxHighlighting: t } = {}) { let n = [], r = [], i = a(), s = o(void 0, !0, !0), c = new f(e), l = async (...e) => c.getLanguageServiceWorker(...e); return S = l, n.push({ dispose() { S = null; } }), n.push(c), n.push(w(E({ languageId: e.getLanguageId(), providers: r, dependencies: { worker: l, codeConverter: i, protocolConverter: s, getSystem: e.getModeConfiguration().getSystem }, opts: { useApiDOMSyntaxHighlighting: t } }))), w(n); } //#endregion //#region src/plugins/editor-monaco-language-apidom/language/actions/dereference.js var O = ({ getSystem: e }) => ({ id: "swagger.editor.apidomDereference", label: "Resolve document", async run(n) { let r = e(), i = r.editorSelectors.selectIsContentFormatJSON(), a = r.editorSelectors.selectIsContentFormatYAML(); if (!i && !a) return; let o = n.getModel(), s = await (await T()(o.uri)).doDeref(o.uri.toString(), { baseURI: globalThis.document.baseURI || globalThis.location.href, format: i ? 0 : a ? 1 : "unknown" }); if (a) { let e = t.dump(t.load(s)); r.editorActions.setContent(e, r.EditorContentOrigin.Resolve); } else i && r.editorActions.setContent(s, r.EditorContentOrigin.Resolve); } }), k = {}, A = { customWorkerPath: void 0, data: {} }, j = class { #e; #t; #n; constructor(e, t) { this.setLanguageId(s), this.setWorkerOptions(e), this.setModeConfiguration(t); } getLanguageId() { return this.#e; } setLanguageId(e) { this.#e = e; } getWorkerOptions() { return this.#t; } setWorkerOptions(e) { this.#t = e || JSON.parse(JSON.stringify(A)); } getModeConfiguration() { return this.#n; } setModeConfiguration(e) { this.#n = e || JSON.parse(JSON.stringify(k)); } }, M = new j(A, k), N = () => i.getLanguages().map(({ id: e }) => e).includes(s), P = ({ createData: e, system: t, useApiDOMSyntaxHighlighting: a }) => { let o = []; o.push(i.registerLanguage({ id: s })), t.monacoInitializationDeferred().promise.then(() => { o.push(r.setLanguageConfiguration(s, c)); }), a ? o.push(n.languages.setMonarchTokensProvider(s, l)) : o.push(n.languages.setMonarchTokensProvider(s, u)), o.push(n.editor.onDidCreateEditor(() => { let { customApiDOMWorkerPath: n, apiDOMContext: r, ...i } = e, s = new j({ apiDOMContext: r, customWorkerPath: n, data: i }, { getSystem: t.getSystem }); o.push(D(s, { useApiDOMSyntaxHighlighting: a })); })), o.push(n.editor.onDidCreateEditor((e) => { o.push(n.editor.onDidCreateModel(() => { let n = O(t); e.getAction(n.id) || o.push(e.addAction(n)); })); })), o.push(n.editor.onDidCreateEditor((e) => { o.push(e.onDidDispose(() => { o.forEach((e) => e.dispose()), o.length = 0; })); })); }, F = ({ createData: e = {}, useApiDOMSyntaxHighlighting: t = !1 } = {}) => (n) => { N() || (P({ createData: e, system: n, useApiDOMSyntaxHighlighting: t }), n.editorActions.setLanguage(M.getLanguageId())); }, I = "editor_monaco_language_apidom_get_json_pointer_position_started", L = "editor_monaco_language_apidom_get_json_pointer_position_success", R = "editor_monaco_language_apidom_get_json_pointer_position_failure", z = ({ jsonPointer: e, requestId: t }) => ({ type: I, payload: e, meta: { requestId: t } }), B = ({ position: e, jsonPointer: t, requestId: n }) => ({ type: L, payload: e, meta: { jsonPointer: t, requestId: n } }), V = ({ error: e, jsonPointer: t, requestId: n }) => ({ type: R, error: !0, payload: e, meta: { jsonPointer: t, requestId: n } }), H = (e) => async (t) => { let { editorActions: n, editorSelectors: r, fn: i } = t, a = i.generateRequestId(); n.getJsonPointerPositionStarted({ jsonPointer: e, requestId: a }); try { let t = r.selectEditor().getModel(), { line: o, character: s } = await (await i.getApiDOMWorker()(t.uri)).getJsonPointerPosition(t.uri.toString(), e), c = { lineNumber: o, column: s - 1 }; return n.getJsonPointerPositionSuccess({ position: c, jsonPointer: e, requestId: a }); } catch (t) { return n.getJsonPointerPositionFailure({ error: t, jsonPointer: e, requestId: a }); } }, U = ({ useApiDOMSyntaxHighlighting: t = !1 }) => (n) => ({ ...r }) => /* @__PURE__ */ e(n, { ...r, bracketPairColorizationEnabled: t }), W = { useApiDOMSyntaxHighlighting: !1 }, G = (e = W) => { let t = typeof e.getSystem == "function", n = t ? W : e, r = () => ({ afterLoad: F(n), rootInjects: { monarchLanguageDef: u, apiDOMMonarchLanguageDef: l, apiDOMLanguageId: s }, wrapComponents: { Editor: U(n) }, fn: { getApiDOMWorker: T }, statePlugins: { editor: { actions: { getJsonPointerPosition: H, getJsonPointerPositionStarted: z, getJsonPointerPositionSuccess: B, getJsonPointerPositionFailure: V } } } }); return t ? r(e) : r; }; //#endregion export { G as default }; //# sourceMappingURL=index.js.map