Skip to content

Commit 236e476

Browse files
authored
Merge pull request #158421 from amunger/undoIWCellDelete
enabled core cell delete for IW and undo operations
2 parents 84c46b7 + 0f48a35 commit 236e476

File tree

5 files changed

+17
-20
lines changed

5 files changed

+17
-20
lines changed

src/vs/workbench/contrib/bulkEdit/browser/bulkCellEdits.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export class BulkCellEdits {
7272

7373
// apply edits
7474
const edits = group.map(entry => entry.cellEdit);
75-
ref.object.notebook.applyEdits(edits, true, undefined, () => undefined, this._undoRedoGroup, true);
75+
const computeUndo = !ref.object.isReadonly;
76+
ref.object.notebook.applyEdits(edits, true, undefined, () => undefined, this._undoRedoGroup, computeUndo);
7677
ref.dispose();
7778

7879
this._progress.report(undefined);

src/vs/workbench/contrib/notebook/browser/controller/cellOperations.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo
123123
const targetCellIndex = editor.getCellIndex(cell);
124124
const containingSelection = selections.find(selection => selection.start <= targetCellIndex && targetCellIndex < selection.end);
125125

126+
const computeUndoRedo = !editor.isReadOnly || textModel.viewType === 'interactive';
126127
if (containingSelection) {
127128
const edits: ICellReplaceEdit[] = selections.reverse().map(selection => ({
128129
editType: CellEditType.Replace, index: selection.start, count: selection.end - selection.start, cells: []
@@ -143,7 +144,7 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo
143144
return { kind: SelectionStateType.Index, focus: { start: 0, end: 0 }, selections: [{ start: 0, end: 0 }] };
144145
}
145146
}
146-
}, undefined, true);
147+
}, undefined, computeUndoRedo);
147148
} else {
148149
const focus = editor.getFocus();
149150
const edits: ICellReplaceEdit[] = [{
@@ -169,14 +170,14 @@ export function runDeleteAction(editor: IActiveNotebookEditor, cell: ICellViewMo
169170

170171
textModel.applyEdits(edits, true, { kind: SelectionStateType.Index, focus: editor.getFocus(), selections: editor.getSelections() }, () => ({
171172
kind: SelectionStateType.Index, focus: newFocus, selections: finalSelections
172-
}), undefined, true);
173+
}), undefined, computeUndoRedo);
173174
} else {
174175
// users decide to delete a cell out of current focus/selection
175176
const newFocus = focus.start > targetCellIndex ? { start: focus.start - 1, end: focus.end - 1 } : focus;
176177

177178
textModel.applyEdits(edits, true, { kind: SelectionStateType.Index, focus: editor.getFocus(), selections: editor.getSelections() }, () => ({
178179
kind: SelectionStateType.Index, focus: newFocus, selections: finalSelections
179-
}), undefined, true);
180+
}), undefined, computeUndoRedo);
180181
}
181182
}
182183
}
@@ -649,6 +650,6 @@ export function insertCellAtIndex(viewModel: NotebookViewModel, index: number, s
649650
}
650651
]
651652
}
652-
], synchronous, { kind: SelectionStateType.Index, focus: viewModel.getFocus(), selections: viewModel.getSelections() }, () => endSelections, undefined, pushUndoStop);
653+
], synchronous, { kind: SelectionStateType.Index, focus: viewModel.getFocus(), selections: viewModel.getSelections() }, () => endSelections, undefined, pushUndoStop && !viewModel.options.isReadOnly);
653654
return viewModel.cellAt(index)!;
654655
}

src/vs/workbench/contrib/notebook/browser/controller/editActions.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ registerAction2(class DeleteCellAction extends NotebookCellAction {
134134
mac: {
135135
primary: KeyMod.CtrlCmd | KeyCode.Backspace
136136
},
137-
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, NOTEBOOK_EDITOR_EDITABLE, ContextKeyExpr.not(InputFocusedContextKey)),
137+
when: ContextKeyExpr.and(NOTEBOOK_EDITOR_FOCUSED, ContextKeyExpr.not(InputFocusedContextKey)),
138138
weight: KeybindingWeight.WorkbenchContrib
139139
},
140140
menu: [
@@ -145,7 +145,6 @@ registerAction2(class DeleteCellAction extends NotebookCellAction {
145145
},
146146
{
147147
id: MenuId.InteractiveCellDelete,
148-
when: NOTEBOOK_EDITOR_EDITABLE,
149148
group: CELL_TITLE_CELL_GROUP_ID
150149
}
151150
],
@@ -154,7 +153,7 @@ registerAction2(class DeleteCellAction extends NotebookCellAction {
154153
}
155154

156155
async runWithContext(accessor: ServicesAccessor, context: INotebookCellActionContext) {
157-
if (!context.notebookEditor.hasModel() || context.notebookEditor.isReadOnly) {
156+
if (!context.notebookEditor.hasModel()) {
158157
return;
159158
}
160159

@@ -202,7 +201,8 @@ registerAction2(class ClearCellOutputsAction extends NotebookCellAction {
202201
return;
203202
}
204203

205-
editor.textModel.applyEdits([{ editType: CellEditType.Output, index, outputs: [] }], true, undefined, () => undefined, undefined, true);
204+
const computeUndoRedo = !editor.isReadOnly;
205+
editor.textModel.applyEdits([{ editType: CellEditType.Output, index, outputs: [] }], true, undefined, () => undefined, undefined, computeUndoRedo);
206206

207207
const runState = notebookExecutionStateService.getCellExecution(context.cell.uri)?.state;
208208
if (runState !== NotebookCellExecutionState.Executing) {
@@ -214,7 +214,7 @@ registerAction2(class ClearCellOutputsAction extends NotebookCellAction {
214214
executionOrder: null,
215215
lastRunSuccess: null
216216
}
217-
}], true, undefined, () => undefined, undefined, true);
217+
}], true, undefined, () => undefined, undefined, computeUndoRedo);
218218
}
219219
}
220220
});
@@ -257,10 +257,11 @@ registerAction2(class ClearAllCellOutputsAction extends NotebookAction {
257257
return;
258258
}
259259

260+
const computeUndoRedo = !editor.isReadOnly;
260261
editor.textModel.applyEdits(
261262
editor.textModel.cells.map((cell, index) => ({
262263
editType: CellEditType.Output, index, outputs: []
263-
})), true, undefined, () => undefined, undefined, true);
264+
})), true, undefined, () => undefined, undefined, computeUndoRedo);
264265

265266
const clearExecutionMetadataEdits = editor.textModel.cells.map((cell, index) => {
266267
const runState = notebookExecutionStateService.getCellExecution(cell.uri)?.state;
@@ -279,7 +280,7 @@ registerAction2(class ClearAllCellOutputsAction extends NotebookAction {
279280
}
280281
}).filter(edit => !!edit) as ICellEditOperation[];
281282
if (clearExecutionMetadataEdits.length) {
282-
context.notebookEditor.textModel.applyEdits(clearExecutionMetadataEdits, true, undefined, () => undefined, undefined, true);
283+
context.notebookEditor.textModel.applyEdits(clearExecutionMetadataEdits, true, undefined, () => undefined, undefined, computeUndoRedo);
283284
}
284285
}
285286
});
@@ -499,7 +500,7 @@ async function setCellToLanguage(languageId: string, context: IChangeCellContext
499500
const index = context.notebookEditor.textModel.cells.indexOf(context.cell.model);
500501
context.notebookEditor.textModel.applyEdits(
501502
[{ editType: CellEditType.CellLanguage, index, language: languageId }],
502-
true, undefined, () => undefined, undefined, true
503+
true, undefined, () => undefined, undefined, !context.notebookEditor.isReadOnly
503504
);
504505
}
505506
}

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

-6
Original file line numberDiff line numberDiff line change
@@ -954,9 +954,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
954954
}
955955

956956
async undo() {
957-
if (this._options.isReadOnly) {
958-
return null;
959-
}
960957

961958
const editStack = this._undoService.getElements(this.uri);
962959
const element = editStack.past.length ? editStack.past[editStack.past.length - 1] : undefined;
@@ -974,9 +971,6 @@ export class NotebookViewModel extends Disposable implements EditorFoldingStateD
974971
}
975972

976973
async redo() {
977-
if (this._options.isReadOnly) {
978-
return null;
979-
}
980974

981975
const editStack = this._undoService.getElements(this.uri);
982976
const element = editStack.future[0];

src/vs/workbench/contrib/notebook/common/model/notebookTextModel.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ export class NotebookTextModel extends Disposable implements INotebookTextModel
402402
true,
403403
undefined, () => undefined,
404404
undefined,
405-
true
405+
false
406406
);
407407
}
408408

0 commit comments

Comments
 (0)