|
| 1 | +/* |
| 2 | + * Copyright (C) 2017 TypeFox and others. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +import { injectable } from "inversify"; |
| 9 | +import { Disposable, Event, Emitter } from "@theia/core/lib/common"; |
| 10 | +import { JSONExt } from "@phosphor/coreutils"; |
| 11 | + |
| 12 | +export interface Marker<T> { |
| 13 | + id: string; |
| 14 | + uri: string; |
| 15 | + owner: string; |
| 16 | + kind: string; |
| 17 | + data: T; |
| 18 | +} |
| 19 | + |
| 20 | +export interface MarkerInfo { |
| 21 | + counter: number; |
| 22 | + uri: string |
| 23 | +} |
| 24 | + |
| 25 | +export class MarkerCollection<T> implements Disposable { |
| 26 | + |
| 27 | + protected readonly markers = new Map<string, Readonly<Marker<T>>[]>(); |
| 28 | + protected readonly idSequences = new Map<string, number>(); |
| 29 | + |
| 30 | + constructor( |
| 31 | + public readonly owner: string, |
| 32 | + public readonly kind: string, |
| 33 | + protected readonly fireDispose: () => void, |
| 34 | + protected readonly fireChange: () => void |
| 35 | + ) { } |
| 36 | + |
| 37 | + dispose(): void { |
| 38 | + this.fireDispose(); |
| 39 | + } |
| 40 | + |
| 41 | + get uris(): string[] { |
| 42 | + return Array.from(this.markers.keys()); |
| 43 | + } |
| 44 | + |
| 45 | + getMarkers(uri: string): Readonly<Marker<T>>[] { |
| 46 | + return this.markers.get(uri) || []; |
| 47 | + } |
| 48 | + |
| 49 | + setMarkers(uri: string, markerData: T[]): void { |
| 50 | + if (markerData.length > 0) { |
| 51 | + this.markers.set(uri, markerData.map(data => this.createMarker(uri, data))); |
| 52 | + } else { |
| 53 | + this.markers.delete(uri); |
| 54 | + } |
| 55 | + this.fireChange(); |
| 56 | + } |
| 57 | + |
| 58 | + protected createMarker(uri: string, data: T): Readonly<Marker<T>> { |
| 59 | + let id; |
| 60 | + const marker = this.getMarkers(uri).find((m: Marker<T>) => JSONExt.deepEqual((m.data as any), (data as any))); |
| 61 | + if (marker) { |
| 62 | + id = marker.id; |
| 63 | + } else { |
| 64 | + id = `${this.owner}_${uri}_${this.nextId(uri)}`; |
| 65 | + } |
| 66 | + return Object.freeze({ |
| 67 | + uri, |
| 68 | + kind: this.kind, |
| 69 | + owner: this.owner, |
| 70 | + id, |
| 71 | + data |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + protected nextId(uri: string): number { |
| 76 | + const id = this.idSequences.get(uri) || 0; |
| 77 | + this.idSequences.set(uri, id + 1); |
| 78 | + return id; |
| 79 | + } |
| 80 | + |
| 81 | +} |
| 82 | + |
| 83 | +@injectable() |
| 84 | +export class MarkerManager { |
| 85 | + |
| 86 | + protected readonly owners = new Map<string, MarkerCollection<object>>(); |
| 87 | + protected readonly onDidChangeMarkersEmitter = new Emitter<void>(); |
| 88 | + |
| 89 | + get onDidChangeMarkers(): Event<void> { |
| 90 | + return this.onDidChangeMarkersEmitter.event; |
| 91 | + } |
| 92 | + protected fireOnDidChangeMarkers(): void { |
| 93 | + this.onDidChangeMarkersEmitter.fire(undefined); |
| 94 | + } |
| 95 | + |
| 96 | + createCollection<T extends object>(owner: string, kind: string): MarkerCollection<T> { |
| 97 | + if (this.owners.has(owner)) { |
| 98 | + throw new Error('marker collection for the given owner already exists, owner: ' + owner); |
| 99 | + } |
| 100 | + const collection = new MarkerCollection<T>(owner, kind, () => this.deleteCollection(owner), () => this.fireOnDidChangeMarkers()); |
| 101 | + this.owners.set(owner, collection); |
| 102 | + return collection; |
| 103 | + } |
| 104 | + |
| 105 | + protected deleteCollection(owner: string): void { |
| 106 | + this.owners.delete(owner); |
| 107 | + } |
| 108 | + |
| 109 | + protected forEachByKind(kind: string, cb: (mc: MarkerCollection<Object>) => void): void { |
| 110 | + this.owners.forEach(collection => { |
| 111 | + if (collection.kind === kind) { |
| 112 | + cb(collection); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + forEachMarkerInfoByKind(kind: string, cb: (markerInfo: MarkerInfo) => void): void { |
| 118 | + this.forEachByKind(kind, collection => collection.uris.forEach(uri => { |
| 119 | + const markers = collection.getMarkers(uri); |
| 120 | + cb({ |
| 121 | + uri: uri, |
| 122 | + counter: markers.length |
| 123 | + }); |
| 124 | + })); |
| 125 | + } |
| 126 | + |
| 127 | + forEachMarkerByUriAndKind(uri: string, kind: string, cb: (marker: Readonly<Marker<Object>>) => void): void { |
| 128 | + this.forEachByKind(kind, collection => collection.getMarkers(uri).forEach(m => cb(m))); |
| 129 | + } |
| 130 | +} |
0 commit comments