Skip to content

Commit 450c0fe

Browse files
committed
Make CellContextKeys a CellPart #131808
1 parent 6723778 commit 450c0fe

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

src/vs/workbench/contrib/notebook/browser/view/cellParts/cellContextKeys.ts

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ import { MarkupCellViewModel } from 'vs/workbench/contrib/notebook/browser/viewM
1212
import { NotebookCellExecutionState } from 'vs/workbench/contrib/notebook/common/notebookCommon';
1313
import { INotebookExecutionStateService } from 'vs/workbench/contrib/notebook/common/notebookExecutionStateService';
1414
import { CellViewModelStateChangeEvent } from 'vs/workbench/contrib/notebook/browser/notebookViewEvents';
15+
import { CellPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellPart';
16+
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
17+
18+
export class CellContextKeyPart extends CellPart {
19+
private cellContextKeyManager: CellContextKeyManager;
20+
21+
constructor(
22+
notebookEditor: INotebookEditorDelegate,
23+
@IInstantiationService private readonly instantiationService: IInstantiationService,
24+
) {
25+
super();
26+
27+
this.cellContextKeyManager = this._register(this.instantiationService.createInstance(CellContextKeyManager, notebookEditor, undefined));
28+
}
29+
30+
protected override didRenderCell(element: ICellViewModel): void {
31+
this.cellContextKeyManager.updateForElement(element);
32+
}
33+
}
1534

1635
export class CellContextKeyManager extends Disposable {
1736

@@ -32,7 +51,7 @@ export class CellContextKeyManager extends Disposable {
3251

3352
constructor(
3453
private readonly notebookEditor: INotebookEditorDelegate,
35-
private element: ICellViewModel,
54+
private element: ICellViewModel | undefined,
3655
@IContextKeyService private readonly _contextKeyService: IContextKeyService,
3756
@INotebookExecutionStateService private readonly _notebookExecutionStateService: INotebookExecutionStateService
3857
) {
@@ -51,18 +70,26 @@ export class CellContextKeyManager extends Disposable {
5170
this.cellOutputCollapsed = NOTEBOOK_CELL_OUTPUT_COLLAPSED.bindTo(this._contextKeyService);
5271
this.cellLineNumbers = NOTEBOOK_CELL_LINE_NUMBERS.bindTo(this._contextKeyService);
5372

54-
this.updateForElement(element);
73+
if (element) {
74+
this.updateForElement(element);
75+
}
5576
});
5677

5778
this._register(this._notebookExecutionStateService.onDidChangeCellExecution(e => {
58-
if (e.affectsCell(this.element.uri)) {
79+
if (this.element && e.affectsCell(this.element.uri)) {
5980
this.updateForExecutionState();
6081
}
6182
}));
6283
}
6384

64-
public updateForElement(element: ICellViewModel) {
85+
public updateForElement(element: ICellViewModel | undefined) {
6586
this.elementDisposables.clear();
87+
this.element = element;
88+
89+
if (!element) {
90+
return;
91+
}
92+
6693
this.elementDisposables.add(element.onDidChangeState(e => this.onDidChangeState(e)));
6794

6895
if (element instanceof CodeCellViewModel) {
@@ -71,7 +98,6 @@ export class CellContextKeyManager extends Disposable {
7198

7299
this.elementDisposables.add(this.notebookEditor.onDidChangeActiveCell(() => this.updateForFocusState()));
73100

74-
this.element = element;
75101
if (this.element instanceof MarkupCellViewModel) {
76102
this.cellType.set('markup');
77103
} else if (this.element instanceof CodeCellViewModel) {
@@ -85,7 +111,7 @@ export class CellContextKeyManager extends Disposable {
85111
this.updateForCollapseState();
86112
this.updateForOutputs();
87113

88-
this.cellLineNumbers.set(this.element.lineNumbers);
114+
this.cellLineNumbers.set(this.element!.lineNumbers);
89115
});
90116
}
91117

@@ -104,7 +130,7 @@ export class CellContextKeyManager extends Disposable {
104130
}
105131

106132
if (e.cellLineNumberChanged) {
107-
this.cellLineNumbers.set(this.element.lineNumbers);
133+
this.cellLineNumbers.set(this.element!.lineNumbers);
108134
}
109135

110136
if (e.inputCollapsedChanged || e.outputCollapsedChanged) {
@@ -114,6 +140,10 @@ export class CellContextKeyManager extends Disposable {
114140
}
115141

116142
private updateForFocusState() {
143+
if (!this.element) {
144+
return;
145+
}
146+
117147
const activeCell = this.notebookEditor.getActiveCell();
118148
this.cellFocused.set(this.notebookEditor.getActiveCell() === this.element);
119149

@@ -126,6 +156,10 @@ export class CellContextKeyManager extends Disposable {
126156
}
127157

128158
private updateForExecutionState() {
159+
if (!this.element) {
160+
return;
161+
}
162+
129163
const internalMetadata = this.element.internalMetadata;
130164
this.cellEditable.set(!this.notebookEditor.isReadOnly);
131165

@@ -152,6 +186,10 @@ export class CellContextKeyManager extends Disposable {
152186
}
153187

154188
private updateForEditState() {
189+
if (!this.element) {
190+
return;
191+
}
192+
155193
if (this.element instanceof MarkupCellViewModel) {
156194
this.markdownEditMode.set(this.element.getEditState() === CellEditState.Editing);
157195
} else {
@@ -160,6 +198,10 @@ export class CellContextKeyManager extends Disposable {
160198
}
161199

162200
private updateForCollapseState() {
201+
if (!this.element) {
202+
return;
203+
}
204+
163205
this.cellContentCollapsed.set(!!this.element.isInputCollapsed);
164206
this.cellOutputCollapsed.set(!!this.element.isOutputCollapsed);
165207
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { ServiceCollection } from 'vs/platform/instantiation/common/serviceColle
2424
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
2525
import { INotificationService } from 'vs/platform/notification/common/notification';
2626
import { ICellViewModel, INotebookEditorDelegate } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
27-
import { CellContextKeyManager } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellContextKeys';
27+
import { CellContextKeyPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellContextKeys';
2828
import { CellDecorations } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellDecorations';
2929
import { CellDragAndDropController, CellDragAndDropPart } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellDnd';
3030
import { CodeCellDragImageRenderer } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellDragRenderer';
@@ -107,7 +107,6 @@ abstract class AbstractCellRenderer {
107107

108108
protected commonRenderElement(element: ICellViewModel, templateData: BaseCellRenderTemplate): void {
109109
templateData.elementDisposables.add(new CellDecorations(templateData.rootContainer, templateData.decorationContainer, element));
110-
templateData.elementDisposables.add(templateData.instantiationService.createInstance(CellContextKeyManager, this.notebookEditor, element));
111110
}
112111
}
113112

@@ -179,7 +178,8 @@ export class MarkupCellRenderer extends AbstractCellRenderer implements IListRen
179178
foldedCellHint,
180179
templateDisposables.add(new CollapsedCellInput(this.notebookEditor, cellInputCollapsedContainer)),
181180
templateDisposables.add(new CellFocusPart(container, undefined, this.notebookEditor)),
182-
templateDisposables.add(new CellDragAndDropPart()),
181+
templateDisposables.add(new CellDragAndDropPart(container)),
182+
templateDisposables.add(this.instantiationService.createInstance(CellContextKeyPart, this.notebookEditor)),
183183
];
184184

185185
const templateData: MarkdownCellRenderTemplate = {
@@ -330,7 +330,8 @@ export class CodeCellRenderer extends AbstractCellRenderer implements IListRende
330330
templateDisposables.add(this.instantiationService.createInstance(CollapsedCellOutput, this.notebookEditor, cellOutputCollapsedContainer)),
331331
templateDisposables.add(new CollapsedCellInput(this.notebookEditor, cellInputCollapsedContainer)),
332332
templateDisposables.add(new CellFocusPart(container, focusSinkElement, this.notebookEditor)),
333-
templateDisposables.add(new CellDragAndDropPart()),
333+
templateDisposables.add(new CellDragAndDropPart(container)),
334+
templateDisposables.add(this.instantiationService.createInstance(CellContextKeyPart, this.notebookEditor)),
334335
];
335336

336337
const templateData: CodeCellRenderTemplate = {

0 commit comments

Comments
 (0)