Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--editors/code/src/commands.ts4
-rw-r--r--editors/code/src/main.ts2
-rw-r--r--editors/code/src/run.ts57
3 files changed, 60 insertions, 3 deletions
diff --git a/editors/code/src/commands.ts b/editors/code/src/commands.ts
index 3ac1a933d9..25b30013fa 100644
--- a/editors/code/src/commands.ts
+++ b/editors/code/src/commands.ts
@@ -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";
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 5e50073069..996298524f 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -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 },
diff --git a/editors/code/src/run.ts b/editors/code/src/run.ts
index 40027cc7c8..95166c427b 100644
--- a/editors/code/src/run.ts
+++ b/editors/code/src/run.ts
@@ -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";
@@ -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;