-
Notifications
You must be signed in to change notification settings - Fork 27.9k
/
markers.ts
75 lines (61 loc) · 3.34 KB
/
markers.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { createDecorator, IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { MarkersModel, compareMarkersByUri } from './markersModel';
import { Disposable, MutableDisposable, IDisposable } from 'vs/base/common/lifecycle';
import { IMarkerService, MarkerSeverity, IMarker } from 'vs/platform/markers/common/markers';
import { IActivityService, NumberBadge } from 'vs/workbench/services/activity/common/activity';
import { localize } from 'vs/nls';
import Constants from './constants';
import { URI } from 'vs/base/common/uri';
import { groupBy } from 'vs/base/common/arrays';
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
import { Event } from 'vs/base/common/event';
import { ResourceMap } from 'vs/base/common/map';
export const IMarkersWorkbenchService = createDecorator<IMarkersWorkbenchService>('markersWorkbenchService');
export interface IMarkersWorkbenchService {
_serviceBrand: undefined;
readonly markersModel: MarkersModel;
}
export class MarkersWorkbenchService extends Disposable implements IMarkersWorkbenchService {
_serviceBrand: undefined;
readonly markersModel: MarkersModel;
constructor(
@IMarkerService private readonly markerService: IMarkerService,
@IInstantiationService instantiationService: IInstantiationService,
) {
super();
this.markersModel = this._register(instantiationService.createInstance(MarkersModel));
this.markersModel.setResourceMarkers(groupBy(this.readMarkers(), compareMarkersByUri).map(group => [group[0].resource, group]));
this._register(Event.debounce<readonly URI[], ResourceMap<URI>>(markerService.onMarkerChanged, (resourcesMap, resources) => {
resourcesMap = resourcesMap ? resourcesMap : new ResourceMap<URI>();
resources.forEach(resource => resourcesMap!.set(resource, resource));
return resourcesMap;
}, 0)(resourcesMap => this.onMarkerChanged(resourcesMap.values())));
}
private onMarkerChanged(resources: URI[]): void {
this.markersModel.setResourceMarkers(resources.map(resource => [resource, this.readMarkers(resource)]));
}
private readMarkers(resource?: URI): IMarker[] {
return this.markerService.read({ resource, severities: MarkerSeverity.Error | MarkerSeverity.Warning | MarkerSeverity.Info });
}
}
export class ActivityUpdater extends Disposable implements IWorkbenchContribution {
private readonly activity = this._register(new MutableDisposable<IDisposable>());
constructor(
@IActivityService private readonly activityService: IActivityService,
@IMarkerService private readonly markerService: IMarkerService
) {
super();
this._register(this.markerService.onMarkerChanged(() => this.updateBadge()));
this.updateBadge();
}
private updateBadge(): void {
const { errors, warnings, infos } = this.markerService.getStatistics();
const total = errors + warnings + infos;
const message = localize('totalProblems', 'Total {0} Problems', total);
this.activity.value = this.activityService.showActivity(Constants.MARKERS_PANEL_ID, new NumberBadge(total, () => message));
}
}