Skip to content

Commit 7955ac4

Browse files
authored
Merge pull request microsoft#209080 from microsoft/merogge/first-code
add run/insert first code block actions when there are multiple
2 parents e2dc70e + 703a1c3 commit 7955ac4

File tree

4 files changed

+83
-7
lines changed

4 files changed

+83
-7
lines changed

src/vs/workbench/contrib/terminalContrib/chat/browser/terminalChat.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ export const enum TerminalChatCommandId {
1919
FeedbackUnhelpful = 'workbench.action.terminal.chat.feedbackUnhelpful',
2020
FeedbackReportIssue = 'workbench.action.terminal.chat.feedbackReportIssue',
2121
RunCommand = 'workbench.action.terminal.chat.runCommand',
22+
RunFirstCommand = 'workbench.action.terminal.chat.runFirstCommand',
2223
InsertCommand = 'workbench.action.terminal.chat.insertCommand',
24+
InsertFirstCommand = 'workbench.action.terminal.chat.insertFirstCommand',
2325
ViewInChat = 'workbench.action.terminal.chat.viewInChat',
2426
PreviousFromHistory = 'workbench.action.terminal.chat.previousFromHistory',
2527
NextFromHistory = 'workbench.action.terminal.chat.nextFromHistory',
@@ -39,6 +41,7 @@ export const enum TerminalChatContextKeyStrings {
3941
ChatAgentRegistered = 'terminalChatAgentRegistered',
4042
ChatResponseEditorFocused = 'terminalChatResponseEditorFocused',
4143
ChatResponseContainsCodeBlock = 'terminalChatResponseContainsCodeBlock',
44+
ChatResponseContainsMultipleCodeBlocks = 'terminalChatResponseContainsMultipleCodeBlocks',
4245
ChatResponseSupportsIssueReporting = 'terminalChatResponseSupportsIssueReporting',
4346
ChatSessionResponseVote = 'terminalChatSessionResponseVote',
4447
}
@@ -61,9 +64,12 @@ export namespace TerminalChatContextKeys {
6164
/** Whether the terminal chat agent has been registered */
6265
export const agentRegistered = new RawContextKey<boolean>(TerminalChatContextKeyStrings.ChatAgentRegistered, false, localize('chatAgentRegisteredContextKey', "Whether the terminal chat agent has been registered."));
6366

64-
/** The type of chat response, if any */
67+
/** The chat response contains at least one code block */
6568
export const responseContainsCodeBlock = new RawContextKey<boolean>(TerminalChatContextKeyStrings.ChatResponseContainsCodeBlock, false, localize('chatResponseContainsCodeBlockContextKey', "Whether the chat response contains a code block."));
6669

70+
/** The chat response contains multiple code blocks */
71+
export const responseContainsMultipleCodeBlocks = new RawContextKey<boolean>(TerminalChatContextKeyStrings.ChatResponseContainsMultipleCodeBlocks, false, localize('chatResponseContainsMultipleCodeBlocksContextKey', "Whether the chat response contains multiple code blocks."));
72+
6773
/** Whether the response supports issue reporting */
6874
export const responseSupportsIssueReporting = new RawContextKey<boolean>(TerminalChatContextKeyStrings.ChatResponseSupportsIssueReporting, false, localize('chatResponseSupportsIssueReportingContextKey', "Whether the response supports issue reporting"));
6975

src/vs/workbench/contrib/terminalContrib/chat/browser/terminalChatActions.ts

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,40 @@ registerActiveXtermAction({
156156
ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
157157
TerminalChatContextKeys.requestActive.negate(),
158158
TerminalChatContextKeys.agentRegistered,
159-
TerminalChatContextKeys.responseContainsCodeBlock
159+
TerminalChatContextKeys.responseContainsCodeBlock,
160+
TerminalChatContextKeys.responseContainsMultipleCodeBlocks.negate()
161+
),
162+
icon: Codicon.play,
163+
keybinding: {
164+
when: TerminalChatContextKeys.requestActive.negate(),
165+
weight: KeybindingWeight.WorkbenchContrib,
166+
primary: KeyMod.CtrlCmd | KeyCode.Enter,
167+
},
168+
menu: {
169+
id: MENU_TERMINAL_CHAT_WIDGET_STATUS,
170+
group: '0_main',
171+
order: 0,
172+
when: ContextKeyExpr.and(TerminalChatContextKeys.responseContainsCodeBlock, TerminalChatContextKeys.responseContainsMultipleCodeBlocks.negate(), TerminalChatContextKeys.requestActive.negate())
173+
},
174+
run: (_xterm, _accessor, activeInstance) => {
175+
if (isDetachedTerminalInstance(activeInstance)) {
176+
return;
177+
}
178+
const contr = TerminalChatController.activeChatWidget || TerminalChatController.get(activeInstance);
179+
contr?.acceptCommand(true);
180+
}
181+
});
182+
183+
registerActiveXtermAction({
184+
id: TerminalChatCommandId.RunFirstCommand,
185+
title: localize2('runFirstCommand', 'Run First Chat Command'),
186+
shortTitle: localize2('runFirst', 'Run First'),
187+
precondition: ContextKeyExpr.and(
188+
ContextKeyExpr.has(`config.${TerminalSettingId.ExperimentalInlineChat}`),
189+
ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
190+
TerminalChatContextKeys.requestActive.negate(),
191+
TerminalChatContextKeys.agentRegistered,
192+
TerminalChatContextKeys.responseContainsMultipleCodeBlocks
160193
),
161194
icon: Codicon.play,
162195
keybinding: {
@@ -168,7 +201,7 @@ registerActiveXtermAction({
168201
id: MENU_TERMINAL_CHAT_WIDGET_STATUS,
169202
group: '0_main',
170203
order: 0,
171-
when: ContextKeyExpr.and(TerminalChatContextKeys.responseContainsCodeBlock, TerminalChatContextKeys.requestActive.negate())
204+
when: ContextKeyExpr.and(TerminalChatContextKeys.responseContainsMultipleCodeBlocks, TerminalChatContextKeys.requestActive.negate())
172205
},
173206
run: (_xterm, _accessor, activeInstance) => {
174207
if (isDetachedTerminalInstance(activeInstance)) {
@@ -188,7 +221,39 @@ registerActiveXtermAction({
188221
ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
189222
TerminalChatContextKeys.requestActive.negate(),
190223
TerminalChatContextKeys.agentRegistered,
191-
TerminalChatContextKeys.responseContainsCodeBlock
224+
TerminalChatContextKeys.responseContainsCodeBlock,
225+
TerminalChatContextKeys.responseContainsMultipleCodeBlocks.negate()
226+
),
227+
keybinding: {
228+
when: TerminalChatContextKeys.requestActive.negate(),
229+
weight: KeybindingWeight.WorkbenchContrib,
230+
primary: KeyMod.Alt | KeyCode.Enter,
231+
},
232+
menu: {
233+
id: MENU_TERMINAL_CHAT_WIDGET_STATUS,
234+
group: '0_main',
235+
order: 1,
236+
when: ContextKeyExpr.and(TerminalChatContextKeys.responseContainsCodeBlock, TerminalChatContextKeys.responseContainsMultipleCodeBlocks.negate(), TerminalChatContextKeys.requestActive.negate())
237+
},
238+
run: (_xterm, _accessor, activeInstance) => {
239+
if (isDetachedTerminalInstance(activeInstance)) {
240+
return;
241+
}
242+
const contr = TerminalChatController.activeChatWidget || TerminalChatController.get(activeInstance);
243+
contr?.acceptCommand(false);
244+
}
245+
});
246+
247+
registerActiveXtermAction({
248+
id: TerminalChatCommandId.InsertFirstCommand,
249+
title: localize2('insertFirstCommand', 'Insert First Chat Command'),
250+
shortTitle: localize2('insertFirst', 'Insert First'),
251+
precondition: ContextKeyExpr.and(
252+
ContextKeyExpr.has(`config.${TerminalSettingId.ExperimentalInlineChat}`),
253+
ContextKeyExpr.or(TerminalContextKeys.processSupported, TerminalContextKeys.terminalHasBeenCreated),
254+
TerminalChatContextKeys.requestActive.negate(),
255+
TerminalChatContextKeys.agentRegistered,
256+
TerminalChatContextKeys.responseContainsMultipleCodeBlocks
192257
),
193258
keybinding: {
194259
when: TerminalChatContextKeys.requestActive.negate(),
@@ -199,7 +264,7 @@ registerActiveXtermAction({
199264
id: MENU_TERMINAL_CHAT_WIDGET_STATUS,
200265
group: '0_main',
201266
order: 1,
202-
when: ContextKeyExpr.and(TerminalChatContextKeys.responseContainsCodeBlock, TerminalChatContextKeys.requestActive.negate())
267+
when: ContextKeyExpr.and(TerminalChatContextKeys.responseContainsMultipleCodeBlocks, TerminalChatContextKeys.requestActive.negate())
203268
},
204269
run: (_xterm, _accessor, activeInstance) => {
205270
if (isDetachedTerminalInstance(activeInstance)) {

src/vs/workbench/contrib/terminalContrib/chat/browser/terminalChatController.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ export class TerminalChatController extends Disposable implements ITerminalContr
6363
private readonly _requestActiveContextKey: IContextKey<boolean>;
6464
private readonly _terminalAgentRegisteredContextKey: IContextKey<boolean>;
6565
private readonly _responseContainsCodeBlockContextKey: IContextKey<boolean>;
66+
private readonly _responseContainsMulitpleCodeBlocksContextKey: IContextKey<boolean>;
6667
private readonly _responseSupportsIssueReportingContextKey: IContextKey<boolean>;
6768
private readonly _sessionResponseVoteContextKey: IContextKey<string | undefined>;
6869

@@ -103,6 +104,7 @@ export class TerminalChatController extends Disposable implements ITerminalContr
103104
this._requestActiveContextKey = TerminalChatContextKeys.requestActive.bindTo(this._contextKeyService);
104105
this._terminalAgentRegisteredContextKey = TerminalChatContextKeys.agentRegistered.bindTo(this._contextKeyService);
105106
this._responseContainsCodeBlockContextKey = TerminalChatContextKeys.responseContainsCodeBlock.bindTo(this._contextKeyService);
107+
this._responseContainsMulitpleCodeBlocksContextKey = TerminalChatContextKeys.responseContainsMultipleCodeBlocks.bindTo(this._contextKeyService);
106108
this._responseSupportsIssueReportingContextKey = TerminalChatContextKeys.responseSupportsIssueReporting.bindTo(this._contextKeyService);
107109
this._sessionResponseVoteContextKey = TerminalChatContextKeys.sessionResponseVote.bindTo(this._contextKeyService);
108110

@@ -327,7 +329,10 @@ export class TerminalChatController extends Disposable implements ITerminalContr
327329
this._chatAccessibilityService.acceptResponse(responseContent, accessibilityRequestId);
328330
const containsCode = responseContent.includes('```');
329331
this._chatWidget?.value.inlineChatWidget.updateChatMessage({ message: new MarkdownString(responseContent), requestId: this._currentRequest.id, providerId: 'terminal' }, false, containsCode);
330-
this._responseContainsCodeBlockContextKey.set(containsCode);
332+
const firstCodeBlock = await this.chatWidget?.inlineChatWidget.getCodeBlockInfo(0);
333+
const secondCodeBlock = await this.chatWidget?.inlineChatWidget.getCodeBlockInfo(1);
334+
this._responseContainsCodeBlockContextKey.set(!!firstCodeBlock);
335+
this._responseContainsMulitpleCodeBlocksContextKey.set(!!secondCodeBlock);
331336
this._chatWidget?.value.inlineChatWidget.updateToolbar(true);
332337
}
333338
const supportIssueReporting = this._currentRequest?.response?.agent?.metadata?.supportIssueReporting;

src/vs/workbench/contrib/terminalContrib/chat/browser/terminalChatWidget.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class TerminalChatWidget extends Disposable {
5757
menu: MENU_TERMINAL_CHAT_WIDGET_STATUS,
5858
options: {
5959
buttonConfigProvider: action => {
60-
if (action.id === TerminalChatCommandId.ViewInChat || action.id === TerminalChatCommandId.RunCommand) {
60+
if (action.id === TerminalChatCommandId.ViewInChat || action.id === TerminalChatCommandId.RunCommand || action.id === TerminalChatCommandId.RunFirstCommand) {
6161
return { isSecondary: false };
6262
} else {
6363
return { isSecondary: true };

0 commit comments

Comments
 (0)