1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
import { IContextKey, IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { CellEditState, CellFocusMode, ICellViewModel, INotebookEditorDelegate } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
import { CellViewModelStateChangeEvent } from 'vs/workbench/contrib/notebook/browser/notebookViewEvents';
import { CellPart } from 'vs/workbench/contrib/notebook/browser/view/cellPart';
import { CodeCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/codeCellViewModel';
import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewModel/markupCellViewModel';
import { NotebookCellExecutionState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
import { NotebookCellExecutionStateContext, NOTEBOOK_CELL_EDITABLE, NOTEBOOK_CELL_EDITOR_FOCUSED, NOTEBOOK_CELL_EXECUTING, NOTEBOOK_CELL_EXECUTION_STATE, NOTEBOOK_CELL_FOCUSED, NOTEBOOK_CELL_HAS_OUTPUTS, NOTEBOOK_CELL_INPUT_COLLAPSED, NOTEBOOK_CELL_LINE_NUMBERS, NOTEBOOK_CELL_MARKDOWN_EDIT_MODE, NOTEBOOK_CELL_OUTPUT_COLLAPSED, NOTEBOOK_CELL_RESOURCE, NOTEBOOK_CELL_TYPE } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
export class CellContextKeyPart extends CellPart {
private cellContextKeyManager: CellContextKeyManager;
constructor(
notebookEditor: INotebookEditorDelegate,
@IInstantiationService private readonly instantiationService: IInstantiationService,
) {
super();
this.cellContextKeyManager = this._register(this.instantiationService.createInstance(CellContextKeyManager, notebookEditor, undefined));
}
protected override didRenderCell(element: ICellViewModel): void {
this.cellContextKeyManager.updateForElement(element);
}
}
export class CellContextKeyManager extends Disposable {
private cellType!: IContextKey<'code' | 'markup'>;
private cellEditable!: IContextKey<boolean>;
private cellFocused!: IContextKey<boolean>;
private cellEditorFocused!: IContextKey<boolean>;
private cellRunState!: IContextKey<NotebookCellExecutionStateContext>;
private cellExecuting!: IContextKey<boolean>;
private cellHasOutputs!: IContextKey<boolean>;
private cellContentCollapsed!: IContextKey<boolean>;
private cellOutputCollapsed!: IContextKey<boolean>;
private cellLineNumbers!: IContextKey<'on' | 'off' | 'inherit'>;
private cellResource!: IContextKey<string>;
private markdownEditMode!: IContextKey<boolean>;
private readonly elementDisposables = this._register(new DisposableStore());
constructor(
private readonly notebookEditor: INotebookEditorDelegate,
private element: ICellViewModel | undefined,
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
@INotebookExecutionStateService private readonly _notebookExecutionStateService: INotebookExecutionStateService
) {
super();
this._contextKeyService.bufferChangeEvents(() => {
this.cellType = NOTEBOOK_CELL_TYPE.bindTo(this._contextKeyService);
this.cellEditable = NOTEBOOK_CELL_EDITABLE.bindTo(this._contextKeyService);
this.cellFocused = NOTEBOOK_CELL_FOCUSED.bindTo(this._contextKeyService);
this.cellEditorFocused = NOTEBOOK_CELL_EDITOR_FOCUSED.bindTo(this._contextKeyService);
this.markdownEditMode = NOTEBOOK_CELL_MARKDOWN_EDIT_MODE.bindTo(this._contextKeyService);
this.cellRunState = NOTEBOOK_CELL_EXECUTION_STATE.bindTo(this._contextKeyService);
this.cellExecuting = NOTEBOOK_CELL_EXECUTING.bindTo(this._contextKeyService);
this.cellHasOutputs = NOTEBOOK_CELL_HAS_OUTPUTS.bindTo(this._contextKeyService);
this.cellContentCollapsed = NOTEBOOK_CELL_INPUT_COLLAPSED.bindTo(this._contextKeyService);
this.cellOutputCollapsed = NOTEBOOK_CELL_OUTPUT_COLLAPSED.bindTo(this._contextKeyService);
this.cellLineNumbers = NOTEBOOK_CELL_LINE_NUMBERS.bindTo(this._contextKeyService);
this.cellResource = NOTEBOOK_CELL_RESOURCE.bindTo(this._contextKeyService);
if (element) {
this.updateForElement(element);
}
});
this._register(this._notebookExecutionStateService.onDidChangeCellExecution(e => {
if (this.element && e.affectsCell(this.element.uri)) {
this.updateForExecutionState();
}
}));
}
public updateForElement(element: ICellViewModel | undefined) {
this.elementDisposables.clear();
this.element = element;
if (!element) {
return;
}
this.elementDisposables.add(element.onDidChangeState(e => this.onDidChangeState(e)));
if (element instanceof CodeCellViewModel) {
this.elementDisposables.add(element.onDidChangeOutputs(() => this.updateForOutputs()));
}
this.elementDisposables.add(this.notebookEditor.onDidChangeActiveCell(() => this.updateForFocusState()));
if (this.element instanceof MarkupCellViewModel) {
this.cellType.set('markup');
} else if (this.element instanceof CodeCellViewModel) {
this.cellType.set('code');
}
this._contextKeyService.bufferChangeEvents(() => {
this.updateForFocusState();
this.updateForExecutionState();
this.updateForEditState();
this.updateForCollapseState();
this.updateForOutputs();
this.cellLineNumbers.set(this.element!.lineNumbers);
this.cellResource.set(this.element!.uri.toString());
});
}
private onDidChangeState(e: CellViewModelStateChangeEvent) {
this._contextKeyService.bufferChangeEvents(() => {
if (e.internalMetadataChanged) {
this.updateForExecutionState();
}
if (e.editStateChanged) {
this.updateForEditState();
}
if (e.focusModeChanged) {
this.updateForFocusState();
}
if (e.cellLineNumberChanged) {
this.cellLineNumbers.set(this.element!.lineNumbers);
}
if (e.inputCollapsedChanged || e.outputCollapsedChanged) {
this.updateForCollapseState();
}
});
}
private updateForFocusState() {
if (!this.element) {
return;
}
const activeCell = this.notebookEditor.getActiveCell();
this.cellFocused.set(this.notebookEditor.getActiveCell() === this.element);
if (activeCell === this.element) {
this.cellEditorFocused.set(this.element.focusMode === CellFocusMode.Editor);
} else {
this.cellEditorFocused.set(false);
}
}
private updateForExecutionState() {
if (!this.element) {
return;
}
const internalMetadata = this.element.internalMetadata;
this.cellEditable.set(!this.notebookEditor.isReadOnly);
const exeState = this._notebookExecutionStateService.getCellExecution(this.element.uri);
if (this.element instanceof MarkupCellViewModel) {
this.cellRunState.reset();
this.cellExecuting.reset();
} else if (exeState?.state === NotebookCellExecutionState.Executing) {
this.cellRunState.set('executing');
this.cellExecuting.set(true);
} else if (exeState?.state === NotebookCellExecutionState.Pending || exeState?.state === NotebookCellExecutionState.Unconfirmed) {
this.cellRunState.set('pending');
this.cellExecuting.set(true);
} else if (internalMetadata.lastRunSuccess === true) {
this.cellRunState.set('succeeded');
this.cellExecuting.set(false);
} else if (internalMetadata.lastRunSuccess === false) {
this.cellRunState.set('failed');
this.cellExecuting.set(false);
} else {
this.cellRunState.set('idle');
this.cellExecuting.set(false);
}
}
private updateForEditState() {
if (!this.element) {
return;
}
if (this.element instanceof MarkupCellViewModel) {
this.markdownEditMode.set(this.element.getEditState() === CellEditState.Editing);
} else {
this.markdownEditMode.set(false);
}
}
private updateForCollapseState() {
if (!this.element) {
return;
}
this.cellContentCollapsed.set(!!this.element.isInputCollapsed);
this.cellOutputCollapsed.set(!!this.element.isOutputCollapsed);
}
private updateForOutputs() {
if (this.element instanceof CodeCellViewModel) {
this.cellHasOutputs.set(this.element.outputsViewModels.length > 0);
} else {
this.cellHasOutputs.set(false);
}
}
}
|