From bcb8c8a4f4bf89bfa4ef0d4f7926173c72fbbe0e Mon Sep 17 00:00:00 2001 From: Rob Lourens Date: Tue, 9 Mar 2021 15:20:10 -0800 Subject: [PATCH] Remove notebook document runnable metadata #106747 --- .../src/singlefolder-tests/notebook.test.ts | 209 ++++++++---------- src/vs/vscode.proposed.d.ts | 7 +- .../api/common/extHostTypeConverters.ts | 2 +- src/vs/workbench/api/common/extHostTypes.ts | 11 +- .../contrib/clipboard/notebookClipboard.ts | 1 - .../notebook/browser/contrib/coreActions.ts | 8 +- .../notebook/browser/notebookBrowser.ts | 1 - .../browser/notebookEditorKernelManager.ts | 23 +- .../browser/viewModel/notebookViewModel.ts | 4 +- .../contrib/notebook/common/notebookCommon.ts | 2 - .../notebook/test/notebookViewModel.test.ts | 6 +- .../test/browser/api/extHostTypes.test.ts | 1 - 12 files changed, 128 insertions(+), 147 deletions(-) diff --git a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts index 983e05e483c..f99f6503c86 100644 --- a/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts +++ b/extensions/vscode-api-tests/src/singlefolder-tests/notebook.test.ts @@ -48,17 +48,104 @@ async function saveAllFilesAndCloseAll(resource: vscode.Uri | undefined) { await documentClosed; } -async function updateNotebookMetadata(uri: vscode.Uri, newMetadata: vscode.NotebookDocumentMetadata) { - const edit = new vscode.WorkspaceEdit(); - edit.replaceNotebookMetadata(uri, newMetadata); - await vscode.workspace.applyEdit(edit); -} - async function withEvent(event: vscode.Event, callback: (e: Promise) => Promise) { const e = asPromise(event); await callback(e); } +const kernel1: vscode.NotebookKernel = { + id: 'mainKernel', + label: 'Notebook Test Kernel', + isPreferred: true, + supportedLanguages: ['typescript', 'javascript'], + executeAllCells: async (_document: vscode.NotebookDocument) => { + const edit = new vscode.WorkspaceEdit(); + + edit.replaceNotebookCellOutput(_document.uri, 0, [new vscode.NotebookCellOutput([ + new vscode.NotebookCellOutputItem('text/plain', ['my output'], undefined) + ])]); + return vscode.workspace.applyEdit(edit); + }, + cancelAllCellsExecution: async (_document: vscode.NotebookDocument) => { }, + executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined) => { + if (!cell) { + cell = document.cells[0]; + } + + if (document.uri.path.endsWith('customRenderer.vsctestnb')) { + const edit = new vscode.WorkspaceEdit(); + edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ + new vscode.NotebookCellOutputItem('text/custom', ['test'], undefined) + ])]); + + return vscode.workspace.applyEdit(edit); + } + + const edit = new vscode.WorkspaceEdit(); + // const previousOutputs = cell.outputs; + edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ + new vscode.NotebookCellOutputItem('text/plain', ['my output'], undefined) + ])]); + + return vscode.workspace.applyEdit(edit); + }, + cancelCellExecution: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell) => { } +}; + +const kernel2: vscode.NotebookKernel = { + id: 'secondaryKernel', + label: 'Notebook Secondary Test Kernel', + isPreferred: false, + supportedLanguages: ['typescript', 'javascript'], + executeAllCells: async (_document: vscode.NotebookDocument) => { + const edit = new vscode.WorkspaceEdit(); + edit.replaceNotebookCellOutput(_document.uri, 0, [new vscode.NotebookCellOutput([ + new vscode.NotebookCellOutputItem('text/plain', ['my second output'], undefined) + ])]); + + return vscode.workspace.applyEdit(edit); + }, + cancelAllCellsExecution: async (_document: vscode.NotebookDocument) => { }, + executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined) => { + if (!cell) { + cell = document.cells[0]; + } + + const edit = new vscode.WorkspaceEdit(); + + if (document.uri.path.endsWith('customRenderer.vsctestnb')) { + edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ + new vscode.NotebookCellOutputItem('text/custom', ['test 2'], undefined) + ])]); + } else { + edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ + new vscode.NotebookCellOutputItem('text/plain', ['my second output'], undefined) + ])]); + } + + return vscode.workspace.applyEdit(edit); + }, + cancelCellExecution: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell) => { } +}; + +class KernelProvider implements vscode.NotebookKernelProvider { + private _onDidChangeKernels = new vscode.EventEmitter(); + onDidChangeKernels = this._onDidChangeKernels.event; + + private _hasKernels = true; + private readonly _kernels = [kernel1, kernel2]; + + provideKernels(): vscode.ProviderResult { + return this._hasKernels ? this._kernels : []; + } + + setHasKernels(hasKernels: boolean): void { + this._hasKernels = hasKernels; + this._onDidChangeKernels.fire(undefined); + } +} +let currentKernerProvider: KernelProvider; + suite('Notebook API tests', function () { const disposables: vscode.Disposable[] = []; @@ -127,87 +214,8 @@ suite('Notebook API tests', function () { } })); - - const kernel: vscode.NotebookKernel = { - id: 'mainKernel', - label: 'Notebook Test Kernel', - isPreferred: true, - supportedLanguages: ['typescript', 'javascript'], - executeAllCells: async (_document: vscode.NotebookDocument) => { - const edit = new vscode.WorkspaceEdit(); - - edit.replaceNotebookCellOutput(_document.uri, 0, [new vscode.NotebookCellOutput([ - new vscode.NotebookCellOutputItem('text/plain', ['my output'], undefined) - ])]); - return vscode.workspace.applyEdit(edit); - }, - cancelAllCellsExecution: async (_document: vscode.NotebookDocument) => { }, - executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined) => { - if (!cell) { - cell = document.cells[0]; - } - - if (document.uri.path.endsWith('customRenderer.vsctestnb')) { - const edit = new vscode.WorkspaceEdit(); - edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ - new vscode.NotebookCellOutputItem('text/custom', ['test'], undefined) - ])]); - - return vscode.workspace.applyEdit(edit); - } - - const edit = new vscode.WorkspaceEdit(); - // const previousOutputs = cell.outputs; - edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ - new vscode.NotebookCellOutputItem('text/plain', ['my output'], undefined) - ])]); - - return vscode.workspace.applyEdit(edit); - }, - cancelCellExecution: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell) => { } - }; - - const kernel2: vscode.NotebookKernel = { - id: 'secondaryKernel', - label: 'Notebook Secondary Test Kernel', - isPreferred: false, - supportedLanguages: ['typescript', 'javascript'], - executeAllCells: async (_document: vscode.NotebookDocument) => { - const edit = new vscode.WorkspaceEdit(); - edit.replaceNotebookCellOutput(_document.uri, 0, [new vscode.NotebookCellOutput([ - new vscode.NotebookCellOutputItem('text/plain', ['my second output'], undefined) - ])]); - - return vscode.workspace.applyEdit(edit); - }, - cancelAllCellsExecution: async (_document: vscode.NotebookDocument) => { }, - executeCell: async (document: vscode.NotebookDocument, cell: vscode.NotebookCell | undefined) => { - if (!cell) { - cell = document.cells[0]; - } - - const edit = new vscode.WorkspaceEdit(); - - if (document.uri.path.endsWith('customRenderer.vsctestnb')) { - edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ - new vscode.NotebookCellOutputItem('text/custom', ['test 2'], undefined) - ])]); - } else { - edit.replaceNotebookCellOutput(document.uri, cell.index, [new vscode.NotebookCellOutput([ - new vscode.NotebookCellOutputItem('text/plain', ['my second output'], undefined) - ])]); - } - - return vscode.workspace.applyEdit(edit); - }, - cancelCellExecution: async (_document: vscode.NotebookDocument, _cell: vscode.NotebookCell) => { } - }; - - disposables.push(vscode.notebook.registerNotebookKernelProvider({ filenamePattern: '*.vsctestnb' }, { - provideKernels: async () => { - return [kernel, kernel2]; - } - })); + currentKernerProvider = new KernelProvider(); + disposables.push(vscode.notebook.registerNotebookKernelProvider({ filenamePattern: '*.vsctestnb' }, currentKernerProvider)); }); test('shared document in notebook editors', async function () { @@ -657,7 +665,7 @@ suite('Notebook API tests', function () { await saveFileAndCloseAll(resource); }); - test('document runnable metadata is respected', async () => { + test('document runnable based on kernel count', async () => { const resource = await createRandomFile('', undefined, '.vsctestnb'); await vscode.commands.executeCommand('vscode.openWith', resource, 'notebookCoreTest'); assert.strictEqual(vscode.window.activeNotebookEditor !== undefined, true, 'notebook first'); @@ -666,18 +674,11 @@ suite('Notebook API tests', function () { const cell = editor.document.cells[0]; assert.strictEqual(cell.outputs.length, 0); - await withEvent(vscode.notebook.onDidChangeNotebookDocumentMetadata, async event => { - updateNotebookMetadata(editor.document.uri, editor.document.metadata.with({ runnable: false })); - await event; - }); - + currentKernerProvider.setHasKernels(false); await vscode.commands.executeCommand('notebook.execute'); assert.strictEqual(cell.outputs.length, 0, 'should not execute'); // not runnable, didn't work - await withEvent(vscode.notebook.onDidChangeNotebookDocumentMetadata, async event => { - updateNotebookMetadata(editor.document.uri, editor.document.metadata.with({ runnable: true })); - await event; - }); + currentKernerProvider.setHasKernels(true); await withEvent(vscode.notebook.onDidChangeCellOutputs, async (event) => { await vscode.commands.executeCommand('notebook.execute'); @@ -710,11 +711,6 @@ suite('Notebook API tests', function () { const editor = vscode.window.activeNotebookEditor!; const cell = editor.document.cells[0]; - await withEvent(vscode.notebook.onDidChangeNotebookDocumentMetadata, async event => { - updateNotebookMetadata(editor.document.uri, editor.document.metadata.with({ runnable: true })); - await event; - }); - await withEvent(vscode.notebook.onDidChangeCellOutputs, async (event) => { await vscode.commands.executeCommand('notebook.execute'); await event; @@ -747,11 +743,6 @@ suite('Notebook API tests', function () { const editor = vscode.window.activeNotebookEditor!; const cell = editor.document.cells[0]; - const metadataChangeEvent = asPromise(vscode.notebook.onDidChangeNotebookDocumentMetadata); - updateNotebookMetadata(editor.document.uri, editor.document.metadata.with({ runnable: true })); - await metadataChangeEvent; - assert.strictEqual(editor.document.metadata.runnable, true); - await withEvent(vscode.notebook.onDidChangeCellOutputs, async (event) => { await vscode.commands.executeCommand('notebook.execute'); await event; @@ -783,10 +774,6 @@ suite('Notebook API tests', function () { const editor = vscode.window.activeNotebookEditor!; const cell = editor.document.cells[0]; - const metadataChangeEvent = asPromise(vscode.notebook.onDidChangeNotebookDocumentMetadata); - updateNotebookMetadata(editor.document.uri, editor.document.metadata.with({ runnable: true })); - await metadataChangeEvent; - await vscode.commands.executeCommand('notebook.cell.execute'); assert.strictEqual(cell.outputs.length, 1, 'should execute'); // runnable, it worked assert.strictEqual(cell.outputs[0].outputs.length, 1); diff --git a/src/vs/vscode.proposed.d.ts b/src/vs/vscode.proposed.d.ts index 5722fa968ce..c79079da2c1 100644 --- a/src/vs/vscode.proposed.d.ts +++ b/src/vs/vscode.proposed.d.ts @@ -1170,15 +1170,12 @@ declare module 'vscode' { // todo@API is this a kernel property? readonly cellHasExecutionOrder: boolean; - // run related, remove infer from kernel, exec - // todo@API infer from kernel // todo@API remove - readonly runnable: boolean; readonly runState: NotebookRunState; - constructor(editable?: boolean, runnable?: boolean, cellEditable?: boolean, cellHasExecutionOrder?: boolean, custom?: { [key: string]: any; }, runState?: NotebookRunState, trusted?: boolean); + constructor(editable?: boolean, cellEditable?: boolean, cellHasExecutionOrder?: boolean, custom?: { [key: string]: any; }, runState?: NotebookRunState, trusted?: boolean); - with(change: { editable?: boolean | null, runnable?: boolean | null, cellEditable?: boolean | null, cellHasExecutionOrder?: boolean | null, custom?: { [key: string]: any; } | null, runState?: NotebookRunState | null, trusted?: boolean | null, }): NotebookDocumentMetadata + with(change: { editable?: boolean | null, cellEditable?: boolean | null, cellHasExecutionOrder?: boolean | null, custom?: { [key: string]: any; } | null, runState?: NotebookRunState | null, trusted?: boolean | null, }): NotebookDocumentMetadata } export interface NotebookDocumentContentOptions { diff --git a/src/vs/workbench/api/common/extHostTypeConverters.ts b/src/vs/workbench/api/common/extHostTypeConverters.ts index cd7ef5f7f2d..fe2aec8f49a 100644 --- a/src/vs/workbench/api/common/extHostTypeConverters.ts +++ b/src/vs/workbench/api/common/extHostTypeConverters.ts @@ -1431,7 +1431,7 @@ export namespace NotebookDocumentMetadata { } export function to(data: notebooks.NotebookDocumentMetadata): types.NotebookDocumentMetadata { - return new types.NotebookDocumentMetadata(data.editable, data.runnable, data.cellEditable, data.cellHasExecutionOrder, data.custom, data.runState, data.trusted); + return new types.NotebookDocumentMetadata(data.editable, data.cellEditable, data.cellHasExecutionOrder, data.custom, data.runState, data.trusted); } } diff --git a/src/vs/workbench/api/common/extHostTypes.ts b/src/vs/workbench/api/common/extHostTypes.ts index 32b1c8bc00f..4103b64c025 100644 --- a/src/vs/workbench/api/common/extHostTypes.ts +++ b/src/vs/workbench/api/common/extHostTypes.ts @@ -3047,7 +3047,6 @@ export class NotebookDocumentMetadata { constructor( readonly editable: boolean = true, - readonly runnable: boolean = true, readonly cellEditable: boolean = true, readonly cellHasExecutionOrder: boolean = true, readonly custom: { [key: string]: any; } = {}, @@ -3057,7 +3056,6 @@ export class NotebookDocumentMetadata { with(change: { editable?: boolean | null, - runnable?: boolean | null, cellEditable?: boolean | null, cellHasExecutionOrder?: boolean | null, custom?: { [key: string]: any; } | null, @@ -3065,18 +3063,13 @@ export class NotebookDocumentMetadata { trusted?: boolean | null, }): NotebookDocumentMetadata { - let { editable, runnable, cellEditable, cellHasExecutionOrder, custom, runState, trusted } = change; + let { editable, cellEditable, cellHasExecutionOrder, custom, runState, trusted } = change; if (editable === undefined) { editable = this.editable; } else if (editable === null) { editable = undefined; } - if (runnable === undefined) { - runnable = this.runnable; - } else if (runnable === null) { - runnable = undefined; - } if (cellEditable === undefined) { cellEditable = this.cellEditable; } else if (cellEditable === null) { @@ -3104,7 +3097,6 @@ export class NotebookDocumentMetadata { } if (editable === this.editable && - runnable === this.runnable && cellEditable === this.cellEditable && cellHasExecutionOrder === this.cellHasExecutionOrder && custom === this.custom && @@ -3117,7 +3109,6 @@ export class NotebookDocumentMetadata { return new NotebookDocumentMetadata( editable, - runnable, cellEditable, cellHasExecutionOrder, custom, diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts b/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts index 39f7ff2face..0d759a8d57d 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/clipboard/notebookClipboard.ts @@ -94,7 +94,6 @@ class NotebookClipboardContribution extends Disposable { const cloneMetadata = (cell: NotebookCellTextModel) => { return { editable: cell.metadata?.editable, - runnable: cell.metadata?.runnable, breakpointMargin: cell.metadata?.breakpointMargin, hasExecutionOrder: cell.metadata?.hasExecutionOrder, inputCollapsed: cell.metadata?.inputCollapsed, diff --git a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts index c8fb1e37e4f..52be2b61bfc 100644 --- a/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts +++ b/src/vs/workbench/contrib/notebook/browser/contrib/coreActions.ts @@ -21,7 +21,7 @@ import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry'; import { IQuickInputService, IQuickPickItem, QuickPickInput } from 'vs/platform/quickinput/common/quickInput'; import { CATEGORIES } from 'vs/workbench/common/actions'; -import { BaseCellRenderTemplate, CellEditState, CellFocusMode, EXECUTE_CELL_COMMAND_ID, EXPAND_CELL_CONTENT_COMMAND_ID, getNotebookEditorFromEditorPane, IActiveNotebookEditor, ICellViewModel, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_EDITOR_FOCUSED, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_KERNEL_COUNT, NOTEBOOK_OUTPUT_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; +import { BaseCellRenderTemplate, CellEditState, CellFocusMode, EXECUTE_CELL_COMMAND_ID, EXPAND_CELL_CONTENT_COMMAND_ID, getNotebookEditorFromEditorPane, IActiveNotebookEditor, ICellViewModel, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_EDITOR_FOCUSED, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_LIST_FOCUSED, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_KERNEL_COUNT, NOTEBOOK_OUTPUT_FOCUSED } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { CellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; import { CellEditType, CellKind, ICellEditOperation, ICellRange, INotebookDocumentFilter, isDocumentExcludePattern, NotebookCellMetadata, NotebookCellRunState, NOTEBOOK_EDITOR_CURSOR_BEGIN_END, NOTEBOOK_EDITOR_CURSOR_BOUNDARY, TransientMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon'; import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService'; @@ -258,6 +258,8 @@ const executeCellCondition = ContextKeyExpr.or( ContextKeyExpr.greater(NOTEBOOK_KERNEL_COUNT.key, 0)), NOTEBOOK_CELL_TYPE.isEqualTo('markdown')); +const executeNotebookCondition = ContextKeyExpr.greater(NOTEBOOK_KERNEL_COUNT.key, 0); + registerAction2(class ExecuteCell extends NotebookCellAction { constructor() { super({ @@ -630,7 +632,7 @@ MenuRegistry.appendMenuItem(MenuId.EditorTitle, { }, order: -1, group: 'navigation', - when: ContextKeyExpr.and(NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK.toNegated(), NOTEBOOK_EDITOR_RUNNABLE) + when: ContextKeyExpr.and(NOTEBOOK_IS_ACTIVE_EDITOR, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK.toNegated(), executeNotebookCondition) }); MenuRegistry.appendMenuItem(MenuId.EditorTitle, { @@ -1514,7 +1516,7 @@ registerAction2(class extends NotebookCellAction { title: localize('clearCellOutputs', 'Clear Cell Outputs'), menu: { id: MenuId.NotebookCellTitle, - when: ContextKeyExpr.and(NOTEBOOK_CELL_TYPE.isEqualTo('code'), NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_CELL_EDITABLE), + when: ContextKeyExpr.and(NOTEBOOK_CELL_TYPE.isEqualTo('code'), executeNotebookCondition, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_CELL_EDITABLE), order: CellToolbarOrder.ClearCellOutput, group: CELL_TITLE_OUTPUT_GROUP_ID }, diff --git a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts index 8f3b0c3c76a..e4714eb81e2 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookBrowser.ts @@ -43,7 +43,6 @@ export const NOTEBOOK_EDITOR_OPEN = new RawContextKey('notebookEditorOp export const NOTEBOOK_CELL_LIST_FOCUSED = new RawContextKey('notebookCellListFocused', false); export const NOTEBOOK_OUTPUT_FOCUSED = new RawContextKey('notebookOutputFocused', false); export const NOTEBOOK_EDITOR_EDITABLE = new RawContextKey('notebookEditable', true); -export const NOTEBOOK_EDITOR_RUNNABLE = new RawContextKey('notebookRunnable', true); export const NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK = new RawContextKey('notebookExecuting', false); // Diff Editor Keys diff --git a/src/vs/workbench/contrib/notebook/browser/notebookEditorKernelManager.ts b/src/vs/workbench/contrib/notebook/browser/notebookEditorKernelManager.ts index 3caee9e9650..ebc29bb5ba6 100644 --- a/src/vs/workbench/contrib/notebook/browser/notebookEditorKernelManager.ts +++ b/src/vs/workbench/contrib/notebook/browser/notebookEditorKernelManager.ts @@ -15,7 +15,7 @@ import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/c import { IStorageService, StorageScope, StorageTarget } from 'vs/platform/storage/common/storage'; import { ThemeIcon } from 'vs/platform/theme/common/themeService'; import { Memento } from 'vs/workbench/common/memento'; -import { ICellViewModel, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_HAS_MULTIPLE_KERNELS, NOTEBOOK_KERNEL_COUNT } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; +import { ICellViewModel, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_HAS_MULTIPLE_KERNELS, NOTEBOOK_KERNEL_COUNT } from 'vs/workbench/contrib/notebook/browser/notebookBrowser'; import { configureKernelIcon } from 'vs/workbench/contrib/notebook/browser/notebookIcons'; import { NotebookKernelProviderAssociation, NotebookKernelProviderAssociations, notebookKernelProviderAssociationsSettingId } from 'vs/workbench/contrib/notebook/browser/notebookKernelAssociation'; import { NotebookViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel'; @@ -46,7 +46,6 @@ export class NotebookEditorKernelManager extends Disposable { private _contributedKernelsComputePromise: CancelablePromise | null = null; private _initialKernelComputationDone: boolean = false; - private readonly _editorRunnable: IContextKey; private readonly _notebookExecuting: IContextKey; private readonly _notebookHasMultipleKernels: IContextKey; private readonly _notebookKernelCount: IContextKey; @@ -105,7 +104,6 @@ export class NotebookEditorKernelManager extends Disposable { this._activeKernelMemento = new Memento(NotebookEditorActiveKernelCache, storageService); - this._editorRunnable = NOTEBOOK_EDITOR_RUNNABLE.bindTo(contextKeyService); this._notebookExecuting = NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK.bindTo(contextKeyService); this._notebookHasMultipleKernels = NOTEBOOK_HAS_MULTIPLE_KERNELS.bindTo(contextKeyService); this._notebookKernelCount = NOTEBOOK_KERNEL_COUNT.bindTo(contextKeyService); @@ -175,7 +173,6 @@ export class NotebookEditorKernelManager extends Disposable { } const notebookMetadata = this._delegate.viewModel.metadata; - this._editorRunnable.set(this._delegate.viewModel.runnable); this._notebookExecuting.set(notebookMetadata.runState === NotebookRunState.Running); } @@ -382,11 +379,11 @@ export class NotebookEditorKernelManager extends Disposable { return; } - if (!this._delegate.viewModel.runnable) { + await this._ensureActiveKernel(); + if (!this.canExecuteNotebook()) { return; } - await this._ensureActiveKernel(); this._activeKernelExecuted = true; await this._activeKernel?.executeNotebookCell!(this._delegate.viewModel.uri, undefined); } @@ -414,15 +411,27 @@ export class NotebookEditorKernelManager extends Disposable { return; } + await this._ensureActiveKernel(); if (!this.canExecuteCell(cell)) { throw new Error('Cell is not executable: ' + cell.uri); } - await this._ensureActiveKernel(); this._activeKernelExecuted = true; await this._activeKernel?.executeNotebookCell!(this._delegate.viewModel.uri, cell.handle); } + private canExecuteNotebook(): boolean { + if (!this.activeKernel) { + return false; + } + + if (!this._delegate.viewModel?.trusted) { + return false; + } + + return true; + } + private canExecuteCell(cell: ICellViewModel): boolean { if (!this.activeKernel) { return false; diff --git a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts index 7b903a74d90..491314b43f4 100644 --- a/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts +++ b/src/vs/workbench/contrib/notebook/browser/viewModel/notebookViewModel.ts @@ -164,8 +164,8 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD return this._notebook.metadata; } - get runnable() { - return !!this._notebook.metadata?.runnable && !!this._notebook.metadata?.trusted; + get trusted() { + return !!this._notebook.metadata?.trusted; } private readonly _onDidChangeViewCells = this._register(new Emitter()); diff --git a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts index c8c40dfb678..7c8e99762d1 100644 --- a/src/vs/workbench/contrib/notebook/common/notebookCommon.ts +++ b/src/vs/workbench/contrib/notebook/common/notebookCommon.ts @@ -60,7 +60,6 @@ export enum NotebookRunState { export const notebookDocumentMetadataDefaults: Required = { editable: true, - runnable: true, cellEditable: true, cellHasExecutionOrder: true, custom: {}, @@ -70,7 +69,6 @@ export const notebookDocumentMetadataDefaults: Required { ['var e = 5;', 'javascript', CellKind.Code, [], { editable: false }], ], (editor, viewModel) => { - viewModel.notebookDocument.metadata = { editable: true, runnable: true, cellEditable: true, cellHasExecutionOrder: true, trusted: true }; + viewModel.notebookDocument.metadata = { editable: true, cellEditable: true, cellHasExecutionOrder: true, trusted: true }; const defaults = { hasExecutionOrder: true }; @@ -173,7 +173,7 @@ suite('NotebookViewModel', () => { ...defaults }); - viewModel.notebookDocument.metadata = { editable: true, runnable: true, cellEditable: true, cellHasExecutionOrder: true, trusted: true }; + viewModel.notebookDocument.metadata = { editable: true, cellEditable: true, cellHasExecutionOrder: true, trusted: true }; assert.deepEqual(viewModel.viewCells[0].getEvaluatedMetadata(viewModel.metadata), { editable: true, @@ -200,7 +200,7 @@ suite('NotebookViewModel', () => { ...defaults }); - viewModel.notebookDocument.metadata = { editable: true, runnable: true, cellEditable: false, cellHasExecutionOrder: true, trusted: true }; + viewModel.notebookDocument.metadata = { editable: true, cellEditable: false, cellHasExecutionOrder: true, trusted: true }; assert.deepEqual(viewModel.viewCells[0].getEvaluatedMetadata(viewModel.metadata), { editable: false, diff --git a/src/vs/workbench/test/browser/api/extHostTypes.test.ts b/src/vs/workbench/test/browser/api/extHostTypes.test.ts index d45f61e97d6..55b761b3d49 100644 --- a/src/vs/workbench/test/browser/api/extHostTypes.test.ts +++ b/src/vs/workbench/test/browser/api/extHostTypes.test.ts @@ -656,7 +656,6 @@ suite('ExtHostTypes', function () { assert.deepStrictEqual(obj.custom, notebookDocumentMetadataDefaults.custom); assert.strictEqual(obj.editable, notebookDocumentMetadataDefaults.editable); assert.strictEqual(obj.runState, notebookDocumentMetadataDefaults.runState); - assert.strictEqual(obj.runnable, notebookDocumentMetadataDefaults.runnable); assert.strictEqual(obj.trusted, notebookDocumentMetadataDefaults.trusted); });