Skip to content

Commit 2c37a5b

Browse files
committed
use same notebook serializer for IW
1 parent 3b921a8 commit 2c37a5b

File tree

3 files changed

+57
-80
lines changed

3 files changed

+57
-80
lines changed

extensions/ipynb/src/ipynbMain.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,18 @@ export function activate(context: vscode.ExtensionContext) {
4343
}
4444
} as vscode.NotebookDocumentContentOptions));
4545

46+
context.subscriptions.push(vscode.workspace.registerNotebookSerializer('interactive', serializer, {
47+
transientOutputs: false,
48+
transientCellMetadata: {
49+
breakpointMargin: true,
50+
custom: false,
51+
attachments: false
52+
},
53+
cellContentMetadata: {
54+
attachments: true
55+
}
56+
} as vscode.NotebookDocumentContentOptions));
57+
4658
vscode.languages.registerCodeLensProvider({ pattern: '**/*.ipynb' }, {
4759
provideCodeLenses: (document) => {
4860
if (

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

Lines changed: 2 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
55

6-
import { VSBuffer } from 'vs/base/common/buffer';
76
import { Iterable } from 'vs/base/common/iterator';
87
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
98
import { Disposable, IDisposable } from 'vs/base/common/lifecycle';
@@ -29,7 +28,6 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
2928
import { IConfigurationRegistry, Extensions as ConfigurationExtensions } from 'vs/platform/configuration/common/configurationRegistry';
3029
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
3130
import { EditorActivation, IResourceEditorInput } from 'vs/platform/editor/common/editor';
32-
import { ExtensionIdentifier } from 'vs/platform/extensions/common/extensions';
3331
import { IFileService } from 'vs/platform/files/common/files';
3432
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
3533
import { InstantiationType, registerSingleton } from 'vs/platform/instantiation/common/extensions';
@@ -56,9 +54,9 @@ import { INotebookEditorOptions } from 'vs/workbench/contrib/notebook/browser/no
5654
import { NotebookEditorWidget } from 'vs/workbench/contrib/notebook/browser/notebookEditorWidget';
5755
import * as icons from 'vs/workbench/contrib/notebook/browser/notebookIcons';
5856
import { INotebookEditorService } from 'vs/workbench/contrib/notebook/browser/services/notebookEditorService';
59-
import { CellEditType, CellKind, CellUri, ICellOutput, INTERACTIVE_WINDOW_EDITOR_ID, NotebookData } from 'vs/workbench/contrib/notebook/common/notebookCommon';
57+
import { CellEditType, CellKind, CellUri, INTERACTIVE_WINDOW_EDITOR_ID } from 'vs/workbench/contrib/notebook/common/notebookCommon';
6058
import { INotebookKernelService } from 'vs/workbench/contrib/notebook/common/notebookKernelService';
61-
import { INotebookSerializer, INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
59+
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
6260
import { columnToEditorGroup } from 'vs/workbench/services/editor/common/editorGroupColumn';
6361
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
6462
import { IEditorResolverService, RegisteredEditorPriority } from 'vs/workbench/services/editor/common/editorResolverService';
@@ -88,78 +86,6 @@ export class InteractiveDocumentContribution extends Disposable implements IWork
8886
) {
8987
super();
9088

91-
const contentOptions = {
92-
transientOutputs: true,
93-
transientCellMetadata: {},
94-
transientDocumentMetadata: {},
95-
cellContentMetadata: {}
96-
};
97-
98-
const serializer: INotebookSerializer = {
99-
options: contentOptions,
100-
dataToNotebook: async (data: VSBuffer): Promise<NotebookData> => {
101-
if (data.byteLength > 0) {
102-
try {
103-
const document = JSON.parse(data.toString()) as { cells: { kind: CellKind; language: string; metadata: any; mime: string | undefined; content: string; outputs?: ICellOutput[] }[] };
104-
return {
105-
cells: document.cells.map(cell => ({
106-
source: cell.content,
107-
language: cell.language,
108-
cellKind: cell.kind,
109-
mime: cell.mime,
110-
outputs: cell.outputs
111-
? cell.outputs.map(output => ({
112-
outputId: output.outputId,
113-
outputs: output.outputs.map(ot => ({
114-
mime: ot.mime,
115-
data: ot.data
116-
}))
117-
}))
118-
: [],
119-
metadata: cell.metadata
120-
})),
121-
metadata: {}
122-
};
123-
} catch (e) {
124-
logService.error(`error when converting data to notebook data object: ${e}`);
125-
}
126-
}
127-
128-
return {
129-
metadata: {},
130-
cells: []
131-
};
132-
133-
},
134-
notebookToData: async (data: NotebookData): Promise<VSBuffer> => {
135-
const cells = data.cells.map(cell => ({
136-
kind: cell.cellKind,
137-
language: cell.language,
138-
metadata: cell.metadata,
139-
mine: cell.mime,
140-
outputs: cell.outputs.map(output => {
141-
return {
142-
outputId: output.outputId,
143-
outputs: output.outputs.map(ot => ({
144-
mime: ot.mime,
145-
data: ot.data
146-
}))
147-
};
148-
}),
149-
content: cell.source
150-
}));
151-
152-
return VSBuffer.fromString(JSON.stringify({
153-
cells: cells
154-
}));
155-
}
156-
};
157-
158-
this._register(notebookService.registerNotebookSerializer('interactive', {
159-
id: new ExtensionIdentifier('interactive.builtin'),
160-
location: undefined
161-
}, serializer));
162-
16389
const info = notebookService.getContributedNotebookType('interactive');
16490

16591
// We need to contribute a notebook type for the Interactive Window to provide notebook models.

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

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import { VSBuffer } from 'vs/base/common/buffer';
77
import { Event } from 'vs/base/common/event';
88
import { IReference } from 'vs/base/common/lifecycle';
99
import * as paths from 'vs/base/common/path';
10-
import { isEqual } from 'vs/base/common/resources';
10+
import { isEqual, joinPath } from 'vs/base/common/resources';
1111
import { URI } from 'vs/base/common/uri';
1212
import { IResolvedTextEditorModel, ITextModelService } from 'vs/editor/common/services/resolverService';
13+
import { IFileDialogService } from 'vs/platform/dialogs/common/dialogs';
1314
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
14-
import { EditorInputCapabilities, IUntypedEditorInput } from 'vs/workbench/common/editor';
15+
import { EditorInputCapabilities, GroupIdentifier, ISaveOptions, IUntypedEditorInput } from 'vs/workbench/common/editor';
1516
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
1617
import { IInteractiveDocumentService } from 'vs/workbench/contrib/interactive/browser/interactiveDocumentService';
1718
import { IInteractiveHistoryService } from 'vs/workbench/contrib/interactive/browser/interactiveHistoryService';
1819
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
1920
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
2021
import { CellKind, ICellDto2, IOutputDto, IResolvedNotebookEditorModel, NotebookCellCollapseState, NotebookCellInternalMetadata, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2122
import { ICompositeNotebookEditorInput, NotebookEditorInput } from 'vs/workbench/contrib/notebook/common/notebookEditorInput';
23+
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
2224

2325
export class InteractiveEditorInput extends EditorInput implements ICompositeNotebookEditorInput {
2426
static create(instantiationService: IInstantiationService, resource: URI, inputResource: URI, title?: string) {
@@ -76,9 +78,10 @@ export class InteractiveEditorInput extends EditorInput implements ICompositeNot
7678
title: string | undefined,
7779
@IInstantiationService instantiationService: IInstantiationService,
7880
@ITextModelService textModelService: ITextModelService,
79-
8081
@IInteractiveDocumentService interactiveDocumentService: IInteractiveDocumentService,
81-
@IInteractiveHistoryService historyService: IInteractiveHistoryService
82+
@IInteractiveHistoryService historyService: IInteractiveHistoryService,
83+
@INotebookService private readonly _notebookService: INotebookService,
84+
@IFileDialogService private readonly _fileDialogService: IFileDialogService,
8285
) {
8386
const input = NotebookEditorInput.create(instantiationService, resource, 'interactive', {});
8487
super();
@@ -162,6 +165,42 @@ export class InteractiveEditorInput extends EditorInput implements ICompositeNot
162165
return this._inputModelRef.object.textEditorModel;
163166
}
164167

168+
override async save(group: GroupIdentifier, options?: ISaveOptions): Promise<EditorInput | IUntypedEditorInput | undefined> {
169+
if (this._editorModelReference) {
170+
171+
if (this.hasCapability(EditorInputCapabilities.Untitled)) {
172+
return this.saveAs(group, options);
173+
} else {
174+
await this._editorModelReference.save(options);
175+
}
176+
177+
return this;
178+
}
179+
180+
return undefined;
181+
}
182+
183+
override async saveAs(group: GroupIdentifier, options?: ISaveOptions): Promise<IUntypedEditorInput | undefined> {
184+
if (!this._editorModelReference) {
185+
return undefined;
186+
}
187+
188+
const provider = this._notebookService.getContributedNotebookType('interactive');
189+
190+
if (!provider) {
191+
return undefined;
192+
}
193+
194+
const pathCandidate = joinPath(await this._fileDialogService.defaultFilePath(), 'scratch.ipynb');
195+
196+
const target = await this._fileDialogService.pickFileToSave(pathCandidate, options?.availableFileSystems);
197+
if (!target) {
198+
return undefined; // save cancelled
199+
}
200+
201+
return await this._editorModelReference.saveAs(target);
202+
}
203+
165204
override matches(otherInput: EditorInput | IUntypedEditorInput): boolean {
166205
if (super.matches(otherInput)) {
167206
return true;

0 commit comments

Comments
 (0)