forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainThreadSCM.ts
432 lines (334 loc) · 12.6 KB
/
mainThreadSCM.ts
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { URI, UriComponents } from 'vs/base/common/uri';
import { Event, Emitter } from 'vs/base/common/event';
import { assign } from 'vs/base/common/objects';
import { IDisposable, DisposableStore, combinedDisposable } from 'vs/base/common/lifecycle';
import { ISCMService, ISCMRepository, ISCMProvider, ISCMResource, ISCMResourceGroup, ISCMResourceDecorations, IInputValidation } from 'vs/workbench/contrib/scm/common/scm';
import { ExtHostContext, MainThreadSCMShape, ExtHostSCMShape, SCMProviderFeatures, SCMRawResourceSplices, SCMGroupFeatures, MainContext, IExtHostContext } from '../common/extHost.protocol';
import { Command } from 'vs/editor/common/modes';
import { extHostNamedCustomer } from 'vs/workbench/api/common/extHostCustomers';
import { ISplice, Sequence } from 'vs/base/common/sequence';
import { CancellationToken } from 'vs/base/common/cancellation';
class MainThreadSCMResourceGroup implements ISCMResourceGroup {
readonly elements: ISCMResource[] = [];
private readonly _onDidSplice = new Emitter<ISplice<ISCMResource>>();
readonly onDidSplice = this._onDidSplice.event;
get hideWhenEmpty(): boolean { return !!this.features.hideWhenEmpty; }
private readonly _onDidChange = new Emitter<void>();
readonly onDidChange: Event<void> = this._onDidChange.event;
constructor(
private readonly sourceControlHandle: number,
private readonly handle: number,
public provider: ISCMProvider,
public features: SCMGroupFeatures,
public label: string,
public id: string
) { }
toJSON(): any {
return {
$mid: 4,
sourceControlHandle: this.sourceControlHandle,
groupHandle: this.handle
};
}
splice(start: number, deleteCount: number, toInsert: ISCMResource[]) {
this.elements.splice(start, deleteCount, ...toInsert);
this._onDidSplice.fire({ start, deleteCount, toInsert });
}
$updateGroup(features: SCMGroupFeatures): void {
this.features = assign(this.features, features);
this._onDidChange.fire();
}
$updateGroupLabel(label: string): void {
this.label = label;
this._onDidChange.fire();
}
}
class MainThreadSCMResource implements ISCMResource {
constructor(
private readonly proxy: ExtHostSCMShape,
private readonly sourceControlHandle: number,
private readonly groupHandle: number,
private readonly handle: number,
public sourceUri: URI,
public resourceGroup: ISCMResourceGroup,
public decorations: ISCMResourceDecorations,
public contextValue: string
) { }
open(preserveFocus: boolean): Promise<void> {
return this.proxy.$executeResourceCommand(this.sourceControlHandle, this.groupHandle, this.handle, preserveFocus);
}
toJSON(): any {
return {
$mid: 3,
sourceControlHandle: this.sourceControlHandle,
groupHandle: this.groupHandle,
handle: this.handle
};
}
}
class MainThreadSCMProvider implements ISCMProvider {
private static ID_HANDLE = 0;
private _id = `scm${MainThreadSCMProvider.ID_HANDLE++}`;
get id(): string { return this._id; }
readonly groups = new Sequence<MainThreadSCMResourceGroup>();
private readonly _groupsByHandle: { [handle: number]: MainThreadSCMResourceGroup; } = Object.create(null);
// get groups(): ISequence<ISCMResourceGroup> {
// return {
// elements: this._groups,
// onDidSplice: this._onDidSplice.event
// };
// // return this._groups
// // .filter(g => g.resources.elements.length > 0 || !g.features.hideWhenEmpty);
// }
private readonly _onDidChangeResources = new Emitter<void>();
readonly onDidChangeResources: Event<void> = this._onDidChangeResources.event;
private features: SCMProviderFeatures = {};
get handle(): number { return this._handle; }
get label(): string { return this._label; }
get rootUri(): URI | undefined { return this._rootUri; }
get contextValue(): string { return this._contextValue; }
get commitTemplate(): string { return this.features.commitTemplate || ''; }
get acceptInputCommand(): Command | undefined { return this.features.acceptInputCommand; }
get statusBarCommands(): Command[] | undefined { return this.features.statusBarCommands; }
get count(): number | undefined { return this.features.count; }
private readonly _onDidChangeCommitTemplate = new Emitter<string>();
readonly onDidChangeCommitTemplate: Event<string> = this._onDidChangeCommitTemplate.event;
private readonly _onDidChangeStatusBarCommands = new Emitter<Command[]>();
get onDidChangeStatusBarCommands(): Event<Command[]> { return this._onDidChangeStatusBarCommands.event; }
private readonly _onDidChange = new Emitter<void>();
readonly onDidChange: Event<void> = this._onDidChange.event;
constructor(
private readonly proxy: ExtHostSCMShape,
private readonly _handle: number,
private readonly _contextValue: string,
private readonly _label: string,
private readonly _rootUri: URI | undefined,
@ISCMService scmService: ISCMService
) { }
$updateSourceControl(features: SCMProviderFeatures): void {
this.features = assign(this.features, features);
this._onDidChange.fire();
if (typeof features.commitTemplate !== 'undefined') {
this._onDidChangeCommitTemplate.fire(this.commitTemplate!);
}
if (typeof features.statusBarCommands !== 'undefined') {
this._onDidChangeStatusBarCommands.fire(this.statusBarCommands!);
}
}
$registerGroup(handle: number, id: string, label: string): void {
const group = new MainThreadSCMResourceGroup(
this.handle,
handle,
this,
{},
label,
id
);
this._groupsByHandle[handle] = group;
this.groups.splice(this.groups.elements.length, 0, [group]);
}
$updateGroup(handle: number, features: SCMGroupFeatures): void {
const group = this._groupsByHandle[handle];
if (!group) {
return;
}
group.$updateGroup(features);
}
$updateGroupLabel(handle: number, label: string): void {
const group = this._groupsByHandle[handle];
if (!group) {
return;
}
group.$updateGroupLabel(label);
}
$spliceGroupResourceStates(splices: SCMRawResourceSplices[]): void {
for (const [groupHandle, groupSlices] of splices) {
const group = this._groupsByHandle[groupHandle];
if (!group) {
console.warn(`SCM group ${groupHandle} not found in provider ${this.label}`);
continue;
}
// reverse the splices sequence in order to apply them correctly
groupSlices.reverse();
for (const [start, deleteCount, rawResources] of groupSlices) {
const resources = rawResources.map(rawResource => {
const [handle, sourceUri, icons, tooltip, strikeThrough, faded, contextValue] = rawResource;
const icon = icons[0];
const iconDark = icons[1] || icon;
const decorations = {
icon: icon ? URI.revive(icon) : undefined,
iconDark: iconDark ? URI.revive(iconDark) : undefined,
tooltip,
strikeThrough,
faded
};
return new MainThreadSCMResource(
this.proxy,
this.handle,
groupHandle,
handle,
URI.revive(sourceUri),
group,
decorations,
contextValue
);
});
group.splice(start, deleteCount, resources);
}
}
this._onDidChangeResources.fire();
}
$unregisterGroup(handle: number): void {
const group = this._groupsByHandle[handle];
if (!group) {
return;
}
delete this._groupsByHandle[handle];
this.groups.splice(this.groups.elements.indexOf(group), 1);
}
async getOriginalResource(uri: URI): Promise<URI | null> {
if (!this.features.hasQuickDiffProvider) {
return null;
}
const result = await this.proxy.$provideOriginalResource(this.handle, uri, CancellationToken.None);
return result && URI.revive(result);
}
toJSON(): any {
return {
$mid: 5,
handle: this.handle
};
}
dispose(): void {
}
}
@extHostNamedCustomer(MainContext.MainThreadSCM)
export class MainThreadSCM implements MainThreadSCMShape {
private readonly _proxy: ExtHostSCMShape;
private _repositories = new Map<number, ISCMRepository>();
private _repositoryDisposables = new Map<number, IDisposable>();
private readonly _disposables = new DisposableStore();
constructor(
extHostContext: IExtHostContext,
@ISCMService private readonly scmService: ISCMService
) {
this._proxy = extHostContext.getProxy(ExtHostContext.ExtHostSCM);
}
dispose(): void {
this._repositories.forEach(r => r.dispose());
this._repositories.clear();
this._repositoryDisposables.forEach(d => d.dispose());
this._repositoryDisposables.clear();
this._disposables.dispose();
}
$registerSourceControl(handle: number, id: string, label: string, rootUri: UriComponents | undefined): void {
const provider = new MainThreadSCMProvider(this._proxy, handle, id, label, rootUri ? URI.revive(rootUri) : undefined, this.scmService);
const repository = this.scmService.registerSCMProvider(provider);
this._repositories.set(handle, repository);
const disposable = combinedDisposable(
Event.filter(repository.onDidChangeSelection, selected => selected)(_ => this._proxy.$setSelectedSourceControl(handle)),
repository.input.onDidChange(value => this._proxy.$onInputBoxValueChange(handle, value))
);
if (repository.selected) {
setTimeout(() => this._proxy.$setSelectedSourceControl(handle), 0);
}
this._repositoryDisposables.set(handle, disposable);
}
$updateSourceControl(handle: number, features: SCMProviderFeatures): void {
const repository = this._repositories.get(handle);
if (!repository) {
return;
}
const provider = repository.provider as MainThreadSCMProvider;
provider.$updateSourceControl(features);
}
$unregisterSourceControl(handle: number): void {
const repository = this._repositories.get(handle);
if (!repository) {
return;
}
this._repositoryDisposables.get(handle)!.dispose();
this._repositoryDisposables.delete(handle);
repository.dispose();
this._repositories.delete(handle);
}
$registerGroup(sourceControlHandle: number, groupHandle: number, id: string, label: string): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
const provider = repository.provider as MainThreadSCMProvider;
provider.$registerGroup(groupHandle, id, label);
}
$updateGroup(sourceControlHandle: number, groupHandle: number, features: SCMGroupFeatures): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
const provider = repository.provider as MainThreadSCMProvider;
provider.$updateGroup(groupHandle, features);
}
$updateGroupLabel(sourceControlHandle: number, groupHandle: number, label: string): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
const provider = repository.provider as MainThreadSCMProvider;
provider.$updateGroupLabel(groupHandle, label);
}
$spliceResourceStates(sourceControlHandle: number, splices: SCMRawResourceSplices[]): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
const provider = repository.provider as MainThreadSCMProvider;
provider.$spliceGroupResourceStates(splices);
}
$unregisterGroup(sourceControlHandle: number, handle: number): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
const provider = repository.provider as MainThreadSCMProvider;
provider.$unregisterGroup(handle);
}
$setInputBoxValue(sourceControlHandle: number, value: string): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
repository.input.value = value;
}
$setInputBoxPlaceholder(sourceControlHandle: number, placeholder: string): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
repository.input.placeholder = placeholder;
}
$setInputBoxVisibility(sourceControlHandle: number, visible: boolean): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
repository.input.visible = visible;
}
$setValidationProviderIsEnabled(sourceControlHandle: number, enabled: boolean): void {
const repository = this._repositories.get(sourceControlHandle);
if (!repository) {
return;
}
if (enabled) {
repository.input.validateInput = async (value, pos): Promise<IInputValidation | undefined> => {
const result = await this._proxy.$validateInput(sourceControlHandle, value, pos);
return result && { message: result[0], type: result[1] };
};
} else {
repository.input.validateInput = async () => undefined;
}
}
}