-
-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathextension.ts
More file actions
107 lines (92 loc) · 4.41 KB
/
extension.ts
File metadata and controls
107 lines (92 loc) · 4.41 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as vscode from 'vscode';
import * as path from 'path';
import { LanguageClient, LanguageClientOptions, ServerOptions, TransportKind } from 'vscode-languageclient/node';
import { AUTH_PROVIDER_ID, ZenStackAuthenticationProvider } from './vscode/zenstack-auth-provider';
import { DocumentationCache } from './vscode/documentation-cache';
import { ZModelPreview } from './vscode/zmodel-preview';
import { ReleaseNotesManager } from './vscode/release-notes-manager';
import telemetry from './vscode/vscode-telemetry';
// Global variables
let client: LanguageClient;
// Utility to require authentication when needed
export async function requireAuth(): Promise<vscode.AuthenticationSession | undefined> {
let session: vscode.AuthenticationSession | undefined;
session = await vscode.authentication.getSession(AUTH_PROVIDER_ID, [], { createIfNone: false });
if (!session) {
const signIn = 'Sign in';
const selection = await vscode.window.showWarningMessage('Please sign in to use this feature', signIn);
telemetry.track('extension:signin:show');
if (selection === signIn) {
telemetry.track('extension:signin:start');
try {
session = await vscode.authentication.getSession(AUTH_PROVIDER_ID, [], { createIfNone: true });
if (session) {
telemetry.track('extension:signin:complete');
vscode.window.showInformationMessage('ZenStack sign-in successful!');
}
} catch (e: unknown) {
telemetry.track('extension:signin:error', { error: e instanceof Error ? e.message : String(e) });
vscode.window.showErrorMessage(
'ZenStack sign-in failed: ' + (e instanceof Error ? e.message : String(e))
);
}
}
}
return session;
}
// This function is called when the extension is activated.
export function activate(context: vscode.ExtensionContext): void {
telemetry.track('extension:activate');
// Initialize and register the ZenStack authentication provider
context.subscriptions.push(new ZenStackAuthenticationProvider(context));
// Start language client
client = startLanguageClient(context);
const documentationCache = new DocumentationCache(context);
context.subscriptions.push(documentationCache);
context.subscriptions.push(new ZModelPreview(context, client, documentationCache));
context.subscriptions.push(new ReleaseNotesManager(context));
}
// This function is called when the extension is deactivated.
export function deactivate(): Thenable<void> | undefined {
if (client) {
return client.stop();
}
return undefined;
}
function startLanguageClient(context: vscode.ExtensionContext): LanguageClient {
const serverModule = context.asAbsolutePath(path.join('bundle', 'language-server', 'main'));
// 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
const serverOptions: ServerOptions = {
run: { module: serverModule, transport: TransportKind.ipc },
debug: {
module: serverModule,
transport: TransportKind.ipc,
options: debugOptions,
},
};
const fileSystemWatcher = vscode.workspace.createFileSystemWatcher('**/*.zmodel');
context.subscriptions.push(fileSystemWatcher);
// Options to control the language client
const clientOptions: LanguageClientOptions = {
documentSelector: [{ scheme: 'file', language: 'zmodel' }],
synchronize: {
// Notify the server about file changes to files contained in the workspace
fileEvents: fileSystemWatcher,
},
};
// Create the language client and start the client.
const client = new LanguageClient('zmodel', 'ZenStack Model', serverOptions, clientOptions);
// Start the client. This will also launch the server
void client.start();
return client;
}