Skip to content

Commit a14ebdf

Browse files
committed
Remove inputCollapsed and outputCollapsed metadata, make them view properties
Fix microsoft#125274
1 parent 2f1e5cb commit a14ebdf

19 files changed

+164
-182
lines changed

extensions/ipynb/src/ipynbMain.ts

-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ export function activate(context: vscode.ExtensionContext) {
3333
transientOutputs: false,
3434
transientCellMetadata: {
3535
breakpointMargin: true,
36-
inputCollapsed: true,
37-
outputCollapsed: true,
3836
custom: false
3937
}
4038
}));

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

+39-56
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55

66
import { KeyChord, KeyCode, KeyMod } from 'vs/base/common/keyCodes';
77
import { Mimes } from 'vs/base/common/mime';
8+
import { IBulkEditService, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
89
import { localize } from 'vs/nls';
910
import { MenuId, registerAction2 } from 'vs/platform/actions/common/actions';
1011
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey';
1112
import { InputFocusedContext, InputFocusedContextKey } from 'vs/platform/contextkey/common/contextkeys';
13+
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
1214
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
15+
import { ResourceNotebookCellEdit } from 'vs/workbench/contrib/bulkEdit/browser/bulkCellEdits';
16+
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinCellsWithSurrounds, moveCellRange } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
1317
import { cellExecutionArgs, CellOverflowToolbarGroups, CellToolbarOrder, CELL_TITLE_CELL_GROUP_ID, INotebookCellActionContext, INotebookCellToolbarActionContext, INotebookCommandContext, NotebookCellAction, NotebookMultiCellAction, parseMultiCellExecutionArgs } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
14-
import { CellFocusMode, EXPAND_CELL_INPUT_COMMAND_ID, EXPAND_CELL_OUTPUT_COMMAND_ID, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
18+
import { CellFocusMode, EXPAND_CELL_INPUT_COMMAND_ID, EXPAND_CELL_OUTPUT_COMMAND_ID, ICellViewModel, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
1519
import * as icons from 'vs/workbench/contrib/notebook/browser/notebookIcons';
16-
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
17-
import { CellEditType, CellKind, ICellEditOperation, NotebookCellMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
18-
import { IBulkEditService, ResourceTextEdit } from 'vs/editor/browser/services/bulkEditService';
19-
import { changeCellToKind, computeCellLinesContents, copyCellRange, joinCellsWithSurrounds, moveCellRange } from 'vs/workbench/contrib/notebook/browser/controller/cellOperations';
20-
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
21-
import { ResourceNotebookCellEdit } from 'vs/workbench/contrib/bulkEdit/browser/bulkCellEdits';
20+
import { CellEditType, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2221

2322
//#region Move/Copy cells
2423
const MOVE_CELL_UP_COMMAND_ID = 'notebook.cell.moveUp';
@@ -322,30 +321,7 @@ const COLLAPSE_CELL_INPUT_COMMAND_ID = 'notebook.cell.collapseCellInput';
322321
const COLLAPSE_CELL_OUTPUT_COMMAND_ID = 'notebook.cell.collapseCellOutput';
323322
const TOGGLE_CELL_OUTPUTS_COMMAND_ID = 'notebook.cell.toggleOutputs';
324323

325-
abstract class ChangeNotebookCellMetadataAction extends NotebookCellAction {
326-
async runWithContext(accessor: ServicesAccessor, context: INotebookCellActionContext): Promise<void> {
327-
const textModel = context.notebookEditor.textModel;
328-
if (!textModel) {
329-
return;
330-
}
331-
332-
const metadataDelta = this.getMetadataDelta();
333-
const edits: ICellEditOperation[] = [];
334-
const targetCells = (context.cell ? [context.cell] : context.selectedCells) ?? [];
335-
for (const cell of targetCells) {
336-
const index = textModel.cells.indexOf(cell.model);
337-
if (index >= 0) {
338-
edits.push({ editType: CellEditType.Metadata, index, metadata: { ...context.cell.metadata, ...metadataDelta } });
339-
}
340-
}
341-
342-
textModel.applyEdits(edits, true, undefined, () => undefined, undefined);
343-
}
344-
345-
abstract getMetadataDelta(): NotebookCellMetadata;
346-
}
347-
348-
registerAction2(class CollapseCellInputAction extends ChangeNotebookCellMetadataAction {
324+
registerAction2(class CollapseCellInputAction extends NotebookMultiCellAction {
349325
constructor() {
350326
super({
351327
id: COLLAPSE_CELL_INPUT_COMMAND_ID,
@@ -364,12 +340,16 @@ registerAction2(class CollapseCellInputAction extends ChangeNotebookCellMetadata
364340
});
365341
}
366342

367-
getMetadataDelta(): NotebookCellMetadata {
368-
return { inputCollapsed: true };
343+
async runWithContext(accessor: ServicesAccessor, context: INotebookCommandContext | INotebookCellToolbarActionContext): Promise<void> {
344+
if (context.ui) {
345+
context.cell.isInputCollapsed = true;
346+
} else {
347+
context.selectedCells.forEach(cell => cell.isInputCollapsed = true);
348+
}
369349
}
370350
});
371351

372-
registerAction2(class ExpandCellInputAction extends ChangeNotebookCellMetadataAction {
352+
registerAction2(class ExpandCellInputAction extends NotebookMultiCellAction {
373353
constructor() {
374354
super({
375355
id: EXPAND_CELL_INPUT_COMMAND_ID,
@@ -388,12 +368,16 @@ registerAction2(class ExpandCellInputAction extends ChangeNotebookCellMetadataAc
388368
});
389369
}
390370

391-
getMetadataDelta(): NotebookCellMetadata {
392-
return { inputCollapsed: false };
371+
async runWithContext(accessor: ServicesAccessor, context: INotebookCommandContext | INotebookCellToolbarActionContext): Promise<void> {
372+
if (context.ui) {
373+
context.cell.isInputCollapsed = false;
374+
} else {
375+
context.selectedCells.forEach(cell => cell.isInputCollapsed = false);
376+
}
393377
}
394378
});
395379

396-
registerAction2(class CollapseCellOutputAction extends ChangeNotebookCellMetadataAction {
380+
registerAction2(class CollapseCellOutputAction extends NotebookMultiCellAction {
397381
constructor() {
398382
super({
399383
id: COLLAPSE_CELL_OUTPUT_COMMAND_ID,
@@ -412,12 +396,16 @@ registerAction2(class CollapseCellOutputAction extends ChangeNotebookCellMetadat
412396
});
413397
}
414398

415-
getMetadataDelta(): NotebookCellMetadata {
416-
return { outputCollapsed: true };
399+
async runWithContext(accessor: ServicesAccessor, context: INotebookCommandContext | INotebookCellToolbarActionContext): Promise<void> {
400+
if (context.ui) {
401+
context.cell.isOutputCollapsed = true;
402+
} else {
403+
context.selectedCells.forEach(cell => cell.isOutputCollapsed = true);
404+
}
417405
}
418406
});
419407

420-
registerAction2(class ExpandCellOuputAction extends ChangeNotebookCellMetadataAction {
408+
registerAction2(class ExpandCellOuputAction extends NotebookMultiCellAction {
421409
constructor() {
422410
super({
423411
id: EXPAND_CELL_OUTPUT_COMMAND_ID,
@@ -436,8 +424,12 @@ registerAction2(class ExpandCellOuputAction extends ChangeNotebookCellMetadataAc
436424
});
437425
}
438426

439-
getMetadataDelta(): NotebookCellMetadata {
440-
return { outputCollapsed: false };
427+
async runWithContext(accessor: ServicesAccessor, context: INotebookCommandContext | INotebookCellToolbarActionContext): Promise<void> {
428+
if (context.ui) {
429+
context.cell.isOutputCollapsed = false;
430+
} else {
431+
context.selectedCells.forEach(cell => cell.isOutputCollapsed = false);
432+
}
441433
}
442434
});
443435

@@ -459,25 +451,16 @@ registerAction2(class extends NotebookMultiCellAction {
459451
}
460452

461453
async runWithContext(accessor: ServicesAccessor, context: INotebookCommandContext | INotebookCellToolbarActionContext): Promise<void> {
462-
const textModel = context.notebookEditor.textModel;
463-
let cells: NotebookCellTextModel[] = [];
454+
let cells: readonly ICellViewModel[] = [];
464455
if (context.ui) {
465-
cells = [context.cell.model];
456+
cells = [context.cell];
466457
} else if (context.selectedCells) {
467-
cells = context.selectedCells.map(cell => cell.model);
468-
} else {
469-
cells = [...textModel.cells];
458+
cells = context.selectedCells;
470459
}
471460

472-
const edits: ICellEditOperation[] = [];
473-
for (const cell of cells) {
474-
const index = textModel.cells.indexOf(cell);
475-
if (index >= 0) {
476-
edits.push({ editType: CellEditType.Metadata, index, metadata: { ...cell.metadata, outputCollapsed: !cell.metadata.outputCollapsed } });
477-
}
461+
for (let cell of cells) {
462+
cell.isOutputCollapsed = !cell.isOutputCollapsed;
478463
}
479-
480-
textModel.applyEdits(edits, true, undefined, () => undefined, undefined);
481464
}
482465
});
483466

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class NotebookViewportContribution extends Disposable implements INotebookEditor
5252
for (let i = 0; i < this._notebookEditor.getLength(); i++) {
5353
const cell = this._notebookEditor.cellAt(i);
5454

55-
if (cell?.cellKind === CellKind.Markup && cell?.getEditState() === CellEditState.Preview && !cell.metadata.inputCollapsed) {
55+
if (cell?.cellKind === CellKind.Markup && cell?.getEditState() === CellEditState.Preview && !cell.isInputCollapsed) {
5656
// TODO@rebornix currently we disable markdown cell rendering in webview for accessibility
5757
// this._notebookEditor.createMarkupPreview(cell);
5858
} else if (cell?.cellKind === CellKind.Code) {
@@ -75,7 +75,7 @@ class NotebookViewportContribution extends Disposable implements INotebookEditor
7575
cellRangesToIndexes(visibleRanges).forEach(index => {
7676
const cell = this._notebookEditor.cellAt(index);
7777

78-
if (cell?.cellKind === CellKind.Markup && cell?.getEditState() === CellEditState.Preview && !cell.metadata.inputCollapsed) {
78+
if (cell?.cellKind === CellKind.Markup && cell?.getEditState() === CellEditState.Preview && !cell.isInputCollapsed) {
7979
(this._notebookEditor as INotebookEditorDelegate).createMarkupPreview(cell);
8080
} else if (cell?.cellKind === CellKind.Code) {
8181
this._renderCell((cell as CodeCellViewModel));
@@ -84,7 +84,7 @@ class NotebookViewportContribution extends Disposable implements INotebookEditor
8484
}
8585

8686
private _renderCell(viewCell: CodeCellViewModel) {
87-
if (viewCell.metadata.outputCollapsed) {
87+
if (viewCell.isOutputCollapsed) {
8888
return;
8989
}
9090

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

-8
Original file line numberDiff line numberDiff line change
@@ -461,14 +461,6 @@ class RegisterSchemasContribution extends Disposable implements IWorkbenchContri
461461
['language']: {
462462
type: 'string',
463463
description: 'The language for the cell'
464-
},
465-
['inputCollapsed']: {
466-
type: 'boolean',
467-
description: `Whether a code cell's editor is collapsed`
468-
},
469-
['outputCollapsed']: {
470-
type: 'boolean',
471-
description: `Whether a code cell's outputs are collapsed`
472464
}
473465
},
474466
// patternProperties: allSettings.patternProperties,

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

+4
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ export interface ICellViewModel extends IGenericCellViewModel {
254254
readonly onDidChangeCellStatusBarItems: Event<void>;
255255
readonly editStateSource: string;
256256
readonly editorAttached: boolean;
257+
isInputCollapsed: boolean;
258+
isOutputCollapsed: boolean;
257259
dragging: boolean;
258260
handle: number;
259261
uri: URI;
@@ -723,6 +725,8 @@ export interface CellViewModelStateChangeEvent {
723725
readonly outputIsFocusedChanged?: boolean;
724726
readonly cellIsHoveredChanged?: boolean;
725727
readonly cellLineNumberChanged?: boolean;
728+
readonly inputCollapsedChanged?: boolean;
729+
readonly outputCollapsedChanged?: boolean;
726730
}
727731

728732
export function getVisibleCells(cells: CellViewModel[], hiddenRanges: ICellRange[]) {

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

+12-10
Original file line numberDiff line numberDiff line change
@@ -1423,17 +1423,17 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
14231423
store.add((cell as CodeCellViewModel).onDidRemoveOutputs((outputs) => {
14241424
outputs.forEach(output => this.removeInset(output));
14251425
}));
1426-
1427-
store.add((cell as CodeCellViewModel).onDidHideOutputs((outputs) => {
1428-
outputs.forEach(output => this.hideInset(output));
1429-
}));
14301426
}
14311427

1432-
if (cell.cellKind === CellKind.Markup) {
1433-
store.add((cell as MarkupCellViewModel).onDidHideInput(() => {
1428+
store.add((cell as CellViewModel).onDidChangeState(e => {
1429+
if (e.inputCollapsedChanged && cell.isInputCollapsed && cell.cellKind === CellKind.Markup) {
14341430
this.hideMarkupPreviews([(cell as MarkupCellViewModel)]);
1435-
}));
1436-
}
1431+
}
1432+
1433+
if (e.outputCollapsedChanged && cell.isOutputCollapsed && cell.cellKind === CellKind.Code) {
1434+
cell.outputsViewModels.forEach(output => this.hideInset(output));
1435+
}
1436+
}));
14371437

14381438
return store;
14391439
}
@@ -1576,7 +1576,9 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
15761576
if (!state) {
15771577
return {
15781578
editingCells: {},
1579-
editorViewStates: {}
1579+
editorViewStates: {},
1580+
collapsedInputCells: {},
1581+
collapsedOutputCells: {},
15801582
};
15811583
}
15821584

@@ -2319,7 +2321,7 @@ export class NotebookEditorWidget extends Disposable implements INotebookEditorD
23192321
output: output.source,
23202322
cellTop,
23212323
outputOffset,
2322-
forceDisplay: !cell.metadata.outputCollapsed,
2324+
forceDisplay: !cell.isOutputCollapsed,
23232325
}], []);
23242326
}
23252327
});

src/vs/workbench/contrib/notebook/browser/view/notebookCellList.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export class NotebookCellList extends WorkbenchList<CellViewModel> implements ID
180180
this._localDisposableStore.add(this.view.onMouseDblClick(() => {
181181
const focus = this.getFocusedElements()[0];
182182

183-
if (focus && focus.cellKind === CellKind.Markup && !focus.metadata.inputCollapsed && !this._viewModel?.options.isReadOnly) {
183+
if (focus && focus.cellKind === CellKind.Markup && !focus.isInputCollapsed && !this._viewModel?.options.isReadOnly) {
184184
// scroll the cell into view if out of viewport
185185
this.revealElementInView(focus);
186186
focus.updateEditState(CellEditState.Editing, 'dbclick');

src/vs/workbench/contrib/notebook/browser/view/renderers/backLayerWebView.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
2828
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
2929
import { IWorkspaceTrustManagementService } from 'vs/platform/workspace/common/workspaceTrust';
3030
import { asWebviewUri, webviewGenericCspSource } from 'vs/workbench/api/common/shared/webview';
31-
import { CellEditState, ICellOutputViewModel, ICommonCellInfo, IDisplayOutputLayoutUpdateRequest, IDisplayOutputViewModel, IFocusNotebookCellOptions, IGenericCellViewModel, IInsetRenderOutput, INotebookEditorCreationOptions, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
31+
import { CellEditState, ICellOutputViewModel, ICellViewModel, ICommonCellInfo, IDisplayOutputLayoutUpdateRequest, IDisplayOutputViewModel, IFocusNotebookCellOptions, IGenericCellViewModel, IInsetRenderOutput, INotebookEditorCreationOptions, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
3232
import { preloadsScriptStr, RendererMetadata } from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewPreloads';
3333
import { transformWebviewThemeVars } from 'vs/workbench/contrib/notebook/browser/view/renderers/webviewThemeMapping';
3434
import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markupCellViewModel';
@@ -836,7 +836,7 @@ var requirejs = (function() {
836836
return false;
837837
}
838838

839-
if (cell.metadata.outputCollapsed) {
839+
if ('isOutputCollapsed' in cell && (cell as ICellViewModel).isOutputCollapsed) {
840840
return false;
841841
}
842842

src/vs/workbench/contrib/notebook/browser/view/renderers/cellContextKeys.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ export class CellContextKeyManager extends Disposable {
5959
this.elementDisposables.add(element.onDidChangeOutputs(() => this.updateForOutputs()));
6060
}
6161

62-
this.elementDisposables.add(element.model.onDidChangeMetadata(() => this.updateForCollapseState()));
6362
this.elementDisposables.add(this.notebookEditor.onDidChangeActiveCell(() => this.updateForFocusState()));
6463

6564
this.element = element;
@@ -98,9 +97,9 @@ export class CellContextKeyManager extends Disposable {
9897
this.cellLineNumbers.set(this.element.lineNumbers);
9998
}
10099

101-
// if (e.collapseStateChanged) {
102-
// this.updateForCollapseState();
103-
// }
100+
if (e.inputCollapsedChanged || e.outputCollapsedChanged) {
101+
this.updateForCollapseState();
102+
}
104103
});
105104
}
106105

@@ -151,8 +150,8 @@ export class CellContextKeyManager extends Disposable {
151150
}
152151

153152
private updateForCollapseState() {
154-
this.cellContentCollapsed.set(!!this.element.metadata.inputCollapsed);
155-
this.cellOutputCollapsed.set(!!this.element.metadata.outputCollapsed);
153+
this.cellContentCollapsed.set(!!this.element.isInputCollapsed);
154+
this.cellOutputCollapsed.set(!!this.element.isOutputCollapsed);
156155
}
157156

158157
private updateForOutputs() {

src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class CellOutputElement extends Disposable {
239239
render(previousSibling: HTMLElement | undefined, forceBreakStreaming: boolean = false): IRenderResult | undefined {
240240
const index = this.viewCell.outputsViewModels.indexOf(this.output);
241241

242-
if (this.viewCell.metadata.outputCollapsed || !this.notebookEditor.hasModel()) {
242+
if (this.viewCell.isOutputCollapsed || !this.notebookEditor.hasModel()) {
243243
return undefined;
244244
}
245245

0 commit comments

Comments
 (0)