Skip to content

Commit 406618d

Browse files
authored
Merge pull request microsoft#187713 from microsoft/aiday/toggleDiffAsSetting
Placing option `Toggle Diff` from inline chat into settings
2 parents dc142e4 + 9dc7dd9 commit 406618d

File tree

5 files changed

+62
-45
lines changed

5 files changed

+62
-45
lines changed

src/vs/workbench/contrib/inlineChat/browser/inlineChatActions.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import { EditorAction2 } from 'vs/editor/browser/editorExtensions';
1010
import { EmbeddedCodeEditorWidget, EmbeddedDiffEditorWidget } from 'vs/editor/browser/widget/embeddedCodeEditorWidget';
1111
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
1212
import { InlineChatController, InlineChatRunOptions } from 'vs/workbench/contrib/inlineChat/browser/inlineChatController';
13-
import { CTX_INLINE_CHAT_FOCUSED, CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST, CTX_INLINE_CHAT_HAS_PROVIDER, CTX_INLINE_CHAT_INNER_CURSOR_FIRST, CTX_INLINE_CHAT_INNER_CURSOR_LAST, CTX_INLINE_CHAT_EMPTY, CTX_INLINE_CHAT_OUTER_CURSOR_POSITION, CTX_INLINE_CHAT_VISIBLE, MENU_INLINE_CHAT_WIDGET, MENU_INLINE_CHAT_WIDGET_DISCARD, MENU_INLINE_CHAT_WIDGET_STATUS, CTX_INLINE_CHAT_LAST_FEEDBACK, CTX_INLINE_CHAT_SHOWING_DIFF, CTX_INLINE_CHAT_EDIT_MODE, EditMode, CTX_INLINE_CHAT_LAST_RESPONSE_TYPE, MENU_INLINE_CHAT_WIDGET_MARKDOWN_MESSAGE, CTX_INLINE_CHAT_MESSAGE_CROP_STATE, CTX_INLINE_CHAT_DOCUMENT_CHANGED, CTX_INLINE_CHAT_DID_EDIT, CTX_INLINE_CHAT_HAS_STASHED_SESSION, MENU_INLINE_CHAT_WIDGET_FEEDBACK, ACTION_ACCEPT_CHANGES, ACTION_REGENERATE_RESPONSE, InlineChatResponseType, CTX_INLINE_CHAT_RESPONSE_TYPES, InlineChateResponseTypes, ACTION_VIEW_IN_CHAT, CTX_INLINE_CHAT_USER_DID_EDIT } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
13+
import { CTX_INLINE_CHAT_FOCUSED, CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST, CTX_INLINE_CHAT_HAS_PROVIDER, CTX_INLINE_CHAT_INNER_CURSOR_FIRST, CTX_INLINE_CHAT_INNER_CURSOR_LAST, CTX_INLINE_CHAT_EMPTY, CTX_INLINE_CHAT_OUTER_CURSOR_POSITION, CTX_INLINE_CHAT_VISIBLE, MENU_INLINE_CHAT_WIDGET, MENU_INLINE_CHAT_WIDGET_DISCARD, MENU_INLINE_CHAT_WIDGET_STATUS, CTX_INLINE_CHAT_LAST_FEEDBACK, CTX_INLINE_CHAT_EDIT_MODE, EditMode, CTX_INLINE_CHAT_LAST_RESPONSE_TYPE, MENU_INLINE_CHAT_WIDGET_MARKDOWN_MESSAGE, CTX_INLINE_CHAT_MESSAGE_CROP_STATE, CTX_INLINE_CHAT_DOCUMENT_CHANGED, CTX_INLINE_CHAT_DID_EDIT, CTX_INLINE_CHAT_HAS_STASHED_SESSION, MENU_INLINE_CHAT_WIDGET_FEEDBACK, ACTION_ACCEPT_CHANGES, ACTION_REGENERATE_RESPONSE, InlineChatResponseType, CTX_INLINE_CHAT_RESPONSE_TYPES, InlineChateResponseTypes, ACTION_VIEW_IN_CHAT, CTX_INLINE_CHAT_USER_DID_EDIT, MENU_INLINE_CHAT_WIDGET_TOGGLE } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
1414
import { localize } from 'vs/nls';
15-
import { IAction2Options, MenuRegistry } from 'vs/platform/actions/common/actions';
15+
import { IAction2Options, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
1616
import { IClipboardService } from 'vs/platform/clipboard/common/clipboardService';
1717
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1818
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
@@ -30,6 +30,7 @@ import { AccessibilityHelpAction } from 'vs/workbench/contrib/accessibility/brow
3030
import { Disposable } from 'vs/base/common/lifecycle';
3131
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
3232
import { Position } from 'vs/editor/common/core/position';
33+
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
3334

3435
CommandsRegistry.registerCommandAlias('interactiveEditor.start', 'inlineChat.start');
3536

@@ -450,21 +451,28 @@ export class ToggleInlineDiff extends AbstractInlineChatAction {
450451
constructor() {
451452
super({
452453
id: 'inlineChat.toggleDiff',
453-
title: localize('toggleDiff', 'Toggle Diff'),
454-
icon: Codicon.diff,
455-
precondition: ContextKeyExpr.and(CTX_INLINE_CHAT_VISIBLE, CTX_INLINE_CHAT_DID_EDIT),
456-
toggled: { condition: CTX_INLINE_CHAT_SHOWING_DIFF, title: localize('toggleDiff2', "Show Inline Diff") },
457-
menu: {
458-
id: MENU_INLINE_CHAT_WIDGET_DISCARD,
459-
when: CTX_INLINE_CHAT_EDIT_MODE.notEqualsTo(EditMode.Preview),
460-
group: '1_config',
461-
order: 9
462-
}
454+
title: {
455+
original: 'Show Diff',
456+
value: localize('showDiff', 'Show Diff'),
457+
mnemonicTitle: localize({ key: 'miShowDiff', comment: ['&& denotes a mnemonic'] }, "&&Show Diff"),
458+
},
459+
toggled: {
460+
condition: ContextKeyExpr.equals('config.inlineChat.showDiff', true),
461+
title: localize('showDiff2', "Show Diff"),
462+
mnemonicTitle: localize({ key: 'miShowDiff2', comment: ['&& denotes a mnemonic'] }, "&&Show Diff")
463+
},
464+
precondition: ContextKeyExpr.notEquals('config.inlineChat.mode', 'preview'),
465+
menu: [
466+
{ id: MenuId.CommandPalette },
467+
{ id: MENU_INLINE_CHAT_WIDGET_TOGGLE }
468+
]
463469
});
464470
}
465471

466-
override runInlineChatCommand(_accessor: ServicesAccessor, ctrl: InlineChatController): void {
467-
ctrl.toggleDiff();
472+
override runInlineChatCommand(accessor: ServicesAccessor, _ctrl: InlineChatController): void {
473+
const configurationService = accessor.get(IConfigurationService);
474+
const newValue = !configurationService.getValue('inlineChat.showDiff');
475+
configurationService.updateValue('inlineChat.showDiff', newValue);
468476
}
469477
}
470478

src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -751,10 +751,6 @@ export class InlineChatController implements IEditorContribution {
751751
}
752752
}
753753

754-
toggleDiff(): void {
755-
this._strategy?.toggleDiff();
756-
}
757-
758754
focus(): void {
759755
this._zone.value.widget.focus();
760756
}

src/vs/workbench/contrib/inlineChat/browser/inlineChatStrategies.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { Event } from 'vs/base/common/event';
77
import { Lazy } from 'vs/base/common/lazy';
8-
import { IDisposable } from 'vs/base/common/lifecycle';
8+
import { DisposableStore, IDisposable } from 'vs/base/common/lifecycle';
99
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
1010
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
1111
import { StableEditorScrollState } from 'vs/editor/browser/stableEditorScroll';
@@ -17,13 +17,14 @@ import { TextEdit } from 'vs/editor/common/languages';
1717
import { ICursorStateComputer, IModelDecorationOptions, IModelDeltaDecoration, ITextModel, IValidEditOperation } from 'vs/editor/common/model';
1818
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
1919
import { localize } from 'vs/nls';
20+
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2021
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
2122
import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
22-
import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage';
23+
import { IStorageService } from 'vs/platform/storage/common/storage';
2324
import { InlineChatFileCreatePreviewWidget, InlineChatLivePreviewWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatLivePreviewWidget';
2425
import { EditResponse, Session } from 'vs/workbench/contrib/inlineChat/browser/inlineChatSession';
2526
import { InlineChatWidget } from 'vs/workbench/contrib/inlineChat/browser/inlineChatWidget';
26-
import { CTX_INLINE_CHAT_SHOWING_DIFF, CTX_INLINE_CHAT_DOCUMENT_CHANGED } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
27+
import { CTX_INLINE_CHAT_DOCUMENT_CHANGED } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
2728
import { IEditorService, SIDE_GROUP } from 'vs/workbench/services/editor/common/editorService';
2829

2930
export abstract class EditModeStrategy {
@@ -42,8 +43,6 @@ export abstract class EditModeStrategy {
4243

4344
abstract renderChanges(response: EditResponse): Promise<void>;
4445

45-
abstract toggleDiff(): void;
46-
4746
abstract hasFocus(): boolean;
4847

4948
abstract getWidgetPosition(): Position | undefined;
@@ -137,10 +136,6 @@ export class PreviewStrategy extends EditModeStrategy {
137136
}
138137
}
139138

140-
toggleDiff(): void {
141-
// nothing to do
142-
}
143-
144139
getWidgetPosition(): Position | undefined {
145140
return;
146141
}
@@ -223,11 +218,11 @@ class InlineDiffDecorations {
223218

224219
export class LiveStrategy extends EditModeStrategy {
225220

226-
private static _inlineDiffStorageKey: string = 'interactiveEditor.storage.inlineDiff';
227221
protected _diffEnabled: boolean = false;
228222

229223
private readonly _inlineDiffDecorations: InlineDiffDecorations;
230-
private readonly _ctxShowingDiff: IContextKey<boolean>;
224+
private readonly _store: DisposableStore = new DisposableStore();
225+
231226
private _lastResponse?: EditResponse;
232227
private _editCount: number = 0;
233228

@@ -236,30 +231,29 @@ export class LiveStrategy extends EditModeStrategy {
236231
protected readonly _editor: ICodeEditor,
237232
protected readonly _widget: InlineChatWidget,
238233
@IContextKeyService contextKeyService: IContextKeyService,
234+
@IConfigurationService configService: IConfigurationService,
239235
@IStorageService protected _storageService: IStorageService,
240236
@IBulkEditService protected readonly _bulkEditService: IBulkEditService,
241237
@IEditorWorkerService protected readonly _editorWorkerService: IEditorWorkerService,
242238
@IInstantiationService private readonly _instaService: IInstantiationService,
243239
) {
244240
super();
245-
this._diffEnabled = _storageService.getBoolean(LiveStrategy._inlineDiffStorageKey, StorageScope.PROFILE, true);
241+
this._diffEnabled = configService.getValue<boolean>('inlineChat.showDiff');
246242

247243
this._inlineDiffDecorations = new InlineDiffDecorations(this._editor, this._diffEnabled);
248-
this._ctxShowingDiff = CTX_INLINE_CHAT_SHOWING_DIFF.bindTo(contextKeyService);
249-
this._ctxShowingDiff.set(this._diffEnabled);
250244
this._inlineDiffDecorations.visible = this._diffEnabled;
245+
246+
this._store.add(configService.onDidChangeConfiguration(e => {
247+
if (e.affectsConfiguration('inlineChat.showDiff')) {
248+
this._diffEnabled = !this._diffEnabled;
249+
this._doToggleDiff();
250+
}
251+
}));
251252
}
252253

253254
override dispose(): void {
254255
this._inlineDiffDecorations.clear();
255-
this._ctxShowingDiff.reset();
256-
}
257-
258-
toggleDiff(): void {
259-
this._diffEnabled = !this._diffEnabled;
260-
this._ctxShowingDiff.set(this._diffEnabled);
261-
this._storageService.store(LiveStrategy._inlineDiffStorageKey, this._diffEnabled, StorageScope.PROFILE, StorageTarget.USER);
262-
this._doToggleDiff();
256+
this._store.dispose();
263257
}
264258

265259
protected _doToggleDiff(): void {
@@ -385,12 +379,13 @@ export class LivePreviewStrategy extends LiveStrategy {
385379
editor: ICodeEditor,
386380
widget: InlineChatWidget,
387381
@IContextKeyService contextKeyService: IContextKeyService,
382+
@IConfigurationService configService: IConfigurationService,
388383
@IStorageService storageService: IStorageService,
389384
@IBulkEditService bulkEditService: IBulkEditService,
390385
@IEditorWorkerService editorWorkerService: IEditorWorkerService,
391386
@IInstantiationService instaService: IInstantiationService,
392387
) {
393-
super(session, editor, widget, contextKeyService, storageService, bulkEditService, editorWorkerService, instaService);
388+
super(session, editor, widget, contextKeyService, configService, storageService, bulkEditService, editorWorkerService, instaService);
394389

395390
this._diffZone = new Lazy(() => instaService.createInstance(InlineChatLivePreviewWidget, editor, session));
396391
this._previewZone = new Lazy(() => instaService.createInstance(InlineChatFileCreatePreviewWidget, editor));

src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import { localize } from 'vs/nls';
1212
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
1313
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
1414
import { ZoneWidget } from 'vs/editor/contrib/zoneWidget/browser/zoneWidget';
15-
import { CTX_INLINE_CHAT_FOCUSED, CTX_INLINE_CHAT_INNER_CURSOR_FIRST, CTX_INLINE_CHAT_INNER_CURSOR_LAST, CTX_INLINE_CHAT_EMPTY, CTX_INLINE_CHAT_OUTER_CURSOR_POSITION, CTX_INLINE_CHAT_VISIBLE, MENU_INLINE_CHAT_WIDGET, MENU_INLINE_CHAT_WIDGET_STATUS, MENU_INLINE_CHAT_WIDGET_MARKDOWN_MESSAGE, CTX_INLINE_CHAT_MESSAGE_CROP_STATE, IInlineChatSlashCommand, MENU_INLINE_CHAT_WIDGET_FEEDBACK, ACTION_REGENERATE_RESPONSE, ACTION_VIEW_IN_CHAT } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
15+
import { CTX_INLINE_CHAT_FOCUSED, CTX_INLINE_CHAT_INNER_CURSOR_FIRST, CTX_INLINE_CHAT_INNER_CURSOR_LAST, CTX_INLINE_CHAT_EMPTY, CTX_INLINE_CHAT_OUTER_CURSOR_POSITION, CTX_INLINE_CHAT_VISIBLE, MENU_INLINE_CHAT_WIDGET, MENU_INLINE_CHAT_WIDGET_STATUS, MENU_INLINE_CHAT_WIDGET_MARKDOWN_MESSAGE, CTX_INLINE_CHAT_MESSAGE_CROP_STATE, IInlineChatSlashCommand, MENU_INLINE_CHAT_WIDGET_FEEDBACK, ACTION_REGENERATE_RESPONSE, ACTION_VIEW_IN_CHAT, MENU_INLINE_CHAT_WIDGET_TOGGLE } from 'vs/workbench/contrib/inlineChat/common/inlineChat';
1616
import { IModelDeltaDecoration, ITextModel } from 'vs/editor/common/model';
17-
import { Dimension, addDisposableListener, getActiveElement, getTotalHeight, getTotalWidth, h, reset } from 'vs/base/browser/dom';
17+
import { EventType, Dimension, addDisposableListener, getActiveElement, getTotalHeight, getTotalWidth, h, reset } from 'vs/base/browser/dom';
1818
import { Emitter, Event, MicrotaskEmitter } from 'vs/base/common/event';
1919
import { IEditorConstructionOptions } from 'vs/editor/browser/config/editorConfiguration';
2020
import { ICodeEditorWidgetOptions } from 'vs/editor/browser/widget/codeEditorWidget';
@@ -50,6 +50,7 @@ import { IdleValue } from 'vs/base/common/async';
5050
import * as aria from 'vs/base/browser/ui/aria/aria';
5151
import { IMenuWorkbenchButtonBarOptions, MenuWorkbenchButtonBar } from 'vs/platform/actions/browser/buttonbar';
5252
import { SlashCommandContentWidget } from 'vs/workbench/contrib/chat/browser/chatSlashCommandContentWidget';
53+
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
5354

5455
const defaultAriaLabel = localize('aria-label', "Inline Chat Input");
5556

@@ -193,7 +194,8 @@ export class InlineChatWidget {
193194
@IKeybindingService private readonly _keybindingService: IKeybindingService,
194195
@IInstantiationService private readonly _instantiationService: IInstantiationService,
195196
@IAccessibilityService private readonly _accessibilityService: IAccessibilityService,
196-
@IConfigurationService private readonly _configurationService: IConfigurationService
197+
@IConfigurationService private readonly _configurationService: IConfigurationService,
198+
@IContextMenuService private readonly _contextMenuService: IContextMenuService,
197199
) {
198200

199201
// input editor logic
@@ -337,6 +339,17 @@ export class InlineChatWidget {
337339
const markdownMessageToolbar = this._instantiationService.createInstance(MenuWorkbenchToolBar, this._elements.messageActions, MENU_INLINE_CHAT_WIDGET_MARKDOWN_MESSAGE, workbenchToolbarOptions);
338340
this._store.add(markdownMessageToolbar.onDidChangeMenuItems(() => this._onDidChangeHeight.fire()));
339341
this._store.add(markdownMessageToolbar);
342+
343+
this._store.add(addDisposableListener(this._elements.root, EventType.CONTEXT_MENU, async (event: MouseEvent) => {
344+
this._onContextMenu(event);
345+
}));
346+
}
347+
348+
private _onContextMenu(event: MouseEvent) {
349+
this._contextMenuService.showContextMenu({
350+
menuId: MENU_INLINE_CHAT_WIDGET_TOGGLE,
351+
getAnchor: () => event,
352+
});
340353
}
341354

342355
private _updateAriaLabel(): void {

src/vs/workbench/contrib/inlineChat/common/inlineChat.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ export const CTX_INLINE_CHAT_MESSAGE_CROP_STATE = new RawContextKey<'cropped' |
126126
export const CTX_INLINE_CHAT_OUTER_CURSOR_POSITION = new RawContextKey<'above' | 'below' | ''>('inlineChatOuterCursorPosition', '', localize('inlineChatOuterCursorPosition', "Whether the cursor of the outer editor is above or below the interactive editor input"));
127127
export const CTX_INLINE_CHAT_HAS_ACTIVE_REQUEST = new RawContextKey<boolean>('inlineChatHasActiveRequest', false, localize('inlineChatHasActiveRequest', "Whether interactive editor has an active request"));
128128
export const CTX_INLINE_CHAT_HAS_STASHED_SESSION = new RawContextKey<boolean>('inlineChatHasStashedSession', false, localize('inlineChatHasStashedSession', "Whether interactive editor has kept a session for quick restore"));
129-
export const CTX_INLINE_CHAT_SHOWING_DIFF = new RawContextKey<boolean>('inlineChatDiff', false, localize('inlineChatDiff', "Whether interactive editor show diffs for changes"));
130129
export const CTX_INLINE_CHAT_LAST_RESPONSE_TYPE = new RawContextKey<InlineChatResponseType | undefined>('inlineChatLastResponseType', undefined, localize('inlineChatResponseType', "What type was the last response of the current interactive editor session"));
131130
export const CTX_INLINE_CHAT_RESPONSE_TYPES = new RawContextKey<InlineChateResponseTypes | undefined>('inlineChatResponseTypes', undefined, localize('inlineChatResponseTypes', "What type was the responses have been receieved"));
132131
export const CTX_INLINE_CHAT_DID_EDIT = new RawContextKey<boolean>('inlineChatDidEdit', undefined, localize('inlineChatDidEdit', "Whether interactive editor did change any code"));
@@ -148,6 +147,7 @@ export const MENU_INLINE_CHAT_WIDGET_MARKDOWN_MESSAGE = MenuId.for('inlineChatWi
148147
export const MENU_INLINE_CHAT_WIDGET_STATUS = MenuId.for('inlineChatWidget.status');
149148
export const MENU_INLINE_CHAT_WIDGET_FEEDBACK = MenuId.for('inlineChatWidget.feedback');
150149
export const MENU_INLINE_CHAT_WIDGET_DISCARD = MenuId.for('inlineChatWidget.undo');
150+
export const MENU_INLINE_CHAT_WIDGET_TOGGLE = MenuId.for('inlineChatWidget.toggle');
151151

152152
// --- colors
153153

@@ -193,6 +193,11 @@ Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfigurat
193193
localize('mode.preview', "Changes are previewed only and need to be accepted via the apply button. Ending a session will discard the changes."),
194194
localize('mode.live', "Changes are applied directly to the document but can be highlighted via inline diffs. Ending a session will keep the changes."),
195195
]
196+
},
197+
'inlineChat.showDiff': {
198+
description: localize('showDiff', "Enable/disable showing the diff when edits are generated. Works only with inlineChat.mode equal to live or livePreview."),
199+
default: true,
200+
type: 'boolean'
196201
}
197202
}
198203
});

0 commit comments

Comments
 (0)