Skip to content

Commit 521cc16

Browse files
committed
Update undo/redo hooks
1 parent 89bca2a commit 521cc16

File tree

1 file changed

+11
-99
lines changed

1 file changed

+11
-99
lines changed

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

Lines changed: 11 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ import { Emitter, Event } from 'vs/base/common/event';
88
import { KeyCode, KeyMod } from 'vs/base/common/keyCodes';
99
import { Disposable, DisposableStore } from 'vs/base/common/lifecycle';
1010
import { ResourceMap } from 'vs/base/common/map';
11+
import { URI } from 'vs/base/common/uri';
1112
import { EditorConfiguration } from 'vs/editor/browser/config/editorConfiguration';
1213
import { ICodeEditor } from 'vs/editor/browser/editorBrowser';
13-
import { RedoCommand, UndoCommand } from 'vs/editor/browser/editorExtensions';
1414
import { CodeEditorWidget } from 'vs/editor/browser/widget/codeEditor/codeEditorWidget';
1515
import { IEditorConfiguration } from 'vs/editor/common/config/editorConfiguration';
1616
import { Position } from 'vs/editor/common/core/position';
@@ -34,7 +34,6 @@ import { ContextKeyExpr, IContextKeyService, RawContextKey } from 'vs/platform/c
3434
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
3535
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
3636
import { IPastFutureElements, IUndoRedoElement, IUndoRedoService, UndoRedoElementType } from 'vs/platform/undoRedo/common/undoRedo';
37-
import { registerWorkbenchContribution2, WorkbenchPhase } from 'vs/workbench/common/contributions';
3837
import { INotebookActionContext, NotebookAction } from 'vs/workbench/contrib/notebook/browser/controller/coreActions';
3938
import { getNotebookEditorFromEditorPane, ICellViewModel, INotebookEditor, INotebookEditorContribution } from 'vs/workbench/contrib/notebook/browser/notebookBrowser';
4039
import { registerNotebookContribution } from 'vs/workbench/contrib/notebook/browser/notebookEditorExtensions';
@@ -79,7 +78,7 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
7978

8079
private readonly anchorDisposables = this._register(new DisposableStore());
8180
private readonly cursorsDisposables = this._register(new DisposableStore());
82-
private cursorsControllers: ResourceMap<[ITextModel, CursorsController]> = new ResourceMap<[ITextModel, CursorsController]>();
81+
private cursorsControllers: ResourceMap<CursorsController> = new ResourceMap<CursorsController>();
8382

8483
private _nbIsMultiSelectSession = NOTEBOOK_MULTI_SELECTION_CONTEXT.IsNotebookMultiSelect.bindTo(this.contextKeyService);
8584
private _nbMultiSelectState = NOTEBOOK_MULTI_SELECTION_CONTEXT.NotebookMultiSelectState.bindTo(this.contextKeyService);
@@ -131,7 +130,7 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
131130
new CursorConfiguration(textModel.getLanguageId(), textModel.getOptions(), editorConfig, this.languageConfigurationService)
132131
));
133132
controller.setSelections(new ViewModelEventsCollector(), undefined, match.wordSelections, CursorChangeReason.Explicit);
134-
this.cursorsControllers.set(match.cellViewModel.uri, [textModel, controller]);
133+
this.cursorsControllers.set(match.cellViewModel.uri, controller);
135134
});
136135
}
137136

@@ -270,42 +269,35 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
270269
return;
271270
}
272271

273-
const textModels = [anchorCellModel];
274-
this.cursorsControllers.forEach(controller => {
275-
const model = controller[0];
276-
textModels.push(model);
277-
});
278-
279272
const newElementsMap: ResourceMap<IUndoRedoElement[]> = new ResourceMap<IUndoRedoElement[]>();
273+
const resources: URI[] = [];
280274

281-
textModels.forEach(model => {
282-
const trackedMatch = this.trackedMatches.find(match => match.cellViewModel.uri.toString() === model.uri.toString());
283-
if (!trackedMatch) {
284-
return;
285-
}
275+
this.trackedMatches.forEach(trackedMatch => {
286276
const undoRedoState = trackedMatch.undoRedoHistory;
287277
if (!undoRedoState) {
288278
return;
289279
}
290280

291-
const currentPastElements = this.undoRedoService.getElements(model.uri).past.slice();
281+
resources.push(trackedMatch.cellViewModel.uri);
282+
283+
const currentPastElements = this.undoRedoService.getElements(trackedMatch.cellViewModel.uri).past.slice();
292284
const oldPastElements = trackedMatch.undoRedoHistory.past.slice();
293285
const newElements = currentPastElements.slice(oldPastElements.length);
294286
if (newElements.length === 0) {
295287
return;
296288
}
297289

298-
newElementsMap.set(model.uri, newElements);
290+
newElementsMap.set(trackedMatch.cellViewModel.uri, newElements);
299291

300-
this.undoRedoService.removeElements(model.uri);
292+
this.undoRedoService.removeElements(trackedMatch.cellViewModel.uri);
301293
oldPastElements.forEach(element => {
302294
this.undoRedoService.pushElement(element);
303295
});
304296
});
305297

306298
this.undoRedoService.pushElement({
307299
type: UndoRedoElementType.Workspace,
308-
resources: textModels.map(model => model.uri),
300+
resources: resources,
309301
label: 'Multi Cursor Edit',
310302
code: 'multiCursorEdit',
311303
confirmBeforeUndo: false,
@@ -553,38 +545,6 @@ export class NotebookMultiCursorController extends Disposable implements INotebo
553545
);
554546
}
555547

556-
async undo() {
557-
const anchorCellModel = this.anchorCell?.[1].getModel();
558-
if (!anchorCellModel) {
559-
// should not happen
560-
return;
561-
}
562-
563-
const models = [anchorCellModel];
564-
this.cursorsControllers.forEach(controller => {
565-
const model = controller[0];
566-
models.push(model);
567-
});
568-
569-
await Promise.all(models.map(model => model.undo()));
570-
}
571-
572-
async redo() {
573-
const anchorCellModel = this.anchorCell?.[1].getModel();
574-
if (!anchorCellModel) {
575-
// should not happen
576-
return;
577-
}
578-
579-
const models = [anchorCellModel];
580-
this.cursorsControllers.forEach(controller => {
581-
const model = controller[0];
582-
models.push(model);
583-
});
584-
585-
await Promise.all(models.map(model => model.redo()));
586-
}
587-
588548
private getWord(selection: Selection, model: ITextModel): IWordAtPosition | null {
589549
const lineNumber = selection.startLineNumber;
590550
const startColumn = selection.startColumn;
@@ -676,53 +636,6 @@ class NotebookExitMultiSelectionAction extends NotebookAction {
676636
}
677637
}
678638

679-
class NotebookMultiCursorUndoRedoContribution extends Disposable {
680-
681-
static readonly ID = 'workbench.contrib.notebook.multiCursorUndoRedo';
682-
683-
constructor(@IEditorService private readonly _editorService: IEditorService) {
684-
super();
685-
686-
const PRIORITY = 10005;
687-
this._register(UndoCommand.addImplementation(PRIORITY, 'notebook-multicursor-undo-redo', () => {
688-
const editor = getNotebookEditorFromEditorPane(this._editorService.activeEditorPane);
689-
if (!editor) {
690-
return false;
691-
}
692-
693-
if (!editor.hasModel()) {
694-
return false;
695-
}
696-
697-
const controller = editor.getContribution<NotebookMultiCursorController>(NotebookMultiCursorController.id);
698-
699-
return controller.undo();
700-
}, ContextKeyExpr.and(
701-
ContextKeyExpr.equals('config.notebook.multiSelect.enabled', true),
702-
NOTEBOOK_IS_ACTIVE_EDITOR,
703-
NOTEBOOK_MULTI_SELECTION_CONTEXT.IsNotebookMultiSelect,
704-
)));
705-
706-
this._register(RedoCommand.addImplementation(PRIORITY, 'notebook-multicursor-undo-redo', () => {
707-
const editor = getNotebookEditorFromEditorPane(this._editorService.activeEditorPane);
708-
if (!editor) {
709-
return false;
710-
}
711-
712-
if (!editor.hasModel()) {
713-
return false;
714-
}
715-
716-
const controller = editor.getContribution<NotebookMultiCursorController>(NotebookMultiCursorController.id);
717-
return controller.redo();
718-
}, ContextKeyExpr.and(
719-
ContextKeyExpr.equals('config.notebook.multiSelect.enabled', true),
720-
NOTEBOOK_IS_ACTIVE_EDITOR,
721-
NOTEBOOK_MULTI_SELECTION_CONTEXT.IsNotebookMultiSelect,
722-
)));
723-
}
724-
}
725-
726639
class NotebookDeleteLeftMultiSelectionAction extends NotebookAction {
727640
constructor() {
728641
super({
@@ -760,5 +673,4 @@ class NotebookDeleteLeftMultiSelectionAction extends NotebookAction {
760673
registerNotebookContribution(NotebookMultiCursorController.id, NotebookMultiCursorController);
761674
registerAction2(NotebookAddMatchToMultiSelectionAction);
762675
registerAction2(NotebookExitMultiSelectionAction);
763-
registerWorkbenchContribution2(NotebookMultiCursorUndoRedoContribution.ID, NotebookMultiCursorUndoRedoContribution, WorkbenchPhase.BlockRestore);
764676
registerAction2(NotebookDeleteLeftMultiSelectionAction);

0 commit comments

Comments
 (0)