Skip to content

Commit 480b18a

Browse files
committed
debug: apply polish to auto attach switcher
1 parent cda3fbe commit 480b18a

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

extensions/debug-auto-launch/src/extension.ts

+35-24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ const localize = nls.loadMessageBundle();
1111
const TEXT_ALWAYS = localize('status.text.auto.attach.always', 'Auto Attach: Always');
1212
const TEXT_SMART = localize('status.text.auto.attach.smart', 'Auto Attach: Smart');
1313
const TEXT_WITH_FLAG = localize('status.text.auto.attach.withFlag', 'Auto Attach: With Flag');
14+
const TEXT_STATE_LABEL = {
15+
[State.Disabled]: localize('debug.javascript.autoAttach.disabled.label', 'Disabled'),
16+
[State.Always]: localize('debug.javascript.autoAttach.always.label', 'Always'),
17+
[State.Smart]: localize('debug.javascript.autoAttach.smart.label', 'Smart'),
18+
[State.OnlyWithFlag]: localize(
19+
'debug.javascript.autoAttach.onlyWithFlag.label',
20+
'Only With Flag',
21+
),
22+
};
1423
const TEXT_STATE_DESCRIPTION = {
1524
[State.Disabled]: localize(
1625
'debug.javascript.autoAttach.disabled.description',
@@ -29,6 +38,8 @@ const TEXT_STATE_DESCRIPTION = {
2938
'Only auto attach when the `--inspect` flag is given',
3039
),
3140
};
41+
const TEXT_TOGGLE_WORKSPACE = localize('scope.workspace', 'Toggle auto attach in this workspace');
42+
const TEXT_TOGGLE_GLOBAL = localize('scope.global', 'Toggle auto attach on this machine');
3243

3344
const TOGGLE_COMMAND = 'extension.node-debug.toggleAutoAttach';
3445
const STORAGE_IPC = 'jsDebugIpcState';
@@ -82,11 +93,6 @@ export async function deactivate(): Promise<void> {
8293
await destroyAttachServer();
8394
}
8495

85-
type StatePickItem =
86-
| (vscode.QuickPickItem & { state: State })
87-
| (vscode.QuickPickItem & { scope: vscode.ConfigurationTarget })
88-
| (vscode.QuickPickItem & { type: 'separator' });
89-
9096
function getDefaultScope(info: ReturnType<vscode.WorkspaceConfiguration['inspect']>) {
9197
if (!info) {
9298
return vscode.ConfigurationTarget.Global;
@@ -101,39 +107,44 @@ function getDefaultScope(info: ReturnType<vscode.WorkspaceConfiguration['inspect
101107
return vscode.ConfigurationTarget.Global;
102108
}
103109

110+
type PickResult = { state: State } | { scope: vscode.ConfigurationTarget } | undefined;
111+
104112
async function toggleAutoAttachSetting(scope?: vscode.ConfigurationTarget): Promise<void> {
105113
const section = vscode.workspace.getConfiguration(SETTING_SECTION);
106114
scope = scope || getDefaultScope(section.inspect(SETTING_STATE));
107115

108-
const stateItems = [State.Always, State.Smart, State.OnlyWithFlag, State.Disabled].map(state => ({
116+
const isGlobalScope = scope === vscode.ConfigurationTarget.Global;
117+
const quickPick = vscode.window.createQuickPick<vscode.QuickPickItem & { state: State }>();
118+
const current = readCurrentState();
119+
120+
quickPick.items = [State.Always, State.Smart, State.OnlyWithFlag, State.Disabled].map(state => ({
109121
state,
110-
label: state.slice(0, 1).toUpperCase() + state.slice(1),
122+
label: TEXT_STATE_LABEL[state],
111123
description: TEXT_STATE_DESCRIPTION[state],
112124
alwaysShow: true,
113125
}));
114126

115-
const scopeItem =
116-
scope === vscode.ConfigurationTarget.Global
117-
? {
118-
label: localize('scope.workspace', 'Toggle in this workspace $(arrow-right)'),
119-
scope: vscode.ConfigurationTarget.Workspace,
120-
}
121-
: {
122-
label: localize('scope.global', 'Toggle for this machine $(arrow-right)'),
123-
scope: vscode.ConfigurationTarget.Global,
124-
};
125-
126-
const quickPick = vscode.window.createQuickPick<StatePickItem>();
127-
// todo: have a separator here, see https://github.com/microsoft/vscode/issues/74967
128-
quickPick.items = [...stateItems, scopeItem];
127+
quickPick.activeItems = quickPick.items.filter(i => i.state === current);
128+
quickPick.title = isGlobalScope ? TEXT_TOGGLE_GLOBAL : TEXT_TOGGLE_WORKSPACE;
129+
quickPick.buttons = [
130+
{
131+
iconPath: new vscode.ThemeIcon(isGlobalScope ? 'folder' : 'globe'),
132+
tooltip: isGlobalScope ? TEXT_TOGGLE_WORKSPACE : TEXT_TOGGLE_GLOBAL,
133+
},
134+
];
129135

130136
quickPick.show();
131-
const current = readCurrentState();
132-
quickPick.activeItems = stateItems.filter(i => i.state === current);
133137

134-
const result = await new Promise<StatePickItem | undefined>(resolve => {
138+
const result = await new Promise<PickResult>(resolve => {
135139
quickPick.onDidAccept(() => resolve(quickPick.selectedItems[0]));
136140
quickPick.onDidHide(() => resolve());
141+
quickPick.onDidTriggerButton(() => {
142+
resolve({
143+
scope: isGlobalScope
144+
? vscode.ConfigurationTarget.Workspace
145+
: vscode.ConfigurationTarget.Global,
146+
});
147+
});
137148
});
138149

139150
quickPick.dispose();

0 commit comments

Comments
 (0)