Skip to content

Commit

Permalink
feat: allow ➡️ to add chat context in background (#213078)
Browse files Browse the repository at this point in the history
  • Loading branch information
joyceerhl authored May 20, 2024
1 parent b973558 commit c00f279
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
8 changes: 8 additions & 0 deletions src/vs/platform/quickinput/browser/pickerQuickAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,14 @@ export abstract class PickerQuickAccessProvider<T extends IPickerQuickAccessItem

// Accept the pick on accept and hide picker
disposables.add(picker.onDidAccept(event => {
if (runOptions?.handleAccept) {
if (!event.inBackground) {
picker.hide(); // hide picker unless we accept in background
}
runOptions.handleAccept?.(picker.activeItems[0]);
return;
}

const [item] = picker.selectedItems;
if (typeof item?.accept === 'function') {
if (!event.inBackground) {
Expand Down
10 changes: 8 additions & 2 deletions src/vs/platform/quickinput/common/quickAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ import { Registry } from 'vs/platform/registry/common/platform';
export interface IQuickAccessProviderRunOptions {
readonly from?: string;
readonly placeholder?: string;
/**
* A handler to invoke when an item is accepted for
* this particular showing of the quick access.
* @param item The item that was accepted.
*/
readonly handleAccept?: (item: IQuickPickItem) => void;
}

/**
Expand Down Expand Up @@ -54,7 +60,7 @@ export interface IQuickAccessOptions {
/**
* Provider specific options for this particular showing of the
* quick access.
*/
*/
readonly providerOptions?: IQuickAccessProviderRunOptions;

/**
Expand All @@ -65,7 +71,7 @@ export interface IQuickAccessOptions {

/**
* A placeholder to use for this particular showing of the quick access.
*/
*/
readonly placeholder?: string;
}

Expand Down
36 changes: 21 additions & 15 deletions src/vs/workbench/contrib/chat/browser/actions/chatContextActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { localize, localize2 } from 'vs/nls';
import { Action2, MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { AnythingQuickAccessProviderRunOptions } from 'vs/platform/quickinput/common/quickAccess';
import { IQuickInputService, QuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { IQuickInputService, IQuickPickItem, QuickPickItem } from 'vs/platform/quickinput/common/quickInput';
import { CHAT_CATEGORY } from 'vs/workbench/contrib/chat/browser/actions/chatActions';
import { IChatWidget, IChatWidgetService } from 'vs/workbench/contrib/chat/browser/chat';
import { SelectAndInsertFileAction } from 'vs/workbench/contrib/chat/browser/contrib/chatDynamicVariables';
Expand Down Expand Up @@ -56,10 +56,26 @@ class AttachContextAction extends Action2 {
});
}

private _attachContext(widget: IChatWidget, ...picks: IQuickPickItem[]) {
widget?.attachContext(...picks.map((p) => ({
fullName: p.label,
icon: 'icon' in p && ThemeIcon.isThemeIcon(p.icon) ? p.icon : undefined,
name: 'name' in p && typeof p.name === 'string' ? p.name : p.label,
value: 'resource' in p && URI.isUri(p.resource) ? p.resource : undefined,
id: 'id' in p && typeof p.id === 'string' ? p.id :
'resource' in p && URI.isUri(p.resource) ? `${SelectAndInsertFileAction.Name}:${p.resource.toString()}` : ''
})));
}

override async run(accessor: ServicesAccessor, ...args: any[]): Promise<void> {
const quickInputService = accessor.get(IQuickInputService);
const chatVariablesService = accessor.get(IChatVariablesService);
const widgetService = accessor.get(IChatWidgetService);
const context: { widget?: IChatWidget } | undefined = args[0];
const widget = context?.widget ?? widgetService.lastFocusedWidget;
if (!widget) {
return;
}

const quickPickItems: (QuickPickItem & { name?: string; icon?: ThemeIcon })[] = [];
for (const variable of chatVariablesService.getVariables()) {
Expand All @@ -72,10 +88,13 @@ class AttachContextAction extends Action2 {
quickPickItems.push(SelectAndInsertFileAction.Item, { type: 'separator' });
}

const picks = await quickInputService.quickAccess.pick('', {
quickInputService.quickAccess.show('', {
enabledProviderPrefixes: [AnythingQuickAccessProvider.PREFIX],
placeholder: localize('chatContext.attach.placeholder', 'Search attachments'),
providerOptions: <AnythingQuickAccessProviderRunOptions>{
handleAccept: (item: IQuickPickItem) => {
this._attachContext(widget, item);
},
additionPicks: quickPickItems,
includeSymbols: false,
filter: (item) => {
Expand All @@ -87,18 +106,5 @@ class AttachContextAction extends Action2 {
}
});

if (picks?.length) {
const context: { widget?: IChatWidget } | undefined = args[0];

const widget = context?.widget ?? widgetService.lastFocusedWidget;
widget?.attachContext(...picks.map((p) => ({
fullName: p.label,
icon: 'icon' in p && ThemeIcon.isThemeIcon(p.icon) ? p.icon : undefined,
name: 'name' in p && typeof p.name === 'string' ? p.name : p.label,
value: 'resource' in p && URI.isUri(p.resource) ? p.resource : undefined,
id: 'id' in p && typeof p.id === 'string' ? p.id :
'resource' in p && URI.isUri(p.resource) ? `${SelectAndInsertFileAction.Name}:${p.resource.toString()}` : ''
})));
}
}
}

0 comments on commit c00f279

Please sign in to comment.