-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathextension.ts
80 lines (69 loc) · 3.74 KB
/
extension.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/********************************************************************************
* Copyright (c) 2023 CrossBreeze.
********************************************************************************/
import { GLSP_PORT_COMMAND, MODELSERVER_PORT_COMMAND } from '@crossbreeze/protocol';
import * as path from 'path';
import * as vscode from 'vscode';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node.js';
let client: LanguageClient | undefined;
// This function is called when the extension is activated.
export async function activate(context: vscode.ExtensionContext): Promise<void> {
const workspacePath = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
if (!workspacePath) {
// if no workspace is open, we do not need to start our servers
return;
}
client = launchLanguageClient(context);
}
// This function is called when the extension is deactivated.
export function deactivate(): Thenable<void> | undefined {
return client?.stop();
}
function launchLanguageClient(context: vscode.ExtensionContext): LanguageClient {
const serverOptions: ServerOptions = createServerOptions(context);
const clientOptions: LanguageClientOptions = createClientOptions(context);
// Start the client. This will also launch the server
const languageClient = new LanguageClient('cross-model', 'CrossModel', serverOptions, clientOptions);
languageClient.start();
vscode.commands.registerCommand(MODELSERVER_PORT_COMMAND, () => languageClient.sendRequest(MODELSERVER_PORT_COMMAND));
vscode.commands.registerCommand(GLSP_PORT_COMMAND, () => languageClient.sendRequest(GLSP_PORT_COMMAND));
return languageClient;
}
function createServerOptions(context: vscode.ExtensionContext): ServerOptions {
// needs to match the configuration in tsconfig.json and webpack.config.js
const serverModule = context.asAbsolutePath(path.join('out', 'main.cjs'));
// The debug options for the server
// --inspect=6009: runs the server in Node's Inspector mode so VS Code can attach to the server for debugging.
// By setting `process.env.DEBUG_BREAK` to a truthy value, the language server will wait until a debugger is attached.
const debugOptions = {
execArgv: ['--nolazy', `--inspect${process.env.DEBUG_BREAK ? '-brk' : ''}=${process.env.DEBUG_SOCKET || '6009'}`]
};
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
return {
run: { module: serverModule, transport: TransportKind.ipc },
debug: { module: serverModule, transport: TransportKind.ipc, options: debugOptions }
};
}
function createClientOptions(context: vscode.ExtensionContext): LanguageClientOptions {
const crossModelWatcher = vscode.workspace.createFileSystemWatcher('**/*.cm');
context.subscriptions.push(crossModelWatcher);
// watch changes to package.json as it contains the dependencies between our systems
const packageWatcher = vscode.workspace.createFileSystemWatcher('**/package.json');
context.subscriptions.push(packageWatcher);
// we listen to directories separately as when we import a library, e.g., a directory within node_modules,
// we only get that notification but not for nested files
const directoryWatcher = vscode.workspace.createFileSystemWatcher('**/*/');
context.subscriptions.push(directoryWatcher);
// Options to control the language client
return {
documentSelector: [
{ scheme: 'file', language: 'cross-model' },
{ scheme: 'file', pattern: '**/package.json' }
],
synchronize: {
// Notify the server about file changes to files contained in the workspace
fileEvents: [crossModelWatcher, packageWatcher, directoryWatcher]
}
};
}