From 6b118c9b8de3c3ed84a16aba2e71fa5a9ada6e74 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 19:58:44 +0100 Subject: Refactor runables --- editors/code/src/commands/index.ts | 5 +- editors/code/src/commands/runnables.ts | 122 ++++++++++++++++----------------- editors/code/src/main.ts | 6 +- 3 files changed, 66 insertions(+), 67 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 325ae3da8..9f4636e52 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -8,7 +8,7 @@ import { parentModule } from './parent_module'; import { syntaxTree } from './syntax_tree'; import { expandMacro } from './expand_macro'; import * as inlayHints from './inlay_hints'; -import * as runnables from './runnables'; +import { run, runSingle } from './runnables'; function collectGarbage(ctx: Ctx): Cmd { return async () => { @@ -22,9 +22,10 @@ export { joinLines, matchingBrace, parentModule, - runnables, syntaxTree, onEnter, inlayHints, collectGarbage, + run, + runSingle }; diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 7728541de..c4be21a0c 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -1,7 +1,67 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import { Server } from '../server'; +import { Ctx, Cmd } from '../ctx'; + +export function run(ctx: Ctx): Cmd { + let prevRunnable: RunnableQuickPick | undefined; + + return async () => { + const editor = ctx.activeRustEditor; + if (!editor) return + + const textDocument: lc.TextDocumentIdentifier = { + uri: editor.document.uri.toString(), + }; + const params: RunnablesParams = { + textDocument, + position: ctx.client.code2ProtocolConverter.asPosition( + editor.selection.active, + ), + }; + const runnables = await ctx.client.sendRequest( + 'rust-analyzer/runnables', + params, + ); + const items: RunnableQuickPick[] = []; + if (prevRunnable) { + items.push(prevRunnable); + } + for (const r of runnables) { + if ( + prevRunnable && + JSON.stringify(prevRunnable.runnable) === JSON.stringify(r) + ) { + continue; + } + items.push(new RunnableQuickPick(r)); + } + const item = await vscode.window.showQuickPick(items); + if (!item) return; + + item.detail = 'rerun'; + prevRunnable = item; + const task = createTask(item.runnable); + return await vscode.tasks.executeTask(task); + } +} + +export function runSingle(ctx: Ctx): Cmd { + return async (runnable: Runnable) => { + const editor = ctx.activeRustEditor; + if (!editor) return + + const task = createTask(runnable); + task.group = vscode.TaskGroup.Build; + task.presentationOptions = { + reveal: vscode.TaskRevealKind.Always, + panel: vscode.TaskPanelKind.Dedicated, + clear: true, + }; + + return vscode.tasks.executeTask(task); + } +} interface RunnablesParams { textDocument: lc.TextDocumentIdentifier; @@ -67,63 +127,3 @@ function createTask(spec: Runnable): vscode.Task { t.presentationOptions.clear = true; return t; } - -let prevRunnable: RunnableQuickPick | undefined; -export async function handle(): Promise { - const editor = vscode.window.activeTextEditor; - if (editor == null || editor.document.languageId !== 'rust') { - return; - } - const textDocument: lc.TextDocumentIdentifier = { - uri: editor.document.uri.toString(), - }; - const params: RunnablesParams = { - textDocument, - position: Server.client.code2ProtocolConverter.asPosition( - editor.selection.active, - ), - }; - const runnables = await Server.client.sendRequest( - 'rust-analyzer/runnables', - params, - ); - const items: RunnableQuickPick[] = []; - if (prevRunnable) { - items.push(prevRunnable); - } - for (const r of runnables) { - if ( - prevRunnable && - JSON.stringify(prevRunnable.runnable) === JSON.stringify(r) - ) { - continue; - } - items.push(new RunnableQuickPick(r)); - } - const item = await vscode.window.showQuickPick(items); - if (!item) { - return; - } - - item.detail = 'rerun'; - prevRunnable = item; - const task = createTask(item.runnable); - return await vscode.tasks.executeTask(task); -} - -export async function handleSingle(runnable: Runnable) { - const editor = vscode.window.activeTextEditor; - if (editor == null || editor.document.languageId !== 'rust') { - return; - } - - const task = createTask(runnable); - task.group = vscode.TaskGroup.Build; - task.presentationOptions = { - reveal: vscode.TaskRevealKind.Always, - panel: vscode.TaskPanelKind.Dedicated, - clear: true, - }; - - return vscode.tasks.executeTask(task); -} diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index b8e3396a6..7ad5e6934 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -20,6 +20,8 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('parentModule', commands.parentModule); ctx.registerCommand('syntaxTree', commands.syntaxTree); ctx.registerCommand('expandMacro', commands.expandMacro); + ctx.registerCommand('run', commands.run); + ctx.registerCommand('runSingle', commands.runSingle); // Internal action for lenses function disposeOnDeactivation(disposable: vscode.Disposable) { context.subscriptions.push(disposable); @@ -29,10 +31,6 @@ export async function activate(context: vscode.ExtensionContext) { disposeOnDeactivation(vscode.commands.registerCommand(name, f)); } - // Commands are requests from vscode to the language server - registerCommand('rust-analyzer.run', commands.runnables.handle); - // Unlike the above this does not send requests to the language server - registerCommand('rust-analyzer.runSingle', commands.runnables.handleSingle); registerCommand( 'rust-analyzer.showReferences', (uri: string, position: lc.Position, locations: lc.Location[]) => { -- cgit v1.2.3