From 73b1c7b7e28e8b8fb0d0e2684484bfd69e010c79 Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Thu, 19 Aug 2021 02:37:12 +0000 Subject: [PATCH 1/3] Re: https://github.com/microsoft/vscode/issues/106679 --- .../browser/view/renderers/cellOutput.ts | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts b/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts index 7d1e9e1b67f..0827fe710b9 100644 --- a/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts +++ b/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts @@ -13,24 +13,25 @@ import { MarshalledId } from 'vs/base/common/marshalling'; import { Schemas } from 'vs/base/common/network'; import * as nls from 'vs/nls'; import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem'; -import { IMenuService, MenuId } from 'vs/platform/actions/common/actions'; +import { Action2, IMenuService, MenuId, registerAction2 } from 'vs/platform/actions/common/actions'; import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IContextMenuService } from 'vs/platform/contextview/browser/contextView'; -import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; +import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IOpenerService } from 'vs/platform/opener/common/opener'; import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput'; import { ThemeIcon } from 'vs/platform/theme/common/themeService'; import { IExtensionsViewPaneContainer, VIEWLET_ID as EXTENSION_VIEWLET_ID } from 'vs/workbench/contrib/extensions/common/extensions'; -import { INotebookCellActionContext } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; -import { CodeCellRenderTemplate, ICellOutputViewModel, ICellViewModel, IInsetRenderOutput, INotebookEditor, IRenderOutput, JUPYTER_EXTENSION_ID, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; +import { INotebookCellActionContext, NOTEBOOK_ACTIONS_CATEGORY } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; +import { CodeCellRenderTemplate, getNotebookEditorFromEditorPane, ICellOutputViewModel, ICellViewModel, IInsetRenderOutput, INotebookEditor, IRenderOutput, JUPYTER_EXTENSION_ID, NOTEBOOK_EDITOR_FOCUSED, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { mimetypeIcon } from 'vs/workbench/contrib/notebook/browser/notebookIcons'; import { getResizesObserver } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellWidgets'; import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel'; import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; -import { BUILTIN_RENDERER_ID, CellUri, IOrderedMimeType, NotebookCellOutputsSplice, RENDERER_NOT_AVAILABLE } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { BUILTIN_RENDERER_ID, CellEditType, CellUri, ICellEditOperation, IOrderedMimeType, NotebookCellOutputsSplice, RENDERER_NOT_AVAILABLE } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { INotebookKernel } from 'vs/workbench/contrib/notebook/common/notebookKernelService'; import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService'; +import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; @@ -852,3 +853,40 @@ const JUPYTER_RENDERER_MIMETYPES = [ 'application/vnd.jupyter.widget-view+json', 'application/vnd.code.notebook.error' ]; + + + + +registerAction2(class extends Action2 { + constructor() { + super({ + id: 'notebook.toggleOutputs', + title: nls.localize('notebookActions.toggleOutputs', 'Toggle Outputs'), + precondition: NOTEBOOK_EDITOR_FOCUSED, + f1: false, + category: NOTEBOOK_ACTIONS_CATEGORY + }); + } + + async run(accessor: ServicesAccessor) { + const editorService = accessor.get(IEditorService); + const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane); + const viewModel = editor?.viewModel; + const textModel = viewModel?.notebookDocument; + + if (!textModel) { + return; + } + + const cells = viewModel.getCells() ?? []; + const edits: ICellEditOperation[] = []; + for (const cell of cells) { + const index = textModel.cells.indexOf(cell.model); + if (index >= 0) { + edits.push({ editType: CellEditType.Metadata, index, metadata: { ...cell.metadata, outputCollapsed: !cell.metadata.outputCollapsed } }); + } + } + + textModel.applyEdits(edits, true, undefined, () => undefined, undefined); + } +}); From cec2f28bf31a7a72029aaac31c31b4a9b560b50a Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Thu, 19 Aug 2021 02:39:24 +0000 Subject: [PATCH 2/3] Remove extra whitespace --- .../contrib/notebook/browser/view/renderers/cellOutput.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts b/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts index 0827fe710b9..329a3788d0a 100644 --- a/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts +++ b/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts @@ -854,9 +854,6 @@ const JUPYTER_RENDERER_MIMETYPES = [ 'application/vnd.code.notebook.error' ]; - - - registerAction2(class extends Action2 { constructor() { super({ From 51c240d75b7b972b759ab69016c1ead078221871 Mon Sep 17 00:00:00 2001 From: Joyce Er Date: Thu, 19 Aug 2021 15:30:33 +0000 Subject: [PATCH 3/3] Extend `NotebookMultiCellAction` and move into contrib --- .../notebook/browser/contrib/coreActions.ts | 41 +++++++++++++++++ .../browser/view/renderers/cellOutput.ts | 45 +++---------------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts index afda964fdf2..e280c748885 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts @@ -71,6 +71,7 @@ const EXECUTE_CELL_INSERT_BELOW = 'notebook.cell.executeAndInsertBelow'; const EXECUTE_CELL_AND_BELOW = 'notebook.cell.executeCellAndBelow'; const EXECUTE_CELLS_ABOVE = 'notebook.cell.executeCellsAbove'; const CLEAR_CELL_OUTPUTS_COMMAND_ID = 'notebook.cell.clearOutputs'; +const TOGGLE_CELL_OUTPUTS_COMMAND_ID = 'notebook.cell.toggleOutputs'; const CENTER_ACTIVE_CELL = 'notebook.centerActiveCell'; const COLLAPSE_CELL_INPUT_COMMAND_ID = 'notebook.cell.collapseCellInput'; @@ -691,6 +692,46 @@ registerAction2(class CancelExecuteCell extends NotebookMultiCellAction { + constructor() { + super({ + id: TOGGLE_CELL_OUTPUTS_COMMAND_ID, + precondition: NOTEBOOK_CELL_LIST_FOCUSED, + title: localize('notebookActions.toggleOutputs', "Toggle Outputs"), + description: { + description: localize('notebookActions.toggleOutputs', "Toggle Outputs"), + args: cellExecutionArgs + } + }); + } + + parseArgs(accessor: ServicesAccessor, ...args: any[]): INotebookActionContext | undefined { + return parseMultiCellExecutionArgs(accessor, ...args); + } + + async runWithContext(accessor: ServicesAccessor, context: INotebookActionContext): Promise { + const textModel = context.notebookEditor.viewModel.notebookDocument; + let cells: ICellViewModel[] = []; + if (context.ui && context.cell) { + cells = [context.cell]; + } else if (context.selectedCells) { + cells = [...context.selectedCells]; + } else { + cells = [...context.notebookEditor.viewModel.getCells()]; + } + + const edits: ICellEditOperation[] = []; + for (const cell of cells) { + const index = textModel.cells.indexOf(cell.model); + if (index >= 0) { + edits.push({ editType: CellEditType.Metadata, index, metadata: { ...cell.metadata, outputCollapsed: !cell.metadata.outputCollapsed } }); + } + } + + textModel.applyEdits(edits, true, undefined, () => undefined, undefined); + } +}); + export class DeleteCellAction extends MenuItemAction { constructor( @IContextKeyService contextKeyService: IContextKeyService, diff --git a/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts b/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts index 329a3788d0a..7d1e9e1b67f 100644 --- a/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts +++ b/src/vs/workbench/contrib/notebook/browser/view/renderers/cellOutput.ts @@ -13,25 +13,24 @@ import { MarshalledId } from 'vs/base/common/marshalling'; import { Schemas } from 'vs/base/common/network'; import * as nls from 'vs/nls'; import { createAndFillInActionBarActions } from 'vs/platform/actions/browser/menuEntryActionViewItem'; -import { Action2, IMenuService, MenuId, registerAction2 } from 'vs/platform/actions/common/actions'; +import { IMenuService, MenuId } from 'vs/platform/actions/common/actions'; import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey'; import { IContextMenuService } from 'vs/platform/contextview/browser/contextView'; -import { IInstantiationService, ServicesAccessor } from 'vs/platform/instantiation/common/instantiation'; +import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding'; import { IOpenerService } from 'vs/platform/opener/common/opener'; import { IQuickInputService, IQuickPickItem } from 'vs/platform/quickinput/common/quickInput'; import { ThemeIcon } from 'vs/platform/theme/common/themeService'; import { IExtensionsViewPaneContainer, VIEWLET_ID as EXTENSION_VIEWLET_ID } from 'vs/workbench/contrib/extensions/common/extensions'; -import { INotebookCellActionContext, NOTEBOOK_ACTIONS_CATEGORY } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; -import { CodeCellRenderTemplate, getNotebookEditorFromEditorPane, ICellOutputViewModel, ICellViewModel, IInsetRenderOutput, INotebookEditor, IRenderOutput, JUPYTER_EXTENSION_ID, NOTEBOOK_EDITOR_FOCUSED, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; +import { INotebookCellActionContext } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions'; +import { CodeCellRenderTemplate, ICellOutputViewModel, ICellViewModel, IInsetRenderOutput, INotebookEditor, IRenderOutput, JUPYTER_EXTENSION_ID, RenderOutputType } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { mimetypeIcon } from 'vs/workbench/contrib/notebook/browser/notebookIcons'; import { getResizesObserver } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellWidgets'; import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel'; import { NotebookTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookTextModel'; -import { BUILTIN_RENDERER_ID, CellEditType, CellUri, ICellEditOperation, IOrderedMimeType, NotebookCellOutputsSplice, RENDERER_NOT_AVAILABLE } from 'vs/workbench/contrib/notebook/common/notebookCommon'; +import { BUILTIN_RENDERER_ID, CellUri, IOrderedMimeType, NotebookCellOutputsSplice, RENDERER_NOT_AVAILABLE } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { INotebookKernel } from 'vs/workbench/contrib/notebook/common/notebookKernelService'; import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService'; -import { IEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IViewletService } from 'vs/workbench/services/viewlet/browser/viewlet'; @@ -853,37 +852,3 @@ const JUPYTER_RENDERER_MIMETYPES = [ 'application/vnd.jupyter.widget-view+json', 'application/vnd.code.notebook.error' ]; - -registerAction2(class extends Action2 { - constructor() { - super({ - id: 'notebook.toggleOutputs', - title: nls.localize('notebookActions.toggleOutputs', 'Toggle Outputs'), - precondition: NOTEBOOK_EDITOR_FOCUSED, - f1: false, - category: NOTEBOOK_ACTIONS_CATEGORY - }); - } - - async run(accessor: ServicesAccessor) { - const editorService = accessor.get(IEditorService); - const editor = getNotebookEditorFromEditorPane(editorService.activeEditorPane); - const viewModel = editor?.viewModel; - const textModel = viewModel?.notebookDocument; - - if (!textModel) { - return; - } - - const cells = viewModel.getCells() ?? []; - const edits: ICellEditOperation[] = []; - for (const cell of cells) { - const index = textModel.cells.indexOf(cell.model); - if (index >= 0) { - edits.push({ editType: CellEditType.Metadata, index, metadata: { ...cell.metadata, outputCollapsed: !cell.metadata.outputCollapsed } }); - } - } - - textModel.applyEdits(edits, true, undefined, () => undefined, undefined); - } -});