Skip to content

Commit b8b1f00

Browse files
authored
Merge pull request microsoft#139157 from microsoft/roblou/issue139153
Collapse interactive window cells by default
2 parents 21434bc + f786c63 commit b8b1f00

File tree

9 files changed

+57
-12
lines changed

9 files changed

+57
-12
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ registerAction2(class extends Action2 {
458458
language,
459459
source: value,
460460
outputs: [],
461-
metadata: {}
461+
metadata: {},
462+
collapseState: {
463+
inputCollapsed: false,
464+
outputCollapsed: false
465+
}
462466
}]
463467
}
464468
)

src/vs/workbench/contrib/interactive/browser/interactiveEditor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export class InteractiveEditor extends EditorPane {
126126
this.#menuService = menuService;
127127
this.#contextMenuService = contextMenuService;
128128

129-
this.#notebookOptions = new NotebookOptions(configurationService, { cellToolbarInteraction: 'hover', globalToolbar: true });
129+
this.#notebookOptions = new NotebookOptions(configurationService, { cellToolbarInteraction: 'hover', globalToolbar: true, defaultCellCollapseConfig: { codeCell: { inputCollapsed: true }} });
130130

131131
codeEditorService.registerDecorationType('interactive-decoration', DECORATION_KEY, {});
132132
this._register(this.#keybindingService.onDidUpdateKeybindings(this.#updateInputDecoration, this));
@@ -319,6 +319,7 @@ export class InteractiveEditor extends EditorPane {
319319

320320
this.#notebookWidget.value?.setParentContextKeyService(this.#contextKeyService);
321321
await this.#notebookWidget.value!.setModel(model.notebook, undefined);
322+
model.notebook.setCellCollapseDefault(this.#notebookOptions.getCellDefaultCollapseConfig());
322323
this.#notebookWidget.value!.setOptions({
323324
isReadOnly: true
324325
});

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ContextKeyExpr, RawContextKey } from 'vs/platform/contextkey/common/con
1414
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
1515
import { CellViewModel, IModelDecorationsChangeAccessor, INotebookEditorViewState, INotebookViewCellsUpdateEvent, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
1616
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
17-
import { CellKind, NotebookCellMetadata, IOrderedMimeType, INotebookRendererInfo, ICellOutput, INotebookCellStatusBarItem, NotebookCellInternalMetadata, NotebookDocumentMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
17+
import { CellKind, NotebookCellMetadata, IOrderedMimeType, INotebookRendererInfo, ICellOutput, INotebookCellStatusBarItem, NotebookCellInternalMetadata, NotebookDocumentMetadata, NotebookCellCollapseState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1818
import { ICellRange, cellRangesToIndexes, reduceCellRanges } from 'vs/workbench/contrib/notebook/common/notebookRange';
1919
import { IWebview } from 'vs/workbench/contrib/webview/browser/webview';
2020
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
@@ -330,6 +330,7 @@ export interface INotebookEditorOptions extends ITextEditorOptions {
330330
readonly cellSelections?: ICellRange[];
331331
readonly isReadOnly?: boolean;
332332
readonly viewState?: INotebookEditorViewState;
333+
readonly defaultCellCollapseState?: NotebookCellCollapseState;
333334
}
334335

335336
export type INotebookEditorContributionCtor = IConstructorSignature1<INotebookEditor, INotebookEditorContribution>;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,14 @@ export abstract class BaseCellViewModel extends Disposable {
198198
this.lineNumbers = 'inherit';
199199
}
200200
}));
201+
202+
if (this.model.collapseState?.inputCollapsed) {
203+
this._inputCollapsed = true;
204+
}
205+
206+
if (this.model.collapseState?.outputCollapsed) {
207+
this._outputCollapsed = true;
208+
}
201209
}
202210

203211

src/vs/workbench/contrib/notebook/common/model/notebookCellTextModel.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { PieceTreeTextBufferBuilder } from 'vs/editor/common/model/pieceTreeText
1515
import { TextModel } from 'vs/editor/common/model/textModel';
1616
import { IModeService } from 'vs/editor/common/services/modeService';
1717
import { NotebookCellOutputTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellOutputTextModel';
18-
import { CellInternalMetadataChangedEvent, CellKind, ICell, ICellOutput, IOutputDto, IOutputItemDto, NotebookCellInternalMetadata, NotebookCellMetadata, NotebookCellOutputsSplice, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
18+
import { CellInternalMetadataChangedEvent, CellKind, ICell, ICellOutput, IOutputDto, IOutputItemDto, NotebookCellCollapseState, NotebookCellInternalMetadata, NotebookCellMetadata, NotebookCellOutputsSplice, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1919

2020
export class NotebookCellTextModel extends Disposable implements ICell {
2121
private readonly _onDidChangeOutputs = this._register(new Emitter<NotebookCellOutputsSplice>());
@@ -192,14 +192,15 @@ export class NotebookCellTextModel extends Disposable implements ICell {
192192

193193
constructor(
194194
readonly uri: URI,
195-
public handle: number,
195+
public readonly handle: number,
196196
private _source: string,
197197
private _language: string,
198198
private _mime: string | undefined,
199-
public cellKind: CellKind,
199+
public readonly cellKind: CellKind,
200200
outputs: IOutputDto[],
201201
metadata: NotebookCellMetadata | undefined,
202202
internalMetadata: NotebookCellInternalMetadata | undefined,
203+
public readonly collapseState: NotebookCellCollapseState | undefined,
203204
public readonly transientOptions: TransientOptions,
204205
private readonly _modeService: IModeService
205206
) {

src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { Emitter, Event, PauseableEmitter } from 'vs/base/common/event';
88
import { Disposable, dispose, IDisposable } from 'vs/base/common/lifecycle';
99
import { URI } from 'vs/base/common/uri';
1010
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
11-
import { INotebookTextModel, NotebookCellOutputsSplice, NotebookDocumentMetadata, NotebookCellMetadata, ICellEditOperation, CellEditType, CellUri, diff, NotebookCellsChangeType, ICellDto2, TransientOptions, NotebookTextModelChangedEvent, IOutputDto, ICellOutput, IOutputItemDto, ISelectionState, NullablePartialNotebookCellMetadata, NotebookCellInternalMetadata, NullablePartialNotebookCellInternalMetadata, NotebookTextModelWillAddRemoveEvent, NotebookCellTextModelSplice, ICell } from 'vs/workbench/contrib/notebook/common/notebookCommon';
11+
import { INotebookTextModel, NotebookCellOutputsSplice, NotebookDocumentMetadata, NotebookCellMetadata, ICellEditOperation, CellEditType, CellUri, diff, NotebookCellsChangeType, ICellDto2, TransientOptions, NotebookTextModelChangedEvent, IOutputDto, ICellOutput, IOutputItemDto, ISelectionState, NullablePartialNotebookCellMetadata, NotebookCellInternalMetadata, NullablePartialNotebookCellInternalMetadata, NotebookTextModelWillAddRemoveEvent, NotebookCellTextModelSplice, ICell, NotebookCellCollapseState, NotebookCellDefaultCollapseConfig, CellKind } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1212
import { IUndoRedoService, UndoRedoElementType, IUndoRedoElement, IResourceUndoRedoElement, UndoRedoGroup, IWorkspaceUndoRedoElement } from 'vs/platform/undoRedo/common/undoRedo';
1313
import { MoveCellEdit, SpliceCellsEdit, CellMetadataEdit } from 'vs/workbench/contrib/notebook/common/model/cellEdit';
1414
import { ISequence, LcsDiff } from 'vs/base/common/diff/diff';
@@ -168,6 +168,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
168168
private _cellhandlePool: number = 0;
169169
private readonly _cellListeners: Map<number, IDisposable> = new Map();
170170
private _cells: NotebookCellTextModel[] = [];
171+
private _defaultCollapseConfig: NotebookCellDefaultCollapseConfig | undefined;
171172

172173
metadata: NotebookDocumentMetadata = {};
173174
transientOptions: TransientOptions = { transientCellMetadata: {}, transientDocumentMetadata: {}, transientOutputs: false };
@@ -269,6 +270,10 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
269270
);
270271
}
271272

273+
setCellCollapseDefault(collapseConfig: NotebookCellDefaultCollapseConfig | undefined) {
274+
this._defaultCollapseConfig = collapseConfig;
275+
}
276+
272277
_initialize(cells: ICellDto2[], triggerDirty?: boolean) {
273278
this._cells = [];
274279
this._versionId = 0;
@@ -277,7 +282,8 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
277282
const mainCells = cells.map(cell => {
278283
const cellHandle = this._cellhandlePool++;
279284
const cellUri = CellUri.generate(this.uri, cellHandle);
280-
return new NotebookCellTextModel(cellUri, cellHandle, cell.source, cell.language, cell.mime, cell.cellKind, cell.outputs, cell.metadata, cell.internalMetadata, this.transientOptions, this._modeService);
285+
const collapseState = this._getDefaultCollapseState(cell);
286+
return new NotebookCellTextModel(cellUri, cellHandle, cell.source, cell.language, cell.mime, cell.cellKind, cell.outputs, cell.metadata, cell.internalMetadata, collapseState, this.transientOptions, this._modeService);
281287
});
282288

283289
for (let i = 0; i < mainCells.length; i++) {
@@ -575,6 +581,11 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
575581
return mergedEdits;
576582
}
577583

584+
private _getDefaultCollapseState(cellDto: ICellDto2): NotebookCellCollapseState | undefined {
585+
const defaultConfig = cellDto.cellKind === CellKind.Code ? this._defaultCollapseConfig?.codeCell : this._defaultCollapseConfig?.markupCell;
586+
return cellDto.collapseState ?? (defaultConfig ?? undefined);
587+
}
588+
578589
private _replaceCells(index: number, count: number, cellDtos: ICellDto2[], synchronous: boolean, computeUndoRedo: boolean): void {
579590

580591
if (count === 0 && cellDtos.length === 0) {
@@ -598,9 +609,10 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
598609
const cells = cellDtos.map(cellDto => {
599610
const cellHandle = this._cellhandlePool++;
600611
const cellUri = CellUri.generate(this.uri, cellHandle);
612+
const collapseState = this._getDefaultCollapseState(cellDto);
601613
const cell = new NotebookCellTextModel(
602614
cellUri, cellHandle,
603-
cellDto.source, cellDto.language, cellDto.mime, cellDto.cellKind, cellDto.outputs || [], cellDto.metadata, cellDto.internalMetadata, this.transientOptions,
615+
cellDto.source, cellDto.language, cellDto.mime, cellDto.cellKind, cellDto.outputs || [], cellDto.metadata, cellDto.internalMetadata, collapseState, this.transientOptions,
604616
this._modeService
605617
);
606618
const textModel = this._modelService.getModel(cellUri);

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,16 @@ export interface NotebookCellInternalMetadata {
109109
didPause?: boolean;
110110
}
111111

112+
export interface NotebookCellCollapseState {
113+
inputCollapsed?: boolean;
114+
outputCollapsed?: boolean;
115+
}
116+
117+
export interface NotebookCellDefaultCollapseConfig {
118+
codeCell?: NotebookCellCollapseState;
119+
markupCell?: NotebookCellCollapseState;
120+
}
121+
112122
export type TransientCellMetadata = { [K in keyof NotebookCellMetadata]?: boolean };
113123
export type TransientDocumentMetadata = { [K in keyof NotebookDocumentMetadata]?: boolean };
114124

@@ -386,6 +396,7 @@ export interface ICellDto2 {
386396
outputs: IOutputDto[];
387397
metadata?: NotebookCellMetadata;
388398
internalMetadata?: NotebookCellInternalMetadata;
399+
collapseState?: NotebookCellCollapseState;
389400
}
390401

391402
export interface ICellReplaceEdit {

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { Emitter } from 'vs/base/common/event';
77
import { Disposable } from 'vs/base/common/lifecycle';
88
import { IConfigurationChangeEvent, IConfigurationService } from 'vs/platform/configuration/common/configuration';
9-
import { NotebookCellInternalMetadata, NotebookSetting, ShowCellStatusBarType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
9+
import { NotebookCellDefaultCollapseConfig, NotebookCellInternalMetadata, NotebookSetting, ShowCellStatusBarType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1010

1111
const SCROLLABLE_ELEMENT_PADDING_TOP = 18;
1212

@@ -106,10 +106,11 @@ const compactConfigConstants = Object.freeze({
106106

107107
export class NotebookOptions extends Disposable {
108108
private _layoutConfiguration: NotebookLayoutConfiguration;
109+
private _cellDefaultCollapseConfig: NotebookCellDefaultCollapseConfig | undefined;
109110
protected readonly _onDidChangeOptions = this._register(new Emitter<NotebookOptionsChangeEvent>());
110111
readonly onDidChangeOptions = this._onDidChangeOptions.event;
111112

112-
constructor(private readonly configurationService: IConfigurationService, private readonly overrides?: { cellToolbarInteraction: string, globalToolbar: boolean }) {
113+
constructor(private readonly configurationService: IConfigurationService, private readonly overrides?: { cellToolbarInteraction: string, globalToolbar: boolean, defaultCellCollapseConfig?: NotebookCellDefaultCollapseConfig }) {
113114
super();
114115
const showCellStatusBar = this.configurationService.getValue<ShowCellStatusBarType>(NotebookSetting.showCellStatusBar);
115116
const globalToolbar = overrides?.globalToolbar ?? this.configurationService.getValue<boolean | undefined>(NotebookSetting.globalToolbar) ?? true;
@@ -170,6 +171,8 @@ export class NotebookOptions extends Disposable {
170171
this._layoutConfiguration = configuration;
171172
this._onDidChangeOptions.fire({ editorTopPadding: true });
172173
}));
174+
175+
this._cellDefaultCollapseConfig = overrides?.defaultCellCollapseConfig;
173176
}
174177

175178
private _updateConfiguration(e: IConfigurationChangeEvent) {
@@ -312,6 +315,10 @@ export class NotebookOptions extends Disposable {
312315
return this.configurationService.getValue<'border' | 'gutter'>(NotebookSetting.focusIndicator) ?? 'gutter';
313316
}
314317

318+
getCellDefaultCollapseConfig(): NotebookCellDefaultCollapseConfig | undefined {
319+
return this._cellDefaultCollapseConfig;
320+
}
321+
315322
getLayoutConfiguration(): NotebookLayoutConfiguration {
316323
return this._layoutConfiguration;
317324
}

src/vs/workbench/contrib/notebook/test/browser/testNotebookEditor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class TestCell extends NotebookCellTextModel {
6565
outputs: IOutputDto[],
6666
modeService: IModeService,
6767
) {
68-
super(CellUri.generate(URI.parse('test:///fake/notebook'), handle), handle, source, language, Mimes.text, cellKind, outputs, undefined, undefined, { transientCellMetadata: {}, transientDocumentMetadata: {}, transientOutputs: false }, modeService);
68+
super(CellUri.generate(URI.parse('test:///fake/notebook'), handle), handle, source, language, Mimes.text, cellKind, outputs, undefined, undefined, undefined, { transientCellMetadata: {}, transientDocumentMetadata: {}, transientOutputs: false }, modeService);
6969
}
7070
}
7171

0 commit comments

Comments
 (0)