Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow ➡️ to add chat context in background #213078

Merged
merged 1 commit into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you use the 'accept' callback on the item to do the same thing? I don't follow

Copy link
Collaborator Author

@joyceerhl joyceerhl May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the consumer of the quick access from chat context action land, we don't control the items in the picker, they are populated from the anything quick access in this case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could overwrite the accept handler for all items in the anything quick access layer, but this feels more explicit

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.
*/
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These got misaligned

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];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could args be undefined here, thus this would throw?

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()}` : ''
})));
}
}
}
Loading