Skip to content

Commit 85ce229

Browse files
committed
Hook up undo/redo in multi cursor editing mode.
1 parent 521cc16 commit 85ce229

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/vs/workbench/contrib/notebook/browser/contrib/multicursor/notebookMulticursor.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import { registerNotebookContribution } from 'vs/workbench/contrib/notebook/brow
4040
import { CellEditorOptions } from 'vs/workbench/contrib/notebook/browser/view/cellParts/cellEditorOptions';
4141
import { NOTEBOOK_CELL_EDITOR_FOCUSED, NOTEBOOK_IS_ACTIVE_EDITOR } from 'vs/workbench/contrib/notebook/common/notebookContextKeys';
4242
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
43+
import { RedoCommand, UndoCommand } from 'vs/editor/browser/editorExtensions';
44+
import { registerWorkbenchContribution2, WorkbenchPhase } from 'vs/workbench/common/contributions';
4345

4446
const NOTEBOOK_ADD_FIND_MATCH_TO_SELECTION_ID = 'notebook.addFindMatchToSelection';
4547

@@ -475,6 +477,30 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
475477
});
476478
}
477479

480+
async undo() {
481+
const models: ITextModel[] = [];
482+
for (const match of this.trackedMatches) {
483+
const model = await match.cellViewModel.resolveTextModel();
484+
if (model) {
485+
models.push(model);
486+
}
487+
}
488+
489+
await Promise.all(models.map(model => model.undo()));
490+
}
491+
492+
async redo() {
493+
const models: ITextModel[] = [];
494+
for (const match of this.trackedMatches) {
495+
const model = await match.cellViewModel.resolveTextModel();
496+
if (model) {
497+
models.push(model);
498+
}
499+
}
500+
501+
await Promise.all(models.map(model => model.redo()));
502+
}
503+
478504
private constructCellEditorOptions(cell: ICellViewModel): EditorConfiguration {
479505
const cellEditorOptions = new CellEditorOptions(this.notebookEditor.getBaseCellEditorOptions(cell.language), this.notebookEditor.notebookOptions, this.configurationService);
480506
const options = cellEditorOptions.getUpdatedValue(cell.internalMetadata, cell.uri);
@@ -670,7 +696,59 @@ class NotebookDeleteLeftMultiSelectionAction extends NotebookAction {
670696
}
671697
}
672698

699+
class NotebookMultiCursorUndoRedoContribution extends Disposable {
700+
701+
static readonly ID = 'workbench.contrib.notebook.multiCursorUndoRedo';
702+
703+
constructor(@IEditorService private readonly _editorService: IEditorService, @IConfigurationService private readonly configurationService: IConfigurationService) {
704+
super();
705+
706+
if (!this.configurationService.getValue<boolean>('notebook.multiSelect.enabled')) {
707+
return;
708+
}
709+
710+
const PRIORITY = 10005;
711+
this._register(UndoCommand.addImplementation(PRIORITY, 'notebook-multicursor-undo-redo', () => {
712+
const editor = getNotebookEditorFromEditorPane(this._editorService.activeEditorPane);
713+
if (!editor) {
714+
return false;
715+
}
716+
717+
if (!editor.hasModel()) {
718+
return false;
719+
}
720+
721+
const controller = editor.getContribution<NotebookMultiCursorController>(NotebookMultiCursorController.id);
722+
723+
return controller.undo();
724+
}, ContextKeyExpr.and(
725+
ContextKeyExpr.equals('config.notebook.multiSelect.enabled', true),
726+
NOTEBOOK_IS_ACTIVE_EDITOR,
727+
NOTEBOOK_MULTI_SELECTION_CONTEXT.IsNotebookMultiSelect,
728+
)));
729+
730+
this._register(RedoCommand.addImplementation(PRIORITY, 'notebook-multicursor-undo-redo', () => {
731+
const editor = getNotebookEditorFromEditorPane(this._editorService.activeEditorPane);
732+
if (!editor) {
733+
return false;
734+
}
735+
736+
if (!editor.hasModel()) {
737+
return false;
738+
}
739+
740+
const controller = editor.getContribution<NotebookMultiCursorController>(NotebookMultiCursorController.id);
741+
return controller.redo();
742+
}, ContextKeyExpr.and(
743+
ContextKeyExpr.equals('config.notebook.multiSelect.enabled', true),
744+
NOTEBOOK_IS_ACTIVE_EDITOR,
745+
NOTEBOOK_MULTI_SELECTION_CONTEXT.IsNotebookMultiSelect,
746+
)));
747+
}
748+
}
749+
673750
registerNotebookContribution(NotebookMultiCursorController.id, NotebookMultiCursorController);
674751
registerAction2(NotebookAddMatchToMultiSelectionAction);
675752
registerAction2(NotebookExitMultiSelectionAction);
676753
registerAction2(NotebookDeleteLeftMultiSelectionAction);
754+
registerWorkbenchContribution2(NotebookMultiCursorUndoRedoContribution.ID, NotebookMultiCursorUndoRedoContribution, WorkbenchPhase.BlockRestore);

0 commit comments

Comments
 (0)