Skip to content

Commit 78f4b54

Browse files
authored
Support reverting cell metadata & cell language in notebook diff view (microsoft#226747)
1 parent 7473068 commit 78f4b54

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/vs/workbench/contrib/notebook/browser/diff/diffElementViewModel.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -824,7 +824,12 @@ export function getFormattedMetadataJSON(documentTextModel: INotebookTextModel,
824824
language,
825825
...filteredMetadata
826826
};
827-
827+
// Give preference to the language we have been given.
828+
// Metadata can contain `language` due to round-tripping of cell metadata.
829+
// I.e. we add it here, and then from SCM when we revert the cell, we get this same metadata back with the `language` property.
830+
if (language) {
831+
obj.language = language;
832+
}
828833
const metadataSource = toFormattedString(obj, {});
829834

830835
return metadataSource;

src/vs/workbench/contrib/notebook/browser/diff/notebookDiffActions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { ICommandActionTitle } from 'vs/platform/action/common/action';
2222
import { DEFAULT_EDITOR_ASSOCIATION } from 'vs/workbench/common/editor';
2323
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
2424
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
25-
import { CellEditType, NOTEBOOK_DIFF_EDITOR_ID } from 'vs/workbench/contrib/notebook/common/notebookCommon';
25+
import { CellEditType, ICellEditOperation, NOTEBOOK_DIFF_EDITOR_ID } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2626
import { ITextResourceConfigurationService } from 'vs/editor/common/services/textResourceConfiguration';
2727
import { NotebookMultiTextDiffEditor } from 'vs/workbench/contrib/notebook/browser/diff/notebookMultiDiffEditor';
2828
import { Codicon } from 'vs/base/common/codicons';
@@ -307,14 +307,24 @@ registerAction2(class extends Action2 {
307307
return;
308308
}
309309

310+
if (!(context.cell instanceof SideBySideDiffElementViewModel)) {
311+
return;
312+
}
313+
310314
const original = context.cell.original;
311315
const modified = context.cell.modified;
312316

313-
if (!original || !modified) {
317+
const modifiedCellIndex = context.cell.mainDocumentTextModel.cells.indexOf(modified.textModel);
318+
if (modifiedCellIndex === -1) {
314319
return;
315320
}
316321

317-
modified.textModel.metadata = original.metadata;
322+
const rawEdits: ICellEditOperation[] = [{ editType: CellEditType.Metadata, index: modifiedCellIndex, metadata: original.metadata }];
323+
if (context.cell.original.language && context.cell.modified.language !== context.cell.original.language) {
324+
rawEdits.push({ editType: CellEditType.CellLanguage, index: modifiedCellIndex, language: context.cell.original.language });
325+
}
326+
327+
context.cell.modifiedDocument.applyEdits(rawEdits, true, undefined, () => undefined, undefined, true);
318328
}
319329
});
320330

src/vs/workbench/contrib/notebook/browser/notebook.contribution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ class CellInfoContentProvider {
449449
resource
450450
);
451451
this._disposables.push(disposables.add(ref.object.notebook.onDidChangeContent(e => {
452-
if (result && e.rawEvents.some(event => event.kind === NotebookCellsChangeType.ChangeCellMetadata && event.index === cellIndex)) {
452+
if (result && e.rawEvents.some(event => (event.kind === NotebookCellsChangeType.ChangeCellMetadata || event.kind === NotebookCellsChangeType.ChangeCellLanguage) && event.index === cellIndex)) {
453453
const value = getFormattedMetadataJSON(ref.object.notebook, cell.metadata, cell.language);
454454
if (result.getValue() !== value) {
455455
result.setValue(getFormattedMetadataJSON(ref.object.notebook, cell.metadata, cell.language));

0 commit comments

Comments
 (0)