-
Notifications
You must be signed in to change notification settings - Fork 28.1k
/
activity.ts
338 lines (279 loc) · 15.6 KB
/
activity.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from 'vs/nls';
import { basename } from 'vs/base/common/resources';
import { IDisposable, dispose, Disposable, DisposableStore, combinedDisposable } from 'vs/base/common/lifecycle';
import { Emitter, Event } from 'vs/base/common/event';
import { VIEW_PANE_ID, ISCMService, ISCMRepository, ISCMViewService } from 'vs/workbench/contrib/scm/common/scm';
import { IActivityService, NumberBadge } from 'vs/workbench/services/activity/common/activity';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { IContextKey, IContextKeyService, RawContextKey } from 'vs/platform/contextkey/common/contextkey';
import { IStatusbarEntry, IStatusbarService, StatusbarAlignment as MainThreadStatusBarAlignment } from 'vs/workbench/services/statusbar/browser/statusbar';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { EditorResourceAccessor } from 'vs/workbench/common/editor';
import { IUriIdentityService } from 'vs/platform/uriIdentity/common/uriIdentity';
import { Schemas } from 'vs/base/common/network';
import { Iterable } from 'vs/base/common/iterator';
import { ITitleService } from 'vs/workbench/services/title/browser/titleService';
import { IEditorGroupContextKeyProvider, IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { EditorInput } from 'vs/workbench/common/editor/editorInput';
import { getRepositoryResourceCount } from 'vs/workbench/contrib/scm/browser/util';
import { autorun, autorunWithStore, derived, IObservable, observableFromEvent } from 'vs/base/common/observable';
import { observableConfigValue } from 'vs/platform/observable/common/platformObservableUtils';
import { derivedObservableWithCache, latestChangedValue, observableFromEventOpts } from 'vs/base/common/observableInternal/utils';
import { Command } from 'vs/editor/common/languages';
import { ISCMHistoryItemGroup } from 'vs/workbench/contrib/scm/common/history';
import { ILogService } from 'vs/platform/log/common/log';
import { IWorkbenchEnvironmentService } from 'vs/workbench/services/environment/common/environmentService';
export class SCMActiveRepositoryController extends Disposable implements IWorkbenchContribution {
private readonly _countBadgeConfig = observableConfigValue<'all' | 'focused' | 'off'>('scm.countBadge', 'all', this.configurationService);
private readonly _repositories = observableFromEvent(
Event.any(this.scmService.onDidAddRepository, this.scmService.onDidRemoveRepository),
() => this.scmService.repositories);
private readonly _focusedRepository = observableFromEventOpts<ISCMRepository | undefined>(
{ equalsFn: () => false },
this.scmViewService.onDidFocusRepository,
() => this.scmViewService.focusedRepository);
private readonly _activeEditor = observableFromEventOpts(
{ equalsFn: () => false },
this.editorService.onDidActiveEditorChange,
() => this.editorService.activeEditor);
private readonly _activeEditorRepository = derivedObservableWithCache<ISCMRepository | undefined>(this, (reader, lastValue) => {
const activeResource = EditorResourceAccessor.getOriginalUri(this._activeEditor.read(reader));
if (!activeResource) {
if (this.environmentService.enableSmokeTestDriver) {
this.logService.info('SCMActiveRepositoryController (activeEditorRepository derived): no activeResource');
}
return lastValue;
}
const repository = this.scmService.getRepository(activeResource);
if (!repository) {
if (this.environmentService.enableSmokeTestDriver) {
this.logService.info(`SCMActiveRepositoryController (activeEditorRepository derived): no repository for '${activeResource.toString()}'`);
}
return lastValue;
}
return Object.create(repository);
});
/**
* The focused repository takes precedence over the active editor repository when the observable
* values are updated in the same transaction (or during the initial read of the observable value).
*/
private readonly _activeRepository = latestChangedValue(this._activeEditorRepository, this._focusedRepository);
private readonly _countBadgeRepositories = derived(reader => {
switch (this._countBadgeConfig.read(reader)) {
case 'all': {
const repositories = this._repositories.read(reader);
return [...Iterable.map(repositories, r => ({ ...r.provider, resourceCount: this._getRepositoryResourceCount(r) }))];
}
case 'focused': {
const repository = this._activeRepository.read(reader);
return repository ? [{ ...repository.provider, resourceCount: this._getRepositoryResourceCount(repository) }] : [];
}
case 'off':
return [];
default:
throw new Error('Invalid countBadge setting');
}
});
private readonly _countBadge = derived(reader => {
let total = 0;
for (const repository of this._countBadgeRepositories.read(reader)) {
const count = repository.count?.read(reader);
const resourceCount = repository.resourceCount.read(reader);
total = total + (count ?? resourceCount);
}
return total;
});
private _activeRepositoryNameContextKey: IContextKey<string>;
private _activeRepositoryBranchNameContextKey: IContextKey<string>;
constructor(
@IActivityService private readonly activityService: IActivityService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IContextKeyService private readonly contextKeyService: IContextKeyService,
@IEditorService private readonly editorService: IEditorService,
@ILogService private readonly logService: ILogService,
@ISCMService private readonly scmService: ISCMService,
@ISCMViewService private readonly scmViewService: ISCMViewService,
@IStatusbarService private readonly statusbarService: IStatusbarService,
@ITitleService private readonly titleService: ITitleService,
@IWorkbenchEnvironmentService private readonly environmentService: IWorkbenchEnvironmentService
) {
super();
this._activeRepositoryNameContextKey = ActiveRepositoryContextKeys.ActiveRepositoryName.bindTo(this.contextKeyService);
this._activeRepositoryBranchNameContextKey = ActiveRepositoryContextKeys.ActiveRepositoryBranchName.bindTo(this.contextKeyService);
this.titleService.registerVariables([
{ name: 'activeRepositoryName', contextKey: ActiveRepositoryContextKeys.ActiveRepositoryName.key },
{ name: 'activeRepositoryBranchName', contextKey: ActiveRepositoryContextKeys.ActiveRepositoryBranchName.key, }
]);
if (this.environmentService.enableSmokeTestDriver) {
this._register(autorun(reader => {
const repository = this._focusedRepository.read(reader);
const commands = repository?.provider.statusBarCommands.read(reader);
this.logService.info('SCMActiveRepositoryController (focusedRepository):', repository?.id ?? 'no id');
this.logService.info('SCMActiveRepositoryController (focusedRepository):', commands ? commands.map(c => c.title).join(', ') : 'no commands');
}));
this._register(autorun(reader => {
const repository = this._activeEditorRepository.read(reader);
const commands = repository?.provider.statusBarCommands.read(reader);
this.logService.info('SCMActiveRepositoryController (activeEditorRepository):', repository?.id ?? 'no id');
this.logService.info('SCMActiveRepositoryController (activeEditorRepository):', commands ? commands.map(c => c.title).join(', ') : 'no commands');
}));
}
this._register(autorunWithStore((reader, store) => {
this._updateActivityCountBadge(this._countBadge.read(reader), store);
}));
this._register(autorunWithStore((reader, store) => {
const repository = this._activeRepository.read(reader);
const commands = repository?.provider.statusBarCommands.read(reader);
if (this.environmentService.enableSmokeTestDriver) {
this.logService.info('SCMActiveRepositoryController (status bar):', repository?.id ?? 'no id');
this.logService.info('SCMActiveRepositoryController (status bar):', commands ? commands.map(c => c.title).join(', ') : 'no commands');
}
this._updateStatusBar(repository, commands ?? [], store);
}));
this._register(autorun(reader => {
const repository = this._activeRepository.read(reader);
const currentHistoryItemGroup = repository?.provider.historyProviderObs.read(reader)?.currentHistoryItemGroupObs.read(reader);
this._updateActiveRepositoryContextKeys(repository, currentHistoryItemGroup);
}));
}
private _getRepositoryResourceCount(repository: ISCMRepository): IObservable<number> {
return observableFromEvent(repository.provider.onDidChangeResources, () => getRepositoryResourceCount(repository.provider));
}
private _updateActivityCountBadge(count: number, store: DisposableStore): void {
if (count === 0) {
return;
}
const badge = new NumberBadge(count, num => localize('scmPendingChangesBadge', '{0} pending changes', num));
store.add(this.activityService.showViewActivity(VIEW_PANE_ID, { badge }));
}
private _updateStatusBar(repository: ISCMRepository | undefined, commands: readonly Command[], store: DisposableStore): void {
if (!repository) {
if (this.environmentService.enableSmokeTestDriver) {
this.logService.info('SCMActiveRepositoryController (status bar): repository is undefined');
}
return;
}
const label = repository.provider.rootUri
? `${basename(repository.provider.rootUri)} (${repository.provider.label})`
: repository.provider.label;
for (let index = 0; index < commands.length; index++) {
const command = commands[index];
const tooltip = `${label}${command.tooltip ? ` - ${command.tooltip}` : ''}`;
// Get a repository agnostic name for the status bar action, derive this from the
// first command argument which is in the form "git.<command>/<number>"
let repoAgnosticActionName = command.arguments?.[0];
if (repoAgnosticActionName && typeof repoAgnosticActionName === 'string') {
repoAgnosticActionName = repoAgnosticActionName
.substring(0, repoAgnosticActionName.lastIndexOf('/'))
.replace(/^git\./, '');
if (repoAgnosticActionName.length > 1) {
repoAgnosticActionName = repoAgnosticActionName[0].toLocaleUpperCase() + repoAgnosticActionName.slice(1);
}
} else {
repoAgnosticActionName = '';
}
const statusbarEntry: IStatusbarEntry = {
name: localize('status.scm', "Source Control") + (repoAgnosticActionName ? ` ${repoAgnosticActionName}` : ''),
text: command.title,
ariaLabel: tooltip,
tooltip,
command: command.id ? command : undefined
};
store.add(index === 0 ?
this.statusbarService.addEntry(statusbarEntry, `status.scm.${index}`, MainThreadStatusBarAlignment.LEFT, 10000) :
this.statusbarService.addEntry(statusbarEntry, `status.scm.${index}`, MainThreadStatusBarAlignment.LEFT, { id: `status.scm.${index - 1}`, alignment: MainThreadStatusBarAlignment.RIGHT, compact: true })
);
}
}
private _updateActiveRepositoryContextKeys(repository: ISCMRepository | undefined, currentHistoryItemGroup: ISCMHistoryItemGroup | undefined): void {
this._activeRepositoryNameContextKey.set(repository?.provider.name ?? '');
this._activeRepositoryBranchNameContextKey.set(currentHistoryItemGroup?.name ?? '');
}
}
const ActiveRepositoryContextKeys = {
ActiveRepositoryName: new RawContextKey<string>('scmActiveRepositoryName', ''),
ActiveRepositoryBranchName: new RawContextKey<string>('scmActiveRepositoryBranchName', ''),
};
export class SCMActiveResourceContextKeyController implements IWorkbenchContribution {
private readonly disposables = new DisposableStore();
private repositoryDisposables = new Set<IDisposable>();
private onDidRepositoryChange = new Emitter<void>();
constructor(
@IEditorGroupsService editorGroupsService: IEditorGroupsService,
@ISCMService private readonly scmService: ISCMService,
@IUriIdentityService private readonly uriIdentityService: IUriIdentityService
) {
const activeResourceHasChangesContextKey = new RawContextKey<boolean>('scmActiveResourceHasChanges', false, localize('scmActiveResourceHasChanges', "Whether the active resource has changes"));
const activeResourceRepositoryContextKey = new RawContextKey<string | undefined>('scmActiveResourceRepository', undefined, localize('scmActiveResourceRepository', "The active resource's repository"));
this.scmService.onDidAddRepository(this.onDidAddRepository, this, this.disposables);
for (const repository of this.scmService.repositories) {
this.onDidAddRepository(repository);
}
// Create context key providers which will update the context keys based on each groups active editor
const hasChangesContextKeyProvider: IEditorGroupContextKeyProvider<boolean> = {
contextKey: activeResourceHasChangesContextKey,
getGroupContextKeyValue: (group) => this.getEditorHasChanges(group.activeEditor),
onDidChange: this.onDidRepositoryChange.event
};
const repositoryContextKeyProvider: IEditorGroupContextKeyProvider<string | undefined> = {
contextKey: activeResourceRepositoryContextKey,
getGroupContextKeyValue: (group) => this.getEditorRepositoryId(group.activeEditor),
onDidChange: this.onDidRepositoryChange.event
};
this.disposables.add(editorGroupsService.registerContextKeyProvider(hasChangesContextKeyProvider));
this.disposables.add(editorGroupsService.registerContextKeyProvider(repositoryContextKeyProvider));
}
private onDidAddRepository(repository: ISCMRepository): void {
const onDidChange = Event.any(repository.provider.onDidChange, repository.provider.onDidChangeResources);
const changeDisposable = onDidChange(() => {
this.onDidRepositoryChange.fire();
});
const onDidRemove = Event.filter(this.scmService.onDidRemoveRepository, e => e === repository);
const removeDisposable = onDidRemove(() => {
disposable.dispose();
this.repositoryDisposables.delete(disposable);
this.onDidRepositoryChange.fire();
});
const disposable = combinedDisposable(changeDisposable, removeDisposable);
this.repositoryDisposables.add(disposable);
}
private getEditorRepositoryId(activeEditor: EditorInput | null): string | undefined {
const activeResource = EditorResourceAccessor.getOriginalUri(activeEditor);
if (activeResource?.scheme === Schemas.file || activeResource?.scheme === Schemas.vscodeRemote) {
const activeResourceRepository = Iterable.find(
this.scmService.repositories,
r => Boolean(r.provider.rootUri && this.uriIdentityService.extUri.isEqualOrParent(activeResource, r.provider.rootUri))
);
return activeResourceRepository?.id;
}
return undefined;
}
private getEditorHasChanges(activeEditor: EditorInput | null): boolean {
const activeResource = EditorResourceAccessor.getOriginalUri(activeEditor);
if (activeResource?.scheme === Schemas.file || activeResource?.scheme === Schemas.vscodeRemote) {
const activeResourceRepository = Iterable.find(
this.scmService.repositories,
r => Boolean(r.provider.rootUri && this.uriIdentityService.extUri.isEqualOrParent(activeResource, r.provider.rootUri))
);
for (const resourceGroup of activeResourceRepository?.provider.groups ?? []) {
if (resourceGroup.resources
.some(scmResource =>
this.uriIdentityService.extUri.isEqual(activeResource, scmResource.sourceUri))) {
return true;
}
}
}
return false;
}
dispose(): void {
this.disposables.dispose();
dispose(this.repositoryDisposables.values());
this.repositoryDisposables.clear();
this.onDidRepositoryChange.dispose();
}
}