forked from TypeFox/monaco-languageclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statemachineClient.ts
144 lines (132 loc) · 5.74 KB
/
statemachineClient.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* --------------------------------------------------------------------------------------------
* Copyright (c) 2024 TypeFox GmbH (http://www.typefox.io). All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */
import { editor } from 'monaco-editor';
import { MonacoLanguageClient, initServices, useOpenEditorStub } from 'monaco-languageclient';
import { BrowserMessageReader, BrowserMessageWriter } from 'vscode-languageserver-protocol/browser.js';
import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient';
import { createConfiguredEditor } from 'vscode/monaco';
import { ExtensionHostKind, registerExtension } from 'vscode/extensions';
import getConfigurationServiceOverride, { updateUserConfiguration } from '@codingame/monaco-vscode-configuration-service-override';
import getEditorServiceOverride from '@codingame/monaco-vscode-editor-service-override';
import getKeybindingsServiceOverride from '@codingame/monaco-vscode-keybindings-service-override';
import getThemeServiceOverride from '@codingame/monaco-vscode-theme-service-override';
import getTextmateServiceOverride from '@codingame/monaco-vscode-textmate-service-override';
import '@codingame/monaco-vscode-theme-defaults-default-extension';
import { Uri } from 'vscode';
const languageId = 'statemachine';
export const setupStatemachineClient = async () => {
const serviceConfig = {
userServices: {
...getThemeServiceOverride(),
...getTextmateServiceOverride(),
...getConfigurationServiceOverride(),
...getEditorServiceOverride(useOpenEditorStub),
...getKeybindingsServiceOverride()
},
debugLogging: true,
workspaceConfig: {
workspaceProvider: {
trusted: true,
workspace: {
workspaceUri: Uri.file('/workspace')
},
async open() {
return false;
}
}
}
};
await initServices(serviceConfig);
// does not do harm
for (let i = 0; i < 3; i++) {
await initServices(serviceConfig);
}
console.log('Setting up Statemachine client configuration ...');
const extension = {
name: 'statemachine-client',
publisher: 'monaco-languageclient-project',
version: '1.0.0',
engines: {
vscode: '*'
},
contributes: {
languages: [{
id: languageId,
extensions: [
`.${languageId}`
],
aliases: [
languageId
],
configuration: './statemachine-configuration.json'
}],
grammars: [{
language: languageId,
scopeName: 'source.statemachine',
path: './statemachine-grammar.json'
}],
keybindings: [{
key: 'ctrl+p',
command: 'editor.action.quickCommand',
when: 'editorTextFocus'
}, {
key: 'ctrl+shift+c',
command: 'editor.action.commentLine',
when: 'editorTextFocus'
}]
}
};
const { registerFileUrl } = registerExtension(extension, ExtensionHostKind.LocalProcess);
registerFileUrl('/statemachine-configuration.json', new URL('../../node_modules/langium-statemachine-dsl/language-configuration.json', window.location.href).href);
registerFileUrl('/statemachine-grammar.json', new URL('../../node_modules/langium-statemachine-dsl/syntaxes/statemachine.tmLanguage.json', window.location.href).href);
updateUserConfiguration(`{
"editor.fontSize": 14,
"workbench.colorTheme": "Default Dark Modern"
}`);
const exampleStatemachineUrl = new URL('./src/langium/example.statemachine', window.location.href).href;
const editorText = await (await fetch(exampleStatemachineUrl)).text();
const editorOptions = {
model: editor.createModel(editorText, languageId, Uri.parse('/workspace/example.statemachine')),
automaticLayout: true
};
createConfiguredEditor(document.getElementById('container')!, editorOptions);
function createLanguageClient(transports: MessageTransports): MonacoLanguageClient {
return new MonacoLanguageClient({
name: 'Statemachine Client',
clientOptions: {
// use a language id as a document selector
documentSelector: [{ language: languageId }],
// disable the default error handler
errorHandler: {
error: () => ({ action: ErrorAction.Continue }),
closed: () => ({ action: CloseAction.DoNotRestart })
}
},
// create a language client connection to the server running in the web worker
connectionProvider: {
get: () => {
return Promise.resolve(transports);
}
}
});
}
const langiumWorkerUrl = new URL('./dist/worker/statemachineServerWorker.js', window.location.href).href;
const worker = new Worker(langiumWorkerUrl, {
type: 'module',
name: 'Statemachine LS'
});
const reader = new BrowserMessageReader(worker);
const writer = new BrowserMessageWriter(worker);
const languageClient = createLanguageClient({ reader, writer });
languageClient.start();
reader.onClose(() => languageClient.stop());
};
export const startStatemachineClient = async () => {
try {
await setupStatemachineClient();
} catch (e) {
console.error(e);
}
};