|
| 1 | +import { activateAutoInsertion, activateDocumentDropEdit, createLabsInfo, middleware } from '@volar/vscode'; |
| 2 | +import * as lsp from '@volar/vscode/node'; |
| 3 | +import * as fs from 'node:fs'; |
| 4 | +import * as path from 'node:path'; |
| 5 | +import { defineExtension, executeCommand, extensionContext, nextTick, onDeactivate, useActiveTextEditor, useCommand, useOutputChannel, useVisibleTextEditors, watch } from 'reactive-vscode'; |
| 6 | +import * as vscode from 'vscode'; |
| 7 | +import { config } from './lib/config'; |
| 8 | +import { activate as activateSplitEditors } from './lib/splitEditors'; |
| 9 | + |
| 10 | +let client: lsp.BaseLanguageClient | undefined; |
| 11 | + |
| 12 | +class _LanguageClient extends lsp.LanguageClient { |
| 13 | + fillInitializeParams(params: lsp.InitializeParams) { |
| 14 | + // fix https://github.com/vuejs/language-tools/issues/1959 |
| 15 | + params.locale = vscode.env.language; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +export const { activate, deactivate } = defineExtension(async () => { |
| 20 | + await vscode.extensions.getExtension('vscode.typescript-language-features')?.activate(); |
| 21 | + |
| 22 | + const context = extensionContext.value!; |
| 23 | + const volarLabs = createLabsInfo(); |
| 24 | + const activeTextEditor = useActiveTextEditor(); |
| 25 | + const visibleTextEditors = useVisibleTextEditors(); |
| 26 | + const { stop } = watch(activeTextEditor, () => { |
| 27 | + |
| 28 | + if (!visibleTextEditors.value.some( |
| 29 | + editor => config.server.includeLanguages.includes(editor.document.languageId) |
| 30 | + )) { |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + nextTick(() => stop()); |
| 35 | + |
| 36 | + watch(() => config.server.includeLanguages, async () => { |
| 37 | + const reload = await vscode.window.showInformationMessage( |
| 38 | + 'Please restart extension host to apply the new language settings.', |
| 39 | + 'Restart Extension Host' |
| 40 | + ); |
| 41 | + if (reload) { |
| 42 | + executeCommand('workbench.action.restartExtensionHost'); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + // Setup typescript.js in production mode |
| 47 | + if (fs.existsSync(path.join(__dirname, 'language-server.js'))) { |
| 48 | + fs.writeFileSync(path.join(__dirname, 'typescript.js'), `module.exports = require("${vscode.env.appRoot.replace(/\\/g, '/')}/extensions/node_modules/typescript/lib/typescript.js");`); |
| 49 | + } |
| 50 | + |
| 51 | + volarLabs.addLanguageClient(client = launch(context)); |
| 52 | + |
| 53 | + const selectors = config.server.includeLanguages; |
| 54 | + |
| 55 | + activateAutoInsertion(selectors, client); |
| 56 | + activateDocumentDropEdit(selectors, client); |
| 57 | + activateSplitEditors(client); |
| 58 | + |
| 59 | + }, { immediate: true }); |
| 60 | + |
| 61 | + useCommand('vue.action.restartServer', async () => { |
| 62 | + await executeCommand('typescript.restartTsServer'); |
| 63 | + await client?.stop(); |
| 64 | + client?.outputChannel.clear(); |
| 65 | + await client?.start(); |
| 66 | + }); |
| 67 | + |
| 68 | + onDeactivate(async () => { |
| 69 | + await client?.stop(); |
| 70 | + }); |
| 71 | + |
| 72 | + return volarLabs.extensionExports; |
| 73 | +}); |
| 74 | + |
| 75 | +function launch(context: vscode.ExtensionContext) { |
| 76 | + const serverModule = vscode.Uri.joinPath(context.extensionUri, 'dist', 'language-server.js'); |
| 77 | + const client = new _LanguageClient( |
| 78 | + 'vue', |
| 79 | + 'Vue', |
| 80 | + { |
| 81 | + run: { |
| 82 | + module: serverModule.fsPath, |
| 83 | + transport: lsp.TransportKind.ipc, |
| 84 | + options: {}, |
| 85 | + }, |
| 86 | + debug: { |
| 87 | + module: serverModule.fsPath, |
| 88 | + transport: lsp.TransportKind.ipc, |
| 89 | + options: { execArgv: ['--nolazy', '--inspect=' + 6009] }, |
| 90 | + } |
| 91 | + }, |
| 92 | + { |
| 93 | + middleware: { |
| 94 | + ...middleware, |
| 95 | + async resolveCodeAction(item, token, next) { |
| 96 | + if (item.kind?.value === 'refactor.move.newFile.dumb' && config.codeActions.askNewComponentName) { |
| 97 | + const inputName = await vscode.window.showInputBox({ value: (item as any).data.original.data.newName }); |
| 98 | + if (!inputName) { |
| 99 | + return item; // cancel |
| 100 | + } |
| 101 | + (item as any).data.original.data.newName = inputName; |
| 102 | + } |
| 103 | + return await (middleware.resolveCodeAction?.(item, token, next) ?? next(item, token)); |
| 104 | + }, |
| 105 | + }, |
| 106 | + documentSelector: config.server.includeLanguages, |
| 107 | + markdown: { |
| 108 | + isTrusted: true, |
| 109 | + supportHtml: true |
| 110 | + }, |
| 111 | + outputChannel: useOutputChannel('Vue Language Server'), |
| 112 | + } |
| 113 | + ); |
| 114 | + |
| 115 | + client.onNotification('tsserver/request', async ([id, command, args]) => { |
| 116 | + const tsserver = (globalThis as any).__TSSERVER__?.semantic; |
| 117 | + if (!tsserver) { |
| 118 | + return; |
| 119 | + } |
| 120 | + try { |
| 121 | + const res = await tsserver.executeImpl(command, args, { |
| 122 | + isAsync: true, |
| 123 | + expectsResult: true, |
| 124 | + lowPriority: true, |
| 125 | + requireSemantic: true, |
| 126 | + })[0]; |
| 127 | + client.sendNotification('tsserver/response', [id, res.body]); |
| 128 | + } catch { |
| 129 | + // noop |
| 130 | + } |
| 131 | + }); |
| 132 | + client.start(); |
| 133 | + |
| 134 | + return client; |
| 135 | +} |
| 136 | + |
| 137 | +try { |
| 138 | + const fs = require('node:fs'); |
| 139 | + const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features')!; |
| 140 | + const readFileSync = fs.readFileSync; |
| 141 | + const extensionJsPath = require.resolve('./dist/extension.js', { |
| 142 | + paths: [tsExtension.extensionPath], |
| 143 | + }); |
| 144 | + |
| 145 | + // @ts-expect-error |
| 146 | + fs.readFileSync = (...args) => { |
| 147 | + if (args[0] === extensionJsPath) { |
| 148 | + let text = readFileSync(...args) as string; |
| 149 | + |
| 150 | + // patch readPlugins |
| 151 | + text = text.replace( |
| 152 | + 'languages:Array.isArray(e.languages)', |
| 153 | + [ |
| 154 | + 'languages:', |
| 155 | + `e.name==='vue-typescript-plugin-pack'?[${config.server.includeLanguages |
| 156 | + .map(lang => `'${lang}'`) |
| 157 | + .join(',')}]`, |
| 158 | + ':Array.isArray(e.languages)' |
| 159 | + ].join('') |
| 160 | + ); |
| 161 | + |
| 162 | + // Expose tsserver process in SingleTsServer constructor |
| 163 | + text = text.replace( |
| 164 | + ',this._callbacks.destroy("server errored")}))', |
| 165 | + s => s + ',globalThis.__TSSERVER__||={},globalThis.__TSSERVER__[arguments[1]]=this' |
| 166 | + ); |
| 167 | + |
| 168 | + /** |
| 169 | + * VSCode >= 1.87.0 |
| 170 | + */ |
| 171 | + |
| 172 | + // patch jsTsLanguageModes |
| 173 | + text = text.replace( |
| 174 | + 't.jsTsLanguageModes=[t.javascript,t.javascriptreact,t.typescript,t.typescriptreact]', |
| 175 | + s => s + '.concat("vue")' |
| 176 | + ); |
| 177 | + // patch isSupportedLanguageMode |
| 178 | + text = text.replace( |
| 179 | + '.languages.match([t.typescript,t.typescriptreact,t.javascript,t.javascriptreact]', |
| 180 | + s => s + '.concat("vue")' |
| 181 | + ); |
| 182 | + |
| 183 | + return text; |
| 184 | + } |
| 185 | + return readFileSync(...args); |
| 186 | + }; |
| 187 | + |
| 188 | + const loadedModule = require.cache[extensionJsPath]; |
| 189 | + if (loadedModule) { |
| 190 | + delete require.cache[extensionJsPath]; |
| 191 | + const patchedModule = require(extensionJsPath); |
| 192 | + Object.assign(loadedModule.exports, patchedModule); |
| 193 | + } |
| 194 | + |
| 195 | + if (tsExtension.isActive) { |
| 196 | + vscode.commands.executeCommand('workbench.action.restartExtensionHost'); |
| 197 | + } |
| 198 | +} catch { } |
0 commit comments