Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/codemirror. A minor test of Codemirror okikio-codemirror.netlify.app
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899// Simple JSON LS in Web Worker that provides completion and hover.// Includes a schema for `tsconfig.json`.import { createProtocolConnection, BrowserMessageReader, BrowserMessageWriter, } from "vscode-languageserver-protocol/browser"; import { CompletionRequest, DidChangeTextDocumentNotification, DidOpenTextDocumentNotification, HoverRequest, InitializeRequest, InitializeResult, TextDocumentSyncKind, PublishDiagnosticsNotification } from "vscode-languageserver-protocol/browser"; import { getLanguageService, TextDocument } from "vscode-json-languageservice"; const jsonService = getLanguageService({ async schemaRequestService(url: string) { const res = await fetch(url); return res.text(); }, }); jsonService.configure({ schemas: [ { // "name": "tsconfig.json", // description: "TypeScript compiler configuration file", uri: "https://json.schemastore.org/tsconfig", fileMatch: ["tsconfig.json"], }, ], }); const docs: { [uri: string]: TextDocument } = {}; const worker: Worker = self as any; const conn = createProtocolConnection( new BrowserMessageReader(worker), new BrowserMessageWriter(worker) ); conn.onRequest( InitializeRequest.type, (params): InitializeResult => { return { capabilities: { textDocumentSync: TextDocumentSyncKind.Incremental, completionProvider: { triggerCharacters: ['"', ":"], }, hoverProvider: true, }, }; } ); conn.onNotification( DidOpenTextDocumentNotification.type, ({ textDocument: { uri, languageId, version, text } }) => { docs[uri] = TextDocument.create(uri, languageId, version, text); } ); conn.onNotification( DidChangeTextDocumentNotification.type, ({ textDocument, contentChanges }) => { const doc = docs[textDocument.uri]; if (doc) { docs[textDocument.uri] = TextDocument.update( doc, contentChanges, textDocument.version || 0 ); } } ); conn.onRequest(PublishDiagnosticsNotification.type, async ({ textDocument, position }) => { const doc = docs[textDocument.uri]; if (!doc) return null; return jsonService.doValidation(doc, jsonService.parseJSONDocument(doc)); }); conn.onRequest(CompletionRequest.type, async ({ textDocument, position }) => { const doc = docs[textDocument.uri]; if (!doc) return null; return jsonService.doComplete( doc, position, jsonService.parseJSONDocument(doc) ); }); conn.onRequest(HoverRequest.type, async ({ textDocument, position }) => { const doc = docs[textDocument.uri]; if (!doc) return null; return jsonService.doHover(doc, position, jsonService.parseJSONDocument(doc)); }); conn.listen();