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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316// Simple JSON LS in Web Worker that provides completion and hover.// Includes a schema for `tsconfig.json`.import { createConnection, BrowserMessageReader, BrowserMessageWriter, TextDocumentSyncKind,} from "vscode-languageserver/browser";import { TextDocument } from 'vscode-languageserver-textdocument';import { createLanguageService, LanguageServiceHost} from "vscode-typescript-languageservice";
import type { VersionedTextDocumentIdentifier } from "vscode-languageserver/browser";
type TS = typeof import('typescript/lib/tsserverlibrary');import type { VirtualTypeScriptEnvironment } from "@typescript/vfs";import type { System, CompilerOptions, CustomTransformers } from "typescript";
importScripts("https://unpkg.com/@typescript/vfs@1.3.5/dist/vfs.globals.js");// importScripts("https://cdnjs.cloudflare.com/ajax/libs/typescript/4.4.4/typescript.min.js");importScripts("https://unpkg.com/typescript@4.4.4/lib/tsserverlibrary.js")
export type VFS = typeof import("@typescript/vfs");export type Diagnostic = import("@codemirror/lint").Diagnostic;
var { createDefaultMapFromCDN, createSystem, createVirtualCompilerHost, createVirtualLanguageServiceHost } = globalThis.tsvfs as VFS;var ts = globalThis.ts as TS;
globalThis.localStorage = globalThis.localStorage ?? {} as Storage;/** * Makes a virtual copy of the TypeScript environment. This is the main API you want to be using with * @typescript/vfs. A lot of the other exposed functions are used by this function to get set up. * * @param sys an object which conforms to the TS Sys (a shim over read/write access to the fs) * @param rootFiles a list of files which are considered inside the project * @param ts a copy pf the TypeScript module * @param compilerOptions the options for this compiler run * @param customTransformers custom transformers for this compiler run */
function createVirtualTypeScriptEnvironment( sys: System, rootFiles: string[], ts: TS, compilerOptions: CompilerOptions = {}, customTransformers?: CustomTransformers): VirtualTypeScriptEnvironment & { languageServiceHost: LanguageServiceHost } { const mergedCompilerOpts = { ...defaultCompilerOptions(ts), ...compilerOptions }
const { languageServiceHost, updateFile } = createVirtualLanguageServiceHost( sys, rootFiles, mergedCompilerOpts, ts, customTransformers ) const languageService = ts.createLanguageService(languageServiceHost) const diagnostics = languageService.getCompilerOptionsDiagnostics()
if (diagnostics.length) { const compilerHost = createVirtualCompilerHost(sys, compilerOptions, ts) throw new Error(ts.formatDiagnostics(diagnostics, compilerHost.compilerHost)) }
return { // @ts-ignore name: "vfs", sys, languageService, languageServiceHost, getSourceFile: fileName => languageService.getProgram()?.getSourceFile(fileName),
createFile: (fileName, content) => { updateFile(ts.createSourceFile(fileName, content, mergedCompilerOpts.target!, false)) }, updateFile: (fileName, content, optPrevTextSpan) => { const prevSourceFile = languageService.getProgram()!.getSourceFile(fileName) if (!prevSourceFile) { throw new Error("Did not find a source file for " + fileName) } const prevFullContents = prevSourceFile.text
// TODO: Validate if the default text span has a fencepost error? const prevTextSpan = optPrevTextSpan ?? ts.createTextSpan(0, prevFullContents.length) const newText = prevFullContents.slice(0, prevTextSpan.start) + content + prevFullContents.slice(prevTextSpan.start + prevTextSpan.length) const newSourceFile = ts.updateSourceFile(prevSourceFile, newText, { span: prevTextSpan, newLength: content.length, })
updateFile(newSourceFile) }, }}
/** The default compiler options if TypeScript could ever change the compiler options */const defaultCompilerOptions = (ts: TS): CompilerOptions => { return { ...ts.getDefaultCompilerOptions(), jsx: ts.JsxEmit.React, strict: true, esModuleInterop: true, module: ts.ModuleKind.ESNext, suppressOutputPathCheck: true, skipLibCheck: true, skipDefaultLibCheck: true, moduleResolution: ts.ModuleResolutionKind.NodeJs, }}
(async () => { const compilerOpts = { target: ts.ScriptTarget.ES2021, module: ts.ModuleKind.ES2020, "lib": [ "es2021", "es2020", "dom", "webworker" ], "esModuleInterop": true, };
let initialText = "const hello = 'hi'";
const fsMap = await createDefaultMapFromCDN(compilerOpts, ts.version, false, ts) const ENTRY_POINT = "index.ts";
// const docs = new Map<string, TextDocument>(); // docs.set(ENTRY_POINT, TextDocument.create(ENTRY_POINT, "ts", Number(ts.version), initialText)); fsMap.set(ENTRY_POINT, initialText);
const system = createSystem(fsMap); const env = createVirtualTypeScriptEnvironment(system, [ENTRY_POINT], ts, compilerOpts); const tsls = createLanguageService(ts, env.languageServiceHost, env.languageService);
const worker: Worker = self as any; const conn = createConnection( new BrowserMessageReader(worker), new BrowserMessageWriter(worker) );
const getDiagnostics = (textDocument: VersionedTextDocumentIdentifier) => { return conn.sendDiagnostics({ uri: textDocument.uri, version: textDocument.version || 0, diagnostics: tsls.doValidation(textDocument.uri, { declaration: true, semantic: true, suggestion: true, syntactic: true, }) }); };
conn.onInitialize(() => { return { capabilities: { textDocumentSync: TextDocumentSyncKind.Incremental, completionProvider: { triggerCharacters: ['"', ":", "'", " ", ".", ","], }, hoverProvider: true,
definitionProvider: true, typeDefinitionProvider: true,
referencesProvider: true,
foldingRangeProvider: true, codeActionProvider: true,
documentFormattingProvider: true, documentRangeFormattingProvider: true,
documentHighlightProvider: true,
documentSymbolProvider: true, workspaceSymbolProvider: true,
renameProvider: true,
signatureHelpProvider: { triggerCharacters: ["(", ","], }, }, }; });
conn.onDidOpenTextDocument(({ textDocument: { uri, languageId, version, text } }) => { let textDocument = TextDocument.create(uri, languageId, version, text); // docs.set(uri, textDocument); // fsMap.set(uri, text); env.createFile(uri, text); getDiagnostics(textDocument); });
conn.onDidChangeTextDocument(({ textDocument, contentChanges }) => { if (fsMap.has(textDocument.uri)) { // const doc = docs.get(textDocument.uri); // docs.set(textDocument.uri, TextDocument.update( // doc, // contentChanges, // textDocument.version || 0 // )); // fsMap.set(textDocument.uri, contentChanges[0].text);
env.updateFile(textDocument.uri, contentChanges[0].text); getDiagnostics(textDocument); } });
conn.onSignatureHelp(({ textDocument, position, context }) => { // env.getSourceFile(textDocument.uri) if (!fsMap.has(textDocument.uri)) return null; return tsls.getSignatureHelp(textDocument.uri, position, context); });
conn.onCompletion(({ textDocument, position, context }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.doComplete(textDocument.uri, position, context as unknown); });
conn.onCompletionResolve((item) => { if (!item) return null; return tsls.doCompletionResolve(item); });
conn.onHover(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.doHover(textDocument.uri, position); });
conn.onDocumentFormatting(({ textDocument, options }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.doFormatting(textDocument.uri, options); });
conn.onDocumentRangeFormatting(({ textDocument, range, options }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.doFormatting(textDocument.uri, options, range); });
conn.onTypeDefinition(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findTypeDefinition(textDocument.uri, position); });
conn.onDefinition(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findDefinition(textDocument.uri, position); });
conn.onReferences(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findReferences(textDocument.uri, position); });
conn.onCodeAction(({ textDocument, range, context }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.getCodeActions(textDocument.uri, range, context); });
conn.onCodeActionResolve((action) => { if (!action) return null; return tsls.doCodeActionResolve(action); });
conn.onDocumentSymbol(({ textDocument }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findDocumentSymbols(textDocument.uri); });
conn.onDefinition(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findDefinition(textDocument.uri, position); });
conn.onFoldingRanges(({ textDocument }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.getFoldingRanges(textDocument.uri); });
conn.onPrepareRename(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.prepareRename(textDocument.uri, position); });
conn.onRenameRequest(({ textDocument, position, newName }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.doRename(textDocument.uri, position, newName); });
conn.onDocumentSymbol(({ textDocument }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findDocumentSymbols(textDocument.uri); });
conn.onWorkspaceSymbol(({ query }) => { if (!query) return null; return tsls.findWorkspaceSymbols(query); });
conn.onDocumentHighlight(({ textDocument, position }) => { if (!fsMap.has(textDocument.uri)) return null; return tsls.findDocumentHighlights(textDocument.uri, position); });
conn.listen();})();