Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/json-language-features/client/src/browser/jsonClientMain.ts
3322 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Disposable, ExtensionContext, Uri, l10n, window } from 'vscode';
7
import { LanguageClientOptions } from 'vscode-languageclient';
8
import { startClient, LanguageClientConstructor, SchemaRequestService, AsyncDisposable, languageServerDescription } from '../jsonClient';
9
import { LanguageClient } from 'vscode-languageclient/browser';
10
11
let client: AsyncDisposable | undefined;
12
13
// this method is called when vs code is activated
14
export async function activate(context: ExtensionContext) {
15
const serverMain = Uri.joinPath(context.extensionUri, 'server/dist/browser/jsonServerMain.js');
16
try {
17
const worker = new Worker(serverMain.toString());
18
worker.postMessage({ i10lLocation: l10n.uri?.toString(false) ?? '' });
19
20
const newLanguageClient: LanguageClientConstructor = (id: string, name: string, clientOptions: LanguageClientOptions) => {
21
return new LanguageClient(id, name, worker, clientOptions);
22
};
23
24
const schemaRequests: SchemaRequestService = {
25
getContent(uri: string) {
26
return fetch(uri, { mode: 'cors' })
27
.then(function (response: any) {
28
return response.text();
29
});
30
}
31
};
32
33
const timer = {
34
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable {
35
const handle = setTimeout(callback, ms, ...args);
36
return { dispose: () => clearTimeout(handle) };
37
}
38
};
39
40
const logOutputChannel = window.createOutputChannel(languageServerDescription, { log: true });
41
context.subscriptions.push(logOutputChannel);
42
43
client = await startClient(context, newLanguageClient, { schemaRequests, timer, logOutputChannel });
44
45
} catch (e) {
46
console.log(e);
47
}
48
}
49
50
export async function deactivate(): Promise<void> {
51
if (client) {
52
await client.dispose();
53
client = undefined;
54
}
55
}
56
57