Skip to content

Commit 674b549

Browse files
committed
Eliminate duplicate runstate, simplify context keys in cellRenderer
1 parent 99ff35b commit 674b549

4 files changed

Lines changed: 26 additions & 45 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ 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, CellEditState, CellRunState, ICellViewModel, INotebookEditor, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
22-
import { CellKind, NOTEBOOK_EDITOR_CURSOR_BOUNDARY } from 'vs/workbench/contrib/notebook/common/notebookCommon';
21+
import { BaseCellRenderTemplate, CellEditState, ICellViewModel, INotebookEditor, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_TYPE, NOTEBOOK_EDITOR_EDITABLE, NOTEBOOK_EDITOR_EXECUTING_NOTEBOOK, NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_RUNNABLE, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
22+
import { CellKind, NOTEBOOK_EDITOR_CURSOR_BOUNDARY, NotebookCellRunState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
2323
import { INotebookService } from 'vs/workbench/contrib/notebook/common/notebookService';
2424
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
2525

@@ -429,7 +429,7 @@ export function getActiveNotebookEditor(editorService: IEditorService): INoteboo
429429
}
430430

431431
async function runCell(context: INotebookCellActionContext): Promise<void> {
432-
if (context.cell.runState === CellRunState.Running) {
432+
if (context.cell.metadata?.runState === NotebookCellRunState.Running) {
433433
return;
434434
}
435435

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ export interface ICellViewModel {
107107
language: string;
108108
cellKind: CellKind;
109109
editState: CellEditState;
110-
readonly runState: CellRunState;
111110
currentTokenSource: CancellationTokenSource | undefined;
112111
focusMode: CellFocusMode;
113112
getText(): string;
@@ -475,11 +474,6 @@ export enum CellRevealPosition {
475474
Center
476475
}
477476

478-
export enum CellRunState {
479-
Idle,
480-
Running
481-
}
482-
483477
export enum CellEditState {
484478
/**
485479
* Default state.
@@ -511,7 +505,6 @@ export interface CellViewModelStateChangeEvent {
511505
metadataChanged?: boolean;
512506
selectionChanged?: boolean;
513507
focusModeChanged?: boolean;
514-
runStateChanged?: boolean;
515508
editStateChanged?: boolean;
516509
languageChanged?: boolean;
517510
foldingStateChanged?: boolean;

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ import { IModeService } from 'vs/editor/common/services/modeService';
3535
import { ContextAwareMenuEntryActionViewItem } from 'vs/platform/actions/browser/menuEntryActionViewItem';
3636
import { IMenu, MenuItemAction } from 'vs/platform/actions/common/actions';
3737
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
38-
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
38+
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
3939
import { IContextMenuService } from 'vs/platform/contextview/browser/contextView';
4040
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
4141
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
4242
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
4343
import { INotificationService } from 'vs/platform/notification/common/notification';
4444
import { BOTTOM_CELL_TOOLBAR_HEIGHT, EDITOR_BOTTOM_PADDING, EDITOR_TOOLBAR_HEIGHT, EDITOR_TOP_MARGIN, EDITOR_TOP_PADDING } from 'vs/workbench/contrib/notebook/browser/constants';
4545
import { CancelCellAction, ChangeCellLanguageAction, ExecuteCellAction, INotebookCellActionContext, InsertCodeCellAction, InsertMarkdownCellAction } from 'vs/workbench/contrib/notebook/browser/contrib/coreActions';
46-
import { BaseCellRenderTemplate, CellEditState, CellRunState, CodeCellRenderTemplate, ICellViewModel, INotebookCellList, INotebookEditor, MarkdownCellRenderTemplate, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
46+
import { BaseCellRenderTemplate, CellEditState, CodeCellRenderTemplate, ICellViewModel, INotebookCellList, INotebookEditor, MarkdownCellRenderTemplate, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_RUNNABLE, NOTEBOOK_CELL_RUN_STATE, NOTEBOOK_CELL_TYPE, NOTEBOOK_VIEW_TYPE } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
4747
import { CellMenus } from 'vs/workbench/contrib/notebook/browser/view/renderers/cellMenus';
4848
import { CodeCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/codeCell';
4949
import { StatefullMarkdownCell } from 'vs/workbench/contrib/notebook/browser/view/renderers/markdownCell';
@@ -978,9 +978,14 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
978978
return templateData;
979979
}
980980

981-
private updateForRunState(element: CodeCellViewModel, templateData: CodeCellRenderTemplate, runStateKey: IContextKey<string>): void {
982-
runStateKey.set(CellRunState[element.runState]);
983-
if (element.runState === CellRunState.Running) {
981+
private updateForRunState(runState: NotebookCellRunState | undefined, templateData: CodeCellRenderTemplate): void {
982+
if (typeof runState === 'undefined') {
983+
runState = NotebookCellRunState.Idle;
984+
}
985+
986+
const runStateKey = NOTEBOOK_CELL_RUN_STATE.bindTo(templateData.contextKeyService);
987+
runStateKey.set(NotebookCellRunState[runState]);
988+
if (runState === NotebookCellRunState.Running) {
984989
templateData.progressBar.infinite().show(500);
985990

986991
templateData.runToolbar.setActions([
@@ -995,7 +1000,10 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
9951000
}
9961001
}
9971002

998-
private updateForMetadata(element: CodeCellViewModel, templateData: CodeCellRenderTemplate, cellEditableKey: IContextKey<boolean>, cellRunnableKey: IContextKey<boolean>): void {
1003+
private updateForMetadata(element: CodeCellViewModel, templateData: CodeCellRenderTemplate): void {
1004+
const cellEditableKey = NOTEBOOK_CELL_EDITABLE.bindTo(templateData.contextKeyService);
1005+
const cellRunnableKey = NOTEBOOK_CELL_RUNNABLE.bindTo(templateData.contextKeyService);
1006+
9991007
const metadata = element.getEvaluatedMetadata(this.notebookEditor.viewModel!.notebookDocument.metadata);
10001008
DOM.toggleClass(templateData.cellContainer, 'runnable', !!metadata.runnable);
10011009
this.renderExecutionOrder(element, templateData);
@@ -1028,6 +1036,8 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
10281036
if (typeof metadata.breakpointMargin === 'boolean') {
10291037
this.editorOptions.setGlyphMargin(metadata.breakpointMargin);
10301038
}
1039+
1040+
this.updateForRunState(metadata.runState, templateData);
10311041
}
10321042

10331043
private renderExecutionOrder(element: CodeCellViewModel, templateData: CodeCellRenderTemplate): void {
@@ -1066,42 +1076,26 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
10661076
templateData.focusIndicator.style.height = `${element.layoutInfo.indicatorHeight}px`;
10671077
}));
10681078

1069-
const contextKeyService = this.contextKeyServiceProvider(templateData.container);
1070-
1071-
const runStateKey = NOTEBOOK_CELL_RUN_STATE.bindTo(contextKeyService);
1072-
runStateKey.set(CellRunState[element.runState]);
1073-
this.updateForRunState(element, templateData, runStateKey);
1074-
elementDisposables.add(element.onDidChangeState((e) => {
1075-
if (e.runStateChanged) {
1076-
this.updateForRunState(element, templateData, runStateKey);
1077-
}
1078-
}));
1079-
1080-
const cellHasOutputsContext = NOTEBOOK_CELL_HAS_OUTPUTS.bindTo(contextKeyService);
1079+
const cellHasOutputsContext = NOTEBOOK_CELL_HAS_OUTPUTS.bindTo(templateData.contextKeyService);
10811080
cellHasOutputsContext.set(element.outputs.length > 0);
10821081
elementDisposables.add(element.onDidChangeOutputs(() => {
10831082
cellHasOutputsContext.set(element.outputs.length > 0);
10841083
}));
10851084

1086-
NOTEBOOK_CELL_TYPE.bindTo(contextKeyService).set('code');
1087-
NOTEBOOK_VIEW_TYPE.bindTo(contextKeyService).set(element.viewType);
1088-
const metadata = element.getEvaluatedMetadata(this.notebookEditor.viewModel!.notebookDocument.metadata);
1089-
const cellEditableKey = NOTEBOOK_CELL_EDITABLE.bindTo(contextKeyService);
1090-
cellEditableKey.set(!!metadata.editable);
1091-
const cellRunnableKey = NOTEBOOK_CELL_RUNNABLE.bindTo(contextKeyService);
1092-
cellRunnableKey.set(!!metadata.runnable);
1093-
this.updateForMetadata(element, templateData, cellEditableKey, cellRunnableKey);
1085+
NOTEBOOK_CELL_TYPE.bindTo(templateData.contextKeyService).set('code');
1086+
NOTEBOOK_VIEW_TYPE.bindTo(templateData.contextKeyService).set(element.viewType);
1087+
this.updateForMetadata(element, templateData);
10941088
elementDisposables.add(element.onDidChangeState((e) => {
10951089
if (e.metadataChanged) {
1096-
this.updateForMetadata(element, templateData, cellEditableKey, cellRunnableKey);
1090+
this.updateForMetadata(element, templateData);
10971091
}
10981092

10991093
if (e.outputIsHoveredChanged) {
11001094
this.updateForHover(element, templateData);
11011095
}
11021096
}));
11031097

1104-
this.setupCellToolbarActions(contextKeyService, templateData, elementDisposables);
1098+
this.setupCellToolbarActions(templateData.contextKeyService, templateData, elementDisposables);
11051099

11061100
const toolbarContext = <INotebookCellActionContext>{
11071101
cell: element,

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as editorCommon from 'vs/editor/common/editorCommon';
1313
import * as model from 'vs/editor/common/model';
1414
import { SearchParams } from 'vs/editor/common/model/textModelSearch';
1515
import { EDITOR_TOOLBAR_HEIGHT, EDITOR_TOP_MARGIN } from 'vs/workbench/contrib/notebook/browser/constants';
16-
import { CellEditState, CellFocusMode, CellRunState, CursorAtBoundary, CellViewModelStateChangeEvent, IEditableCellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
16+
import { CellEditState, CellFocusMode, CursorAtBoundary, CellViewModelStateChangeEvent, IEditableCellViewModel } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
1717
import { CellKind, NotebookCellMetadata, NotebookDocumentMetadata } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1818
import { NotebookCellTextModel } from 'vs/workbench/contrib/notebook/common/model/notebookCellTextModel';
1919

@@ -65,21 +65,15 @@ export abstract class BaseCellViewModel extends Disposable {
6565
}
6666
}
6767

68-
// TODO@roblourens - move any "run"/"status" concept to Code-specific places
6968
private _currentTokenSource: CancellationTokenSource | undefined;
7069
public set currentTokenSource(v: CancellationTokenSource | undefined) {
7170
this._currentTokenSource = v;
72-
this._onDidChangeState.fire({ runStateChanged: true });
7371
}
7472

7573
public get currentTokenSource(): CancellationTokenSource | undefined {
7674
return this._currentTokenSource;
7775
}
7876

79-
get runState(): CellRunState {
80-
return this._currentTokenSource ? CellRunState.Running : CellRunState.Idle;
81-
}
82-
8377
private _focusMode: CellFocusMode = CellFocusMode.Container;
8478
get focusMode() {
8579
return this._focusMode;

0 commit comments

Comments
 (0)