Path: blob/main/extensions/json-language-features/client/src/browser/jsonClientMain.ts
3322 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { Disposable, ExtensionContext, Uri, l10n, window } from 'vscode';6import { LanguageClientOptions } from 'vscode-languageclient';7import { startClient, LanguageClientConstructor, SchemaRequestService, AsyncDisposable, languageServerDescription } from '../jsonClient';8import { LanguageClient } from 'vscode-languageclient/browser';910let client: AsyncDisposable | undefined;1112// this method is called when vs code is activated13export async function activate(context: ExtensionContext) {14const serverMain = Uri.joinPath(context.extensionUri, 'server/dist/browser/jsonServerMain.js');15try {16const worker = new Worker(serverMain.toString());17worker.postMessage({ i10lLocation: l10n.uri?.toString(false) ?? '' });1819const newLanguageClient: LanguageClientConstructor = (id: string, name: string, clientOptions: LanguageClientOptions) => {20return new LanguageClient(id, name, worker, clientOptions);21};2223const schemaRequests: SchemaRequestService = {24getContent(uri: string) {25return fetch(uri, { mode: 'cors' })26.then(function (response: any) {27return response.text();28});29}30};3132const timer = {33setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable {34const handle = setTimeout(callback, ms, ...args);35return { dispose: () => clearTimeout(handle) };36}37};3839const logOutputChannel = window.createOutputChannel(languageServerDescription, { log: true });40context.subscriptions.push(logOutputChannel);4142client = await startClient(context, newLanguageClient, { schemaRequests, timer, logOutputChannel });4344} catch (e) {45console.log(e);46}47}4849export async function deactivate(): Promise<void> {50if (client) {51await client.dispose();52client = undefined;53}54}555657