|
| 1 | +import { createDefaultMapFromCDN, createSystem, createVirtualTypeScriptEnvironment } from '@typescript/vfs'; |
| 2 | + |
| 3 | +import type { CompletionContext } from '@codemirror/autocomplete'; |
| 4 | +import type { VirtualTypeScriptEnvironment } from '@typescript/vfs'; |
| 5 | + |
| 6 | +let env: VirtualTypeScriptEnvironment | null = null; |
| 7 | +let currentVersion: string | null = null; |
| 8 | + |
| 9 | +const getEnv = async (version: string) => { |
| 10 | + if (env && currentVersion === version) { |
| 11 | + return env; |
| 12 | + } |
| 13 | + |
| 14 | + const ts = await import('typescript'); |
| 15 | + |
| 16 | + const compilerOptions = { |
| 17 | + lib: ['es2024'], |
| 18 | + module: ts.ModuleKind.ESNext, |
| 19 | + target: ts.ScriptTarget.ESNext, |
| 20 | + }; |
| 21 | + |
| 22 | + const fsMap = await createDefaultMapFromCDN(compilerOptions, ts.version, true, ts); |
| 23 | + |
| 24 | + const { default: joiDts } = version.startsWith('17.') |
| 25 | + ? await import('joi-17/lib/index.d.ts?raw') |
| 26 | + : await import('joi-18/lib/index.d.ts?raw'); |
| 27 | + |
| 28 | + fsMap.set('/node_modules/joi/index.d.ts', joiDts); |
| 29 | + fsMap.set('/node_modules/joi/package.json', JSON.stringify({ name: 'joi', types: 'index.d.ts' })); |
| 30 | + |
| 31 | + if (version.startsWith('18.')) { |
| 32 | + const { default: standardSchemaTypes } = await import('../node_modules/@standard-schema/spec/dist/index.d.ts?raw'); |
| 33 | + fsMap.set('/node_modules/@standard-schema/spec/index.d.ts', standardSchemaTypes); |
| 34 | + fsMap.set( |
| 35 | + '/node_modules/@standard-schema/spec/package.json', |
| 36 | + JSON.stringify({ name: '@standard-schema/spec', types: 'index.d.ts' }), |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + const system = createSystem(fsMap); |
| 41 | + env = createVirtualTypeScriptEnvironment(system, [], ts, compilerOptions); |
| 42 | + currentVersion = version; |
| 43 | + |
| 44 | + return env; |
| 45 | +}; |
| 46 | + |
| 47 | +const filterJoiOperations = (name: string) => |
| 48 | + name.startsWith('$') || |
| 49 | + name.startsWith('_') || |
| 50 | + name.startsWith('validate') || |
| 51 | + name === 'cache' || |
| 52 | + name === 'ValidationError'; |
| 53 | + |
| 54 | +export const joiCompletionSource = async (context: CompletionContext, version: string) => { |
| 55 | + try { |
| 56 | + const tsEnv = await getEnv(version); |
| 57 | + const code = context.state.doc.toString(); |
| 58 | + |
| 59 | + const prefix = "import Joi from 'joi';\n"; |
| 60 | + const wrappedCode = prefix + code; |
| 61 | + const pos = context.pos + prefix.length; |
| 62 | + |
| 63 | + const fileName = 'index.ts'; |
| 64 | + if (tsEnv.getSourceFile(fileName)) { |
| 65 | + tsEnv.updateFile(fileName, wrappedCode); |
| 66 | + } else { |
| 67 | + tsEnv.createFile(fileName, wrappedCode); |
| 68 | + } |
| 69 | + |
| 70 | + const completions = tsEnv.languageService.getCompletionsAtPosition(fileName, pos, {}); |
| 71 | + if (!completions) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + const word = context.matchBefore(/\w*/); |
| 76 | + if (!word && !context.explicit) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + return { |
| 81 | + from: word ? word.from : context.pos, |
| 82 | + options: completions.entries |
| 83 | + .filter((entry) => !filterJoiOperations(entry.name)) |
| 84 | + .map((entry) => { |
| 85 | + let type = 'variable'; |
| 86 | + if (entry.kind === 'method') { |
| 87 | + type = 'method'; |
| 88 | + } else if (entry.kind === 'property') { |
| 89 | + type = 'property'; |
| 90 | + } |
| 91 | + |
| 92 | + return { |
| 93 | + boost: entry.sortText ? -Number(entry.sortText) : 0, |
| 94 | + info: () => { |
| 95 | + const details = tsEnv.languageService.getCompletionEntryDetails( |
| 96 | + fileName, |
| 97 | + pos, |
| 98 | + entry.name, |
| 99 | + {}, |
| 100 | + entry.source, |
| 101 | + {}, |
| 102 | + entry.data, |
| 103 | + ); |
| 104 | + if (!details) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + const doc = `${entry.kind} ${entry.name} |
| 109 | +${details.documentation?.map((d) => d.text).join('\n') || ''}`; |
| 110 | + |
| 111 | + const div = document.createElement('div'); |
| 112 | + div.className = 'cm-completionInfo-text'; |
| 113 | + div.style.whiteSpace = 'pre-wrap'; |
| 114 | + div.textContent = doc; |
| 115 | + return div; |
| 116 | + }, |
| 117 | + label: entry.name, |
| 118 | + type, |
| 119 | + }; |
| 120 | + }), |
| 121 | + }; |
| 122 | + } catch (error) { |
| 123 | + console.error('Joi completion error:', error); |
| 124 | + return null; |
| 125 | + } |
| 126 | +}; |
0 commit comments