Skip to content

feat: Extend vscode 'run' command with optional mode argument for run… #20077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions editors/code/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,11 +1114,11 @@ export function applySnippetWorkspaceEditCommand(_ctx: CtxInit): Cmd {
};
}

export function run(ctx: CtxInit): Cmd {
export function run(ctx: CtxInit, mode?: "cursor"): Cmd {
let prevRunnable: RunnableQuickPick | undefined;

return async () => {
const item = await selectRunnable(ctx, prevRunnable);
const item = await selectRunnable(ctx, prevRunnable, false, true, mode);
if (!item) return;

item.detail = "rerun";
Expand Down
2 changes: 1 addition & 1 deletion editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function createCommands(): Record<string, CommandFactory> {
viewCrateGraph: { enabled: commands.viewCrateGraph },
viewFullCrateGraph: { enabled: commands.viewFullCrateGraph },
expandMacro: { enabled: commands.expandMacro },
run: { enabled: commands.run },
run: { enabled: (ctx) => (mode?: "cursor") => commands.run(ctx, mode)() },
copyRunCommandLine: { enabled: commands.copyRunCommandLine },
debug: { enabled: commands.debug },
newDebugConfig: { enabled: commands.newDebugConfig },
Expand Down
57 changes: 57 additions & 0 deletions editors/code/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ export async function selectRunnable(
prevRunnable?: RunnableQuickPick,
debuggeeOnly = false,
showButtons: boolean = true,
mode?: "cursor",
): Promise<RunnableQuickPick | undefined> {
const editor = ctx.activeRustEditor ?? ctx.activeCargoTomlEditor;
if (!editor) return;

if (mode === "cursor") {
return selectRunnableAtCursor(ctx, editor, prevRunnable);
}

// show a placeholder while we get the runnables from the server
const quickPick = vscode.window.createQuickPick();
quickPick.title = "Select Runnable";
Expand Down Expand Up @@ -54,6 +59,58 @@ export async function selectRunnable(
);
}

async function selectRunnableAtCursor(
ctx: CtxInit,
editor: RustEditor,
prevRunnable?: RunnableQuickPick,
): Promise<RunnableQuickPick | undefined> {
const runnableQuickPicks = await getRunnables(ctx.client, editor, prevRunnable, false);
let runnableQuickPickAtCursor = null;
const cursorPosition = ctx.client.code2ProtocolConverter.asPosition(editor.selection.active);
for (const runnableQuickPick of runnableQuickPicks) {
if (!runnableQuickPick.runnable.location?.targetRange) {
continue;
}
const runnableQuickPickRange = runnableQuickPick.runnable.location.targetRange;
if (
runnableQuickPickAtCursor?.runnable?.location?.targetRange != null &&
rangeContainsOtherRange(
runnableQuickPickRange,
runnableQuickPickAtCursor.runnable.location.targetRange,
)
) {
continue;
}
if (rangeContainsPosition(runnableQuickPickRange, cursorPosition)) {
runnableQuickPickAtCursor = runnableQuickPick;
}
}
if (runnableQuickPickAtCursor == null) {
return;
}
return Promise.resolve(runnableQuickPickAtCursor);
}

function rangeContainsPosition(range: lc.Range, position: lc.Position): boolean {
return (
(position.line > range.start.line ||
(position.line === range.start.line && position.character >= range.start.character)) &&
(position.line < range.end.line ||
(position.line === range.end.line && position.character <= range.end.character))
);
}

function rangeContainsOtherRange(range: lc.Range, otherRange: lc.Range) {
return (
(range.start.line < otherRange.start.line ||
(range.start.line === otherRange.start.line &&
range.start.character <= otherRange.start.character)) &&
(range.end.line > otherRange.end.line ||
(range.end.line === otherRange.end.line &&
range.end.character >= otherRange.end.character))
);
}

export class RunnableQuickPick implements vscode.QuickPickItem {
public label: string;
public description?: string | undefined;
Expand Down