diff options
Diffstat (limited to 'editors/code/src/server.ts')
-rw-r--r-- | editors/code/src/server.ts | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/editors/code/src/server.ts b/editors/code/src/server.ts new file mode 100644 index 000000000..c1c95e008 --- /dev/null +++ b/editors/code/src/server.ts | |||
@@ -0,0 +1,74 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | import * as lc from 'vscode-languageclient' | ||
3 | |||
4 | import { Highlighter, Decoration } from './highlighting'; | ||
5 | |||
6 | export class Config { | ||
7 | highlightingOn = true; | ||
8 | |||
9 | constructor() { | ||
10 | vscode.workspace.onDidChangeConfiguration(_ => this.userConfigChanged()); | ||
11 | this.userConfigChanged(); | ||
12 | } | ||
13 | |||
14 | userConfigChanged() { | ||
15 | let config = vscode.workspace.getConfiguration('ra-lsp'); | ||
16 | if (config.has('highlightingOn')) { | ||
17 | this.highlightingOn = config.get('highlightingOn') as boolean; | ||
18 | }; | ||
19 | |||
20 | if (!this.highlightingOn) { | ||
21 | Server.highlighter.removeHighlights(); | ||
22 | } | ||
23 | } | ||
24 | } | ||
25 | |||
26 | export class Server { | ||
27 | static highlighter = new Highlighter(); | ||
28 | static config = new Config(); | ||
29 | static client: lc.LanguageClient; | ||
30 | |||
31 | |||
32 | static start() { | ||
33 | let run: lc.Executable = { | ||
34 | command: "ra_lsp_server", | ||
35 | options: { cwd: "." } | ||
36 | } | ||
37 | let serverOptions: lc.ServerOptions = { | ||
38 | run, | ||
39 | debug: run | ||
40 | }; | ||
41 | |||
42 | let clientOptions: lc.LanguageClientOptions = { | ||
43 | documentSelector: [{ scheme: 'file', language: 'rust' }], | ||
44 | }; | ||
45 | |||
46 | Server.client = new lc.LanguageClient( | ||
47 | 'ra-lsp', | ||
48 | 'rust-analyzer languge server', | ||
49 | serverOptions, | ||
50 | clientOptions, | ||
51 | ); | ||
52 | Server.client.onReady().then(() => { | ||
53 | Server.client.onNotification( | ||
54 | "m/publishDecorations", | ||
55 | (params: PublishDecorationsParams) => { | ||
56 | let editor = vscode.window.visibleTextEditors.find( | ||
57 | (editor) => editor.document.uri.toString() == params.uri | ||
58 | ) | ||
59 | if (!Server.config.highlightingOn || !editor) return; | ||
60 | Server.highlighter.setHighlights( | ||
61 | editor, | ||
62 | params.decorations, | ||
63 | ) | ||
64 | } | ||
65 | ) | ||
66 | }) | ||
67 | Server.client.start(); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | interface PublishDecorationsParams { | ||
72 | uri: string, | ||
73 | decorations: Decoration[], | ||
74 | } | ||