Skip to content

Commit be34739

Browse files
authored
editors - use canSerialize properly (microsoft#205856)
1 parent 67f9158 commit be34739

File tree

8 files changed

+31
-25
lines changed

8 files changed

+31
-25
lines changed

src/vs/workbench/common/editor/editorGroupModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,7 @@ export class EditorGroupModel extends Disposable implements IEditorGroupModel {
10331033

10341034
const editorSerializer = registry.getEditorSerializer(editor);
10351035
if (editorSerializer) {
1036-
const value = editorSerializer.serialize(editor);
1036+
const value = editorSerializer.canSerialize(editor) ? editorSerializer.serialize(editor) : undefined;
10371037

10381038
// Editor can be serialized
10391039
if (typeof value === 'string') {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,12 @@ interface ISerializedChatEditorInput {
183183
}
184184

185185
export class ChatEditorInputSerializer implements IEditorSerializer {
186-
canSerialize(input: EditorInput): boolean {
187-
return input instanceof ChatEditorInput;
186+
canSerialize(input: EditorInput): input is ChatEditorInput & { readonly sessionId: string } {
187+
return input instanceof ChatEditorInput && typeof input.sessionId === 'string';
188188
}
189189

190190
serialize(input: EditorInput): string | undefined {
191-
if (!(input instanceof ChatEditorInput)) {
192-
return undefined;
193-
}
194-
195-
if (typeof input.sessionId !== 'string') {
191+
if (!this.canSerialize(input)) {
196192
return undefined;
197193
}
198194

src/vs/workbench/contrib/interactive/browser/interactive.contribution.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { parse } from 'vs/base/common/marshalling';
1010
import { Schemas } from 'vs/base/common/network';
1111
import { extname, isEqual } from 'vs/base/common/resources';
1212
import { isFalsyOrWhitespace } from 'vs/base/common/strings';
13-
import { assertType } from 'vs/base/common/types';
1413
import { URI, UriComponents } from 'vs/base/common/uri';
1514
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
1615
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditorWidget';
@@ -263,13 +262,19 @@ type interactiveEditorInputData = { resource: URI; inputResource: URI; name: str
263262
export class InteractiveEditorSerializer implements IEditorSerializer {
264263
public static readonly ID = InteractiveEditorInput.ID;
265264

266-
canSerialize(editor: EditorInput): boolean {
267-
const interactiveEditorInput = editor as InteractiveEditorInput;
268-
return URI.isUri(interactiveEditorInput?.primary?.resource) && URI.isUri(interactiveEditorInput?.inputResource);
265+
canSerialize(editor: EditorInput): editor is InteractiveEditorInput {
266+
if (!(editor instanceof InteractiveEditorInput)) {
267+
return false;
268+
}
269+
270+
return URI.isUri(editor.primary.resource) && URI.isUri(editor.inputResource);
269271
}
270272

271-
serialize(input: EditorInput): string {
272-
assertType(input instanceof InteractiveEditorInput);
273+
serialize(input: EditorInput): string | undefined {
274+
if (!this.canSerialize(input)) {
275+
return undefined;
276+
}
277+
273278
return JSON.stringify({
274279
resource: input.primary.resource,
275280
inputResource: input.inputResource,

src/vs/workbench/contrib/multiDiffEditor/browser/multiDiffEditorInput.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,16 @@ interface ISerializedMultiDiffEditorInput {
364364

365365
export class MultiDiffEditorSerializer implements IEditorSerializer {
366366

367-
// TODO@bpasero, @aiday-mar: following canSerialize should be removed (debt item)
368-
canSerialize(editor: EditorInput): boolean {
367+
canSerialize(editor: EditorInput): editor is MultiDiffEditorInput {
369368
return editor instanceof MultiDiffEditorInput && !editor.isTransient;
370369
}
371370

372371
serialize(editor: MultiDiffEditorInput): string | undefined {
373-
const shouldSerialize = editor instanceof MultiDiffEditorInput && !editor.isTransient;
374-
return shouldSerialize ? JSON.stringify(editor.serialize()) : undefined;
372+
if (!this.canSerialize(editor)) {
373+
return undefined;
374+
}
375+
376+
return JSON.stringify(editor.serialize());
375377
}
376378

377379
deserialize(instantiationService: IInstantiationService, serializedEditor: string): EditorInput | undefined {

src/vs/workbench/contrib/searchEditor/browser/searchEditor.contribution.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ class SearchEditorInputSerializer implements IEditorSerializer {
113113
}
114114

115115
serialize(input: SearchEditorInput) {
116+
if (!this.canSerialize(input)) {
117+
return undefined;
118+
}
119+
116120
if (input.isDisposed()) {
117121
return JSON.stringify({ modelUri: undefined, dirty: false, config: input.tryReadConfigSync(), name: input.getName(), matchRanges: [], backingUri: input.backingUri?.toString() } as SerializedSearchEditor);
118122
}

src/vs/workbench/contrib/terminal/browser/terminalEditorSerializer.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ export class TerminalInputSerializer implements IEditorSerializer {
1414
@ITerminalEditorService private readonly _terminalEditorService: ITerminalEditorService
1515
) { }
1616

17-
public canSerialize(editorInput: TerminalEditorInput): boolean {
18-
return !!editorInput.terminalInstance?.persistentProcessId;
17+
public canSerialize(editorInput: TerminalEditorInput): editorInput is TerminalEditorInput & { readonly terminalInstance: ITerminalInstance } {
18+
return typeof editorInput.terminalInstance?.persistentProcessId === 'number' && editorInput.terminalInstance.shouldPersist;
1919
}
2020

2121
public serialize(editorInput: TerminalEditorInput): string | undefined {
22-
if (!editorInput.terminalInstance?.persistentProcessId || !editorInput.terminalInstance.shouldPersist) {
22+
if (!this.canSerialize(editorInput)) {
2323
return;
2424
}
25-
const term = JSON.stringify(this._toJson(editorInput.terminalInstance));
26-
return term;
25+
return JSON.stringify(this._toJson(editorInput.terminalInstance));
2726
}
2827

2928
public deserialize(instantiationService: IInstantiationService, serializedEditorInput: string): EditorInput | undefined {

src/vs/workbench/contrib/webviewPanel/browser/webviewEditorInputSerializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class WebviewEditorInputSerializer implements IEditorSerializer {
5858
}
5959

6060
public serialize(input: WebviewInput): string | undefined {
61-
if (!this._webviewWorkbenchService.shouldPersist(input)) {
61+
if (!this.canSerialize(input)) {
6262
return undefined;
6363
}
6464

src/vs/workbench/services/untitled/common/untitledTextEditorHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class UntitledTextEditorInputSerializer implements IEditorSerializer {
4040
}
4141

4242
serialize(editorInput: EditorInput): string | undefined {
43-
if (!this.filesConfigurationService.isHotExitEnabled || editorInput.isDisposed()) {
43+
if (!this.canSerialize(editorInput)) {
4444
return undefined;
4545
}
4646

0 commit comments

Comments
 (0)