Skip to content

Commit 933bdbc

Browse files
committed
add NotebookCell#index, microsoft#106637
1 parent b929102 commit 933bdbc

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

src/vs/vscode.proposed.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,6 +1313,7 @@ declare module 'vscode' {
13131313
}
13141314

13151315
export interface NotebookCell {
1316+
readonly index: number;
13161317
readonly notebook: NotebookDocument;
13171318
readonly uri: Uri;
13181319
readonly cellKind: CellKind;

src/vs/workbench/api/common/extHostNotebookDocument.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class ExtHostCell extends Disposable {
115115
const that = this;
116116
const document = this._extHostDocument.getDocument(this.uri)!.document;
117117
this._cell = Object.freeze({
118+
get index() { return that._notebook.getCellIndex(that); },
118119
notebook: that._notebook.notebookDocument,
119120
uri: that.uri,
120121
cellKind: this._cellData.cellKind,
@@ -487,6 +488,9 @@ export class ExtHostNotebookDocument extends Disposable {
487488
return this._cells.find(cell => cell.handle === cellHandle);
488489
}
489490

491+
getCellIndex(cell: ExtHostCell): number {
492+
return this._cells.indexOf(cell);
493+
}
490494

491495
addEdit(item: vscode.NotebookDocumentEditEvent): number {
492496
return this._edits.add([item]);

src/vs/workbench/test/browser/api/extHostNotebook.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,4 +257,51 @@ suite('NotebookCell#Document', function () {
257257
assert.equal(cells.document.notebook === notebook.notebookDocument, true);
258258
}
259259
});
260+
261+
test('cell#index', function () {
262+
263+
assert.equal(notebook.notebookDocument.cells.length, 2);
264+
const [first, second] = notebook.notebookDocument.cells;
265+
assert.equal(first.index, 0);
266+
assert.equal(second.index, 1);
267+
268+
// remove first cell
269+
extHostNotebooks.$acceptModelChanged(notebook.uri, {
270+
versionId: notebook.notebookDocument.version + 1,
271+
rawEvents: [{
272+
kind: NotebookCellsChangeType.ModelChange,
273+
changes: [[0, 1, []]]
274+
}]
275+
}, false);
276+
277+
assert.equal(notebook.notebookDocument.cells.length, 1);
278+
assert.equal(second.index, 0);
279+
280+
extHostNotebooks.$acceptModelChanged(notebookUri, {
281+
versionId: notebook.notebookDocument.version + 1,
282+
rawEvents: [{
283+
kind: NotebookCellsChangeType.ModelChange,
284+
changes: [[0, 0, [{
285+
handle: 2,
286+
uri: CellUri.generate(notebookUri, 2),
287+
source: ['Hello', 'World', 'Hello World!'],
288+
eol: '\n',
289+
language: 'test',
290+
cellKind: CellKind.Code,
291+
outputs: [],
292+
}, {
293+
handle: 3,
294+
uri: CellUri.generate(notebookUri, 3),
295+
source: ['Hallo', 'Welt', 'Hallo Welt!'],
296+
eol: '\n',
297+
language: 'test',
298+
cellKind: CellKind.Code,
299+
outputs: [],
300+
}]]]
301+
}]
302+
}, false);
303+
304+
assert.equal(notebook.notebookDocument.cells.length, 3);
305+
assert.equal(second.index, 2);
306+
});
260307
});

0 commit comments

Comments
 (0)