aboutsummaryrefslogtreecommitdiff
path: root/code/src/extension.ts
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-27 20:52:43 +0100
committerAleksey Kladov <[email protected]>2018-08-27 20:52:43 +0100
commit422efe59afe333a8371afe6f9fb45f159a6fb07f (patch)
treecfdc2a4209419e690257cff2235b3fd3f75fe53d /code/src/extension.ts
parent57518153147ad53639f16cc940d219dc582c550a (diff)
you better run
Diffstat (limited to 'code/src/extension.ts')
-rw-r--r--code/src/extension.ts104
1 files changed, 72 insertions, 32 deletions
diff --git a/code/src/extension.ts b/code/src/extension.ts
index b9d009776..7acde195d 100644
--- a/code/src/extension.ts
+++ b/code/src/extension.ts
@@ -1,6 +1,7 @@
1'use strict'; 1'use strict';
2import * as vscode from 'vscode'; 2import * as vscode from 'vscode';
3import * as lc from 'vscode-languageclient' 3import * as lc from 'vscode-languageclient'
4import { DH_UNABLE_TO_CHECK_GENERATOR } from 'constants';
4 5
5 6
6let client: lc.LanguageClient; 7let client: lc.LanguageClient;
@@ -12,10 +13,10 @@ let uris = {
12 13
13export function activate(context: vscode.ExtensionContext) { 14export function activate(context: vscode.ExtensionContext) {
14 let textDocumentContentProvider = new TextDocumentContentProvider() 15 let textDocumentContentProvider = new TextDocumentContentProvider()
15 let dispose = (disposable) => { 16 let dispose = (disposable: vscode.Disposable) => {
16 context.subscriptions.push(disposable); 17 context.subscriptions.push(disposable);
17 } 18 }
18 let registerCommand = (name, f) => { 19 let registerCommand = (name: string, f: any) => {
19 dispose(vscode.commands.registerCommand(name, f)) 20 dispose(vscode.commands.registerCommand(name, f))
20 } 21 }
21 22
@@ -42,7 +43,7 @@ export function activate(context: vscode.ExtensionContext) {
42 textDocument: { uri: editor.document.uri.toString() }, 43 textDocument: { uri: editor.document.uri.toString() },
43 offsets: editor.selections.map((s) => { 44 offsets: editor.selections.map((s) => {
44 return client.code2ProtocolConverter.asPosition(s.active) 45 return client.code2ProtocolConverter.asPosition(s.active)
45 }) 46 })
46 } 47 }
47 let response = await client.sendRequest<lc.Position[]>("m/findMatchingBrace", request) 48 let response = await client.sendRequest<lc.Position[]>("m/findMatchingBrace", request)
48 editor.selections = editor.selections.map((sel, idx) => { 49 editor.selections = editor.selections.map((sel, idx) => {
@@ -71,8 +72,8 @@ export function activate(context: vscode.ExtensionContext) {
71 let request: lc.TextDocumentIdentifier = { 72 let request: lc.TextDocumentIdentifier = {
72 uri: editor.document.uri.toString() 73 uri: editor.document.uri.toString()
73 } 74 }
74 let response = await client.sendRequest<lc.TextDocumentIdentifier>("m/parentModule", request) 75 let response = await client.sendRequest<lc.Location[]>("m/parentModule", request)
75 let loc: lc.Location = response[0] 76 let loc = response[0]
76 if (loc == null) return 77 if (loc == null) return
77 let uri = client.protocol2CodeConverter.asUri(loc.uri) 78 let uri = client.protocol2CodeConverter.asUri(loc.uri)
78 let range = client.protocol2CodeConverter.asRange(loc.range) 79 let range = client.protocol2CodeConverter.asRange(loc.range)
@@ -82,19 +83,40 @@ export function activate(context: vscode.ExtensionContext) {
82 e.revealRange(range, vscode.TextEditorRevealType.InCenter) 83 e.revealRange(range, vscode.TextEditorRevealType.InCenter)
83 }) 84 })
84 85
85 registerCommand('libsyntax-rust.run', async (cmd: ProcessSpec) => { 86 let prevRunnable: RunnableQuickPick | undefined = undefined
86 let task = createTask(cmd) 87 registerCommand('libsyntax-rust.run', async () => {
87 await vscode.tasks.executeTask(task) 88 let editor = vscode.window.activeTextEditor
89 if (editor == null || editor.document.languageId != "rust") return
90 let textDocument: lc.TextDocumentIdentifier = {
91 uri: editor.document.uri.toString()
92 }
93 let params: RunnablesParams = {
94 textDocument,
95 position: client.code2ProtocolConverter.asPosition(editor.selection.active)
96 }
97 let runnables = await client.sendRequest<Runnable[]>('m/runnables', params)
98 let items: RunnableQuickPick[] = []
99 if (prevRunnable) {
100 items.push(prevRunnable)
101 }
102 for (let r of runnables) {
103 items.push(new RunnableQuickPick(r))
104 }
105 let item = await vscode.window.showQuickPick(items)
106 if (item) {
107 item.detail = "last"
108 prevRunnable = item
109 let task = createTask(item.runnable)
110 return await vscode.tasks.executeTask(task)
111 }
88 }) 112 })
89 registerCommand('libsyntax-rust.createFile', async (uri_: string) => { 113 registerCommand('libsyntax-rust.createFile', async (uri_: string) => {
90 console.log(`uri: ${uri_}`)
91 let uri = vscode.Uri.parse(uri_) 114 let uri = vscode.Uri.parse(uri_)
92 let edit = new vscode.WorkspaceEdit() 115 let edit = new vscode.WorkspaceEdit()
93 edit.createFile(uri) 116 edit.createFile(uri)
94 await vscode.workspace.applyEdit(edit) 117 await vscode.workspace.applyEdit(edit)
95 let doc = await vscode.workspace.openTextDocument(uri) 118 let doc = await vscode.workspace.openTextDocument(uri)
96 await vscode.window.showTextDocument(doc) 119 await vscode.window.showTextDocument(doc)
97 console.log("Done")
98 }) 120 })
99 121
100 dispose(vscode.workspace.registerTextDocumentContentProvider( 122 dispose(vscode.workspace.registerTextDocumentContentProvider(
@@ -113,13 +135,13 @@ export function activate(context: vscode.ExtensionContext) {
113 135
114// We need to order this after LS updates, but there's no API for that. 136// We need to order this after LS updates, but there's no API for that.
115// Hence, good old setTimeout. 137// Hence, good old setTimeout.
116function afterLs(f) { 138function afterLs(f: () => any) {
117 setTimeout(f, 10) 139 setTimeout(f, 10)
118} 140}
119 141
120export function deactivate(): Thenable<void> { 142export function deactivate(): Thenable<void> {
121 if (!client) { 143 if (!client) {
122 return undefined; 144 return Promise.resolve();
123 } 145 }
124 return client.stop(); 146 return client.stop();
125} 147}
@@ -148,7 +170,7 @@ function startServer() {
148 ); 170 );
149 client.onReady().then(() => { 171 client.onReady().then(() => {
150 client.onNotification( 172 client.onNotification(
151 new lc.NotificationType("m/publishDecorations"), 173 "m/publishDecorations",
152 (params: PublishDecorationsParams) => { 174 (params: PublishDecorationsParams) => {
153 let editor = vscode.window.visibleTextEditors.find( 175 let editor = vscode.window.visibleTextEditors.find(
154 (editor) => editor.document.uri.toString() == params.uri 176 (editor) => editor.document.uri.toString() == params.uri
@@ -164,11 +186,11 @@ function startServer() {
164 new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"), 186 new lc.RequestType<lc.Position, void, any, any>("m/moveCursor"),
165 (params: lc.Position, token: lc.CancellationToken) => { 187 (params: lc.Position, token: lc.CancellationToken) => {
166 let editor = vscode.window.activeTextEditor; 188 let editor = vscode.window.activeTextEditor;
167 if (editor == null) return 189 if (!editor) return
168 if (!editor.selection.isEmpty) return 190 if (!editor.selection.isEmpty) return
169 let position = client.protocol2CodeConverter.asPosition(params) 191 let position = client.protocol2CodeConverter.asPosition(params)
170 afterLs(() => { 192 afterLs(() => {
171 editor.selection = new vscode.Selection(position, position) 193 editor!.selection = new vscode.Selection(position, position)
172 }) 194 })
173 } 195 }
174 ) 196 )
@@ -200,8 +222,8 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider
200} 222}
201 223
202 224
203const decorations = (() => { 225const decorations: { [index: string]: vscode.TextEditorDecorationType } = (() => {
204 const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj }) 226 const decor = (obj: any) => vscode.window.createTextEditorDecorationType({ color: obj })
205 return { 227 return {
206 background: decor("#3F3F3F"), 228 background: decor("#3F3F3F"),
207 error: vscode.window.createTextEditorDecorationType({ 229 error: vscode.window.createTextEditorDecorationType({
@@ -224,24 +246,24 @@ function setHighlights(
224 editor: vscode.TextEditor, 246 editor: vscode.TextEditor,
225 highlihgs: Array<Decoration> 247 highlihgs: Array<Decoration>
226) { 248) {
227 let byTag = {} 249 let byTag: Map<string, vscode.Range[]> = new Map()
228 for (let tag in decorations) { 250 for (let tag in decorations) {
229 byTag[tag] = [] 251 byTag.set(tag, [])
230 } 252 }
231 253
232 for (let d of highlihgs) { 254 for (let d of highlihgs) {
233 if (!byTag[d.tag]) { 255 if (!byTag.get(d.tag)) {
234 console.log(`unknown tag ${d.tag}`) 256 console.log(`unknown tag ${d.tag}`)
235 continue 257 continue
236 } 258 }
237 byTag[d.tag].push( 259 byTag.get(d.tag)!.push(
238 client.protocol2CodeConverter.asRange(d.range) 260 client.protocol2CodeConverter.asRange(d.range)
239 ) 261 )
240 } 262 }
241 263
242 for (let tag in byTag) { 264 for (let tag of byTag.keys()) {
243 let dec = decorations[tag] 265 let dec: vscode.TextEditorDecorationType = decorations[tag]
244 let ranges = byTag[tag] 266 let ranges = byTag.get(tag)!
245 editor.setDecorations(dec, ranges) 267 editor.setDecorations(dec, ranges)
246 } 268 }
247} 269}
@@ -276,17 +298,36 @@ interface PublishDecorationsParams {
276 decorations: Decoration[], 298 decorations: Decoration[],
277} 299}
278 300
279interface Decoration { 301interface RunnablesParams {
280 range: lc.Range, 302 textDocument: lc.TextDocumentIdentifier,
281 tag: string, 303 position?: lc.Position,
282} 304}
283 305
284interface ProcessSpec { 306interface Runnable {
307 range: lc.Range;
308 label: string;
285 bin: string; 309 bin: string;
286 args: string[]; 310 args: string[];
287 env: { [key: string]: string }; 311 env: { [index: string]: string },
288} 312}
289 313
314class RunnableQuickPick implements vscode.QuickPickItem {
315 label: string;
316 description?: string | undefined;
317 detail?: string | undefined;
318 picked?: boolean | undefined;
319
320 constructor(public runnable: Runnable) {
321 this.label = runnable.label
322 }
323}
324
325interface Decoration {
326 range: lc.Range,
327 tag: string,
328}
329
330
290interface CargoTaskDefinition extends vscode.TaskDefinition { 331interface CargoTaskDefinition extends vscode.TaskDefinition {
291 type: 'cargo'; 332 type: 'cargo';
292 label: string; 333 label: string;
@@ -295,8 +336,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition {
295 env?: { [key: string]: string }; 336 env?: { [key: string]: string };
296} 337}
297 338
298 339function createTask(spec: Runnable): vscode.Task {
299function createTask(spec: ProcessSpec): vscode.Task {
300 const TASK_SOURCE = 'Rust'; 340 const TASK_SOURCE = 'Rust';
301 let definition: CargoTaskDefinition = { 341 let definition: CargoTaskDefinition = {
302 type: 'cargo', 342 type: 'cargo',
@@ -313,7 +353,7 @@ function createTask(spec: ProcessSpec): vscode.Task {
313 }; 353 };
314 let exec = new vscode.ShellExecution(execCmd, execOption); 354 let exec = new vscode.ShellExecution(execCmd, execOption);
315 355
316 let f = vscode.workspace.workspaceFolders[0] 356 let f = vscode.workspace.workspaceFolders![0]
317 let t = new vscode.Task(definition, f, definition.label, TASK_SOURCE, exec, ['$rustc']); 357 let t = new vscode.Task(definition, f, definition.label, TASK_SOURCE, exec, ['$rustc']);
318 return t; 358 return t;
319} 359}