Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/html-language-features/client/src/node/htmlClientMain.ts
3323 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 { getNodeFileFS } from './nodeFs';
7
import { Disposable, ExtensionContext, l10n } from 'vscode';
8
import { startClient, LanguageClientConstructor, AsyncDisposable } from '../htmlClient';
9
import { ServerOptions, TransportKind, LanguageClientOptions, LanguageClient } from 'vscode-languageclient/node';
10
import * as fs from 'fs';
11
import TelemetryReporter from '@vscode/extension-telemetry';
12
13
14
let telemetry: TelemetryReporter | undefined;
15
let client: AsyncDisposable | undefined;
16
17
// this method is called when vs code is activated
18
export async function activate(context: ExtensionContext) {
19
20
const clientPackageJSON = getPackageInfo(context);
21
telemetry = new TelemetryReporter(clientPackageJSON.aiKey);
22
23
const serverMain = `./server/${clientPackageJSON.main.indexOf('/dist/') !== -1 ? 'dist' : 'out'}/node/htmlServerMain`;
24
const serverModule = context.asAbsolutePath(serverMain);
25
26
// The debug options for the server
27
const debugOptions = { execArgv: ['--nolazy', '--inspect=' + (8000 + Math.round(Math.random() * 999))] };
28
29
// If the extension is launch in debug mode the debug server options are use
30
// Otherwise the run options are used
31
const serverOptions: ServerOptions = {
32
run: { module: serverModule, transport: TransportKind.ipc },
33
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
34
};
35
36
const newLanguageClient: LanguageClientConstructor = (id: string, name: string, clientOptions: LanguageClientOptions) => {
37
return new LanguageClient(id, name, serverOptions, clientOptions);
38
};
39
40
const timer = {
41
setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): Disposable {
42
const handle = setTimeout(callback, ms, ...args);
43
return { dispose: () => clearTimeout(handle) };
44
}
45
};
46
47
48
// pass the location of the localization bundle to the server
49
process.env['VSCODE_L10N_BUNDLE_LOCATION'] = l10n.uri?.toString() ?? '';
50
51
client = await startClient(context, newLanguageClient, { fileFs: getNodeFileFS(), TextDecoder, telemetry, timer });
52
}
53
54
export async function deactivate(): Promise<void> {
55
if (client) {
56
await client.dispose();
57
client = undefined;
58
}
59
}
60
61
interface IPackageInfo {
62
name: string;
63
version: string;
64
aiKey: string;
65
main: string;
66
}
67
68
function getPackageInfo(context: ExtensionContext): IPackageInfo {
69
const location = context.asAbsolutePath('./package.json');
70
try {
71
return JSON.parse(fs.readFileSync(location).toString());
72
} catch (e) {
73
console.log(`Problems reading ${location}: ${e}`);
74
return { name: '', version: '', aiKey: '', main: '' };
75
}
76
}
77
78