diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 23:04:09 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 23:04:09 +0100 |
commit | f99551f46b1583ed43751e4d26fe76a0b913eb5f (patch) | |
tree | f0ab26d58c5bfd7237eb2f0117fcc141a1c3a5f4 /code/src/extension.ts | |
parent | 9863b9161d8d702848516be70c5c8161b7f382e8 (diff) |
reorganize
Diffstat (limited to 'code/src/extension.ts')
-rw-r--r-- | code/src/extension.ts | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/code/src/extension.ts b/code/src/extension.ts new file mode 100644 index 000000000..bdad1568a --- /dev/null +++ b/code/src/extension.ts | |||
@@ -0,0 +1,194 @@ | |||
1 | 'use strict'; | ||
2 | import * as vscode from 'vscode'; | ||
3 | import * as lc from 'vscode-languageclient' | ||
4 | |||
5 | |||
6 | let client: lc.LanguageClient; | ||
7 | |||
8 | let uris = { | ||
9 | syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') | ||
10 | } | ||
11 | |||
12 | |||
13 | export function activate(context: vscode.ExtensionContext) { | ||
14 | let textDocumentContentProvider = new TextDocumentContentProvider() | ||
15 | let dispose = (disposable) => { | ||
16 | context.subscriptions.push(disposable); | ||
17 | } | ||
18 | let registerCommand = (name, f) => { | ||
19 | dispose(vscode.commands.registerCommand(name, f)) | ||
20 | } | ||
21 | |||
22 | registerCommand('libsyntax-rust.syntaxTree', () => openDoc(uris.syntaxTree)) | ||
23 | registerCommand('libsyntax-rust.extendSelection', async () => { | ||
24 | let editor = vscode.window.activeTextEditor | ||
25 | if (editor == null || editor.document.languageId != "rust") return | ||
26 | let request: ExtendSelectionParams = { | ||
27 | textDocument: { uri: editor.document.uri.toString() }, | ||
28 | selections: editor.selections.map((s) => { | ||
29 | return { start: s.start, end: s.end }; | ||
30 | }) | ||
31 | } | ||
32 | let response = await client.sendRequest<ExtendSelectionResult>("m/extendSelection", request) | ||
33 | editor.selections = response.selections.map((range) => { | ||
34 | return new vscode.Selection( | ||
35 | new vscode.Position(range.start.line, range.start.character), | ||
36 | new vscode.Position(range.end.line, range.end.character), | ||
37 | ) | ||
38 | }) | ||
39 | }) | ||
40 | |||
41 | dispose(vscode.workspace.registerTextDocumentContentProvider( | ||
42 | 'libsyntax-rust', | ||
43 | textDocumentContentProvider | ||
44 | )) | ||
45 | startServer() | ||
46 | vscode.workspace.onDidChangeTextDocument((event: vscode.TextDocumentChangeEvent) => { | ||
47 | let doc = event.document | ||
48 | if (doc.languageId != "rust") return | ||
49 | // We need to order this after LS updates, but there's no API for that. | ||
50 | // Hence, good old setTimeout. | ||
51 | setTimeout(() => { | ||
52 | textDocumentContentProvider.eventEmitter.fire(uris.syntaxTree) | ||
53 | }, 10) | ||
54 | }, null, context.subscriptions) | ||
55 | } | ||
56 | |||
57 | export function deactivate(): Thenable<void> { | ||
58 | if (!client) { | ||
59 | return undefined; | ||
60 | } | ||
61 | return client.stop(); | ||
62 | } | ||
63 | |||
64 | function startServer() { | ||
65 | let run: lc.Executable = { | ||
66 | command: "m", | ||
67 | // args: ["run", "--package", "m"], | ||
68 | options: { cwd: "." } | ||
69 | } | ||
70 | let serverOptions: lc.ServerOptions = { | ||
71 | run, | ||
72 | debug: run | ||
73 | }; | ||
74 | |||
75 | let clientOptions: lc.LanguageClientOptions = { | ||
76 | documentSelector: [{ scheme: 'file', language: 'rust' }], | ||
77 | }; | ||
78 | |||
79 | client = new lc.LanguageClient( | ||
80 | 'm', | ||
81 | 'm languge server', | ||
82 | serverOptions, | ||
83 | clientOptions, | ||
84 | ); | ||
85 | client.onReady().then(() => { | ||
86 | client.onNotification( | ||
87 | new lc.NotificationType("m/publishDecorations"), | ||
88 | (params: PublishDecorationsParams) => { | ||
89 | let editor = vscode.window.visibleTextEditors.find( | ||
90 | (editor) => editor.document.uri.toString() == params.uri | ||
91 | ) | ||
92 | if (editor == null) return; | ||
93 | setHighlights( | ||
94 | editor, | ||
95 | params.decorations, | ||
96 | ) | ||
97 | } | ||
98 | ) | ||
99 | }) | ||
100 | client.start(); | ||
101 | } | ||
102 | |||
103 | async function openDoc(uri: vscode.Uri) { | ||
104 | let document = await vscode.workspace.openTextDocument(uri) | ||
105 | return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true) | ||
106 | } | ||
107 | |||
108 | class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
109 | public eventEmitter = new vscode.EventEmitter<vscode.Uri>() | ||
110 | public syntaxTree: string = "Not available" | ||
111 | |||
112 | public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> { | ||
113 | let editor = vscode.window.activeTextEditor; | ||
114 | if (editor == null) return "" | ||
115 | let request: SyntaxTreeParams = { | ||
116 | textDocument: { uri: editor.document.uri.toString() } | ||
117 | }; | ||
118 | return client.sendRequest<SyntaxTreeResult>("m/syntaxTree", request); | ||
119 | } | ||
120 | |||
121 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
122 | return this.eventEmitter.event | ||
123 | } | ||
124 | } | ||
125 | |||
126 | |||
127 | const decorations = (() => { | ||
128 | const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj }) | ||
129 | return { | ||
130 | background: decor("#3F3F3F"), | ||
131 | error: vscode.window.createTextEditorDecorationType({ | ||
132 | borderColor: "red", | ||
133 | borderStyle: "none none dashed none", | ||
134 | }), | ||
135 | comment: decor("#7F9F7F"), | ||
136 | string: decor("#CC9393"), | ||
137 | keyword: decor("#F0DFAF"), | ||
138 | function: decor("#93E0E3"), | ||
139 | parameter: decor("#94BFF3"), | ||
140 | builtin: decor("#DD6718"), | ||
141 | text: decor("#DCDCCC"), | ||
142 | attribute: decor("#BFEBBF"), | ||
143 | literal: decor("#DFAF8F"), | ||
144 | } | ||
145 | })() | ||
146 | |||
147 | function setHighlights( | ||
148 | editor: vscode.TextEditor, | ||
149 | highlihgs: Array<Decoration> | ||
150 | ) { | ||
151 | let byTag = {} | ||
152 | for (let tag in decorations) { | ||
153 | byTag[tag] = [] | ||
154 | } | ||
155 | |||
156 | for (let d of highlihgs) { | ||
157 | if (!byTag[d.tag]) { | ||
158 | console.log(`unknown tag ${d.tag}`) | ||
159 | continue | ||
160 | } | ||
161 | byTag[d.tag].push(d.range) | ||
162 | } | ||
163 | |||
164 | for (let tag in byTag) { | ||
165 | let dec = decorations[tag] | ||
166 | let ranges = byTag[tag] | ||
167 | editor.setDecorations(dec, ranges) | ||
168 | } | ||
169 | } | ||
170 | |||
171 | interface SyntaxTreeParams { | ||
172 | textDocument: lc.TextDocumentIdentifier; | ||
173 | } | ||
174 | |||
175 | type SyntaxTreeResult = string | ||
176 | |||
177 | interface ExtendSelectionParams { | ||
178 | textDocument: lc.TextDocumentIdentifier; | ||
179 | selections: lc.Range[]; | ||
180 | } | ||
181 | |||
182 | interface ExtendSelectionResult { | ||
183 | selections: lc.Range[]; | ||
184 | } | ||
185 | |||
186 | interface PublishDecorationsParams { | ||
187 | uri: string, | ||
188 | decorations: Decoration[], | ||
189 | } | ||
190 | |||
191 | interface Decoration { | ||
192 | range: lc.Range, | ||
193 | tag: string, | ||
194 | } | ||