Skip to content

Commit 555257b

Browse files
committed
refactor the ask-to-select-prompt dialog
1 parent a51c2a3 commit 555257b

File tree

4 files changed

+45
-29
lines changed

4 files changed

+45
-29
lines changed

src/vs/workbench/contrib/chat/browser/actions/chatAttachPromptAction/chatAttachPromptAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export const ATTACH_PROMPT_ACTION_ID = 'workbench.action.chat.attach.prompt';
2424
* Options for the {@link AttachPromptAction} action.
2525
*/
2626
export interface IChatAttachPromptActionOptions extends Pick<
27-
ISelectPromptOptions, 'resource' | 'widget'
27+
ISelectPromptOptions, 'resource' | 'widget' | 'viewsService'
2828
> { }
2929

3030
/**

src/vs/workbench/contrib/chat/browser/actions/chatAttachPromptAction/dialogs/askToSelectPrompt.ts

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const askToSelectPrompt = async (
102102
}
103103

104104
// otherwise show the prompt file selection dialog
105-
const { viewsService, openerService } = options;
105+
const { openerService } = options;
106106

107107
const quickPick = quickInputService.createQuickPick<WithUriValue<IQuickPickItem>>();
108108
quickPick.activeItems = activeItem ? [activeItem] : [];
@@ -126,36 +126,16 @@ export const askToSelectPrompt = async (
126126
});
127127

128128
disposables.add(quickPick.onDidAccept(async (event) => {
129+
const { selectedItems } = quickPick;
129130
const { alt, ctrlCmd } = quickPick.keyMods;
130131

131-
// TODO: @lego - refactor?
132-
// if `cmd`/`ctrl` key was pressed, open the selected prompt file(s)
132+
// if `super` key was pressed, open the selected prompt file(s)
133133
if (ctrlCmd) {
134-
for (const selectedItem of quickPick.selectedItems) {
135-
await openerService.open(selectedItem.value);
136-
}
137-
138-
// if user submitted their selection, close the dialog
139-
if (!event.inBackground) {
140-
disposables.dispose();
141-
}
142-
143-
return;
134+
return await openFiles(selectedItems, openerService);
144135
}
145136

146137
// otherwise attach the selected prompt to a chat input
147-
lastActiveWidget = await getChatWidgetObject(
148-
options,
149-
alt,
150-
viewsService,
151-
);
152-
153-
for (const selectedItem of quickPick.selectedItems) {
154-
lastActiveWidget
155-
.attachmentModel
156-
.promptInstructions
157-
.add(selectedItem.value);
158-
}
138+
lastActiveWidget = await attachFiles(selectedItems, options, alt);
159139

160140
// if user submitted their selection, close the dialog
161141
if (!event.inBackground) {
@@ -216,18 +196,51 @@ const createPlaceholderText = (options: ISelectPromptOptions): string => {
216196
return text;
217197
};
218198

199+
/**
200+
* Opens provided files in the editor.
201+
*/
202+
const openFiles = async (
203+
files: readonly WithUriValue<IQuickPickItem>[],
204+
openerService: IOpenerService,
205+
) => {
206+
for (const file of files) {
207+
await openerService.open(file.value);
208+
}
209+
};
210+
211+
/**
212+
* Attaches provided files to a chat input.
213+
*/
214+
const attachFiles = async (
215+
files: readonly WithUriValue<IQuickPickItem>[],
216+
options: ISelectPromptOptions,
217+
altOption: boolean,
218+
): Promise<IChatWidget> => {
219+
const widget = await getChatWidgetObject(options, altOption);
220+
221+
for (const file of files) {
222+
widget
223+
.attachmentModel
224+
.promptInstructions
225+
.add(file.value);
226+
}
227+
228+
return widget;
229+
};
230+
219231
/**
220232
* Gets a chat widget based on the provided {@link IChatAttachPromptActionOptions.widget widget}
221233
* reference. If no widget reference is provided, the function will reveal a `chat panel` by default
222234
* (either a last focused, or a new one), but if the {@link altOption} is set to `true`, a `chat edits`
223235
* panel will be revealed instead (likewise either a last focused, or a new one).
236+
*
237+
* @throws if failed to reveal a chat widget.
224238
*/
225239
const getChatWidgetObject = async (
226240
options: IChatAttachPromptActionOptions,
227241
altOption: boolean,
228-
viewsService: IViewsService,
229242
): Promise<IChatWidget> => {
230-
const { widget } = options;
243+
const { widget, viewsService } = options;
231244

232245
// if no widget reference is present, the command was triggered from outside of
233246
// an active chat input, so we reveal a chat widget window based on the `alt`

src/vs/workbench/contrib/chat/browser/actions/chatContextActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export class AttachContextAction extends Action2 {
615615
toAttach.push(convertBufferToScreenshotVariable(blob));
616616
}
617617
} else if (isPromptInstructionsQuickPickItem(pick)) {
618-
const options: IChatAttachPromptActionOptions = { widget };
618+
const options: IChatAttachPromptActionOptions = { widget, viewsService };
619619
await commandService.executeCommand(ATTACH_PROMPT_ACTION_ID, options);
620620
} else {
621621
// Anything else is an attachment

src/vs/workbench/contrib/chat/browser/promptSyntax/contributions/usePromptCommand.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { URI } from '../../../../../../base/common/uri.js';
88
import { CHAT_CATEGORY } from '../../actions/chatActions.js';
99
import { IChatWidget, IChatWidgetService } from '../../chat.js';
1010
import { KeyMod, KeyCode } from '../../../../../../base/common/keyCodes.js';
11+
import { IViewsService } from '../../../../../services/views/common/viewsService.js';
1112
import { isPromptFile } from '../../../../../../platform/prompts/common/constants.js';
1213
import { IEditorService } from '../../../../../services/editor/common/editorService.js';
1314
import { ICommandService } from '../../../../../../platform/commands/common/commands.js';
@@ -51,10 +52,12 @@ const command = async (
5152
accessor: ServicesAccessor,
5253
): Promise<void> => {
5354
const commandService = accessor.get(ICommandService);
55+
const viewsService = accessor.get(IViewsService);
5456

5557
const options: IChatAttachPromptActionOptions = {
5658
resource: getActivePromptUri(accessor),
5759
widget: getFocusedChatWidget(accessor),
60+
viewsService,
5861
};
5962

6063
await commandService.executeCommand(ATTACH_PROMPT_ACTION_ID, options);

0 commit comments

Comments
 (0)