Skip to content

Commit 58e840c

Browse files
author
Benjamin Pasero
committed
editors - shouldRestoreViewState
1 parent daddf50 commit 58e840c

6 files changed

Lines changed: 26 additions & 39 deletions

File tree

src/vs/editor/common/services/textResourceConfigurationServiceImpl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export class TextResourceConfigurationService extends Disposable implements ITex
2828
this._register(this.configurationService.onDidChangeConfiguration(e => this._onDidChangeConfiguration.fire(this.toResourceConfigurationChangeEvent(e))));
2929
}
3030

31-
getValue<T>(resource: URI, section?: string): T;
32-
getValue<T>(resource: URI, at?: IPosition, section?: string): T;
33-
getValue<T>(resource: URI, arg2?: any, arg3?: any): T {
31+
getValue<T>(resource: URI | undefined, section?: string): T;
32+
getValue<T>(resource: URI | undefined, at?: IPosition, section?: string): T;
33+
getValue<T>(resource: URI | undefined, arg2?: any, arg3?: any): T {
3434
if (typeof arg3 === 'string') {
3535
return this._getValue(resource, Position.isIPosition(arg2) ? arg2 : null, arg3);
3636
}
@@ -98,7 +98,7 @@ export class TextResourceConfigurationService extends Disposable implements ITex
9898
return ConfigurationTarget.USER_LOCAL;
9999
}
100100

101-
private _getValue<T>(resource: URI, position: IPosition | null, section: string | undefined): T {
101+
private _getValue<T>(resource: URI | undefined, position: IPosition | null, section: string | undefined): T {
102102
const language = resource ? this.getLanguage(resource, position) : undefined;
103103
if (typeof section === 'undefined') {
104104
return this.configurationService.getValue<T>({ resource, overrideIdentifier: language });

src/vs/workbench/browser/parts/editor/textDiffEditor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ export class TextDiffEditor extends BaseTextEditor implements ITextDiffEditorPan
270270
return; // unable to retrieve input resource
271271
}
272272

273-
// Clear view state if input is disposed
274-
if (input.isDisposed()) {
273+
// Clear view state if input is disposed or we are configured to not storing any state
274+
if (input.isDisposed() || (!this.shouldRestoreViewState && (!this.group || !this.group.isOpened(input)))) {
275275
super.clearTextEditorViewState([resource]);
276276
}
277277

src/vs/workbench/browser/parts/editor/textEditor.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,12 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditorPa
4545
private lastAppliedEditorOptions?: IEditorOptions;
4646
private editorMemento: IEditorMemento<IEditorViewState>;
4747

48+
private _shouldRestoreViewState: boolean | undefined;
49+
protected get shouldRestoreViewState(): boolean | undefined { return this._shouldRestoreViewState; }
50+
4851
private _instantiationService: IInstantiationService;
49-
protected get instantiationService(): IInstantiationService {
50-
return this._instantiationService;
51-
}
52-
protected set instantiationService(value: IInstantiationService) {
53-
this._instantiationService = value;
54-
}
52+
protected get instantiationService(): IInstantiationService { return this._instantiationService; }
53+
protected set instantiationService(value: IInstantiationService) { this._instantiationService = value; }
5554

5655
constructor(
5756
id: string,
@@ -83,16 +82,24 @@ export abstract class BaseTextEditor extends BaseEditor implements ITextEditorPa
8382
this.editorContainer?.setAttribute('aria-label', ariaLabel);
8483
this.editorControl?.updateOptions({ ariaLabel });
8584
}));
85+
86+
this.updateRestoreViewStateConfiguration();
8687
}
8788

8889
protected handleConfigurationChangeEvent(configuration?: IEditorConfiguration): void {
90+
this.updateRestoreViewStateConfiguration();
91+
8992
if (this.isVisible()) {
9093
this.updateEditorConfiguration(configuration);
9194
} else {
9295
this.hasPendingConfigurationChange = true;
9396
}
9497
}
9598

99+
private updateRestoreViewStateConfiguration(): void {
100+
this._shouldRestoreViewState = this.textResourceConfigurationService.getValue(undefined, 'workbench.editor.restoreViewState') ?? true /* default */;
101+
}
102+
96103
private consumePendingConfigurationChangeEvent(): void {
97104
if (this.hasPendingConfigurationChange) {
98105
this.updateEditorConfiguration();

src/vs/workbench/contrib/files/browser/editors/textFileEditor.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { basename } from 'vs/base/common/resources';
1111
import { Action } from 'vs/base/common/actions';
1212
import { VIEWLET_ID, TEXT_FILE_EDITOR_ID, IExplorerService } from 'vs/workbench/contrib/files/common/files';
1313
import { ITextFileService, TextFileOperationError, TextFileOperationResult } from 'vs/workbench/services/textfile/common/textfiles';
14-
import { BaseTextEditor, IEditorConfiguration } from 'vs/workbench/browser/parts/editor/textEditor';
14+
import { BaseTextEditor } from 'vs/workbench/browser/parts/editor/textEditor';
1515
import { EditorOptions, TextEditorOptions, IEditorCloseEvent } from 'vs/workbench/common/editor';
1616
import { BinaryEditorModel } from 'vs/workbench/common/editor/binaryEditorModel';
1717
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
@@ -31,7 +31,6 @@ import { IEditorGroupView } from 'vs/workbench/browser/parts/editor/editor';
3131
import { createErrorWithActions } from 'vs/base/common/errorsWithActions';
3232
import { MutableDisposable } from 'vs/base/common/lifecycle';
3333
import { EditorActivation, IEditorOptions } from 'vs/platform/editor/common/editor';
34-
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
3534
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
3635

3736
/**
@@ -41,7 +40,6 @@ export class TextFileEditor extends BaseTextEditor {
4140

4241
static readonly ID = TEXT_FILE_EDITOR_ID;
4342

44-
private restoreViewState: boolean | undefined;
4543
private readonly groupListener = this._register(new MutableDisposable());
4644

4745
constructor(
@@ -57,13 +55,10 @@ export class TextFileEditor extends BaseTextEditor {
5755
@IEditorGroupsService editorGroupService: IEditorGroupsService,
5856
@ITextFileService private readonly textFileService: ITextFileService,
5957
@IExplorerService private readonly explorerService: IExplorerService,
60-
@IConfigurationService private readonly configurationService: IConfigurationService,
6158
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
6259
) {
6360
super(TextFileEditor.ID, telemetryService, instantiationService, storageService, textResourceConfigurationService, themeService, editorService, editorGroupService);
6461

65-
this.updateRestoreViewStateConfiguration();
66-
6762
// Clear view state for deleted files
6863
this._register(this.fileService.onDidFilesChange(e => this.onDidFilesChange(e)));
6964

@@ -84,16 +79,6 @@ export class TextFileEditor extends BaseTextEditor {
8479
}
8580
}
8681

87-
protected handleConfigurationChangeEvent(configuration?: IEditorConfiguration): void {
88-
super.handleConfigurationChangeEvent(configuration);
89-
90-
this.updateRestoreViewStateConfiguration();
91-
}
92-
93-
private updateRestoreViewStateConfiguration(): void {
94-
this.restoreViewState = this.configurationService.getValue('workbench.editor.restoreViewState') ?? true /* default */;
95-
}
96-
9782
getTitle(): string {
9883
return this.input ? this.input.getName() : nls.localize('textFileEditor', "Text File Editor");
9984
}
@@ -121,7 +106,7 @@ export class TextFileEditor extends BaseTextEditor {
121106
// If the editor is not active, we can only clear the view state because it needs
122107
// an active editor with the file opened, so we check for the restoreViewState flag
123108
// being set.
124-
if (editor === this.input || !this.restoreViewState) {
109+
if (editor === this.input || !this.shouldRestoreViewState) {
125110
this.doSaveOrClearTextEditorViewState(editor);
126111
}
127112
}
@@ -277,7 +262,7 @@ export class TextFileEditor extends BaseTextEditor {
277262

278263
// If the user configured to not restore view state, we clear the view
279264
// state unless the editor is still opened in the group.
280-
if (!this.restoreViewState && (!this.group || !this.group.isOpened(input))) {
265+
if (!this.shouldRestoreViewState && (!this.group || !this.group.isOpened(input))) {
281266
this.clearTextEditorViewState([input.resource], this.group);
282267
}
283268

src/vs/workbench/contrib/files/electron-sandbox/textFileEditor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { ITextFileService } from 'vs/workbench/services/textfile/common/textfile
2323
import { IPreferencesService } from 'vs/workbench/services/preferences/common/preferences';
2424
import { IExplorerService } from 'vs/workbench/contrib/files/common/files';
2525
import { IElectronService } from 'vs/platform/electron/electron-sandbox/electron';
26-
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
2726
import { IUriIdentityService } from 'vs/workbench/services/uriIdentity/common/uriIdentity';
2827

2928
/**
@@ -46,10 +45,9 @@ export class NativeTextFileEditor extends TextFileEditor {
4645
@IElectronService private readonly electronService: IElectronService,
4746
@IPreferencesService private readonly preferencesService: IPreferencesService,
4847
@IExplorerService explorerService: IExplorerService,
49-
@IConfigurationService configurationService: IConfigurationService,
5048
@IUriIdentityService uriIdentityService: IUriIdentityService
5149
) {
52-
super(telemetryService, fileService, viewletService, instantiationService, contextService, storageService, textResourceConfigurationService, editorService, themeService, editorGroupService, textFileService, explorerService, configurationService, uriIdentityService);
50+
super(telemetryService, fileService, viewletService, instantiationService, contextService, storageService, textResourceConfigurationService, editorService, themeService, editorGroupService, textFileService, explorerService, uriIdentityService);
5351
}
5452

5553
protected handleSetInputError(error: Error, input: FileEditorInput, options: EditorOptions | undefined): void {

src/vs/workbench/services/textfile/electron-browser/nativeTextFileService.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,10 @@ export interface IEncodingOverride {
325325
}
326326

327327
export class EncodingOracle extends Disposable implements IResourceEncodings {
328+
328329
private _encodingOverrides: IEncodingOverride[];
329-
protected get encodingOverrides(): IEncodingOverride[] {
330-
return this._encodingOverrides;
331-
}
332-
protected set encodingOverrides(value: IEncodingOverride[]) {
333-
this._encodingOverrides = value;
334-
}
330+
protected get encodingOverrides(): IEncodingOverride[] { return this._encodingOverrides; }
331+
protected set encodingOverrides(value: IEncodingOverride[]) { this._encodingOverrides = value; }
335332

336333
constructor(
337334
@ITextResourceConfigurationService private textResourceConfigurationService: ITextResourceConfigurationService,

0 commit comments

Comments
 (0)