Refactor runables

This commit is contained in:
Aleksey Kladov 2019-12-30 19:58:44 +01:00
parent 237abb85c4
commit 6b118c9b8d
3 changed files with 66 additions and 67 deletions

View File

@ -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
};

View File

@ -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<Runnable[]>(
'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<vscode.TaskExecution | undefined> {
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<Runnable[]>(
'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);
}

View File

@ -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[]) => {