|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import * as vscode from 'vscode'; |
| 5 | +import { LanguageClientOptions } from 'vscode-languageclient'; |
| 6 | +import { LanguageClient } from 'vscode-languageclient/browser'; |
| 7 | +import { ILSExtensionApi } from '../activation/node/languageServerFolderService'; |
| 8 | +import { PYLANCE_EXTENSION_ID } from '../common/constants'; |
| 9 | + |
| 10 | +interface BrowserConfig { |
| 11 | + distUrl: string; // URL to Pylance's dist folder. |
| 12 | +} |
| 13 | + |
| 14 | +export async function activate(context: vscode.ExtensionContext): Promise<void> { |
| 15 | + // Run in a promise and return early so that VS Code can go activate Pylance. |
| 16 | + runPylance(context); |
| 17 | +} |
| 18 | + |
| 19 | +async function runPylance(context: vscode.ExtensionContext): Promise<void> { |
| 20 | + const pylanceExtension = vscode.extensions.getExtension<ILSExtensionApi>(PYLANCE_EXTENSION_ID); |
| 21 | + const pylanceApi = await pylanceExtension?.activate(); |
| 22 | + if (!pylanceApi?.languageServerFolder) { |
| 23 | + throw new Error('Could not find Pylance extension'); |
| 24 | + } |
| 25 | + |
| 26 | + const { path: distUrl } = await pylanceApi.languageServerFolder(); |
| 27 | + |
| 28 | + try { |
| 29 | + const worker = new Worker(`${distUrl}/browser.server.bundle.js`); |
| 30 | + |
| 31 | + // Pass the configuration as the first message to the worker so it can |
| 32 | + // have info like the URL of the dist folder early enough. |
| 33 | + // |
| 34 | + // This is the same method used by the TS worker: |
| 35 | + // https://github.com/microsoft/vscode/blob/90aa979bb75a795fd8c33d38aee263ea655270d0/extensions/typescript-language-features/src/tsServer/serverProcess.browser.ts#L55 |
| 36 | + const config: BrowserConfig = { |
| 37 | + distUrl, |
| 38 | + }; |
| 39 | + worker.postMessage(config); |
| 40 | + |
| 41 | + const clientOptions: LanguageClientOptions = { |
| 42 | + // Register the server for python source files. |
| 43 | + documentSelector: [ |
| 44 | + { |
| 45 | + language: 'python', |
| 46 | + }, |
| 47 | + ], |
| 48 | + synchronize: { |
| 49 | + // Synchronize the setting section to the server. |
| 50 | + configurationSection: ['python'], |
| 51 | + }, |
| 52 | + }; |
| 53 | + |
| 54 | + const languageClient = new LanguageClient('python', 'Python Language Server', clientOptions, worker); |
| 55 | + const disposable = languageClient.start(); |
| 56 | + |
| 57 | + context.subscriptions.push(disposable); |
| 58 | + } catch (e) { |
| 59 | + console.log(e); |
| 60 | + } |
| 61 | +} |
0 commit comments