Enable VSCode extension APIs for opening notebooks. (#18080)

* Also moved notebook conversion classes to a common folder, and moved several conversion utility methods to a common file.
This commit is contained in:
Cory Rivera
2022-01-13 16:40:48 -08:00
committed by GitHub
parent 117d9365d5
commit 050d2cc98f
14 changed files with 243 additions and 199 deletions

View File

@@ -27,6 +27,8 @@ import { localize } from 'vs/nls';
import { IFileService } from 'vs/platform/files/common/files';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { NotebookEditor } from 'sql/workbench/contrib/notebook/browser/notebookEditor';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { NewNotebookAction } from 'sql/workbench/contrib/notebook/browser/notebookActions';
class MainThreadNotebookEditor extends Disposable {
private _contentChangedEmitter = new Emitter<NotebookContentChange>();
@@ -320,7 +322,8 @@ export class MainThreadNotebookDocumentsAndEditors extends Disposable implements
@IInstantiationService private _instantiationService: IInstantiationService,
@INotebookService private readonly _notebookService: INotebookService,
@IFileService private readonly _fileService: IFileService,
@ITextFileService private readonly _textFileService: ITextFileService
@ITextFileService private readonly _textFileService: ITextFileService,
@ICommandService private readonly _commandService: ICommandService
) {
super();
if (extHostContext) {
@@ -709,4 +712,12 @@ export class MainThreadNotebookDocumentsAndEditors extends Disposable implements
}
});
}
$createNotebookDocument(providerId: string, contents: azdata.nb.INotebookContents): Promise<azdata.nb.NotebookDocument> {
return this._commandService.executeCommand(NewNotebookAction.INTERNAL_NEW_NOTEBOOK_CMD_ID, {
providerId: providerId,
initialContent: contents,
initialDirtyState: false
});
}
}

View File

@@ -13,10 +13,10 @@ import { URI, UriComponents } from 'vs/base/common/uri';
import { ExtHostNotebookShape, MainThreadNotebookShape, SqlMainContext } from 'sql/workbench/api/common/sqlExtHost.protocol';
import { IExecuteManagerDetails, INotebookSessionDetails, INotebookKernelDetails, INotebookFutureDetails, FutureMessageType, ISerializationManagerDetails } from 'sql/workbench/api/common/sqlExtHostTypes';
import { VSCodeSerializationProvider } from 'sql/workbench/api/common/vscodeSerializationProvider';
import { VSCodeSerializationProvider } from 'sql/workbench/api/common/notebooks/vscodeSerializationProvider';
import { IExtensionDescription } from 'vs/platform/extensions/common/extensions';
import { ADSNotebookController } from 'sql/workbench/api/common/adsNotebookController';
import { VSCodeExecuteProvider } from 'sql/workbench/api/common/vscodeExecuteProvider';
import { ADSNotebookController } from 'sql/workbench/api/common/notebooks/adsNotebookController';
import { VSCodeExecuteProvider } from 'sql/workbench/api/common/notebooks/vscodeExecuteProvider';
import { ExtHostNotebookDocumentsAndEditors } from 'sql/workbench/api/common/extHostNotebookDocumentsAndEditors';
type Adapter = azdata.nb.NotebookSerializationProvider | azdata.nb.SerializationManager | azdata.nb.NotebookExecuteProvider | azdata.nb.ExecuteManager | azdata.nb.ISession | azdata.nb.IKernel | azdata.nb.IFuture;

View File

@@ -21,7 +21,7 @@ import {
} from 'sql/workbench/api/common/sqlExtHost.protocol';
import { ExtHostNotebookDocumentData } from 'sql/workbench/api/common/extHostNotebookDocumentData';
import { ExtHostNotebookEditor } from 'sql/workbench/api/common/extHostNotebookEditor';
import { VSCodeNotebookDocument } from 'sql/workbench/api/common/vscodeNotebookDocument';
import { VSCodeNotebookDocument } from 'sql/workbench/api/common/notebooks/vscodeNotebookDocument';
type Adapter = azdata.nb.NavigationProvider;
@@ -266,5 +266,8 @@ export class ExtHostNotebookDocumentsAndEditors implements ExtHostNotebookDocume
});
}
createNotebookDocument(providerId: string, contents: azdata.nb.INotebookContents): Promise<azdata.nb.NotebookDocument> {
return this._proxy.$createNotebookDocument(providerId, contents);
}
//#endregion
}

View File

@@ -13,7 +13,7 @@ import { ExtHostNotebookDocumentsAndEditors } from 'sql/workbench/api/common/ext
import { URI } from 'vs/base/common/uri';
import { NotebookCellExecutionTaskState } from 'vs/workbench/api/common/extHostNotebookKernels';
import { asArray } from 'vs/base/common/arrays';
import { convertToADSCellOutput } from 'sql/workbench/api/common/vscodeSerializationProvider';
import { convertToADSCellOutput } from 'sql/workbench/api/common/notebooks/notebookUtils';
type SelectionChangedEvent = { selected: boolean, notebook: vscode.NotebookDocument; };
type MessageReceivedEvent = { editor: vscode.NotebookEditor, message: any; };

View File

@@ -0,0 +1,127 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { URI } from 'vs/base/common/uri';
import { asArray } from 'vs/base/common/arrays';
import { VSBuffer } from 'vs/base/common/buffer';
import { OutputTypes } from 'sql/workbench/services/notebook/common/contracts';
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
import { NotebookCellKind } from 'vs/workbench/api/common/extHostTypes';
export function convertToVSCodeNotebookCell(cellSource: string | string[], index: number, uri: URI, language: string): vscode.NotebookCell {
return <vscode.NotebookCell>{
index: index,
document: <vscode.TextDocument>{
uri: uri,
languageId: language,
getText: () => Array.isArray(cellSource) ? cellSource.join('') : cellSource,
},
notebook: <vscode.NotebookDocument>{
uri: uri
}
};
}
export function convertToADSCellOutput(outputs: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], executionOrder?: number): azdata.nb.IDisplayResult[] {
return asArray(outputs).map(output => {
let outputData = {};
for (let item of output.items) {
outputData[item.mime] = VSBuffer.wrap(item.data).toString();
}
return {
output_type: 'execute_result',
data: outputData,
execution_count: executionOrder,
metadata: output.metadata,
id: output.id
};
});
}
export function convertToVSCodeCellOutput(output: azdata.nb.ICellOutput): vscode.NotebookCellOutput {
let convertedOutputItems: vscode.NotebookCellOutputItem[];
switch (output.output_type) {
case OutputTypes.ExecuteResult:
case OutputTypes.DisplayData:
case OutputTypes.UpdateDisplayData:
let displayOutput = output as azdata.nb.IDisplayResult;
convertedOutputItems = Object.keys(displayOutput.data).map<vscode.NotebookCellOutputItem>(key => {
return {
mime: key,
data: VSBuffer.fromString(displayOutput.data[key]).buffer
};
});
break;
case OutputTypes.Stream:
let streamOutput = output as azdata.nb.IStreamResult;
convertedOutputItems = [{
mime: 'text/html',
data: VSBuffer.fromString(Array.isArray(streamOutput.text) ? streamOutput.text.join('') : streamOutput.text).buffer
}];
break;
case OutputTypes.Error:
let errorOutput = output as azdata.nb.IErrorResult;
let errorString = errorOutput.ename + ': ' + errorOutput.evalue + (errorOutput.traceback ? '\n' + errorOutput.traceback?.join('\n') : '');
convertedOutputItems = [{
mime: 'text/html',
data: VSBuffer.fromString(errorString).buffer
}];
break;
}
return {
items: convertedOutputItems,
metadata: output.metadata,
id: output.id
};
}
export function convertToADSNotebookContents(notebookData: vscode.NotebookData): azdata.nb.INotebookContents {
let result = {
cells: notebookData.cells?.map<azdata.nb.ICellContents>(cell => {
let executionOrder = cell.executionSummary?.executionOrder;
return {
cell_type: cell.kind === NotebookCellKind.Code ? 'code' : 'markdown',
source: cell.value,
metadata: {
language: cell.languageId
},
execution_count: executionOrder,
outputs: cell.outputs ? convertToADSCellOutput(cell.outputs, executionOrder) : undefined
};
}),
metadata: notebookData.metadata ?? {},
nbformat: notebookData.metadata?.custom?.nbformat ?? NBFORMAT,
nbformat_minor: notebookData.metadata?.custom?.nbformat_minor ?? NBFORMAT_MINOR
};
// Clear out extra lingering vscode custom metadata
delete result.metadata.custom;
return result;
}
export function convertToVSCodeNotebookData(notebook: azdata.nb.INotebookContents): vscode.NotebookData {
let result: vscode.NotebookData = {
cells: notebook.cells?.map<vscode.NotebookCellData>(cell => {
return {
kind: cell.cell_type === 'code' ? NotebookCellKind.Code : NotebookCellKind.Markup,
value: Array.isArray(cell.source) ? cell.source.join('\n') : cell.source,
languageId: cell.metadata?.language,
outputs: cell.outputs?.map<vscode.NotebookCellOutput>(output => convertToVSCodeCellOutput(output)),
executionSummary: {
executionOrder: cell.execution_count
}
};
}),
metadata: notebook.metadata
};
result.metadata.custom = {
nbformat: notebook.nbformat,
nbformat_minor: notebook.nbformat_minor
};
return result;
}

View File

@@ -5,9 +5,9 @@
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { ADSNotebookController } from 'sql/workbench/api/common/adsNotebookController';
import { ADSNotebookController } from 'sql/workbench/api/common/notebooks/adsNotebookController';
import * as nls from 'vs/nls';
import { URI } from 'vs/base/common/uri';
import { convertToVSCodeNotebookCell } from 'sql/workbench/api/common/notebooks/notebookUtils';
class VSCodeFuture implements azdata.nb.IFuture {
private _inProgress = true;
@@ -305,17 +305,3 @@ export class VSCodeExecuteProvider implements azdata.nb.NotebookExecuteProvider
// No-op
}
}
export function convertToVSCodeNotebookCell(cellSource: string | string[], index: number, uri: URI, language: string): vscode.NotebookCell {
return <vscode.NotebookCell>{
index: index,
document: <vscode.TextDocument>{
uri: uri,
languageId: language,
getText: () => Array.isArray(cellSource) ? cellSource.join('') : cellSource,
},
notebook: <vscode.NotebookDocument>{
uri: uri
}
};
}

View File

@@ -5,7 +5,7 @@
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { convertToVSCodeNotebookCell } from 'sql/workbench/api/common/vscodeExecuteProvider';
import { convertToVSCodeNotebookCell } from 'sql/workbench/api/common/notebooks/notebookUtils';
export class VSCodeNotebookDocument implements vscode.NotebookDocument {
private readonly _convertedCells: vscode.NotebookCell[];

View File

@@ -5,7 +5,7 @@
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { VSCodeNotebookDocument } from 'sql/workbench/api/common/vscodeNotebookDocument';
import { VSCodeNotebookDocument } from 'sql/workbench/api/common/notebooks/vscodeNotebookDocument';
import { functionalityNotSupportedError } from 'sql/base/common/locConstants';
export class VSCodeNotebookEditor implements vscode.NotebookEditor {

View File

@@ -0,0 +1,55 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { VSBuffer } from 'vs/base/common/buffer';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { convertToADSNotebookContents, convertToVSCodeNotebookData } from 'sql/workbench/api/common/notebooks/notebookUtils';
export class VSCodeContentManager implements azdata.nb.ContentManager {
constructor(private readonly _serializer: vscode.NotebookSerializer) {
}
public async deserializeNotebook(contents: string): Promise<azdata.nb.INotebookContents> {
let buffer = VSBuffer.fromString(contents);
let notebookData = await this._serializer.deserializeNotebook(buffer.buffer, new CancellationTokenSource().token);
return convertToADSNotebookContents(notebookData);
}
public async serializeNotebook(notebook: azdata.nb.INotebookContents): Promise<string> {
let notebookData = convertToVSCodeNotebookData(notebook);
let bytes = await this._serializer.serializeNotebook(notebookData, new CancellationTokenSource().token);
let buffer = VSBuffer.wrap(bytes);
return buffer.toString();
}
}
class VSCodeSerializationManager implements azdata.nb.SerializationManager {
private _manager: VSCodeContentManager;
constructor(serializer: vscode.NotebookSerializer) {
this._manager = new VSCodeContentManager(serializer);
}
public get contentManager(): azdata.nb.ContentManager {
return this._manager;
}
}
/**
* A Notebook Serialization Provider that is used to convert VS Code notebook extension APIs into ADS equivalents.
*/
export class VSCodeSerializationProvider implements azdata.nb.NotebookSerializationProvider {
private _manager: VSCodeSerializationManager;
constructor(public readonly providerId: string, serializer: vscode.NotebookSerializer) {
this._manager = new VSCodeSerializationManager(serializer);
}
public getSerializationManager(notebookUri: vscode.Uri): Thenable<azdata.nb.SerializationManager> {
return Promise.resolve(this._manager);
}
}

View File

@@ -1007,6 +1007,7 @@ export interface MainThreadNotebookDocumentsAndEditorsShape extends IDisposable
$clearAllOutputs(id: string): Promise<boolean>;
$changeKernel(id: string, kernel: azdata.nb.IKernelSpec): Promise<boolean>;
$registerNavigationProvider(providerId: string, handle: number);
$createNotebookDocument(providerId: string, contents: azdata.nb.INotebookContents): Promise<azdata.nb.NotebookDocument>;
}
export interface ExtHostExtensionManagementShape {

View File

@@ -1,150 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type * as vscode from 'vscode';
import type * as azdata from 'azdata';
import { VSBuffer } from 'vs/base/common/buffer';
import { NotebookCellKind } from 'vs/workbench/api/common/extHostTypes';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { OutputTypes } from 'sql/workbench/services/notebook/common/contracts';
import { asArray } from 'vs/base/common/arrays';
import { NBFORMAT, NBFORMAT_MINOR } from 'sql/workbench/common/constants';
export class VSCodeContentManager implements azdata.nb.ContentManager {
constructor(private readonly _serializer: vscode.NotebookSerializer) {
}
public async deserializeNotebook(contents: string): Promise<azdata.nb.INotebookContents> {
let buffer = VSBuffer.fromString(contents);
let notebookData = await this._serializer.deserializeNotebook(buffer.buffer, new CancellationTokenSource().token);
let result = {
cells: notebookData.cells?.map<azdata.nb.ICellContents>(cell => {
let executionOrder = cell.executionSummary?.executionOrder;
return {
cell_type: cell.kind === NotebookCellKind.Code ? 'code' : 'markdown',
source: cell.value,
metadata: {
language: cell.languageId
},
execution_count: executionOrder,
outputs: cell.outputs ? convertToADSCellOutput(cell.outputs, executionOrder) : undefined
};
}),
metadata: notebookData.metadata ?? {},
nbformat: notebookData.metadata?.custom?.nbformat ?? NBFORMAT,
nbformat_minor: notebookData.metadata?.custom?.nbformat_minor ?? NBFORMAT_MINOR
};
// Clear out extra lingering vscode custom metadata
delete result.metadata.custom;
return result;
}
public async serializeNotebook(notebook: azdata.nb.INotebookContents): Promise<string> {
let notebookData: vscode.NotebookData = {
cells: notebook.cells?.map<vscode.NotebookCellData>(cell => {
return {
kind: cell.cell_type === 'code' ? NotebookCellKind.Code : NotebookCellKind.Markup,
value: Array.isArray(cell.source) ? cell.source.join('\n') : cell.source,
languageId: cell.metadata?.language,
outputs: cell.outputs?.map<vscode.NotebookCellOutput>(output => convertToVSCodeCellOutput(output)),
executionSummary: {
executionOrder: cell.execution_count
}
};
}),
metadata: notebook.metadata
};
notebookData.metadata.custom = {
nbformat: notebook.nbformat,
nbformat_minor: notebook.nbformat_minor
};
let bytes = await this._serializer.serializeNotebook(notebookData, new CancellationTokenSource().token);
let buffer = VSBuffer.wrap(bytes);
return buffer.toString();
}
}
class VSCodeSerializationManager implements azdata.nb.SerializationManager {
private _manager: VSCodeContentManager;
constructor(serializer: vscode.NotebookSerializer) {
this._manager = new VSCodeContentManager(serializer);
}
public get contentManager(): azdata.nb.ContentManager {
return this._manager;
}
}
/**
* A Notebook Serialization Provider that is used to convert VS Code notebook extension APIs into ADS equivalents.
*/
export class VSCodeSerializationProvider implements azdata.nb.NotebookSerializationProvider {
private _manager: VSCodeSerializationManager;
constructor(public readonly providerId: string, serializer: vscode.NotebookSerializer) {
this._manager = new VSCodeSerializationManager(serializer);
}
public getSerializationManager(notebookUri: vscode.Uri): Thenable<azdata.nb.SerializationManager> {
return Promise.resolve(this._manager);
}
}
export function convertToADSCellOutput(outputs: vscode.NotebookCellOutput | vscode.NotebookCellOutput[], executionOrder?: number): azdata.nb.IDisplayResult[] {
return asArray(outputs).map(output => {
let outputData = {};
for (let item of output.items) {
outputData[item.mime] = VSBuffer.wrap(item.data).toString();
}
return {
output_type: 'execute_result',
data: outputData,
execution_count: executionOrder,
metadata: output.metadata,
id: output.id
};
});
}
export function convertToVSCodeCellOutput(output: azdata.nb.ICellOutput): vscode.NotebookCellOutput {
let convertedOutputItems: vscode.NotebookCellOutputItem[];
switch (output.output_type) {
case OutputTypes.ExecuteResult:
case OutputTypes.DisplayData:
case OutputTypes.UpdateDisplayData:
let displayOutput = output as azdata.nb.IDisplayResult;
convertedOutputItems = Object.keys(displayOutput.data).map<vscode.NotebookCellOutputItem>(key => {
return {
mime: key,
data: VSBuffer.fromString(displayOutput.data[key]).buffer
};
});
break;
case OutputTypes.Stream:
let streamOutput = output as azdata.nb.IStreamResult;
convertedOutputItems = [{
mime: 'text/html',
data: VSBuffer.fromString(Array.isArray(streamOutput.text) ? streamOutput.text.join('') : streamOutput.text).buffer
}];
break;
case OutputTypes.Error:
let errorOutput = output as azdata.nb.IErrorResult;
let errorString = errorOutput.ename + ': ' + errorOutput.evalue + (errorOutput.traceback ? '\n' + errorOutput.traceback?.join('\n') : '');
convertedOutputItems = [{
mime: 'text/html',
data: VSBuffer.fromString(errorString).buffer
}];
break;
}
return {
items: convertedOutputItems,
metadata: output.metadata,
id: output.id
};
}