Skip to content

Commit 18606bc

Browse files
authored
Save interactive session editor input in view state (microsoft#178081)
Save interactive session editor input in view history
1 parent f60a608 commit 18606bc

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/vs/workbench/contrib/interactiveSession/browser/interactiveSessionWidget.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storag
3131
import { defaultButtonStyles } from 'vs/platform/theme/browser/defaultStyles';
3232
import { foreground } from 'vs/platform/theme/common/colorRegistry';
3333
import { DEFAULT_FONT_FAMILY } from 'vs/workbench/browser/style';
34+
import { Memento } from 'vs/workbench/common/memento';
3435
import { getSimpleCodeEditorWidgetOptions, getSimpleEditorOptions } from 'vs/workbench/contrib/codeEditor/browser/simpleEditorOptions';
3536
import { IInteractiveSessionExecuteActionContext } from 'vs/workbench/contrib/interactiveSession/browser/actions/interactiveSessionExecuteActions';
3637
import { IInteractiveSessionWidget } from 'vs/workbench/contrib/interactiveSession/browser/interactiveSession';
@@ -62,6 +63,11 @@ function revealLastElement(list: WorkbenchObjectTree<any>) {
6263
list.scrollTop = list.scrollHeight - list.renderHeight;
6364
}
6465

66+
interface IViewState {
67+
history: string[];
68+
inputValue: string;
69+
}
70+
6571
const HISTORY_STORAGE_KEY = 'interactiveSession.history';
6672
const INPUT_EDITOR_MAX_HEIGHT = 250;
6773

@@ -138,6 +144,9 @@ export class InteractiveSessionWidget extends Disposable implements IInteractive
138144
private lastSlashCommands: IInteractiveSlashCommand[] | undefined;
139145
private slashCommandsPromise: Promise<IInteractiveSlashCommand[] | undefined> | undefined;
140146

147+
private memento: Memento;
148+
private viewState: IViewState;
149+
141150
constructor(
142151
private readonly providerId: string,
143152
readonly viewId: string | undefined,
@@ -160,7 +169,12 @@ export class InteractiveSessionWidget extends Disposable implements IInteractive
160169
this._register((interactiveSessionWidgetService as InteractiveSessionWidgetService).register(this));
161170
this.initializeSessionModel(true);
162171

163-
this.history = new HistoryNavigator(JSON.parse(this.storageService.get(this.getHistoryStorageKey(), StorageScope.WORKSPACE, '[]')), 50);
172+
const oldPersistedHistory = JSON.parse(this.storageService.get(this.getHistoryStorageKey(), StorageScope.WORKSPACE, '[]'));
173+
this.memento = new Memento('interactive-session-' + this.providerId, storageService);
174+
this.viewState = this.memento.getMemento(StorageScope.WORKSPACE, StorageTarget.USER) as IViewState;
175+
176+
const history = this.viewState.history ?? oldPersistedHistory;
177+
this.history = new HistoryNavigator(history, 50);
164178
}
165179

166180
get element(): HTMLElement {
@@ -247,8 +261,13 @@ export class InteractiveSessionWidget extends Disposable implements IInteractive
247261
if (!this.inputModel) {
248262
this.inputModel = this.modelService.getModel(this.inputUri) || this.modelService.createModel('', null, this.inputUri, true);
249263
this.inputModel.updateOptions({ bracketColorizationOptions: { enabled: false, independentColorPoolPerBracketType: false } });
264+
this._inputEditor.setModel(this.inputModel);
265+
if (this.viewState.inputValue) {
266+
this.inputModel.setValue(this.viewState.inputValue);
267+
const lineNumber = this.inputModel.getLineCount();
268+
this._inputEditor.setPosition({ lineNumber, column: this.inputModel.getLineMaxColumn(lineNumber) });
269+
}
250270
}
251-
this._inputEditor.setModel(this.inputModel);
252271

253272
// Not sure why this is needed- the view is being rendered before it's visible, and then the list content doesn't show up
254273
this.onDidChangeItems();
@@ -613,14 +632,13 @@ export class InteractiveSessionWidget extends Disposable implements IInteractive
613632

614633
saveState(): void {
615634
const inputHistory = this.history.getHistory();
616-
if (inputHistory.length) {
617-
this.storageService.store(this.getHistoryStorageKey(), JSON.stringify(inputHistory), StorageScope.WORKSPACE, StorageTarget.USER);
618-
} else {
619-
this.storageService.remove(this.getHistoryStorageKey(), StorageScope.WORKSPACE);
620-
}
635+
this.viewState.history = inputHistory;
636+
this.viewState.inputValue = this._inputEditor.getValue();
637+
this.memento.saveMemento();
621638
}
622639

623640
public override dispose(): void {
641+
this.saveState();
624642
super.dispose();
625643

626644
if (this.viewModel) {

0 commit comments

Comments
 (0)