Skip to content

Commit fee0a9f

Browse files
committed
"rust-analyzer.newDebugConfig" command
1 parent 155f060 commit fee0a9f

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

editors/code/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@
125125
"title": "Debug",
126126
"category": "Rust Analyzer"
127127
},
128+
{
129+
"command": "rust-analyzer.newDebugConfig",
130+
"title": "Generate launch configuration",
131+
"category": "Rust Analyzer"
132+
},
128133
{
129134
"command": "rust-analyzer.analyzerStatus",
130135
"title": "Status",

editors/code/src/commands/runnables.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as lc from 'vscode-languageclient';
33
import * as ra from '../rust-analyzer-api';
44

55
import { Ctx, Cmd } from '../ctx';
6-
import { startDebugSession } from '../debug';
6+
import { startDebugSession, getDebugConfiguration } from '../debug';
77

88
async function selectRunnable(ctx: Ctx, prevRunnable: RunnableQuickPick | undefined): Promise<RunnableQuickPick | undefined> {
99
const editor = ctx.activeRustEditor;
@@ -86,6 +86,34 @@ export function debugSingle(ctx: Ctx): Cmd {
8686
};
8787
}
8888

89+
export function newDebugConfig(ctx: Ctx): Cmd {
90+
return async () => {
91+
const scope = ctx.activeRustEditor?.document.uri;
92+
if (!scope) return;
93+
94+
const item = await selectRunnable(ctx, undefined);
95+
if (!item) return;
96+
97+
const debugConfig = await getDebugConfiguration(ctx, item.runnable);
98+
if (!debugConfig) return;
99+
100+
const wsLaunchSection = vscode.workspace.getConfiguration("launch", scope);
101+
const configurations = wsLaunchSection.get<any[]>("configurations") || [];
102+
103+
const index = configurations.findIndex(c => c.name === debugConfig.name);
104+
if (index !== -1) {
105+
const answer = await vscode.window.showErrorMessage(`Launch configuration '${debugConfig.name}' already exists!`, 'Cancel', 'Update');
106+
if (answer === "Cancel") return;
107+
108+
configurations[index] = debugConfig;
109+
} else {
110+
configurations.push(debugConfig);
111+
}
112+
113+
await wsLaunchSection.update("configurations", configurations);
114+
};
115+
}
116+
89117
class RunnableQuickPick implements vscode.QuickPickItem {
90118
public label: string;
91119
public description?: string | undefined;

editors/code/src/debug.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ export async function getDebugConfiguration(ctx: Ctx, config: ra.Runnable): Prom
5757
debugEngine = vscode.extensions.getExtension(engineId);
5858
if (debugEngine) break;
5959
}
60-
}
61-
else {
60+
} else {
6261
debugEngine = vscode.extensions.getExtension(debugOptions.engine);
6362
}
6463

editors/code/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ export async function activate(context: vscode.ExtensionContext) {
7878
ctx.registerCommand('expandMacro', commands.expandMacro);
7979
ctx.registerCommand('run', commands.run);
8080
ctx.registerCommand('debug', commands.debug);
81+
ctx.registerCommand('newDebugConfig', commands.newDebugConfig);
8182

8283
defaultOnEnter.dispose();
8384
ctx.registerCommand('onEnter', commands.onEnter);

0 commit comments

Comments
 (0)