-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
73 lines (59 loc) · 1.75 KB
/
index.js
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
import CodeMirror from 'codemirror';
import 'codemirror/mode/javascript/javascript';
import 'codemirror/mode/htmlmixed/htmlmixed';
import 'codemirror/mode/css/css';
import 'codemirror/mode/go/go';
import 'codemirror/mode/python/python';
import 'codemirror/lib/codemirror.css';
import 'codemirror/theme/idea.css';
// This addon is required to be installed by the caller
import 'codemirror/addon/hint/show-hint.css';
import 'codemirror/addon/hint/show-hint';
import 'lsp-editor-adapter/lib/codemirror-lsp.css';
import { LspWsConnection, CodeMirrorAdapter } from 'lsp-editor-adapter';
let sample = '';
let modes = {
javascript: 'javascript',
html: 'htmlmixed',
css: 'css',
};
let documents = {
javascript: 'file.js',
html: 'file.html',
css: 'file.css',
};
let editor = CodeMirror(document.querySelector('.editor'), {
theme: 'idea',
lineNumbers: true,
value: sample,
gutters: ['CodeMirror-lsp'],
});
document.querySelector('select').addEventListener('change', () => {
switchSources();
});
let connection;
let adapter;
function switchSources() {
if (connection) {
connection.close();
}
if (adapter) {
adapter.remove();
}
let value = document.querySelector('select').value.toLowerCase();
if (typeof modes[value] !== undefined) {
editor.setOption('mode', modes[value]);
}
editor.setValue('');
connection = new LspWsConnection({
serverUri: 'wss://lsp-server.wylie.su/' + value,
languageId: value,
rootUri: 'file:///usr/src/app/sources',
documentUri: 'file:///usr/src/app/sources/' + documents[value],
documentText: () => editor.getValue(),
}).connect(new WebSocket('wss://lsp-server.wylie.su/' + value));
adapter = new CodeMirrorAdapter(connection, {
quickSuggestionsDelay: 10,
}, editor);
}
switchSources();