|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +import { window, Uri, Disposable, Event, EventEmitter, DecorationData, DecorationProvider } from 'vscode'; |
| 9 | +import { Repository, GitResourceGroup } from './repository'; |
| 10 | +import { Model } from './model'; |
| 11 | + |
| 12 | +class GitDecorationProvider implements DecorationProvider { |
| 13 | + |
| 14 | + private readonly _onDidChangeDecorations = new EventEmitter<Uri[]>(); |
| 15 | + readonly onDidChangeDecorations: Event<Uri[]> = this._onDidChangeDecorations.event; |
| 16 | + |
| 17 | + private disposables: Disposable[] = []; |
| 18 | + private decorations = new Map<string, DecorationData>(); |
| 19 | + |
| 20 | + constructor(private repository: Repository) { |
| 21 | + this.disposables.push( |
| 22 | + window.registerDecorationProvider(this, repository.root), |
| 23 | + repository.onDidRunOperation(this.onDidRunOperation, this) |
| 24 | + ); |
| 25 | + } |
| 26 | + |
| 27 | + private onDidRunOperation(): void { |
| 28 | + let newDecorations = new Map<string, DecorationData>(); |
| 29 | + this.collectDecorationData(this.repository.indexGroup, newDecorations); |
| 30 | + this.collectDecorationData(this.repository.workingTreeGroup, newDecorations); |
| 31 | + |
| 32 | + let uris: Uri[] = []; |
| 33 | + newDecorations.forEach((value, uriString) => { |
| 34 | + if (this.decorations.has(uriString)) { |
| 35 | + this.decorations.delete(uriString); |
| 36 | + } else { |
| 37 | + uris.push(Uri.parse(uriString)); |
| 38 | + } |
| 39 | + }); |
| 40 | + this.decorations.forEach((value, uriString) => { |
| 41 | + uris.push(Uri.parse(uriString)); |
| 42 | + }); |
| 43 | + this.decorations = newDecorations; |
| 44 | + this._onDidChangeDecorations.fire(uris); |
| 45 | + } |
| 46 | + |
| 47 | + private collectDecorationData(group: GitResourceGroup, bucket: Map<string, DecorationData>): void { |
| 48 | + group.resourceStates.forEach(r => { |
| 49 | + if (r.resourceDecoration) { |
| 50 | + bucket.set(r.original.toString(), r.resourceDecoration); |
| 51 | + } |
| 52 | + }); |
| 53 | + } |
| 54 | + |
| 55 | + provideDecoration(uri: Uri): DecorationData | undefined { |
| 56 | + return this.decorations.get(uri.toString()); |
| 57 | + } |
| 58 | + |
| 59 | + dispose(): void { |
| 60 | + this.disposables.forEach(d => d.dispose()); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +export class GitDecorations { |
| 66 | + |
| 67 | + private disposables: Disposable[] = []; |
| 68 | + private providers = new Map<Repository, GitDecorationProvider>(); |
| 69 | + |
| 70 | + constructor(private model: Model) { |
| 71 | + this.disposables.push( |
| 72 | + model.onDidOpenRepository(this.onDidOpenRepository, this), |
| 73 | + model.onDidCloseRepository(this.onDidCloseRepository, this) |
| 74 | + ); |
| 75 | + model.repositories.forEach(this.onDidOpenRepository, this); |
| 76 | + } |
| 77 | + |
| 78 | + private onDidOpenRepository(repository: Repository): void { |
| 79 | + const provider = new GitDecorationProvider(repository); |
| 80 | + this.providers.set(repository, provider); |
| 81 | + } |
| 82 | + |
| 83 | + private onDidCloseRepository(repository: Repository): void { |
| 84 | + const provider = this.providers.get(repository); |
| 85 | + if (provider) { |
| 86 | + provider.dispose(); |
| 87 | + this.providers.delete(repository); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + dispose(): void { |
| 92 | + this.disposables.forEach(d => d.dispose()); |
| 93 | + this.providers.forEach(value => value.dispose); |
| 94 | + this.providers.clear(); |
| 95 | + } |
| 96 | +} |
0 commit comments