-
Notifications
You must be signed in to change notification settings - Fork 367
/
client.ts
129 lines (114 loc) · 3.48 KB
/
client.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import * as monaco from 'monaco-editor-core';
import { listen, MessageConnection, Disposable } from 'vscode-ws-jsonrpc';
import {
MonacoLanguageClient,
CloseAction,
ErrorAction,
MonacoServices,
createConnection,
} from 'monaco-languageclient';
import normalizeUrl = require('normalize-url');
const ReconnectingWebSocket = require('reconnecting-websocket');
// register Monaco languages
monaco.languages.setMonarchTokensProvider('botframeworklu', {
ignoreCase: true,
tokenizer: {
root: [],
},
});
monaco.languages.register({
id: 'bflu',
extensions: ['.lu'],
aliases: ['LU', 'language-understanding'],
mimetypes: ['application/lu'],
});
monaco.languages.setLanguageConfiguration('bflu', {
autoClosingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
],
});
monaco.editor.defineTheme('lutheme', {
base: 'vs',
inherit: false,
colors: {},
rules: [],
});
// create Monaco editor
const value = `@ ml a
@ composite la = [a, b]
`;
const editor = monaco.editor.create(document.getElementById('container')!, {
model: monaco.editor.createModel(value, 'bflu', monaco.Uri.parse('inmemory://model.json')),
glyphMargin: true,
autoClosingBrackets: 'always',
wordBasedSuggestions: false,
autoIndent: true,
formatOnType: true,
lightbulb: {
enabled: true,
},
theme: 'lutheme',
});
// install Monaco language client services
MonacoServices.install(editor);
// create the web socket
const url = createUrl('/luServer');
const webSocket = createWebSocket(url);
// listen when the web socket is opened
listen({
webSocket,
onConnection: connection => {
// create and start the language client
const languageClient = createLanguageClient(connection);
const disposable: Disposable = languageClient.start();
connection.onClose(() => disposable.dispose());
// editor.addCommand(monaco.KeyMod.Alt | monaco.KeyCode.F9, function () {
// connection.sendRequest(DocumentOnTypeFormattingRequest.type );
// });
},
});
const keybinding = editor.addCommand(monaco.KeyMod.Alt | monaco.KeyCode.F9, function() {
if (webSocket.OPEN) {
}
});
function createLanguageClient(connection: MessageConnection): MonacoLanguageClient {
return new MonacoLanguageClient({
name: 'LU Language Client',
clientOptions: {
// use a language id as a document selector
documentSelector: ['bflu'],
// disable the default error handler
errorHandler: {
error: () => ErrorAction.Continue,
closed: () => CloseAction.DoNotRestart,
},
//middleware
},
// create a language client connection from the JSON RPC connection on demand
connectionProvider: {
get: (errorHandler, closeHandler) => {
return Promise.resolve(createConnection(connection, errorHandler, closeHandler));
},
},
});
}
function createUrl(path: string): string {
const protocol = location.protocol === 'https:' ? 'wss' : 'ws';
// return normalizeUrl(`${protocol}://${location.host}${location.pathname}${path}`);
return normalizeUrl(`${protocol}://localhost:5000${location.pathname}${path}`);
}
function createWebSocket(url: string): WebSocket {
const socketOptions = {
maxReconnectionDelay: 10000,
minReconnectionDelay: 1000,
reconnectionDelayGrowFactor: 1.3,
connectionTimeout: 10000,
maxRetries: Infinity,
debug: false,
};
return new ReconnectingWebSocket(url, [], socketOptions);
}