Skip to content

Commit ac31286

Browse files
authored
Merge pull request microsoft#202452 from microsoft/roblou/angry-snake
Fix chat input history navigation
2 parents b8683a4 + eadc914 commit ac31286

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

src/vs/workbench/contrib/chat/browser/chatInputPart.ts

+22-19
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import { CONTEXT_CHAT_INPUT_CURSOR_AT_TOP, CONTEXT_CHAT_INPUT_HAS_TEXT, CONTEXT_
3939
import { chatAgentLeader } from 'vs/workbench/contrib/chat/common/chatParserTypes';
4040
import { IChatReplyFollowup } from 'vs/workbench/contrib/chat/common/chatService';
4141
import { IChatResponseViewModel } from 'vs/workbench/contrib/chat/common/chatViewModel';
42-
import { IChatWidgetHistoryService } from 'vs/workbench/contrib/chat/common/chatWidgetHistoryService';
42+
import { IChatHistoryEntry, IChatWidgetHistoryService } from 'vs/workbench/contrib/chat/common/chatWidgetHistoryService';
4343
import { getSimpleCodeEditorWidgetOptions, getSimpleEditorOptions } from 'vs/workbench/contrib/codeEditor/browser/simpleEditorOptions';
4444
import { ChatSubmitEditorAction, ChatSubmitSecondaryAgentEditorAction } from 'vs/workbench/contrib/chat/browser/actions/chatActions';
4545
import { IPosition } from 'vs/editor/common/core/position';
@@ -82,10 +82,10 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
8282
return this._inputEditor;
8383
}
8484

85-
private history: HistoryNavigator<string>;
86-
private historyStates: Map<string, any> = new Map();
85+
private history: HistoryNavigator<IChatHistoryEntry>;
8786
private historyNavigationBackwardsEnablement!: IContextKey<boolean>;
8887
private historyNavigationForewardsEnablement!: IContextKey<boolean>;
88+
private onHistoryEntry = false;
8989
private inputModel: ITextModel | undefined;
9090
private inputEditorHasText: IContextKey<boolean>;
9191
private chatCursorAtTop: IContextKey<boolean>;
@@ -133,11 +133,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
133133
setState(providerId: string, inputValue: string | undefined): void {
134134
this.providerId = providerId;
135135
const history = this.historyService.getHistory(providerId);
136-
this.historyStates = new Map(history.map(h => [h.text, h.state]));
137-
const historyTexts: string[] = [];
138-
this.historyStates.forEach((_, str) => historyTexts.push(str));
139-
140-
this.history = new HistoryNavigator(historyTexts, 50);
136+
this.history = new HistoryNavigator(history, 50);
141137

142138
if (typeof inputValue === 'string') {
143139
this.setValue(inputValue);
@@ -157,13 +153,15 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
157153
}
158154

159155
private navigateHistory(previous: boolean): void {
160-
const historyInput = (previous ?
156+
const historyEntry = (previous ?
161157
(this.history.previous() ?? this.history.first()) : this.history.next())
162-
?? '';
158+
?? { text: '' };
159+
160+
this.onHistoryEntry = previous || this.history.current() !== null;
163161

164-
aria.status(historyInput);
165-
this.setValue(historyInput);
166-
this._onDidLoadInputState.fire(this.historyStates.get(historyInput));
162+
aria.status(historyEntry.text);
163+
this.setValue(historyEntry.text);
164+
this._onDidLoadInputState.fire(historyEntry.state);
167165
if (previous) {
168166
this._inputEditor.setPosition({ lineNumber: 1, column: 1 });
169167
} else {
@@ -196,8 +194,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
196194
*/
197195
async acceptInput(userQuery?: string, inputState?: any): Promise<void> {
198196
if (userQuery) {
199-
this.history.add(userQuery);
200-
this.historyStates.set(userQuery, inputState);
197+
this.history.add({ text: userQuery, state: inputState });
201198
}
202199

203200
if (this.accessibilityService.isScreenReaderOptimized() && isMacintosh) {
@@ -272,6 +269,10 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
272269
const model = this._inputEditor.getModel();
273270
const inputHasText = !!model && model.getValueLength() > 0;
274271
this.inputEditorHasText.set(inputHasText);
272+
if (!this.onHistoryEntry) {
273+
this.historyNavigationForewardsEnablement.set(!inputHasText);
274+
this.historyNavigationBackwardsEnablement.set(!inputHasText);
275+
}
275276
}));
276277
this._register(this._inputEditor.onDidFocusEditorText(() => {
277278
this._onDidFocus.fire();
@@ -289,9 +290,12 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
289290
}
290291

291292
const atTop = e.position.column === 1 && e.position.lineNumber === 1;
292-
this.historyNavigationBackwardsEnablement.set(atTop);
293293
this.chatCursorAtTop.set(atTop);
294-
this.historyNavigationForewardsEnablement.set(e.position.equals(getLastPosition(model)));
294+
295+
if (this.onHistoryEntry) {
296+
this.historyNavigationBackwardsEnablement.set(atTop);
297+
this.historyNavigationForewardsEnablement.set(e.position.equals(getLastPosition(model)));
298+
}
295299
}));
296300

297301
this.toolbar = this._register(this.instantiationService.createInstance(MenuWorkbenchToolBar, inputContainer, MenuId.ChatExecute, {
@@ -381,8 +385,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
381385

382386
saveState(): void {
383387
const inputHistory = this.history.getHistory();
384-
const historyEntries = inputHistory.map(entry => ({ text: entry, state: this.historyStates.get(entry) }));
385-
this.historyService.saveHistory(this.providerId!, historyEntries);
388+
this.historyService.saveHistory(this.providerId!, inputHistory);
386389
}
387390
}
388391

0 commit comments

Comments
 (0)