1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { SingleProxyRPCProtocol } from 'vs/workbench/api/test/common/testRPCProtocol';
import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService';
import { ModelService } from 'vs/editor/common/services/modelService';
import { TestCodeEditorService } from 'vs/editor/test/browser/editorTestServices';
import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles';
import { IWorkspaceTextEditDto, WorkspaceEditType } from 'vs/workbench/api/common/extHost.protocol';
import { mock } from 'vs/base/test/common/mock';
import { Event } from 'vs/base/common/event';
import { URI } from 'vs/base/common/uri';
import { Range } from 'vs/editor/common/core/range';
import { Position } from 'vs/editor/common/core/position';
import { IModelService } from 'vs/editor/common/services/model';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { TestFileService, TestEditorService, TestEditorGroupsService, TestEnvironmentService, TestLifecycleService } from 'vs/workbench/test/browser/workbenchTestServices';
import { BulkEditService } from 'vs/workbench/contrib/bulkEdit/browser/bulkEditService';
import { NullLogService, ILogService } from 'vs/platform/log/common/log';
import { ITextModelService, IResolvedTextEditorModel } from 'vs/editor/common/services/resolverService';
import { IReference, ImmortalReference, DisposableStore } from 'vs/base/common/lifecycle';
import { LabelService } from 'vs/workbench/services/label/common/labelService';
import { TestThemeService } from 'vs/platform/theme/test/common/testThemeService';
import { IEditorWorkerService } from 'vs/editor/common/services/editorWorker';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
import { IFileService } from 'vs/platform/files/common/files';
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { InstantiationService } from 'vs/platform/instantiation/common/instantiationService';
import { IBulkEditService } from 'vs/editor/browser/services/bulkEditService';
import { SyncDescriptor } from 'vs/platform/instantiation/common/descriptors';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
import { ILabelService } from 'vs/platform/label/common/label';
import { IWorkingCopyFileService, IMoveOperation, IDeleteOperation, ICopyOperation, ICreateFileOperation, ICreateOperation } from 'vs/workbench/services/workingCopy/common/workingCopyFileService';
import { UndoRedoService } from 'vs/platform/undoRedo/common/undoRedoService';
import { TestDialogService } from 'vs/platform/dialogs/test/common/testDialogService';
import { IDialogService } from 'vs/platform/dialogs/common/dialogs';
import { IUndoRedoService } from 'vs/platform/undoRedo/common/undoRedo';
import { TestNotificationService } from 'vs/platform/notification/test/common/testNotificationService';
import { INotificationService } from 'vs/platform/notification/common/notification';
import { TestTextResourcePropertiesService, TestContextService } from 'vs/workbench/test/common/workbenchTestServices';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
import { extUri } from 'vs/base/common/resources';
import { ITextSnapshot } from 'vs/editor/common/model';
import { ILifecycleService } from 'vs/workbench/services/lifecycle/common/lifecycle';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { IPaneCompositePartService } from 'vs/workbench/services/panecomposite/browser/panecomposite';
import { TestLanguageConfigurationService } from 'vs/editor/test/common/modes/testLanguageConfigurationService';
import { LanguageService } from 'vs/editor/common/services/languageService';
import { LanguageFeatureDebounceService } from 'vs/editor/common/services/languageFeatureDebounce';
import { LanguageFeaturesService } from 'vs/editor/common/services/languageFeaturesService';
import { MainThreadBulkEdits } from 'vs/workbench/api/browser/mainThreadBulkEdits';
suite('MainThreadEditors', () => {
let disposables: DisposableStore;
const resource = URI.parse('foo:bar');
let modelService: IModelService;
let bulkEdits: MainThreadBulkEdits;
const movedResources = new Map<URI, URI>();
const copiedResources = new Map<URI, URI>();
const createdResources = new Set<URI>();
const deletedResources = new Set<URI>();
setup(() => {
disposables = new DisposableStore();
movedResources.clear();
copiedResources.clear();
createdResources.clear();
deletedResources.clear();
const configService = new TestConfigurationService();
const dialogService = new TestDialogService();
const notificationService = new TestNotificationService();
const undoRedoService = new UndoRedoService(dialogService, notificationService);
const themeService = new TestThemeService();
const logService = new NullLogService();
modelService = new ModelService(
configService,
new TestTextResourcePropertiesService(configService),
themeService,
logService,
undoRedoService,
disposables.add(new LanguageService()),
new TestLanguageConfigurationService(),
new LanguageFeatureDebounceService(logService),
new LanguageFeaturesService()
);
const services = new ServiceCollection();
services.set(IBulkEditService, new SyncDescriptor(BulkEditService));
services.set(ILabelService, new SyncDescriptor(LabelService));
services.set(ILogService, new NullLogService());
services.set(IWorkspaceContextService, new TestContextService());
services.set(IEnvironmentService, TestEnvironmentService);
services.set(IWorkbenchEnvironmentService, TestEnvironmentService);
services.set(IConfigurationService, configService);
services.set(IDialogService, dialogService);
services.set(INotificationService, notificationService);
services.set(IUndoRedoService, undoRedoService);
services.set(IModelService, modelService);
services.set(ICodeEditorService, new TestCodeEditorService(themeService));
services.set(IFileService, new TestFileService());
services.set(IEditorService, new TestEditorService());
services.set(ILifecycleService, new TestLifecycleService());
services.set(IEditorGroupsService, new TestEditorGroupsService());
services.set(ITextFileService, new class extends mock<ITextFileService>() {
override isDirty() { return false; }
override files = <any>{
onDidSave: Event.None,
onDidRevert: Event.None,
onDidChangeDirty: Event.None
};
override create(operations: { resource: URI }[]) {
for (const o of operations) {
createdResources.add(o.resource);
}
return Promise.resolve(Object.create(null));
}
override async getEncodedReadable(resource: URI, value?: string | ITextSnapshot): Promise<any> {
return undefined;
}
});
services.set(IWorkingCopyFileService, new class extends mock<IWorkingCopyFileService>() {
override onDidRunWorkingCopyFileOperation = Event.None;
override createFolder(operations: ICreateOperation[]): any {
this.create(operations);
}
override create(operations: ICreateFileOperation[]) {
for (const operation of operations) {
createdResources.add(operation.resource);
}
return Promise.resolve(Object.create(null));
}
override move(operations: IMoveOperation[]) {
const { source, target } = operations[0].file;
movedResources.set(source, target);
return Promise.resolve(Object.create(null));
}
override copy(operations: ICopyOperation[]) {
const { source, target } = operations[0].file;
copiedResources.set(source, target);
return Promise.resolve(Object.create(null));
}
override delete(operations: IDeleteOperation[]) {
for (const operation of operations) {
deletedResources.add(operation.resource);
}
return Promise.resolve(undefined);
}
});
services.set(ITextModelService, new class extends mock<ITextModelService>() {
override createModelReference(resource: URI): Promise<IReference<IResolvedTextEditorModel>> {
const textEditorModel = new class extends mock<IResolvedTextEditorModel>() {
override textEditorModel = modelService.getModel(resource)!;
};
textEditorModel.isReadonly = () => false;
return Promise.resolve(new ImmortalReference(textEditorModel));
}
});
services.set(IEditorWorkerService, new class extends mock<IEditorWorkerService>() {
});
services.set(IPaneCompositePartService, new class extends mock<IPaneCompositePartService>() implements IPaneCompositePartService {
override onDidPaneCompositeOpen = Event.None;
override onDidPaneCompositeClose = Event.None;
override getActivePaneComposite() {
return undefined;
}
});
services.set(IUriIdentityService, new class extends mock<IUriIdentityService>() {
override get extUri() { return extUri; }
});
const instaService = new InstantiationService(services);
bulkEdits = instaService.createInstance(MainThreadBulkEdits, SingleProxyRPCProtocol(null));
});
teardown(() => {
disposables.dispose();
});
test(`applyWorkspaceEdit returns false if model is changed by user`, () => {
const model = modelService.createModel('something', null, resource);
const workspaceResourceEdit: IWorkspaceTextEditDto = {
_type: WorkspaceEditType.Text,
resource: resource,
modelVersionId: model.getVersionId(),
edit: {
text: 'asdfg',
range: new Range(1, 1, 1, 1)
}
};
// Act as if the user edited the model
model.applyEdits([EditOperation.insert(new Position(0, 0), 'something')]);
return bulkEdits.$tryApplyWorkspaceEdit({ edits: [workspaceResourceEdit] }).then((result) => {
assert.strictEqual(result, false);
});
});
test(`issue #54773: applyWorkspaceEdit checks model version in race situation`, () => {
const model = modelService.createModel('something', null, resource);
const workspaceResourceEdit1: IWorkspaceTextEditDto = {
_type: WorkspaceEditType.Text,
resource: resource,
modelVersionId: model.getVersionId(),
edit: {
text: 'asdfg',
range: new Range(1, 1, 1, 1)
}
};
const workspaceResourceEdit2: IWorkspaceTextEditDto = {
_type: WorkspaceEditType.Text,
resource: resource,
modelVersionId: model.getVersionId(),
edit: {
text: 'asdfg',
range: new Range(1, 1, 1, 1)
}
};
const p1 = bulkEdits.$tryApplyWorkspaceEdit({ edits: [workspaceResourceEdit1] }).then((result) => {
// first edit request succeeds
assert.strictEqual(result, true);
});
const p2 = bulkEdits.$tryApplyWorkspaceEdit({ edits: [workspaceResourceEdit2] }).then((result) => {
// second edit request fails
assert.strictEqual(result, false);
});
return Promise.all([p1, p2]);
});
test(`applyWorkspaceEdit with only resource edit`, () => {
return bulkEdits.$tryApplyWorkspaceEdit({
edits: [
{ _type: WorkspaceEditType.File, oldUri: resource, newUri: resource, options: undefined },
{ _type: WorkspaceEditType.File, oldUri: undefined, newUri: resource, options: undefined },
{ _type: WorkspaceEditType.File, oldUri: resource, newUri: undefined, options: undefined }
]
}).then((result) => {
assert.strictEqual(result, true);
assert.strictEqual(movedResources.get(resource), resource);
assert.strictEqual(createdResources.has(resource), true);
assert.strictEqual(deletedResources.has(resource), true);
});
});
});
|