diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index b010b799b11..a3d42a81f89 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1313,6 +1313,7 @@ declare module 'vscode' { } export interface NotebookCell { + readonly index: number; readonly notebook: NotebookDocument; readonly uri: Uri; readonly cellKind: CellKind; diff --git a/src/vs/workbench/api/common/extHostNotebookDocument.ts b/src/vs/workbench/api/common/extHostNotebookDocument.ts index c7136fa76d8..c881d9a5d36 100644 --- a/src/vs/workbench/api/common/extHostNotebookDocument.ts +++ b/src/vs/workbench/api/common/extHostNotebookDocument.ts @@ -115,6 +115,7 @@ export class ExtHostCell extends Disposable { const that = this; const document = this._extHostDocument.getDocument(this.uri)!.document; this._cell = Object.freeze({ + get index() { return that._notebook.getCellIndex(that); }, notebook: that._notebook.notebookDocument, uri: that.uri, cellKind: this._cellData.cellKind, @@ -487,6 +488,9 @@ export class ExtHostNotebookDocument extends Disposable { return this._cells.find(cell => cell.handle === cellHandle); } + getCellIndex(cell: ExtHostCell): number { + return this._cells.indexOf(cell); + } addEdit(item: vscode.NotebookDocumentEditEvent): number { return this._edits.add([item]); diff --git a/src/vs/workbench/test/browser/api/extHostNotebook.test.ts b/src/vs/workbench/test/browser/api/extHostNotebook.test.ts index 95fffee5ecf..1bdb68cac39 100644 --- a/src/vs/workbench/test/browser/api/extHostNotebook.test.ts +++ b/src/vs/workbench/test/browser/api/extHostNotebook.test.ts @@ -257,4 +257,51 @@ suite('NotebookCell#Document', function () { assert.equal(cells.document.notebook === notebook.notebookDocument, true); } }); + + test('cell#index', function () { + + assert.equal(notebook.notebookDocument.cells.length, 2); + const [first, second] = notebook.notebookDocument.cells; + assert.equal(first.index, 0); + assert.equal(second.index, 1); + + // remove first cell + extHostNotebooks.$acceptModelChanged(notebook.uri, { + versionId: notebook.notebookDocument.version + 1, + rawEvents: [{ + kind: NotebookCellsChangeType.ModelChange, + changes: [[0, 1, []]] + }] + }, false); + + assert.equal(notebook.notebookDocument.cells.length, 1); + assert.equal(second.index, 0); + + extHostNotebooks.$acceptModelChanged(notebookUri, { + versionId: notebook.notebookDocument.version + 1, + rawEvents: [{ + kind: NotebookCellsChangeType.ModelChange, + changes: [[0, 0, [{ + handle: 2, + uri: CellUri.generate(notebookUri, 2), + source: ['Hello', 'World', 'Hello World!'], + eol: '\n', + language: 'test', + cellKind: CellKind.Code, + outputs: [], + }, { + handle: 3, + uri: CellUri.generate(notebookUri, 3), + source: ['Hallo', 'Welt', 'Hallo Welt!'], + eol: '\n', + language: 'test', + cellKind: CellKind.Code, + outputs: [], + }]]] + }] + }, false); + + assert.equal(notebook.notebookDocument.cells.length, 3); + assert.equal(second.index, 2); + }); });