Skip to content

Commit 541ff18

Browse files
committed
Add cell collapse state to cell metadata
This needs more discussion - is collapse state metadata or view state?
1 parent a3dd5c3 commit 541ff18

File tree

8 files changed

+48
-33
lines changed

8 files changed

+48
-33
lines changed

src/vs/vscode.proposed.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,16 @@ declare module 'vscode' {
13571357
*/
13581358
lastRunDuration?: number;
13591359

1360+
/**
1361+
* Whether a code cell's editor is collapsed
1362+
*/
1363+
inputCollapsed?: boolean;
1364+
1365+
/**
1366+
* Whether a code cell's outputs are collapsed
1367+
*/
1368+
outputCollapsed?: boolean;
1369+
13601370
/**
13611371
* Additional attributes of a cell metadata.
13621372
*/

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { InputFocusedContext, InputFocusedContextKey } from 'vs/platform/context
1818
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
1919
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
2020
import { IQuickInputService, IQuickPickItem, QuickPickInput } from 'vs/platform/quickinput/common/quickInput';
21-
import { BaseCellRenderTemplate, CellCollapseState, CellEditState, CellFocusMode, ICellViewModel, INotebookEditor, NOTEBOOK_CELL_CONTENT_COLLAPSED, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_OUTPUT_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
21+
import { BaseCellRenderTemplate, CellCollapseState, CellEditState, CellFocusMode, ICellViewModel, INotebookEditor, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_OUTPUT_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
2222
import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
2323
import { CellKind, CellUri, NotebookCellRunState, NOTEBOOK_EDITOR_CURSOR_BOUNDARY } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2424
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
@@ -72,7 +72,7 @@ const CENTER_ACTIVE_CELL = 'notebook.centerActiveCell';
7272
const FOCUS_IN_OUTPUT_COMMAND_ID = 'notebook.cell.focusInOutput';
7373
const FOCUS_OUT_OUTPUT_COMMAND_ID = 'notebook.cell.focusOutOutput';
7474

75-
const COLLAPSE_CELL_CONTENT_COMMAND_ID = 'notebook.cell.collapseCellContent';
75+
const COLLAPSE_CELL_INPUT_COMMAND_ID = 'notebook.cell.collapseCellContent';
7676
const COLLAPSE_CELL_OUTPUT_COMMAND_ID = 'notebook.cell.collapseCellOutput';
7777
const EXPAND_CELL_CONTENT_COMMAND_ID = 'notebook.cell.expandCellContent';
7878
const EXPAND_CELL_OUTPUT_COMMAND_ID = 'notebook.cell.expandCellOutput';
@@ -1461,16 +1461,16 @@ registerAction2(class extends NotebookCellAction {
14611461
registerAction2(class extends NotebookCellAction {
14621462
constructor() {
14631463
super({
1464-
id: COLLAPSE_CELL_CONTENT_COMMAND_ID,
1465-
title: localize('notebookActions.collapseCellContent', "Collapse Cell Content"),
1464+
id: COLLAPSE_CELL_INPUT_COMMAND_ID,
1465+
title: localize('notebookActions.collapseCellInput', "Collapse Cell Input"),
14661466
keybinding: {
1467-
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_CONTENT_COLLAPSED.toNegated(), InputFocusedContext.toNegated()),
1467+
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_INPUT_COLLAPSED.toNegated(), InputFocusedContext.toNegated()),
14681468
primary: KeyChord(KeyCode.KEY_C, KeyCode.KEY_C),
14691469
weight: KeybindingWeight.WorkbenchContrib
14701470
},
14711471
menu: {
14721472
id: MenuId.NotebookCellTitle,
1473-
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_CONTENT_COLLAPSED.toNegated()),
1473+
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_INPUT_COLLAPSED.toNegated()),
14741474
group: '3_collapse',
14751475
}
14761476
});
@@ -1487,13 +1487,13 @@ registerAction2(class extends NotebookCellAction {
14871487
id: EXPAND_CELL_CONTENT_COMMAND_ID,
14881488
title: localize('notebookActions.expandCellContent', "Expand Cell Content"),
14891489
keybinding: {
1490-
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_CONTENT_COLLAPSED),
1490+
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_INPUT_COLLAPSED),
14911491
primary: KeyChord(KeyCode.KEY_C, KeyCode.KEY_C),
14921492
weight: KeybindingWeight.WorkbenchContrib
14931493
},
14941494
menu: {
14951495
id: MenuId.NotebookCellTitle,
1496-
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_CONTENT_COLLAPSED),
1496+
when: ContextKeyExpr.and(NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_INPUT_COLLAPSED),
14971497
group: '3_collapse',
14981498
}
14991499
});

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const NOTEBOOK_CELL_RUNNABLE = new RawContextKey<boolean>('notebookCellRu
4848
export const NOTEBOOK_CELL_MARKDOWN_EDIT_MODE = new RawContextKey<boolean>('notebookCellMarkdownEditMode', false); // bool
4949
export const NOTEBOOK_CELL_RUN_STATE = new RawContextKey<string>('notebookCellRunState', undefined); // idle, running
5050
export const NOTEBOOK_CELL_HAS_OUTPUTS = new RawContextKey<boolean>('notebookCellHasOutputs', false); // bool
51-
export const NOTEBOOK_CELL_CONTENT_COLLAPSED = new RawContextKey<boolean>('notebookCellContentIsCollapsed', false); // bool
51+
export const NOTEBOOK_CELL_INPUT_COLLAPSED = new RawContextKey<boolean>('notebookCellInputIsCollapsed', false); // bool
5252
export const NOTEBOOK_CELL_OUTPUT_COLLAPSED = new RawContextKey<boolean>('notebookCellOutputIsCollapsed', false); // bool
5353

5454
// Kernels
@@ -569,7 +569,6 @@ export interface CellViewModelStateChangeEvent {
569569
focusModeChanged?: boolean;
570570
editStateChanged?: boolean;
571571
languageChanged?: boolean;
572-
collapseStateChanged?: boolean;
573572
foldingStateChanged?: boolean;
574573
contentChanged?: boolean;
575574
outputIsHoveredChanged?: boolean;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
77
import { INotebookTextModel, NotebookCellRunState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
88
import { BaseCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/baseCellViewModel';
9-
import { NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_HAS_OUTPUTS, CellViewModelStateChangeEvent, CellEditState, NOTEBOOK_CELL_CONTENT_COLLAPSED, CellCollapseState, NOTEBOOK_CELL_OUTPUT_COLLAPSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
9+
import { NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_HAS_OUTPUTS, CellViewModelStateChangeEvent, CellEditState, NOTEBOOK_CELL_INPUT_COLLAPSED, CellCollapseState, NOTEBOOK_CELL_OUTPUT_COLLAPSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
1010
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
1111
import { MarkdownCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markdownCellViewModel';
1212
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
@@ -41,7 +41,7 @@ export class CellContextKeyManager extends Disposable {
4141
this.markdownEditMode = NOTEBOOK_CELL_MARKDOWN_EDIT_MODE.bindTo(this.contextKeyService);
4242
this.cellRunState = NOTEBOOK_CELL_RUN_STATE.bindTo(this.contextKeyService);
4343
this.cellHasOutputs = NOTEBOOK_CELL_HAS_OUTPUTS.bindTo(this.contextKeyService);
44-
this.cellContentCollapsed = NOTEBOOK_CELL_CONTENT_COLLAPSED.bindTo(this.contextKeyService);
44+
this.cellContentCollapsed = NOTEBOOK_CELL_INPUT_COLLAPSED.bindTo(this.contextKeyService);
4545
this.cellOutputCollapsed = NOTEBOOK_CELL_OUTPUT_COLLAPSED.bindTo(this.contextKeyService);
4646

4747
this.updateForElement(element);
@@ -56,6 +56,8 @@ export class CellContextKeyManager extends Disposable {
5656
this.elementDisposables.add(element.onDidChangeOutputs(() => this.updateForOutputs()));
5757
}
5858

59+
this.elementDisposables.add(element.model.onDidChangeMetadata(() => this.updateForCollapseState()));
60+
5961
this.element = element;
6062
if (this.element instanceof MarkdownCellViewModel) {
6163
this.cellType.set('markdown');
@@ -83,9 +85,9 @@ export class CellContextKeyManager extends Disposable {
8385
this.updateForEditState();
8486
}
8587

86-
if (e.collapseStateChanged) {
87-
this.updateForCollapseState();
88-
}
88+
// if (e.collapseStateChanged) {
89+
// this.updateForCollapseState();
90+
// }
8991
});
9092
}
9193

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,18 @@ export class CodeCell extends Disposable {
9191
if (e.focusModeChanged) {
9292
updateForFocusMode();
9393
}
94-
95-
if (e.collapseStateChanged) {
96-
updateForCollapseState();
97-
}
9894
}));
9995
updateForFocusMode();
10096

10197
templateData.editor?.updateOptions({ readOnly: !(viewCell.getEvaluatedMetadata(notebookEditor.viewModel!.metadata).editable) });
10298
this._register(viewCell.onDidChangeState((e) => {
10399
if (e.metadataChanged) {
104100
templateData.editor?.updateOptions({ readOnly: !(viewCell.getEvaluatedMetadata(notebookEditor.viewModel!.metadata).editable) });
101+
102+
// TODO@rob this isn't nice
103+
this.viewCell.layoutChange({});
104+
updateForCollapseState();
105+
this.relayoutCell();
105106
}
106107
}));
107108

@@ -110,11 +111,6 @@ export class CodeCell extends Disposable {
110111
const mode = this._modeService.create(viewCell.language);
111112
templateData.editor?.getModel()?.setMode(mode.languageIdentifier);
112113
}
113-
114-
if (e.collapseStateChanged) {
115-
this.viewCell.layoutChange({});
116-
this.relayoutCell();
117-
}
118114
}));
119115

120116
this._register(viewCell.onDidChangeLayout((e) => {

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,15 @@ export class StatefulMarkdownCell extends Disposable {
5050
if (e.editStateChanged) {
5151
this.localDisposables.clear();
5252
this.viewUpdate();
53-
} else if (e.contentChanged || e.collapseStateChanged) {
53+
} else if (e.contentChanged) {
5454
this.viewUpdate();
5555
}
5656
}));
5757

58+
this._register(viewCell.model.onDidChangeMetadata(() => {
59+
this.viewUpdate();
60+
}));
61+
5862
this._register(getResizesObserver(this.markdownContainer, undefined, () => {
5963
if (viewCell.editState === CellEditState.Preview) {
6064
this.viewCell.renderedMarkdownHeight = templateData.container.clientHeight;

src/vs/workbench/contrib/notebook/browser/viewModel/baseCellViewModel.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,26 @@ export abstract class BaseCellViewModel extends Disposable {
6060
}
6161
}
6262

63-
private _collapseState: CellCollapseState = CellCollapseState.Normal;
6463
public get collapseState(): CellCollapseState {
65-
return this._collapseState;
64+
return this.metadata?.inputCollapsed ? CellCollapseState.Collapsed : CellCollapseState.Normal;
6665
}
6766

6867
public set collapseState(v: CellCollapseState) {
69-
this._collapseState = v;
70-
this._onDidChangeState.fire({ collapseStateChanged: true });
68+
this.model.metadata = {
69+
...this.metadata,
70+
...{ inputCollapsed: v === CellCollapseState.Collapsed }
71+
};
7172
}
7273

73-
private _outputCollapseState: CellCollapseState = CellCollapseState.Normal;
7474
public get outputCollapseState(): CellCollapseState {
75-
return this._outputCollapseState;
75+
return this.metadata?.outputCollapsed ? CellCollapseState.Collapsed : CellCollapseState.Normal;
7676
}
7777

7878
public set outputCollapseState(v: CellCollapseState) {
79-
this._outputCollapseState = v;
80-
this._onDidChangeState.fire({ collapseStateChanged: true });
79+
this.model.metadata = {
80+
...this.metadata,
81+
...{ outputCollapsed: v === CellCollapseState.Collapsed }
82+
};
8183
}
8284

8385
private _focusMode: CellFocusMode = CellFocusMode.Container;

src/vs/workbench/contrib/notebook/common/notebookCommon.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ export interface NotebookCellMetadata {
9797
runState?: NotebookCellRunState;
9898
runStartTime?: number;
9999
lastRunDuration?: number;
100+
inputCollapsed?: boolean;
101+
outputCollapsed?: boolean;
100102
custom?: { [key: string]: unknown };
101103
}
102104

0 commit comments

Comments
 (0)