diff options
Diffstat (limited to 'codeless/src')
-rw-r--r-- | codeless/src/extension.ts | 199 |
1 files changed, 0 insertions, 199 deletions
diff --git a/codeless/src/extension.ts b/codeless/src/extension.ts deleted file mode 100644 index 99cbfc78e..000000000 --- a/codeless/src/extension.ts +++ /dev/null | |||
@@ -1,199 +0,0 @@ | |||
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: "cargo", | ||
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 | console.log("A"); | ||
90 | console.log(params.uri); | ||
91 | console.log(vscode.window.activeTextEditor.document.uri.toString()); | ||
92 | console.log("B"); | ||
93 | |||
94 | let editor = vscode.window.visibleTextEditors.find( | ||
95 | (editor) => editor.document.uri.toString() == params.uri | ||
96 | ) | ||
97 | if (editor == null) return; | ||
98 | setHighlights( | ||
99 | editor, | ||
100 | params.decorations, | ||
101 | ) | ||
102 | } | ||
103 | ) | ||
104 | }) | ||
105 | client.start(); | ||
106 | } | ||
107 | |||
108 | async function openDoc(uri: vscode.Uri) { | ||
109 | let document = await vscode.workspace.openTextDocument(uri) | ||
110 | return vscode.window.showTextDocument(document, vscode.ViewColumn.Two, true) | ||
111 | } | ||
112 | |||
113 | class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { | ||
114 | public eventEmitter = new vscode.EventEmitter<vscode.Uri>() | ||
115 | public syntaxTree: string = "Not available" | ||
116 | |||
117 | public provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> { | ||
118 | let editor = vscode.window.activeTextEditor; | ||
119 | if (editor == null) return "" | ||
120 | let request: SyntaxTreeParams = { | ||
121 | textDocument: { uri: editor.document.uri.toString() } | ||
122 | }; | ||
123 | return client.sendRequest<SyntaxTreeResult>("m/syntaxTree", request); | ||
124 | } | ||
125 | |||
126 | get onDidChange(): vscode.Event<vscode.Uri> { | ||
127 | return this.eventEmitter.event | ||
128 | } | ||
129 | } | ||
130 | |||
131 | |||
132 | const decorations = (() => { | ||
133 | const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj }) | ||
134 | return { | ||
135 | background: decor("#3F3F3F"), | ||
136 | error: vscode.window.createTextEditorDecorationType({ | ||
137 | borderColor: "red", | ||
138 | borderStyle: "none none dashed none", | ||
139 | }), | ||
140 | comment: decor("#7F9F7F"), | ||
141 | string: decor("#CC9393"), | ||
142 | keyword: decor("#F0DFAF"), | ||
143 | function: decor("#93E0E3"), | ||
144 | parameter: decor("#94BFF3"), | ||
145 | builtin: decor("#DD6718"), | ||
146 | text: decor("#DCDCCC"), | ||
147 | attribute: decor("#BFEBBF"), | ||
148 | literal: decor("#DFAF8F"), | ||
149 | } | ||
150 | })() | ||
151 | |||
152 | function setHighlights( | ||
153 | editor: vscode.TextEditor, | ||
154 | highlihgs: Array<Decoration> | ||
155 | ) { | ||
156 | let byTag = {} | ||
157 | for (let tag in decorations) { | ||
158 | byTag[tag] = [] | ||
159 | } | ||
160 | |||
161 | for (let d of highlihgs) { | ||
162 | if (!byTag[d.tag]) { | ||
163 | console.log(`unknown tag ${d.tag}`) | ||
164 | continue | ||
165 | } | ||
166 | byTag[d.tag].push(d.range) | ||
167 | } | ||
168 | |||
169 | for (let tag in byTag) { | ||
170 | let dec = decorations[tag] | ||
171 | let ranges = byTag[tag] | ||
172 | editor.setDecorations(dec, ranges) | ||
173 | } | ||
174 | } | ||
175 | |||
176 | interface SyntaxTreeParams { | ||
177 | textDocument: lc.TextDocumentIdentifier; | ||
178 | } | ||
179 | |||
180 | type SyntaxTreeResult = string | ||
181 | |||
182 | interface ExtendSelectionParams { | ||
183 | textDocument: lc.TextDocumentIdentifier; | ||
184 | selections: lc.Range[]; | ||
185 | } | ||
186 | |||
187 | interface ExtendSelectionResult { | ||
188 | selections: lc.Range[]; | ||
189 | } | ||
190 | |||
191 | interface PublishDecorationsParams { | ||
192 | uri: string, | ||
193 | decorations: Decoration[], | ||
194 | } | ||
195 | |||
196 | interface Decoration { | ||
197 | range: lc.Range, | ||
198 | tag: string, | ||
199 | } | ||