提交 0da75b92 编写于 作者: J Johannes Rieken

notebooks - use ResourceMap

上级 e3dc5a33
......@@ -25,6 +25,7 @@ import { asWebviewUri, WebviewInitData } from 'vs/workbench/api/common/shared/we
import { CellEditType, CellOutputKind, diff, ICellDeleteEdit, ICellEditOperation, ICellInsertEdit, IMainCellDto, INotebookDisplayOrder, INotebookEditData, INotebookKernelInfoDto2, IOutputRenderRequest, IOutputRenderResponse, IOutputRenderResponseCellInfo, IOutputRenderResponseOutputInfo, IProcessedOutput, IRawOutput, NotebookCellMetadata, NotebookCellsChangedEvent, NotebookCellsChangeType, NotebookCellsSplice2, NotebookDataDto, notebookDocumentMetadataDefaults } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import * as vscode from 'vscode';
import { Cache } from './cache';
import { ResourceMap } from 'vs/base/common/map';
interface IObservable<T> {
......@@ -881,8 +882,8 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
private readonly _notebookContentProviders = new Map<string, { readonly provider: vscode.NotebookContentProvider, readonly extension: IExtensionDescription; }>();
private readonly _notebookKernels = new Map<string, { readonly kernel: vscode.NotebookKernel, readonly extension: IExtensionDescription; }>();
private readonly _notebookKernelProviders = new Map<number, ExtHostNotebookKernelProviderAdapter>();
private readonly _documents = new Map<string, ExtHostNotebookDocument>();
private readonly _unInitializedDocuments = new Map<string, ExtHostNotebookDocument>();
private readonly _documents = new ResourceMap<ExtHostNotebookDocument>();
private readonly _unInitializedDocuments = new ResourceMap<ExtHostNotebookDocument>();
private readonly _editors = new Map<string, { editor: ExtHostNotebookEditor; }>();
private readonly _webviewComm = new Map<string, ExtHostWebviewCommWrapper>();
private readonly _notebookOutputRenderers = new Map<string, ExtHostNotebookOutputRenderer>();
......@@ -980,7 +981,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
throw new Error(`Notebook renderer for '${id}' is not registered`);
}
const document = this._documents.get(URI.revive(uriComponents).toString());
const document = this._documents.get(URI.revive(uriComponents));
if (!document) {
return;
......@@ -1018,7 +1019,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
throw new Error(`Notebook renderer for '${id}' is not registered`);
}
const document = this._documents.get(URI.revive(uriComponents).toString());
const document = this._documents.get(URI.revive(uriComponents));
if (!document) {
return;
......@@ -1076,7 +1077,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
const listener = provider.onDidChangeNotebook
? provider.onDidChangeNotebook(e => {
const document = this._documents.get(URI.revive(e.document.uri).toString());
const document = this._documents.get(URI.revive(e.document.uri));
if (!document) {
throw new Error(`Notebook document ${e.document.uri.toString()} not found`);
......@@ -1120,7 +1121,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
private _withAdapter<T>(handle: number, uri: UriComponents, callback: (adapter: ExtHostNotebookKernelProviderAdapter, document: ExtHostNotebookDocument) => Promise<T>) {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return [];
......@@ -1169,57 +1170,56 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
async $resolveNotebookData(viewType: string, uri: UriComponents, backupId?: string): Promise<NotebookDataDto | undefined> {
const provider = this._notebookContentProviders.get(viewType);
const revivedUri = URI.revive(uri);
if (!provider) {
return;
}
if (provider) {
let storageRoot: URI | undefined;
if (this._extensionStoragePaths) {
storageRoot = this._extensionStoragePaths.workspaceValue(provider.extension) ?? this._extensionStoragePaths.globalValue(provider.extension);
}
let storageRoot: URI | undefined;
if (this._extensionStoragePaths) {
storageRoot = this._extensionStoragePaths.workspaceValue(provider.extension) ?? this._extensionStoragePaths.globalValue(provider.extension);
}
let document = this._documents.get(URI.revive(uri).toString());
if (!document) {
const that = this;
document = this._unInitializedDocuments.get(revivedUri.toString()) ?? new ExtHostNotebookDocument(this._proxy, this._documentsAndEditors, {
emitModelChange(event: vscode.NotebookCellsChangeEvent): void {
that._onDidChangeNotebookCells.fire(event);
},
emitCellOutputsChange(event: vscode.NotebookCellOutputsChangeEvent): void {
that._onDidChangeCellOutputs.fire(event);
},
emitCellLanguageChange(event: vscode.NotebookCellLanguageChangeEvent): void {
that._onDidChangeCellLanguage.fire(event);
},
emitCellMetadataChange(event: vscode.NotebookCellMetadataChangeEvent): void {
that._onDidChangeCellMetadata.fire(event);
},
}, viewType, revivedUri, this, storageRoot);
this._unInitializedDocuments.set(revivedUri.toString(), document);
}
let document = this._documents.get(revivedUri);
const rawCells = await provider.provider.openNotebook(URI.revive(uri), { backupId });
const dto = {
metadata: {
...notebookDocumentMetadataDefaults,
...rawCells.metadata
if (!document) {
const that = this;
document = this._unInitializedDocuments.get(revivedUri) ?? new ExtHostNotebookDocument(this._proxy, this._documentsAndEditors, {
emitModelChange(event: vscode.NotebookCellsChangeEvent): void {
that._onDidChangeNotebookCells.fire(event);
},
languages: rawCells.languages,
cells: rawCells.cells.map(cell => ({
...cell,
outputs: cell.outputs.map(o => addIdToOutput(o))
})),
};
return dto;
emitCellOutputsChange(event: vscode.NotebookCellOutputsChangeEvent): void {
that._onDidChangeCellOutputs.fire(event);
},
emitCellLanguageChange(event: vscode.NotebookCellLanguageChangeEvent): void {
that._onDidChangeCellLanguage.fire(event);
},
emitCellMetadataChange(event: vscode.NotebookCellMetadataChangeEvent): void {
that._onDidChangeCellMetadata.fire(event);
},
}, viewType, revivedUri, this, storageRoot);
this._unInitializedDocuments.set(revivedUri, document);
}
return;
const rawCells = await provider.provider.openNotebook(URI.revive(uri), { backupId });
const dto = {
metadata: {
...notebookDocumentMetadataDefaults,
...rawCells.metadata
},
languages: rawCells.languages,
cells: rawCells.cells.map(cell => ({
...cell,
outputs: cell.outputs.map(o => addIdToOutput(o))
})),
};
return dto;
}
async $resolveNotebookEditor(viewType: string, uri: UriComponents, editorId: string): Promise<void> {
const provider = this._notebookContentProviders.get(viewType);
const revivedUri = URI.revive(uri);
const document = this._documents.get(revivedUri.toString());
const document = this._documents.get(revivedUri);
if (!document || !provider) {
return;
}
......@@ -1258,7 +1258,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $executeNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return;
......@@ -1279,7 +1279,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $cancelNotebookByAttachedKernel(viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return;
......@@ -1316,7 +1316,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $executeNotebook2(kernelId: string, viewType: string, uri: UriComponents, cellHandle: number | undefined): Promise<void> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document || document.viewType !== viewType) {
return;
......@@ -1338,7 +1338,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $saveNotebook(viewType: string, uri: UriComponents, token: CancellationToken): Promise<boolean> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return false;
}
......@@ -1352,7 +1352,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $saveNotebookAs(viewType: string, uri: UriComponents, target: UriComponents, token: CancellationToken): Promise<boolean> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return false;
}
......@@ -1366,7 +1366,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $undoNotebook(viewType: string, uri: UriComponents, editId: number, isDirty: boolean): Promise<void> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return;
}
......@@ -1376,7 +1376,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
async $redoNotebook(viewType: string, uri: UriComponents, editId: number, isDirty: boolean): Promise<void> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
if (!document) {
return;
}
......@@ -1386,7 +1386,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
async $backup(viewType: string, uri: UriComponents, cancellation: CancellationToken): Promise<string | undefined> {
const document = this._documents.get(URI.revive(uri).toString());
const document = this._documents.get(URI.revive(uri));
const provider = this._notebookContentProviders.get(viewType);
if (document && provider && provider.provider.backupNotebook) {
......@@ -1435,7 +1435,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
$acceptModelChanged(uriComponents: UriComponents, event: NotebookCellsChangedEvent): void {
const document = this._documents.get(URI.revive(uriComponents).toString());
const document = this._documents.get(URI.revive(uriComponents));
if (document) {
document.acceptModelChanged(event);
......@@ -1443,7 +1443,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
public $acceptModelSaved(uriComponents: UriComponents): void {
const document = this._documents.get(URI.revive(uriComponents).toString());
const document = this._documents.get(URI.revive(uriComponents));
if (document) {
// this.$acceptDirtyStateChanged(uriComponents, false);
this._onDidSaveNotebookDocument.fire(document);
......@@ -1519,18 +1519,17 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
if (delta.removedDocuments) {
delta.removedDocuments.forEach((uri) => {
const revivedUri = URI.revive(uri);
const revivedUriStr = revivedUri.toString();
const document = this._documents.get(revivedUriStr);
const document = this._documents.get(revivedUri);
if (document) {
document.dispose();
this._documents.delete(revivedUriStr);
this._documents.delete(revivedUri);
this._documentsAndEditors.$acceptDocumentsAndEditorsDelta({ removedDocuments: document.cells.map(cell => cell.uri) });
this._onDidCloseNotebookDocument.fire(document);
}
[...this._editors.values()].forEach((e) => {
if (e.editor.uri.toString() === revivedUriStr) {
if (e.editor.uri.toString() === revivedUri.toString()) {
e.editor.dispose();
this._editors.delete(e.editor.id);
editorChanged = true;
......@@ -1545,7 +1544,6 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
delta.addedDocuments.forEach(modelData => {
const revivedUri = URI.revive(modelData.uri);
const revivedUriStr = revivedUri.toString();
const viewType = modelData.viewType;
const entry = this._notebookContentProviders.get(viewType);
let storageRoot: URI | undefined;
......@@ -1553,10 +1551,10 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
storageRoot = this._extensionStoragePaths.workspaceValue(entry.extension) ?? this._extensionStoragePaths.globalValue(entry.extension);
}
if (!this._documents.has(revivedUriStr)) {
if (!this._documents.has(revivedUri)) {
const that = this;
const document = this._unInitializedDocuments.get(revivedUriStr) ?? new ExtHostNotebookDocument(this._proxy, this._documentsAndEditors, {
const document = this._unInitializedDocuments.get(revivedUri) ?? new ExtHostNotebookDocument(this._proxy, this._documentsAndEditors, {
emitModelChange(event: vscode.NotebookCellsChangeEvent): void {
that._onDidChangeNotebookCells.fire(event);
},
......@@ -1571,7 +1569,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
}, viewType, revivedUri, this, storageRoot);
this._unInitializedDocuments.delete(revivedUriStr);
this._unInitializedDocuments.delete(revivedUri);
if (modelData.metadata) {
document.metadata = {
...notebookDocumentMetadataDefaults,
......@@ -1592,8 +1590,8 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
// add cell document as vscode.TextDocument
addedCellDocuments.push(...modelData.cells.map(ExtHostCell.asModelAddData));
this._documents.get(revivedUriStr)?.dispose();
this._documents.set(revivedUriStr, document);
this._documents.get(revivedUri)?.dispose();
this._documents.set(revivedUri, document);
// create editor if populated
if (modelData.attachedEditor) {
......@@ -1606,7 +1604,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
addedDocuments: addedCellDocuments
});
const document = this._documents.get(revivedUriStr)!;
const document = this._documents.get(revivedUri)!;
this._onDidOpenNotebookDocument.fire(document);
});
}
......@@ -1618,7 +1616,7 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN
}
const revivedUri = URI.revive(editorModelData.documentUri);
const document = this._documents.get(revivedUri.toString());
const document = this._documents.get(revivedUri);
if (document) {
this._createExtHostEditor(document, editorModelData.id, editorModelData.selections);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册