diff options
Diffstat (limited to 'codeless/src/extension.ts')
-rw-r--r-- | codeless/src/extension.ts | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/codeless/src/extension.ts b/codeless/src/extension.ts new file mode 100644 index 000000000..712d93e04 --- /dev/null +++ b/codeless/src/extension.ts | |||
@@ -0,0 +1,88 @@ | |||
1 | 'use strict'; | ||
2 | import * as vscode from 'vscode'; | ||
3 | import { | ||
4 | LanguageClient, | ||
5 | LanguageClientOptions, | ||
6 | ServerOptions, | ||
7 | TransportKind, | ||
8 | Executable, | ||
9 | TextDocumentIdentifier | ||
10 | } from 'vscode-languageclient'; | ||
11 | |||
12 | |||
13 | let client: LanguageClient; | ||
14 | |||
15 | let uris = { | ||
16 | syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') | ||
17 | } | ||
18 | |||
19 | |||
20 | export function activate(context: vscode.ExtensionContext) { | ||
21 | let dispose = (disposable) => { | ||
22 | context.subscriptions.push(disposable); | ||
23 | } | ||
24 | let registerCommand = (name, f) => { | ||
25 | dispose(vscode.commands.registerCommand(name, f)) | ||
26 | } | ||
27 | |||
28 | registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) | ||
29 | dispose(vscode.workspace.registerTextDocumentContentProvider( | ||
30 | 'libsyntax-rust', | ||
31 | new TextDocumentContentProvider() | ||
32 | )) | ||
33 | startServer() | ||
34 | } | ||
35 | |||
36 | export function deactivate(): Thenable<void> { | ||
37 | if (!client) { | ||
38 | return undefined; | ||
39 | } | ||
40 | return client.stop(); | ||
41 | } | ||
42 | |||
43 | function startServer() { | ||
44 | let run: Executable = { | ||
45 | command: "cargo", | ||
46 | args: ["run"], | ||
47 | options: { | ||
48 | cwd: "./server" | ||
49 | } | ||
50 | } | ||
51 | let serverOptions: ServerOptions = { | ||
52 | run, | ||
53 | debug: run | ||
54 | }; | ||
55 | |||
56 | let clientOptions: LanguageClientOptions = { | ||
57 | documentSelector: [{ scheme: 'file', language: 'rust' }], | ||
58 | }; | ||
59 | |||
60 | client = new LanguageClient( | ||
61 | 'm', | ||
62 | 'm languge server', | ||
63 | serverOptions, | ||
64 | clientOptions, | ||
65 | ); | ||
66 | client.start(); | ||
67 | } | ||
68 | |||
69 | async function openDoc(uri: vscode.Uri) { | ||
70 | let document = await vscode.workspace.openTextDocument(uri) | ||
71 | return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true) | ||
72 | } | ||
73 | |||
74 | class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
75 | public eventEmitter = new vscode.EventEmitter<vscode.Uri>() | ||
76 | public syntaxTree: string = "Not available" | ||
77 | |||
78 | public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> { | ||
79 | let editor = vscode.window.activeTextEditor; | ||
80 | if (editor == null) return "" | ||
81 | let textDocument: TextDocumentIdentifier = { uri: editor.document.uri.toString() }; | ||
82 | return client.sendRequest("m/syntaxTree", { textDocument }) | ||
83 | } | ||
84 | |||
85 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
86 | return this.eventEmitter.event | ||
87 | } | ||
88 | } | ||