Skip to content

Commit

Permalink
Merge pull request #139157 from microsoft/roblou/issue139153
Browse files Browse the repository at this point in the history
Collapse interactive window cells by default
  • Loading branch information
roblourens committed Dec 15, 2021
2 parents 21434bc + f786c63 commit b8b1f00
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ registerAction2(class extends Action2 {
language,
source: value,
outputs: [],
metadata: {}
metadata: {},
collapseState: {
inputCollapsed: false,
outputCollapsed: false
}
}]
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class InteractiveEditor extends EditorPane {
this.#menuService = menuService;
this.#contextMenuService = contextMenuService;

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

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

this.#notebookWidget.value?.setParentContextKeyService(this.#contextKeyService);
await this.#notebookWidget.value!.setModel(model.notebook, undefined);
model.notebook.setCellCollapseDefault(this.#notebookOptions.getCellDefaultCollapseConfig());
this.#notebookWidget.value!.setOptions({
isReadOnly: true
});
Expand Down
3 changes: 2 additions & 1 deletion src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ContextKeyExpr, RawContextKey } from 'vs/platform/contextkey/common/con
import { OutputRenderer } from 'vs/workbench/contrib/notebook/browser/view/output/outputRenderer';
import { CellViewModel, IModelDecorationsChangeAccessor, INotebookEditorViewState, INotebookViewCellsUpdateEvent, NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
import { CellKind, NotebookCellMetadata, IOrderedMimeType, INotebookRendererInfo, ICellOutput, INotebookCellStatusBarItem, NotebookCellInternalMetadata, NotebookDocumentMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellKind, NotebookCellMetadata, IOrderedMimeType, INotebookRendererInfo, ICellOutput, INotebookCellStatusBarItem, NotebookCellInternalMetadata, NotebookDocumentMetadata, NotebookCellCollapseState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { ICellRange, cellRangesToIndexes, reduceCellRanges } from 'vs/workbench/contrib/notebook/common/notebookRange';
import { IWebview } from 'vs/workbench/contrib/webview/browser/webview';
import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel';
Expand Down Expand Up @@ -330,6 +330,7 @@ export interface INotebookEditorOptions extends ITextEditorOptions {
readonly cellSelections?: ICellRange[];
readonly isReadOnly?: boolean;
readonly viewState?: INotebookEditorViewState;
readonly defaultCellCollapseState?: NotebookCellCollapseState;
}

export type INotebookEditorContributionCtor = IConstructorSignature1<INotebookEditor, INotebookEditorContribution>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ export abstract class BaseCellViewModel extends Disposable {
this.lineNumbers = 'inherit';
}
}));

if (this.model.collapseState?.inputCollapsed) {
this._inputCollapsed = true;
}

if (this.model.collapseState?.outputCollapsed) {
this._outputCollapsed = true;
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { PieceTreeTextBufferBuilder } from 'vs/editor/common/model/pieceTreeText
import { TextModel } from 'vs/editor/common/model/textModel';
import { IModeService } from 'vs/editor/common/services/modeService';
import { NotebookCellOutputTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellOutputTextModel';
import { CellInternalMetadataChangedEvent, CellKind, ICell, ICellOutput, IOutputDto, IOutputItemDto, NotebookCellInternalMetadata, NotebookCellMetadata, NotebookCellOutputsSplice, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { CellInternalMetadataChangedEvent, CellKind, ICell, ICellOutput, IOutputDto, IOutputItemDto, NotebookCellCollapseState, NotebookCellInternalMetadata, NotebookCellMetadata, NotebookCellOutputsSplice, TransientOptions } from 'vs/workbench/contrib/notebook/common/notebookCommon';

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

constructor(
readonly uri: URI,
public handle: number,
public readonly handle: number,
private _source: string,
private _language: string,
private _mime: string | undefined,
public cellKind: CellKind,
public readonly cellKind: CellKind,
outputs: IOutputDto[],
metadata: NotebookCellMetadata | undefined,
internalMetadata: NotebookCellInternalMetadata | undefined,
public readonly collapseState: NotebookCellCollapseState | undefined,
public readonly transientOptions: TransientOptions,
private readonly _modeService: IModeService
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Emitter, Event, PauseableEmitter } from 'vs/base/common/event';
import { Disposable, dispose, IDisposable } from 'vs/base/common/lifecycle';
import { URI } from 'vs/base/common/uri';
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
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';
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';
import { IUndoRedoService, UndoRedoElementType, IUndoRedoElement, IResourceUndoRedoElement, UndoRedoGroup, IWorkspaceUndoRedoElement } from 'vs/platform/undoRedo/common/undoRedo';
import { MoveCellEdit, SpliceCellsEdit, CellMetadataEdit } from 'vs/workbench/contrib/notebook/common/model/cellEdit';
import { ISequence, LcsDiff } from 'vs/base/common/diff/diff';
Expand Down Expand Up @@ -168,6 +168,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
private _cellhandlePool: number = 0;
private readonly _cellListeners: Map<number, IDisposable> = new Map();
private _cells: NotebookCellTextModel[] = [];
private _defaultCollapseConfig: NotebookCellDefaultCollapseConfig | undefined;

metadata: NotebookDocumentMetadata = {};
transientOptions: TransientOptions = { transientCellMetadata: {}, transientDocumentMetadata: {}, transientOutputs: false };
Expand Down Expand Up @@ -269,6 +270,10 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
);
}

setCellCollapseDefault(collapseConfig: NotebookCellDefaultCollapseConfig | undefined) {
this._defaultCollapseConfig = collapseConfig;
}

_initialize(cells: ICellDto2[], triggerDirty?: boolean) {
this._cells = [];
this._versionId = 0;
Expand All @@ -277,7 +282,8 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
const mainCells = cells.map(cell => {
const cellHandle = this._cellhandlePool++;
const cellUri = CellUri.generate(this.uri, cellHandle);
return new NotebookCellTextModel(cellUri, cellHandle, cell.source, cell.language, cell.mime, cell.cellKind, cell.outputs, cell.metadata, cell.internalMetadata, this.transientOptions, this._modeService);
const collapseState = this._getDefaultCollapseState(cell);
return new NotebookCellTextModel(cellUri, cellHandle, cell.source, cell.language, cell.mime, cell.cellKind, cell.outputs, cell.metadata, cell.internalMetadata, collapseState, this.transientOptions, this._modeService);
});

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

private _getDefaultCollapseState(cellDto: ICellDto2): NotebookCellCollapseState | undefined {
const defaultConfig = cellDto.cellKind === CellKind.Code ? this._defaultCollapseConfig?.codeCell : this._defaultCollapseConfig?.markupCell;
return cellDto.collapseState ?? (defaultConfig ?? undefined);
}

private _replaceCells(index: number, count: number, cellDtos: ICellDto2[], synchronous: boolean, computeUndoRedo: boolean): void {

if (count === 0 && cellDtos.length === 0) {
Expand All @@ -598,9 +609,10 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
const cells = cellDtos.map(cellDto => {
const cellHandle = this._cellhandlePool++;
const cellUri = CellUri.generate(this.uri, cellHandle);
const collapseState = this._getDefaultCollapseState(cellDto);
const cell = new NotebookCellTextModel(
cellUri, cellHandle,
cellDto.source, cellDto.language, cellDto.mime, cellDto.cellKind, cellDto.outputs || [], cellDto.metadata, cellDto.internalMetadata, this.transientOptions,
cellDto.source, cellDto.language, cellDto.mime, cellDto.cellKind, cellDto.outputs || [], cellDto.metadata, cellDto.internalMetadata, collapseState, this.transientOptions,
this._modeService
);
const textModel = this._modelService.getModel(cellUri);
Expand Down
11 changes: 11 additions & 0 deletions src/vs/workbench/contrib/notebook/common/notebookCommon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ export interface NotebookCellInternalMetadata {
didPause?: boolean;
}

export interface NotebookCellCollapseState {
inputCollapsed?: boolean;
outputCollapsed?: boolean;
}

export interface NotebookCellDefaultCollapseConfig {
codeCell?: NotebookCellCollapseState;
markupCell?: NotebookCellCollapseState;
}

export type TransientCellMetadata = { [K in keyof NotebookCellMetadata]?: boolean };
export type TransientDocumentMetadata = { [K in keyof NotebookDocumentMetadata]?: boolean };

Expand Down Expand Up @@ -386,6 +396,7 @@ export interface ICellDto2 {
outputs: IOutputDto[];
metadata?: NotebookCellMetadata;
internalMetadata?: NotebookCellInternalMetadata;
collapseState?: NotebookCellCollapseState;
}

export interface ICellReplaceEdit {
Expand Down
11 changes: 9 additions & 2 deletions src/vs/workbench/contrib/notebook/common/notebookOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { IConfigurationChangeEvent, IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { NotebookCellInternalMetadata, NotebookSetting, ShowCellStatusBarType } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookCellDefaultCollapseConfig, NotebookCellInternalMetadata, NotebookSetting, ShowCellStatusBarType } from 'vs/workbench/contrib/notebook/common/notebookCommon';

const SCROLLABLE_ELEMENT_PADDING_TOP = 18;

Expand Down Expand Up @@ -106,10 +106,11 @@ const compactConfigConstants = Object.freeze({

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

constructor(private readonly configurationService: IConfigurationService, private readonly overrides?: { cellToolbarInteraction: string, globalToolbar: boolean }) {
constructor(private readonly configurationService: IConfigurationService, private readonly overrides?: { cellToolbarInteraction: string, globalToolbar: boolean, defaultCellCollapseConfig?: NotebookCellDefaultCollapseConfig }) {
super();
const showCellStatusBar = this.configurationService.getValue<ShowCellStatusBarType>(NotebookSetting.showCellStatusBar);
const globalToolbar = overrides?.globalToolbar ?? this.configurationService.getValue<boolean | undefined>(NotebookSetting.globalToolbar) ?? true;
Expand Down Expand Up @@ -170,6 +171,8 @@ export class NotebookOptions extends Disposable {
this._layoutConfiguration = configuration;
this._onDidChangeOptions.fire({ editorTopPadding: true });
}));

this._cellDefaultCollapseConfig = overrides?.defaultCellCollapseConfig;
}

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

getCellDefaultCollapseConfig(): NotebookCellDefaultCollapseConfig | undefined {
return this._cellDefaultCollapseConfig;
}

getLayoutConfiguration(): NotebookLayoutConfiguration {
return this._layoutConfiguration;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class TestCell extends NotebookCellTextModel {
outputs: IOutputDto[],
modeService: IModeService,
) {
super(CellUri.generate(URI.parse('test:///fake/notebook'), handle), handle, source, language, Mimes.text, cellKind, outputs, undefined, undefined, { transientCellMetadata: {}, transientDocumentMetadata: {}, transientOutputs: false }, modeService);
super(CellUri.generate(URI.parse('test:///fake/notebook'), handle), handle, source, language, Mimes.text, cellKind, outputs, undefined, undefined, undefined, { transientCellMetadata: {}, transientDocumentMetadata: {}, transientOutputs: false }, modeService);
}
}

Expand Down

0 comments on commit b8b1f00

Please sign in to comment.