Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/bulkEdit/browser/opaqueEdits.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { CancellationToken } from '../../../../base/common/cancellation.js';
7
import { isObject } from '../../../../base/common/types.js';
8
import { URI } from '../../../../base/common/uri.js';
9
import { ResourceEdit } from '../../../../editor/browser/services/bulkEditService.js';
10
import { ICustomEdit, WorkspaceEditMetadata } from '../../../../editor/common/languages.js';
11
import { IProgress } from '../../../../platform/progress/common/progress.js';
12
import { IUndoRedoService, UndoRedoElementType, UndoRedoGroup, UndoRedoSource } from '../../../../platform/undoRedo/common/undoRedo.js';
13
14
export class ResourceAttachmentEdit extends ResourceEdit implements ICustomEdit {
15
16
static is(candidate: any): candidate is ICustomEdit {
17
if (candidate instanceof ResourceAttachmentEdit) {
18
return true;
19
} else {
20
return isObject(candidate)
21
&& (Boolean((<ICustomEdit>candidate).undo && (<ICustomEdit>candidate).redo));
22
}
23
}
24
25
static lift(edit: ICustomEdit): ResourceAttachmentEdit {
26
if (edit instanceof ResourceAttachmentEdit) {
27
return edit;
28
} else {
29
return new ResourceAttachmentEdit(edit.resource, edit.undo, edit.redo, edit.metadata);
30
}
31
}
32
33
constructor(
34
readonly resource: URI,
35
readonly undo: () => Promise<void> | void,
36
readonly redo: () => Promise<void> | void,
37
metadata?: WorkspaceEditMetadata
38
) {
39
super(metadata);
40
}
41
}
42
43
export class OpaqueEdits {
44
45
constructor(
46
private readonly _undoRedoGroup: UndoRedoGroup,
47
private readonly _undoRedoSource: UndoRedoSource | undefined,
48
private readonly _progress: IProgress<void>,
49
private readonly _token: CancellationToken,
50
private readonly _edits: ResourceAttachmentEdit[],
51
@IUndoRedoService private readonly _undoRedoService: IUndoRedoService,
52
) { }
53
54
async apply(): Promise<readonly URI[]> {
55
const resources: URI[] = [];
56
57
for (const edit of this._edits) {
58
if (this._token.isCancellationRequested) {
59
break;
60
}
61
62
await edit.redo();
63
64
this._undoRedoService.pushElement({
65
type: UndoRedoElementType.Resource,
66
resource: edit.resource,
67
label: edit.metadata?.label || 'Custom Edit',
68
code: 'paste',
69
undo: edit.undo,
70
redo: edit.redo,
71
}, this._undoRedoGroup, this._undoRedoSource);
72
73
this._progress.report(undefined);
74
resources.push(edit.resource);
75
}
76
77
return resources;
78
}
79
}
80
81