Skip to content

Commit bf99292

Browse files
committed
add commands vscode.executeNotebookToData and vscode.executeDataToNotebook to execute notebook serializers by type, microsoft#125757
1 parent 3de6bc7 commit bf99292

File tree

8 files changed

+69
-12
lines changed

8 files changed

+69
-12
lines changed

src/vs/workbench/api/browser/mainThreadNotebook.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import { NotebookDto } from 'vs/workbench/api/browser/mainThreadNotebookDto';
1212
import { extHostNamedCustomer, IExtHostContext } from 'vs/workbench/services/extensions/common/extHostCustomers';
1313
import { INotebookCellStatusBarService } from 'vs/workbench/contrib/notebook/common/notebookCellStatusBarService';
1414
import { INotebookCellStatusBarItemProvider, INotebookContributionData, NotebookData as NotebookData, NotebookExtensionDescription, TransientCellMetadata, TransientDocumentMetadata, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
15-
import { INotebookContentProvider, INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
15+
import { INotebookContentProvider, INotebookService, SimpleNotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookService';
1616
import { SerializableObjectWithBuffers } from 'vs/workbench/services/extensions/common/proxyIdentifier';
1717
import { ExtHostContext, ExtHostNotebookShape, MainContext, MainThreadNotebookShape } from '../common/extHost.protocol';
1818
import { ILogService } from 'vs/platform/log/common/log';
1919
import { StopWatch } from 'vs/base/common/stopwatch';
20+
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
21+
import { assertType } from 'vs/base/common/types';
2022

2123
@extHostNamedCustomer(MainContext.MainThreadNotebook)
2224
export class MainThreadNotebooks implements MainThreadNotebookShape {
@@ -184,3 +186,36 @@ export class MainThreadNotebooks implements MainThreadNotebookShape {
184186
}
185187
}
186188
}
189+
190+
CommandsRegistry.registerCommand('_executeDataToNotebook', async (accessor, ...args) => {
191+
192+
const [notebookType, bytes] = args;
193+
assertType(typeof notebookType === 'string', 'string');
194+
assertType(bytes instanceof VSBuffer, 'VSBuffer');
195+
196+
const notebookService = accessor.get(INotebookService);
197+
const info = await notebookService.withNotebookDataProvider(notebookType);
198+
if (!(info instanceof SimpleNotebookProviderInfo)) {
199+
return;
200+
}
201+
202+
const dto = await info.serializer.dataToNotebook(bytes);
203+
return NotebookDto.toNotebookDataDto(dto);
204+
});
205+
206+
CommandsRegistry.registerCommand('_executeNotebookToData', async (accessor, ...args) => {
207+
208+
const [notebookType, dto] = args;
209+
assertType(typeof notebookType === 'string', 'string');
210+
assertType(typeof dto === 'object', 'NotebookDataDto');
211+
212+
const notebookService = accessor.get(INotebookService);
213+
const info = await notebookService.withNotebookDataProvider(notebookType);
214+
if (!(info instanceof SimpleNotebookProviderInfo)) {
215+
return;
216+
}
217+
218+
const data = NotebookDto.fromNotebookDataDto(dto);
219+
const bytes = await info.serializer.notebookToData(data);
220+
return bytes;
221+
});

src/vs/workbench/api/common/extHostCommands.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ export class ExtHostCommands implements ExtHostCommandsShape {
189189
} else if (value instanceof Uint8Array) {
190190
hasBuffers = true;
191191
return VSBuffer.wrap(value);
192+
} else if (value instanceof VSBuffer) {
193+
hasBuffers = true;
194+
return value;
192195
}
193196
if (!Array.isArray(value)) {
194197
return value;

src/vs/workbench/api/common/extHostNotebook.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { URI, UriComponents } from 'vs/base/common/uri';
1717
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
1818
import { Cache } from 'vs/workbench/api/common/cache';
1919
import { ExtHostNotebookShape, IMainContext, IModelAddedData, INotebookCellStatusBarListDto, INotebookDocumentsAndEditorsDelta, INotebookDocumentShowOptions, INotebookEditorAddData, MainContext, MainThreadNotebookDocumentsShape, MainThreadNotebookEditorsShape, MainThreadNotebookShape, NotebookDataDto } from 'vs/workbench/api/common/extHost.protocol';
20-
import { CommandsConverter, ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
20+
import { ApiCommand, ApiCommandArgument, ApiCommandResult, CommandsConverter, ExtHostCommands } from 'vs/workbench/api/common/extHostCommands';
2121
import { ExtHostDocuments } from 'vs/workbench/api/common/extHostDocuments';
2222
import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors';
2323
import { IExtensionStoragePaths } from 'vs/workbench/api/common/extHostStoragePaths';
@@ -98,6 +98,8 @@ export class ExtHostNotebookController implements ExtHostNotebookShape {
9898
return arg;
9999
}
100100
});
101+
102+
ExtHostNotebookController._registerApiCommands(commands);
101103
}
102104

103105
getEditorById(editorId: string): ExtHostNotebookEditor {
@@ -513,4 +515,24 @@ export class ExtHostNotebookController implements ExtHostNotebookShape {
513515
this._onDidChangeActiveNotebookEditor.fire(this._activeNotebookEditor?.apiEditor);
514516
}
515517
}
518+
519+
private static _registerApiCommands(extHostCommands: ExtHostCommands) {
520+
521+
const notebookTypeArg = ApiCommandArgument.String.with('notebookType', 'A notebook type');
522+
523+
const commandDataToNotebook = new ApiCommand(
524+
'vscode.executeDataToNotebook', '_executeDataToNotebook', 'Invoke notebook serializer',
525+
[notebookTypeArg, new ApiCommandArgument<Uint8Array, VSBuffer>('data', 'Bytes to convert to data', v => v instanceof Uint8Array, v => VSBuffer.wrap(v))],
526+
new ApiCommandResult<NotebookDataDto, vscode.NotebookData>('Notebook Data', dto => typeConverters.NotebookData.to(dto))
527+
);
528+
529+
const commandNotebookToData = new ApiCommand(
530+
'vscode.executeNotebookToData', '_executeNotebookToData', 'Invoke notebook serializer',
531+
[notebookTypeArg, new ApiCommandArgument<vscode.NotebookData, NotebookDataDto>('NotebookData', 'Notebook data to convert to bytes', v => true, v => typeConverters.NotebookData.from(v))],
532+
new ApiCommandResult<VSBuffer, Uint8Array>('Bytes', dto => dto.buffer)
533+
);
534+
535+
extHostCommands.registerApiCommand(commandDataToNotebook);
536+
extHostCommands.registerApiCommand(commandNotebookToData);
537+
}
516538
}

src/vs/workbench/contrib/notebook/browser/notebookServiceImpl.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,10 @@ export class NotebookService extends Disposable implements INotebookService {
594594
return this._registerProviderData(viewType, new SimpleNotebookProviderInfo(viewType, serializer, extensionData));
595595
}
596596

597-
async withNotebookDataProvider(resource: URI, viewType?: string): Promise<ComplexNotebookProviderInfo | SimpleNotebookProviderInfo> {
598-
const providers = this.notebookProviderInfoStore.getContributedNotebook(resource);
599-
// If we have a viewtype specified we want that data provider, as the resource won't always map correctly
600-
const selected = viewType ? providers.find(p => p.id === viewType) : providers[0];
597+
async withNotebookDataProvider(viewType: string): Promise<ComplexNotebookProviderInfo | SimpleNotebookProviderInfo> {
598+
const selected = this.notebookProviderInfoStore.get(viewType);
601599
if (!selected) {
602-
throw new Error(`NO contribution for resource: '${resource.toString()}'`);
600+
throw new Error(`UNKNOWN notebook type '${viewType}'`);
603601
}
604602
await this.canResolve(selected.id);
605603
const result = this._notebookProviders.get(selected.id);
@@ -714,4 +712,3 @@ export class NotebookService extends Disposable implements INotebookService {
714712
}
715713

716714
}
717-

src/vs/workbench/contrib/notebook/common/notebookEditorInput.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ export class NotebookEditorInput extends AbstractResourceEditorInput {
262262
}
263263

264264
if (this.options._backupId) {
265-
const info = await this._notebookService.withNotebookDataProvider(this._editorModelReference.object.notebook.uri, this._editorModelReference.object.notebook.viewType);
265+
const info = await this._notebookService.withNotebookDataProvider(this._editorModelReference.object.notebook.viewType);
266266
if (!(info instanceof SimpleNotebookProviderInfo)) {
267267
throw new Error('CANNOT open file notebook with this provider');
268268
}

src/vs/workbench/contrib/notebook/common/notebookEditorModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ export class NotebookFileWorkingCopyModelFactory implements IStoredFileWorkingCo
665665

666666
async createModel(resource: URI, stream: VSBufferReadableStream, token: CancellationToken): Promise<NotebookFileWorkingCopyModel> {
667667

668-
const info = await this._notebookService.withNotebookDataProvider(resource, this._viewType);
668+
const info = await this._notebookService.withNotebookDataProvider(this._viewType);
669669
if (!(info instanceof SimpleNotebookProviderInfo)) {
670670
throw new Error('CANNOT open file notebook with this provider');
671671
}

src/vs/workbench/contrib/notebook/common/notebookEditorModelResolverServiceImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class NotebookModelReferenceCollection extends ReferenceCollection<Promise<IReso
6161

6262
protected async createReferencedObject(key: string, viewType: string, hasAssociatedFilePath: boolean): Promise<IResolvedNotebookEditorModel> {
6363
const uri = URI.parse(key);
64-
const info = await this._notebookService.withNotebookDataProvider(uri, viewType);
64+
const info = await this._notebookService.withNotebookDataProvider(viewType);
6565

6666
let result: IResolvedNotebookEditorModel;
6767

src/vs/workbench/contrib/notebook/common/notebookService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export interface INotebookService {
6868

6969
registerNotebookController(viewType: string, extensionData: NotebookExtensionDescription, controller: INotebookContentProvider): IDisposable;
7070
registerNotebookSerializer(viewType: string, extensionData: NotebookExtensionDescription, serializer: INotebookSerializer): IDisposable;
71-
withNotebookDataProvider(resource: URI, viewType?: string): Promise<ComplexNotebookProviderInfo | SimpleNotebookProviderInfo>;
71+
withNotebookDataProvider(viewType: string): Promise<ComplexNotebookProviderInfo | SimpleNotebookProviderInfo>;
7272

7373
getOutputMimeTypeInfo(textModel: NotebookTextModel, kernelProvides: readonly string[] | undefined, output: IOutputDto): readonly IOrderedMimeType[];
7474

0 commit comments

Comments
 (0)