Path: blob/main/src/vs/workbench/contrib/bulkEdit/browser/opaqueEdits.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { CancellationToken } from '../../../../base/common/cancellation.js';6import { isObject } from '../../../../base/common/types.js';7import { URI } from '../../../../base/common/uri.js';8import { ResourceEdit } from '../../../../editor/browser/services/bulkEditService.js';9import { ICustomEdit, WorkspaceEditMetadata } from '../../../../editor/common/languages.js';10import { IProgress } from '../../../../platform/progress/common/progress.js';11import { IUndoRedoService, UndoRedoElementType, UndoRedoGroup, UndoRedoSource } from '../../../../platform/undoRedo/common/undoRedo.js';1213export class ResourceAttachmentEdit extends ResourceEdit implements ICustomEdit {1415static is(candidate: any): candidate is ICustomEdit {16if (candidate instanceof ResourceAttachmentEdit) {17return true;18} else {19return isObject(candidate)20&& (Boolean((<ICustomEdit>candidate).undo && (<ICustomEdit>candidate).redo));21}22}2324static lift(edit: ICustomEdit): ResourceAttachmentEdit {25if (edit instanceof ResourceAttachmentEdit) {26return edit;27} else {28return new ResourceAttachmentEdit(edit.resource, edit.undo, edit.redo, edit.metadata);29}30}3132constructor(33readonly resource: URI,34readonly undo: () => Promise<void> | void,35readonly redo: () => Promise<void> | void,36metadata?: WorkspaceEditMetadata37) {38super(metadata);39}40}4142export class OpaqueEdits {4344constructor(45private readonly _undoRedoGroup: UndoRedoGroup,46private readonly _undoRedoSource: UndoRedoSource | undefined,47private readonly _progress: IProgress<void>,48private readonly _token: CancellationToken,49private readonly _edits: ResourceAttachmentEdit[],50@IUndoRedoService private readonly _undoRedoService: IUndoRedoService,51) { }5253async apply(): Promise<readonly URI[]> {54const resources: URI[] = [];5556for (const edit of this._edits) {57if (this._token.isCancellationRequested) {58break;59}6061await edit.redo();6263this._undoRedoService.pushElement({64type: UndoRedoElementType.Resource,65resource: edit.resource,66label: edit.metadata?.label || 'Custom Edit',67code: 'paste',68undo: edit.undo,69redo: edit.redo,70}, this._undoRedoGroup, this._undoRedoSource);7172this._progress.report(undefined);73resources.push(edit.resource);74}7576return resources;77}78}798081