From d1354bb26be8d41ea713a837d70d16e5a93f5475 Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Wed, 11 Mar 2020 11:09:39 +0100 Subject: [PATCH] adopt CellUri --- .../workbench/api/common/extHostNotebook.ts | 82 +++++-------------- .../notebook/browser/notebook.contribution.ts | 31 ++++--- .../contrib/notebook/common/notebookCommon.ts | 27 ------ .../notebook/test/testNotebookEditor.ts | 9 +- 4 files changed, 42 insertions(+), 107 deletions(-) diff --git a/src/vs/workbench/api/common/extHostNotebook.ts b/src/vs/workbench/api/common/extHostNotebook.ts index d0f274f21a4..22df2ef1ddf 100644 --- a/src/vs/workbench/api/common/extHostNotebook.ts +++ b/src/vs/workbench/api/common/extHostNotebook.ts @@ -12,7 +12,7 @@ import { DisposableStore, Disposable } from 'vs/base/common/lifecycle'; import { readonly } from 'vs/base/common/errors'; import { Emitter, Event } from 'vs/base/common/event'; import { ExtHostDocumentsAndEditors } from 'vs/workbench/api/common/extHostDocumentsAndEditors'; -import { INotebookDisplayOrder, parseCellUri, parseCellHandle, ITransformedDisplayOutputDto, IOrderedMimeType, IStreamOutput, IErrorOutput, mimeTypeSupportedByCore, IOutput, sortMimeTypes, diff, generateCellPath } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { INotebookDisplayOrder, ITransformedDisplayOutputDto, IOrderedMimeType, IStreamOutput, IErrorOutput, mimeTypeSupportedByCore, IOutput, sortMimeTypes, diff, CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { ISplice } from 'vs/base/common/sequence'; export class ExtHostCell implements vscode.NotebookCell { @@ -312,17 +312,15 @@ export class ExtHostNotebookDocument extends Disposable implements vscode.Notebo return this.cells.find(cell => cell.handle === cellHandle); } - attachCellTextDocument(cellHandle: number, textDocument: vscode.TextDocument) { - let cell = this.cells.find(cell => cell.handle === cellHandle); - + attachCellTextDocument(textDocument: vscode.TextDocument) { + let cell = this.cells.find(cell => cell.uri.toString() === textDocument.uri.toString()); if (cell) { cell.attachTextDocument(textDocument); } } - detachCellTextDocument(cellHandle: number, textDocument: vscode.TextDocument) { - let cell = this.cells.find(cell => cell.handle === cellHandle); - + detachCellTextDocument(textDocument: vscode.TextDocument) { + let cell = this.cells.find(cell => cell.uri.toString() === textDocument.uri.toString()); if (cell) { cell.detachTextDocument(textDocument); } @@ -342,44 +340,22 @@ export class ExtHostNotebookEditor extends Disposable implements vscode.Notebook ) { super(); this._register(this._documentsAndEditors.onDidAddDocuments(documents => { - for (const data of documents) { - let textDocument = data.document; - let parsedCellUri = parseCellUri(textDocument.uri); - - if (!parsedCellUri) { - continue; - } - - let notebookUri = parsedCellUri.notebook; - let cellFsPath = textDocument.uri.fsPath; - - const cellHandle = parseCellHandle(cellFsPath); - - if (cellHandle !== undefined) { - if (this.document.uri.fsPath === notebookUri.fsPath) { - document.attachCellTextDocument(cellHandle, textDocument); + for (const { document: textDocument } of documents) { + let data = CellUri.parse(textDocument.uri); + if (data) { + if (this.document.uri.toString() === data.notebook.toString()) { + document.attachCellTextDocument(textDocument); } } } })); this._register(this._documentsAndEditors.onDidRemoveDocuments(documents => { - for (const data of documents) { - let textDocument = data.document; - let parsedCellUri = parseCellUri(textDocument.uri); - - if (!parsedCellUri) { - continue; - } - - let notebookUri = parsedCellUri.notebook; - let cellFsPath = textDocument.uri.fsPath; - - const cellHandle = parseCellHandle(cellFsPath); - - if (cellHandle !== undefined) { - if (this.document.uri.fsPath === notebookUri.fsPath) { - document.detachCellTextDocument(cellHandle, textDocument); + for (const { document: textDocument } of documents) { + let data = CellUri.parse(textDocument.uri); + if (data) { + if (this.document.uri.toString() === data.notebook.toString()) { + document.detachCellTextDocument(textDocument); } } } @@ -388,12 +364,7 @@ export class ExtHostNotebookEditor extends Disposable implements vscode.Notebook createCell(content: string, language: string, type: CellKind, outputs: vscode.CellOutput[]): vscode.NotebookCell { const handle = ExtHostNotebookEditor._cellhandlePool++; - const uri = URI.from({ - scheme: 'vscode-notebook', - authority: this.document.viewType, - path: generateCellPath(type, handle), - query: this.document.uri.toString() - }); + const uri = CellUri.generate(this.document.uri, handle); const cell = new ExtHostCell(handle, uri, content, type, language, outputs); return cell; } @@ -568,27 +539,14 @@ export class ExtHostNotebookController implements ExtHostNotebookShape, ExtHostN document?.insertCell(index, rawCell!); let allDocuments = this._documentsAndEditors.allDocuments(); - for (let i = 0; i < allDocuments.length; i++) { - let textDocument = allDocuments[i].document; - let parsedCellUri = parseCellUri(textDocument.uri); - - if (!parsedCellUri) { - continue; - } - - let notebookUri = parsedCellUri.notebook; - let cellFsPath = textDocument.uri.fsPath; - const cellHandle = parseCellHandle(cellFsPath); - - if (cellHandle !== undefined) { - if (uri.fsPath === notebookUri.fsPath && Number(cellHandle) === rawCell.handle) { + for (let { document: textDocument } of allDocuments) { + let data = CellUri.parse(textDocument.uri); + if (data) { + if (uri.toString() === data.notebook.toString() && textDocument.uri.toString() === rawCell.uri.toString()) { rawCell.attachTextDocument(textDocument); } - } } - - return { uri: rawCell.uri, handle: rawCell.handle, diff --git a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts index e10c3fcde87..4e3e2f82da4 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts @@ -27,9 +27,8 @@ import { IModeService } from 'vs/editor/common/services/modeService'; import { IDisposable } from 'vs/base/common/lifecycle'; import { assertType } from 'vs/base/common/types'; import { parse } from 'vs/base/common/marshalling'; -import { parseCellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { ResourceMap } from 'vs/base/common/map'; -import { isFalsyOrEmpty } from 'vs/base/common/arrays'; // Output renderers registration @@ -40,6 +39,7 @@ import 'vs/workbench/contrib/notebook/browser/view/output/transforms/richTransfo // Actions import 'vs/workbench/contrib/notebook/browser/contrib/notebookActions'; import { basename } from 'vs/base/common/resources'; +import { NotebookProviderInfo } from 'vs/workbench/contrib/notebook/common/notebookProvider'; Registry.as(EditorExtensions.Editors).registerEditor( EditorDescriptor.create( @@ -86,6 +86,10 @@ Registry.as(EditorInputExtensions.EditorInputFactor } ); +function getFirstNotebookInfo(notebookService: INotebookService, uri: URI): NotebookProviderInfo | undefined { + return notebookService.getContributedNotebookProviders(uri)[0]; +} + export class NotebookContribution implements IWorkbenchContribution { private _resourceMapping = new ResourceMap(); @@ -111,18 +115,18 @@ export class NotebookContribution implements IWorkbenchContribution { return undefined; } - const data = parseCellUri(resource); - if (data) { + let info: NotebookProviderInfo | undefined; + const data = CellUri.parse(resource); + if (data && (info = getFirstNotebookInfo(this.notebookService, data.notebook))) { // cell-uri -> open (container) notebook const name = basename(data.notebook); - const input = this.instantiationService.createInstance(NotebookEditorInput, data.notebook, name, data.viewType); + const input = this.instantiationService.createInstance(NotebookEditorInput, data.notebook, name, info.id); this._resourceMapping.set(resource, input); return { override: this.editorService.openEditor(input, new NotebookEditorOptions({ ...options, forceReload: true, cellOptions: { resource, options } }), group) }; } - const notebookProviders = this.notebookService.getContributedNotebookProviders(resource); - const viewType = !isFalsyOrEmpty(notebookProviders) ? notebookProviders[0].id : undefined; - if (viewType === undefined) { + info = getFirstNotebookInfo(this.notebookService, resource); + if (!info) { return undefined; } @@ -134,7 +138,7 @@ export class NotebookContribution implements IWorkbenchContribution { } } - const input = this.instantiationService.createInstance(NotebookEditorInput, resource, originalInput.getName(), viewType); + const input = this.instantiationService.createInstance(NotebookEditorInput, resource, originalInput.getName(), info.id); this._resourceMapping.set(resource, input); return { override: this.editorService.openEditor(input, options, group) }; @@ -163,11 +167,16 @@ class CellContentProvider implements ITextModelContentProvider { if (existing) { return existing; } - const data = parseCellUri(resource); + const data = CellUri.parse(resource); + // const data = parseCellUri(resource); if (!data) { return null; } - const notebook = await this._notebookService.resolveNotebook(data.viewType, data.notebook); + const info = getFirstNotebookInfo(this._notebookService, data.notebook); + if (!info) { + return null; + } + const notebook = await this._notebookService.resolveNotebook(info.id, data.notebook); if (!notebook) { return null; } diff --git a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts index f64073f50c3..a3cd4890ff3 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts @@ -171,22 +171,6 @@ export type NotebookCellOutputsSplice = [ IOutput[] ]; -export function parseCellUri(resource: URI): { viewType: string, notebook: URI } | undefined { - //vscode-notebook:///cell_.ext - if (resource.scheme !== 'vscode-notebook') { - return undefined; - } - // @todo Jo,Peng: `authority` will be transformed to lower case in `URI.toString()`, so we won't retrive the same viewType later on. - const viewType = resource.authority; - const notebook = URI.parse(resource.query); - return { viewType, notebook }; -} - -export function generateCellPath(cellKind: CellKind, cellHandle: number): string { - return `/cell_${cellHandle}${cellKind === CellKind.Markdown ? '.md' : ''}`; -} - - export namespace CellUri { export const scheme = 'vscode-notebook'; @@ -214,17 +198,6 @@ export namespace CellUri { } } -export function parseCellHandle(path: string): number | undefined { - const regex = new RegExp(/cell_(\d*)(\.)?/g); - let matches = regex.exec(path); - - if (matches && matches.length > 1) { - return Number(matches[1]); - } - - return; -} - export function mimeTypeSupportedByCore(mimeType: string) { if ([ 'application/json', diff --git a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts index b20c40caf5a..3402ed06f14 100644 --- a/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts +++ b/src/vs/workbench/contrib/notebook/test/testNotebookEditor.ts @@ -7,7 +7,7 @@ import { Emitter, Event } from 'vs/base/common/event'; import { Disposable } from 'vs/base/common/lifecycle'; import { URI } from 'vs/base/common/uri'; import { PieceTreeTextBufferFactory } from 'vs/editor/common/model/pieceTreeTextBuffer/pieceTreeTextBufferBuilder'; -import { CellKind, generateCellPath, ICell, INotebook, IOutput, NotebookCellOutputsSplice, NotebookCellsSplice } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { CellKind, ICell, INotebook, IOutput, NotebookCellOutputsSplice, NotebookCellsSplice, CellUri } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { NotebookViewModel, IModelDecorationsChangeAccessor } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookCellViewModel'; @@ -45,12 +45,7 @@ export class TestCell implements ICell { outputs: IOutput[] ) { this._outputs = outputs; - this.uri = URI.from({ - scheme: 'vscode-notebook', - authority: viewType, - path: generateCellPath(cellKind, handle), - query: '' - }); + this.uri = CellUri.generate(URI.parse('test:///fake/notebook'), handle); } resolveTextBufferFactory(): PieceTreeTextBufferFactory { -- GitLab