Skip to content

Commit

Permalink
add NotebookDocumentContentCellChange#document, #145793
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Mar 23, 2022
1 parent c86a266 commit 9d23244
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/vs/workbench/api/common/extHost.protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2040,7 +2040,7 @@ export type NotebookRawContentEventDto =
| notebookCommon.NotebookCellsChangeMetadataEvent
| notebookCommon.NotebookCellsChangeInternalMetadataEvent
// | notebookCommon.NotebookDocumentChangeMetadataEvent
// | notebookCommon.NotebookCellContentChangeEvent
| notebookCommon.NotebookCellContentChangeEvent
// | notebookCommon.NotebookDocumentUnknownChangeEvent
;

Expand Down
6 changes: 6 additions & 0 deletions src/vs/workbench/api/common/extHostNotebookDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ export class ExtHostNotebookDocument {

} else if (rawEvent.kind === notebookCommon.NotebookCellsChangeType.ChangeLanguage) {
this._changeCellLanguage(rawEvent.index, rawEvent.language);
relaxedCellChanges.push({ cell: this._cells[rawEvent.index].apiCell, document: this._cells[rawEvent.index].apiCell.document });

} else if (rawEvent.kind === notebookCommon.NotebookCellsChangeType.ChangeCellContent) {
relaxedCellChanges.push({ cell: this._cells[rawEvent.index].apiCell, document: this._cells[rawEvent.index].apiCell.document });

} else if (rawEvent.kind === notebookCommon.NotebookCellsChangeType.ChangeCellMime) {
this._changeCellMime(rawEvent.index, rawEvent.mime);
} else if (rawEvent.kind === notebookCommon.NotebookCellsChangeType.ChangeCellMetadata) {
Expand All @@ -282,6 +287,7 @@ export class ExtHostNotebookDocument {
const existing = map.get(relaxedCellChange.cell);
if (existing === undefined) {
const newLen = result.cellChanges.push({
document: undefined,
executionSummary: undefined,
metadata: undefined,
outputs: undefined,
Expand Down
62 changes: 62 additions & 0 deletions src/vs/workbench/api/test/browser/extHostNotebook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,12 @@ suite('NotebookCell#Document', function () {
assert.deepStrictEqual(first.metadata, first.cell.metadata);
assert.deepStrictEqual(first.executionSummary, undefined);
assert.deepStrictEqual(first.outputs, undefined);
assert.deepStrictEqual(first.document, undefined);

assert.deepStrictEqual(second.outputs, second.cell.outputs);
assert.deepStrictEqual(second.metadata, second.cell.metadata);
assert.deepStrictEqual(second.executionSummary, undefined);
assert.deepStrictEqual(second.document, undefined);
});

test('onDidChangeNotebook-event, notebook metadata', async function () {
Expand Down Expand Up @@ -493,4 +495,64 @@ suite('NotebookCell#Document', function () {
assert.ok(Object.isFrozen(event.notebook));
assert.ok(!Object.isFrozen(event.metadata));
});

test('change cell language and onDidChangeNotebookDocument', async function () {

const p = Event.toPromise(extHostNotebookDocuments.onDidChangeNotebookDocument);

const first = notebook.apiNotebook.cellAt(0);
assert.strictEqual(first.document.languageId, 'markdown');

extHostNotebookDocuments.$acceptModelChanged(notebook.uri, new SerializableObjectWithBuffers({
versionId: 12,
rawEvents: [{
kind: NotebookCellsChangeType.ChangeLanguage,
index: 0,
language: 'fooLang'
}]
}), false);

const event = await p;

assert.strictEqual(event.notebook === notebook.apiNotebook, true);
assert.strictEqual(event.contentChanges.length, 0);
assert.strictEqual(event.cellChanges.length, 1);

const [cellChange] = event.cellChanges;

assert.strictEqual(cellChange.cell === first, true);
assert.ok(cellChange.document === first.document);
assert.ok(cellChange.executionSummary === undefined);
assert.ok(cellChange.metadata === undefined);
assert.ok(cellChange.outputs === undefined);
});

test('change notebook cell document and onDidChangeNotebookDocument', async function () {

const p = Event.toPromise(extHostNotebookDocuments.onDidChangeNotebookDocument);

const first = notebook.apiNotebook.cellAt(0);

extHostNotebookDocuments.$acceptModelChanged(notebook.uri, new SerializableObjectWithBuffers({
versionId: 12,
rawEvents: [{
kind: NotebookCellsChangeType.ChangeCellContent,
index: 0
}]
}), false);

const event = await p;

assert.strictEqual(event.notebook === notebook.apiNotebook, true);
assert.strictEqual(event.contentChanges.length, 0);
assert.strictEqual(event.cellChanges.length, 1);

const [cellChange] = event.cellChanges;

assert.strictEqual(cellChange.cell === first, true);
assert.ok(cellChange.document === first.document);
assert.ok(cellChange.executionSummary === undefined);
assert.ok(cellChange.metadata === undefined);
assert.ok(cellChange.outputs === undefined);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
switch (e) {
case 'content':
this._pauseableEmitter.fire({
rawEvents: [{ kind: NotebookCellsChangeType.ChangeCellContent, transient: false }],
rawEvents: [{ kind: NotebookCellsChangeType.ChangeCellContent, index: this._getCellIndexByHandle(cell.handle), transient: false }],
versionId: this.versionId,
synchronous: true,
endSelectionState: undefined
Expand Down
1 change: 1 addition & 0 deletions src/vs/workbench/contrib/notebook/common/notebookCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ export interface NotebookCellsInitializeEvent<T> {

export interface NotebookCellContentChangeEvent {
readonly kind: NotebookCellsChangeType.ChangeCellContent;
readonly index: number;
}

export interface NotebookCellsModelChangedEvent<T> {
Expand Down
8 changes: 8 additions & 0 deletions src/vscode-dts/vscode.proposed.notebookDocumentEvents.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ declare module 'vscode' {
*/
readonly cell: NotebookCell;

/**
* The document of the cell or `undefined` when it did not change.
*
* *Note* that you should use the {@link workspace.onDidChangeTextDocument onDidChangeTextDocument}-event
* for detailed change information, like what edits have been performed.
*/
readonly document: TextDocument | undefined;

/**
* The new metadata of the cell or `undefined` when it did not change.
*/
Expand Down

0 comments on commit 9d23244

Please sign in to comment.