From 13c66745802d895e655f08fc7796146e36b6deaf Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 26 Mar 2021 12:32:57 +0100 Subject: [PATCH] add `NotebookDocument#getCells(range?)`, https://github.com/microsoft/vscode/issues/119602 --- src/vs/vscode.proposed.d.ts | 16 +++++++++- .../api/common/extHostNotebookDocument.ts | 29 +++++++++++++++++-- src/vs/workbench/api/common/extHostTypes.ts | 16 ++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 407f4ad13b4..4245c92aeb0 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1067,13 +1067,24 @@ declare module 'vscode' { readonly isDirty: boolean; readonly isUntitled: boolean; - readonly cells: ReadonlyArray; readonly metadata: NotebookDocumentMetadata; // todo@API should we really expose this? readonly viewType: string; + /** @deprecated Use `getCells(<...>) instead */ + readonly cells: ReadonlyArray; + + /** + * Get the cells of this notebook. A subset can be retrieved by providing + * a range. The range will be adjuset to the notebook. + * + * @param range A notebook range. + * @returns The cells contained by the range or all cells. + */ + getCells(range?: NotebookCellRange): ReadonlyArray; + /** * Save the document. The saving will be handled by the corresponding content provider * @@ -1084,6 +1095,7 @@ declare module 'vscode' { save(): Thenable; } + // todo@API RENAME to NotebookRange // todo@API maybe have a NotebookCellPosition sibling export class NotebookCellRange { readonly start: number; @@ -1095,6 +1107,8 @@ declare module 'vscode' { readonly isEmpty: boolean; constructor(start: number, end: number); + + with(change: { start?: number, end?: number }): NotebookCellRange; } export enum NotebookEditorRevealType { diff --git a/src/vs/workbench/api/common/extHostNotebookDocument.ts b/src/vs/workbench/api/common/extHostNotebookDocument.ts index c923100305a..aaf7203dfd2 100644 --- a/src/vs/workbench/api/common/extHostNotebookDocument.ts +++ b/src/vs/workbench/api/common/extHostNotebookDocument.ts @@ -177,10 +177,16 @@ export class ExtHostNotebookDocument extends Disposable { get viewType() { return that._viewType; }, get isDirty() { return that._isDirty; }, get isUntitled() { return that.uri.scheme === Schemas.untitled; }, - get cells(): ReadonlyArray { return that._cells.map(cell => cell.cell); }, get metadata() { return that._metadata; }, set metadata(_value: Required) { throw new Error('Use WorkspaceEdit to update metadata.'); }, - save() { return that._save(); } + get cells(): ReadonlyArray { return that._cells.map(cell => cell.cell); }, + getCells(range) { + const cells = range ? that._getCells(range) : that._cells; + return cells.map(cell => cell.cell); + }, + save() { + return that._save(); + } }); } return this._notebook; @@ -225,6 +231,25 @@ export class ExtHostNotebookDocument extends Disposable { } } + private _validateRange(range: vscode.NotebookCellRange): vscode.NotebookCellRange { + if (range.start < 0) { + range = range.with({ start: 0 }); + } + if (range.end > this._cells.length) { + range = range.with({ end: this._cells.length }); + } + return range; + } + + private _getCells(range: vscode.NotebookCellRange): ExtHostCell[] { + range = this._validateRange(range); + const result: ExtHostCell[] = []; + for (let i = range.start; i < range.end; i++) { + result.push(this._cells[i]); + } + return result; + } + private async _save(): Promise { if (this._disposed) { return Promise.reject(new Error('Notebook has been closed')); diff --git a/src/vs/workbench/api/common/extHostTypes.ts b/src/vs/workbench/api/common/extHostTypes.ts index 655b37984c4..cc7cd797140 100644 --- a/src/vs/workbench/api/common/extHostTypes.ts +++ b/src/vs/workbench/api/common/extHostTypes.ts @@ -2922,6 +2922,22 @@ export class NotebookCellRange { this._start = start; this._end = end; } + + with(change: { start?: number, end?: number }): NotebookCellRange { + let start = this._start; + let end = this._end; + + if (change.start !== undefined) { + start = change.start; + } + if (change.end !== undefined) { + end = change.end; + } + if (start === this._start && end === this._end) { + return this; + } + return new NotebookCellRange(start, end); + } } export class NotebookCellMetadata {